Skip to content

Deflake ExecTest #2120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2022
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
28 changes: 28 additions & 0 deletions util/src/test/java/io/kubernetes/client/ExecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.assertEquals;

import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import io.kubernetes.client.Exec.ExecProcess;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
Expand All @@ -35,13 +39,27 @@
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/** Tests for the Exec helper class */
public class ExecTest {

public static class CountRequestAction extends PostServeAction {
@Override
public String getName() {
return "semaphore";
}

@Override
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
Semaphore count = (Semaphore) parameters.get("semaphore");
count.release();
}
}

private static final String OUTPUT_EXIT0 = "{\"metadata\":{},\"status\":\"Success\"}";
private static final String OUTPUT_EXIT1 =
"{\"metadata\":{},\"status\":\"Failure\",\"message\":\"command terminated with non-zero exit code: Error executing in Docker Container: 1\",\"reason\":\"NonZeroExitCode\",\"details\":{\"causes\":[{\"reason\":\"ExitCode\",\"message\":\"1\"}]}}";
Expand Down Expand Up @@ -135,8 +153,13 @@ public void testUrl() throws IOException, ApiException, InterruptedException {

V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace));

Semaphore getCount = new Semaphore(2);
Parameters getParams = new Parameters();
getParams.put("semaphore", getCount);

wireMockRule.stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
.withPostServeAction("semaphore", getParams)
.willReturn(
aResponse()
.withStatus(404)
Expand All @@ -153,6 +176,11 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
.execute()
.waitFor();

// These will be released for each web call above.
// There is a race between the above waitFor() and the request actually being recorded
// by WireMock. This fixes it.
getCount.acquire(2);

wireMockRule.verify(
getRequestedFor(
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
Expand Down