Skip to content

Commit c7862a1

Browse files
authored
Merge pull request #3989 from karthikkondapally/exectest-imp
Improve ExecTest.java
2 parents 6b42bf3 + fe78455 commit c7862a1

File tree

1 file changed

+38
-48
lines changed

1 file changed

+38
-48
lines changed

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

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@
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.MethodSource;
56+
57+
import static org.junit.jupiter.params.provider.Arguments.arguments;
58+
5159

5260
/** Tests for the Exec helper class */
5361
class ExecTest {
@@ -75,6 +83,8 @@ public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters)
7583
private static final String OUTPUT_EXIT_BAD_INT =
7684
"{\"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\"}]}}";
7785

86+
private static final int EXPECTED_ERROR_EXIT_CODE = -1975219;
87+
7888
private String namespace;
7989
private String podName;
8090
private String[] cmd;
@@ -93,10 +103,7 @@ void setup() {
93103

94104
namespace = "default";
95105
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"};
100107
}
101108

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

159166
@Test
160167
void terminalResize() throws IOException, InterruptedException {
161-
final Throwable throwable = mock(Throwable.class);
162168
final ExecProcess process = new ExecProcess(client);
163169
ByteArrayOutputStream bos = new ByteArrayOutputStream();
164170

@@ -181,7 +187,7 @@ void defaultUnhandledError() throws IOException, InterruptedException {
181187

182188
verify(throwable, times(1)).printStackTrace();
183189
assertThat(process.isAlive()).isFalse();
184-
assertThat(process.exitValue()).isEqualTo(-1975219);
190+
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
185191
}
186192

187193
@Test
@@ -197,7 +203,7 @@ void customUnhandledError() throws IOException, InterruptedException {
197203
verify(throwable, times(0)).printStackTrace();
198204
verify(consumer, times(1)).accept(throwable);
199205
assertThat(process.isAlive()).isFalse();
200-
assertThat(process.exitValue()).isEqualTo(-1975219);
206+
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
201207
}
202208

203209
@Test
@@ -240,7 +246,10 @@ void url() throws IOException, ApiException, InterruptedException {
240246
.withQueryParam("stderr", equalTo("true"))
241247
.withQueryParam("container", equalTo("container"))
242248
.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+
244253

245254
apiServer.verify(
246255
getRequestedFor(
@@ -250,50 +259,31 @@ 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")));
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")));
274265

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-
}
282266

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));
289269
}
290270

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);
297287
}
298288

299289
@Test

0 commit comments

Comments
 (0)