45
45
import java .nio .charset .StandardCharsets ;
46
46
import java .util .concurrent .CountDownLatch ;
47
47
import java .util .function .Consumer ;
48
+ import java .util .stream .Stream ;
49
+
48
50
import org .junit .jupiter .api .BeforeEach ;
49
51
import org .junit .jupiter .api .Test ;
50
52
import org .junit .jupiter .api .extension .RegisterExtension ;
53
+ import org .junit .jupiter .params .ParameterizedTest ;
54
+ import org .junit .jupiter .params .provider .Arguments ;
55
+ import org .junit .jupiter .params .provider .MethodSource ;
56
+
57
+ import static org .junit .jupiter .params .provider .Arguments .arguments ;
58
+
51
59
52
60
/** Tests for the Exec helper class */
53
61
class ExecTest {
@@ -75,6 +83,8 @@ public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters)
75
83
private static final String OUTPUT_EXIT_BAD_INT =
76
84
"{\" metadata\" :{},\" status\" :\" Failure\" ,\" message\" :\" command terminated with non-zero exit code: Error executing in Docker Container: 126\" ,\" reason\" :\" NonZeroExitCode\" ,\" details\" :{\" causes\" :[{\" reason\" :\" ExitCode\" ,\" message\" :\" not a number\" }]}}" ;
77
85
86
+ private static final int EXPECTED_ERROR_EXIT_CODE = -1975219 ;
87
+
78
88
private String namespace ;
79
89
private String podName ;
80
90
private String [] cmd ;
@@ -93,10 +103,7 @@ void setup() {
93
103
94
104
namespace = "default" ;
95
105
podName = "apod" ;
96
- // TODO: When WireMock supports multiple query params with the same name expand
97
- // this
98
- // See: https://github.com/tomakehurst/wiremock/issues/398
99
- cmd = new String [] {"cmd" };
106
+ cmd = new String [] {"sh" , "-c" , "echo Hello from inside the pod && ls /tmp" };
100
107
}
101
108
102
109
public static InputStream makeStream (int streamNum , byte [] data ) {
@@ -158,7 +165,6 @@ void execProcess() throws IOException, InterruptedException {
158
165
159
166
@ Test
160
167
void terminalResize () throws IOException , InterruptedException {
161
- final Throwable throwable = mock (Throwable .class );
162
168
final ExecProcess process = new ExecProcess (client );
163
169
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
164
170
@@ -181,7 +187,7 @@ void defaultUnhandledError() throws IOException, InterruptedException {
181
187
182
188
verify (throwable , times (1 )).printStackTrace ();
183
189
assertThat (process .isAlive ()).isFalse ();
184
- assertThat (process .exitValue ()).isEqualTo (- 1975219 );
190
+ assertThat (process .exitValue ()).isEqualTo (EXPECTED_ERROR_EXIT_CODE );
185
191
}
186
192
187
193
@ Test
@@ -197,7 +203,7 @@ void customUnhandledError() throws IOException, InterruptedException {
197
203
verify (throwable , times (0 )).printStackTrace ();
198
204
verify (consumer , times (1 )).accept (throwable );
199
205
assertThat (process .isAlive ()).isFalse ();
200
- assertThat (process .exitValue ()).isEqualTo (- 1975219 );
206
+ assertThat (process .exitValue ()).isEqualTo (EXPECTED_ERROR_EXIT_CODE );
201
207
}
202
208
203
209
@ Test
@@ -240,7 +246,10 @@ void url() throws IOException, ApiException, InterruptedException {
240
246
.withQueryParam ("stderr" , equalTo ("true" ))
241
247
.withQueryParam ("container" , equalTo ("container" ))
242
248
.withQueryParam ("tty" , equalTo ("false" ))
243
- .withQueryParam ("command" , equalTo ("cmd" )));
249
+ .withQueryParam ("command" , equalTo ("sh" ))
250
+ .withQueryParam ("command" , equalTo ("-c" ))
251
+ .withQueryParam ("command" , equalTo ("echo Hello from inside the pod && ls /tmp" )));
252
+
244
253
245
254
apiServer .verify (
246
255
getRequestedFor (
@@ -250,50 +259,31 @@ void url() throws IOException, ApiException, InterruptedException {
250
259
.withQueryParam ("stderr" , equalTo ("false" ))
251
260
.withQueryParam ("container" , equalTo ("container" ))
252
261
.withQueryParam ("tty" , equalTo ("false" ))
253
- .withQueryParam ("command" , equalTo ("cmd" )));
254
-
255
- assertThat (p .exitValue ()).isEqualTo (-1975219 );
256
- verify (consumer , times (1 )).accept (any (Throwable .class ));
257
- }
258
-
259
- @ Test
260
- void exit0 () {
261
- InputStream inputStream =
262
- new ByteArrayInputStream (OUTPUT_EXIT0 .getBytes (StandardCharsets .UTF_8 ));
263
- int exitCode = Exec .parseExitCode (client , inputStream );
264
- assertThat (exitCode ).isZero ();
265
- }
266
-
267
- @ Test
268
- void exit1 () {
269
- InputStream inputStream =
270
- new ByteArrayInputStream (OUTPUT_EXIT1 .getBytes (StandardCharsets .UTF_8 ));
271
- int exitCode = Exec .parseExitCode (client , inputStream );
272
- assertThat (exitCode ).isEqualTo (1 );
273
- }
262
+ .withQueryParam ("command" , equalTo ("sh" ))
263
+ .withQueryParam ("command" , equalTo ("-c" ))
264
+ .withQueryParam ("command" , equalTo ("echo Hello from inside the pod && ls /tmp" )));
274
265
275
- @ Test
276
- void exit126 () {
277
- InputStream inputStream =
278
- new ByteArrayInputStream (OUTPUT_EXIT126 .getBytes (StandardCharsets .UTF_8 ));
279
- int exitCode = Exec .parseExitCode (client , inputStream );
280
- assertThat (exitCode ).isEqualTo (126 );
281
- }
282
266
283
- @ Test
284
- void incompleteData1 () {
285
- InputStream inputStream =
286
- new ByteArrayInputStream (BAD_OUTPUT_INCOMPLETE_MSG1 .getBytes (StandardCharsets .UTF_8 ));
287
- int exitCode = Exec .parseExitCode (client , inputStream );
288
- assertThat (exitCode ).isEqualTo (-1 );
267
+ assertThat (p .exitValue ()).isEqualTo (EXPECTED_ERROR_EXIT_CODE );
268
+ verify (consumer , times (1 )).accept (any (Throwable .class ));
289
269
}
290
270
291
- @ Test
292
- void nonZeroBadIntExit () {
293
- InputStream inputStream =
294
- new ByteArrayInputStream (OUTPUT_EXIT_BAD_INT .getBytes (StandardCharsets .UTF_8 ));
295
- int exitCode = Exec .parseExitCode (client , inputStream );
296
- assertThat (exitCode ).isEqualTo (-1 );
271
+ static Stream <Arguments > exitCodeTestData () {
272
+ return Stream .of (
273
+ arguments (OUTPUT_EXIT0 , 0 ),
274
+ arguments (OUTPUT_EXIT1 , 1 ),
275
+ arguments (OUTPUT_EXIT126 , 126 ),
276
+ arguments (BAD_OUTPUT_INCOMPLETE_MSG1 , -1 ),
277
+ arguments (OUTPUT_EXIT_BAD_INT , -1 )
278
+ );
279
+ }
280
+
281
+ @ ParameterizedTest
282
+ @ MethodSource ("exitCodeTestData" )
283
+ void testExitCodeParsing (String output , int expectedExitCode ) {
284
+ InputStream inputStream = new ByteArrayInputStream (output .getBytes (StandardCharsets .UTF_8 ));
285
+ int exitCode = Exec .parseExitCode (client , inputStream );
286
+ assertThat (exitCode ).isEqualTo (expectedExitCode );
297
287
}
298
288
299
289
@ Test
0 commit comments