Skip to content

Commit 0c4ced3

Browse files
improve ExecTest.java
1 parent 9527c2a commit 0c4ced3

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@
4545
import java.nio.charset.StandardCharsets;
4646
import java.util.concurrent.CountDownLatch;
4747
import java.util.function.Consumer;
48+
import java.util.stream.Stream;
49+
4850
import org.junit.jupiter.api.BeforeEach;
4951
import org.junit.jupiter.api.Test;
5052
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.CsvSource;
56+
import org.junit.jupiter.params.provider.MethodSource;
57+
58+
import static org.junit.jupiter.params.provider.Arguments.arguments;
59+
5160

5261
/** Tests for the Exec helper class */
5362
class ExecTest {
@@ -75,6 +84,8 @@ public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters)
7584
private static final String OUTPUT_EXIT_BAD_INT =
7685
"{\"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\"}]}}";
7786

87+
private static final int EXPECTED_ERROR_EXIT_CODE = -1975219;
88+
7889
private String namespace;
7990
private String podName;
8091
private String[] cmd;
@@ -93,10 +104,7 @@ void setup() {
93104

94105
namespace = "default";
95106
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"};
107+
cmd = new String[] {"cmd1","cmd2"};
100108
}
101109

102110
public static InputStream makeStream(int streamNum, byte[] data) {
@@ -158,7 +166,6 @@ void execProcess() throws IOException, InterruptedException {
158166

159167
@Test
160168
void terminalResize() throws IOException, InterruptedException {
161-
final Throwable throwable = mock(Throwable.class);
162169
final ExecProcess process = new ExecProcess(client);
163170
ByteArrayOutputStream bos = new ByteArrayOutputStream();
164171

@@ -181,7 +188,7 @@ void defaultUnhandledError() throws IOException, InterruptedException {
181188

182189
verify(throwable, times(1)).printStackTrace();
183190
assertThat(process.isAlive()).isFalse();
184-
assertThat(process.exitValue()).isEqualTo(-1975219);
191+
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
185192
}
186193

187194
@Test
@@ -197,7 +204,7 @@ void customUnhandledError() throws IOException, InterruptedException {
197204
verify(throwable, times(0)).printStackTrace();
198205
verify(consumer, times(1)).accept(throwable);
199206
assertThat(process.isAlive()).isFalse();
200-
assertThat(process.exitValue()).isEqualTo(-1975219);
207+
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
201208
}
202209

203210
@Test
@@ -240,7 +247,9 @@ void url() throws IOException, ApiException, InterruptedException {
240247
.withQueryParam("stderr", equalTo("true"))
241248
.withQueryParam("container", equalTo("container"))
242249
.withQueryParam("tty", equalTo("false"))
243-
.withQueryParam("command", equalTo("cmd")));
250+
.withQueryParam("command", equalTo("cmd1"))
251+
.withQueryParam("command", equalTo("cmd2")));
252+
244253

245254
apiServer.verify(
246255
getRequestedFor(
@@ -250,50 +259,29 @@ void url() throws IOException, ApiException, InterruptedException {
250259
.withQueryParam("stderr", equalTo("false"))
251260
.withQueryParam("container", equalTo("container"))
252261
.withQueryParam("tty", equalTo("false"))
253-
.withQueryParam("command", equalTo("cmd")));
262+
.withQueryParam("command", equalTo("cmd1"))
263+
.withQueryParam("command", equalTo("cmd2")));
254264

255-
assertThat(p.exitValue()).isEqualTo(-1975219);
265+
assertThat(p.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
256266
verify(consumer, times(1)).accept(any(Throwable.class));
257267
}
258268

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-
}
274-
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-
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);
289-
}
290-
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);
269+
static Stream<Arguments> exitCodeTestData() {
270+
return Stream.of(
271+
arguments(OUTPUT_EXIT0, 0),
272+
arguments(OUTPUT_EXIT1, 1),
273+
arguments(OUTPUT_EXIT126, 126),
274+
arguments(BAD_OUTPUT_INCOMPLETE_MSG1, -1),
275+
arguments(OUTPUT_EXIT_BAD_INT, -1)
276+
);
277+
}
278+
279+
@ParameterizedTest
280+
@MethodSource("exitCodeTestData")
281+
void testExitCodeParsing(String output, int expectedExitCode) {
282+
InputStream inputStream = new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8));
283+
int exitCode = Exec.parseExitCode(client, inputStream);
284+
assertThat(exitCode).isEqualTo(expectedExitCode);
297285
}
298286

299287
@Test

0 commit comments

Comments
 (0)