Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 38 additions & 48 deletions util/src/test/java/io/kubernetes/client/ExecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.params.provider.Arguments.arguments;


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

private static final int EXPECTED_ERROR_EXIT_CODE = -1975219;

private String namespace;
private String podName;
private String[] cmd;
Expand All @@ -93,10 +103,7 @@ void setup() {

namespace = "default";
podName = "apod";
// TODO: When WireMock supports multiple query params with the same name expand
// this
// See: https://github.com/tomakehurst/wiremock/issues/398
cmd = new String[] {"cmd"};
cmd = new String[] {"sh", "-c", "echo Hello from inside the pod && ls /tmp"};
}

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

@Test
void terminalResize() throws IOException, InterruptedException {
final Throwable throwable = mock(Throwable.class);
final ExecProcess process = new ExecProcess(client);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

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

verify(throwable, times(1)).printStackTrace();
assertThat(process.isAlive()).isFalse();
assertThat(process.exitValue()).isEqualTo(-1975219);
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
}

@Test
Expand All @@ -197,7 +203,7 @@ void customUnhandledError() throws IOException, InterruptedException {
verify(throwable, times(0)).printStackTrace();
verify(consumer, times(1)).accept(throwable);
assertThat(process.isAlive()).isFalse();
assertThat(process.exitValue()).isEqualTo(-1975219);
assertThat(process.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
}

@Test
Expand Down Expand Up @@ -240,7 +246,10 @@ void url() throws IOException, ApiException, InterruptedException {
.withQueryParam("stderr", equalTo("true"))
.withQueryParam("container", equalTo("container"))
.withQueryParam("tty", equalTo("false"))
.withQueryParam("command", equalTo("cmd")));
.withQueryParam("command", equalTo("sh"))
.withQueryParam("command", equalTo("-c"))
.withQueryParam("command", equalTo("echo Hello from inside the pod && ls /tmp")));


apiServer.verify(
getRequestedFor(
Expand All @@ -250,50 +259,31 @@ void url() throws IOException, ApiException, InterruptedException {
.withQueryParam("stderr", equalTo("false"))
.withQueryParam("container", equalTo("container"))
.withQueryParam("tty", equalTo("false"))
.withQueryParam("command", equalTo("cmd")));

assertThat(p.exitValue()).isEqualTo(-1975219);
verify(consumer, times(1)).accept(any(Throwable.class));
}

@Test
void exit0() {
InputStream inputStream =
new ByteArrayInputStream(OUTPUT_EXIT0.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isZero();
}

@Test
void exit1() {
InputStream inputStream =
new ByteArrayInputStream(OUTPUT_EXIT1.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isEqualTo(1);
}
.withQueryParam("command", equalTo("sh"))
.withQueryParam("command", equalTo("-c"))
.withQueryParam("command", equalTo("echo Hello from inside the pod && ls /tmp")));

@Test
void exit126() {
InputStream inputStream =
new ByteArrayInputStream(OUTPUT_EXIT126.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isEqualTo(126);
}

@Test
void incompleteData1() {
InputStream inputStream =
new ByteArrayInputStream(BAD_OUTPUT_INCOMPLETE_MSG1.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isEqualTo(-1);
assertThat(p.exitValue()).isEqualTo(EXPECTED_ERROR_EXIT_CODE);
verify(consumer, times(1)).accept(any(Throwable.class));
}

@Test
void nonZeroBadIntExit() {
InputStream inputStream =
new ByteArrayInputStream(OUTPUT_EXIT_BAD_INT.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isEqualTo(-1);
static Stream<Arguments> exitCodeTestData() {
return Stream.of(
arguments(OUTPUT_EXIT0, 0),
arguments(OUTPUT_EXIT1, 1),
arguments(OUTPUT_EXIT126, 126),
arguments(BAD_OUTPUT_INCOMPLETE_MSG1, -1),
arguments(OUTPUT_EXIT_BAD_INT, -1)
);
}

@ParameterizedTest
@MethodSource("exitCodeTestData")
void testExitCodeParsing(String output, int expectedExitCode) {
InputStream inputStream = new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8));
int exitCode = Exec.parseExitCode(client, inputStream);
assertThat(exitCode).isEqualTo(expectedExitCode);
}

@Test
Expand Down
Loading