1
1
/*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2018 Playtika
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2018 Playtika
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
23
*/
24
24
package com .playtika .test .common .utils ;
25
25
26
- import com .github .dockerjava .api .DockerClient ;
27
- import com .github .dockerjava .api .command .ExecCreateCmdResponse ;
28
- import com .github .dockerjava .core .command .ExecStartResultCallback ;
29
- import lombok .Value ;
30
26
import lombok .experimental .UtilityClass ;
31
27
import lombok .extern .slf4j .Slf4j ;
32
28
import org .slf4j .Logger ;
29
+ import org .testcontainers .containers .Container ;
30
+ import org .testcontainers .containers .ContainerState ;
33
31
import org .testcontainers .containers .GenericContainer ;
34
32
import org .testcontainers .containers .output .OutputFrame ;
35
33
36
- import java .io .ByteArrayOutputStream ;
37
34
import java .io .IOException ;
38
35
import java .net .ServerSocket ;
39
- import java .nio .charset .StandardCharsets ;
40
36
import java .time .Duration ;
41
37
import java .time .Instant ;
42
38
import java .util .Arrays ;
@@ -58,8 +54,7 @@ public long startAndLogTime(GenericContainer container) {
58
54
59
55
if (startupTime < 10L ) {
60
56
log .info ("{} startup time is {} seconds" , container .getDockerImageName (), startupTime );
61
- } else
62
- if (startupTime < 20L ) {
57
+ } else if (startupTime < 20L ) {
63
58
log .warn ("{} startup time is {} seconds" , container .getDockerImageName (), startupTime );
64
59
} else {
65
60
log .error ("{} startup time is {} seconds" , container .getDockerImageName (), startupTime );
@@ -92,36 +87,12 @@ public static Consumer<OutputFrame> containerLogsConsumer(Logger log) {
92
87
};
93
88
}
94
89
95
- public static ExecCmdResult execCmd (DockerClient dockerClient , String containerId , String [] command ) {
96
- ExecCreateCmdResponse cmd = dockerClient .execCreateCmd (containerId )
97
- .withAttachStdout (true )
98
- .withAttachStderr (true )
99
- .withCmd (command )
100
- .exec ();
101
-
102
- String cmdStdout ;
103
- String cmdStderr ;
104
-
105
- try (ByteArrayOutputStream stdout = new ByteArrayOutputStream ();
106
- ByteArrayOutputStream stderr = new ByteArrayOutputStream ();
107
- ExecStartResultCallback cmdCallback = new ExecStartResultCallback (stdout , stderr )) {
108
- dockerClient .execStartCmd (cmd .getId ()).exec (cmdCallback ).awaitCompletion ();
109
- cmdStdout = stdout .toString (StandardCharsets .UTF_8 .name ());
110
- cmdStderr = stderr .toString (StandardCharsets .UTF_8 .name ());
90
+ public static Container .ExecResult executeInContainer (ContainerState container , String [] command ) {
91
+ try {
92
+ return container .execInContainer (command );
111
93
} catch (Exception e ) {
112
- String format = String .format ("Exception was thrown when executing: %s, for container: %s " , Arrays .toString (command ), containerId );
94
+ String format = String .format ("Exception was thrown when executing: %s, for container: %s " , Arrays .toString (command ), container . getContainerId () );
113
95
throw new IllegalStateException (format , e );
114
96
}
115
-
116
- int exitCode = dockerClient .inspectExecCmd (cmd .getId ()).exec ().getExitCode ();
117
- String output = cmdStdout .isEmpty () ? cmdStderr : cmdStdout ;
118
- return new ExecCmdResult (exitCode , output );
119
97
}
120
-
121
- @ Value
122
- public static class ExecCmdResult {
123
- int exitCode ;
124
- String output ;
125
- }
126
-
127
98
}
0 commit comments