Skip to content

Commit 0f2a3be

Browse files
Deflake a test.
1 parent a98b888 commit 0f2a3be

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2121
import static org.junit.Assert.assertEquals;
2222

23+
import com.github.tomakehurst.wiremock.core.Admin;
24+
import com.github.tomakehurst.wiremock.extension.Parameters;
25+
import com.github.tomakehurst.wiremock.extension.PostServeAction;
2326
import com.github.tomakehurst.wiremock.junit.WireMockRule;
27+
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
2428
import io.kubernetes.client.Exec.ExecProcess;
2529
import io.kubernetes.client.openapi.ApiClient;
2630
import io.kubernetes.client.openapi.ApiException;
@@ -35,13 +39,27 @@
3539
import java.io.OutputStream;
3640
import java.nio.charset.StandardCharsets;
3741
import java.util.concurrent.CountDownLatch;
42+
import java.util.concurrent.Semaphore;
3843
import org.junit.Before;
3944
import org.junit.Rule;
4045
import org.junit.Test;
4146

4247
/** Tests for the Exec helper class */
4348
public class ExecTest {
4449

50+
public static class CountRequestAction extends PostServeAction {
51+
@Override
52+
public String getName() {
53+
return "semaphore";
54+
}
55+
56+
@Override
57+
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
58+
Semaphore count = (Semaphore) parameters.get("semaphore");
59+
count.release();
60+
}
61+
}
62+
4563
private static final String OUTPUT_EXIT0 = "{\"metadata\":{},\"status\":\"Success\"}";
4664
private static final String OUTPUT_EXIT1 =
4765
"{\"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 {
135153

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

156+
Semaphore getCount = new Semaphore(2);
157+
Parameters getParams = new Parameters();
158+
getParams.put("semaphore", getCount);
159+
138160
wireMockRule.stubFor(
139161
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
162+
.withPostServeAction("semaphore", getParams)
140163
.willReturn(
141164
aResponse()
142165
.withStatus(404)
@@ -153,6 +176,11 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
153176
.execute()
154177
.waitFor();
155178

179+
// These will be released for each web call above.
180+
// There is a race between the above waitFor() and the request actually being recorded
181+
// by WireMock. This fixes it.
182+
getCount.acquire(2);
183+
156184
wireMockRule.verify(
157185
getRequestedFor(
158186
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))

0 commit comments

Comments
 (0)