From 0f2a3bec9eb88f48b9773cd485f6acecf948ba2f Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 1 Feb 2022 01:03:19 +0000 Subject: [PATCH] Deflake a test. --- .../java/io/kubernetes/client/ExecTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/util/src/test/java/io/kubernetes/client/ExecTest.java b/util/src/test/java/io/kubernetes/client/ExecTest.java index 8075e9de51..c92a35b877 100644 --- a/util/src/test/java/io/kubernetes/client/ExecTest.java +++ b/util/src/test/java/io/kubernetes/client/ExecTest.java @@ -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; @@ -35,6 +39,7 @@ 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; @@ -42,6 +47,19 @@ /** 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\"}]}}"; @@ -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) @@ -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"))