Skip to content

Commit 732fed0

Browse files
Deflake a test.
1 parent a98b888 commit 732fed0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
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;
28+
import com.google.common.io.ByteStreams;
2429
import io.kubernetes.client.Exec.ExecProcess;
2530
import io.kubernetes.client.openapi.ApiClient;
2631
import io.kubernetes.client.openapi.ApiException;
@@ -35,13 +40,27 @@
3540
import java.io.OutputStream;
3641
import java.nio.charset.StandardCharsets;
3742
import java.util.concurrent.CountDownLatch;
43+
import java.util.concurrent.Semaphore;
3844
import org.junit.Before;
3945
import org.junit.Rule;
4046
import org.junit.Test;
4147

4248
/** Tests for the Exec helper class */
4349
public class ExecTest {
4450

51+
public static class CountRequestAction extends PostServeAction {
52+
@Override
53+
public String getName() {
54+
return "semaphore";
55+
}
56+
57+
@Override
58+
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
59+
Semaphore count = (Semaphore) parameters.get("semaphore");
60+
count.release();
61+
}
62+
}
63+
4564
private static final String OUTPUT_EXIT0 = "{\"metadata\":{},\"status\":\"Success\"}";
4665
private static final String OUTPUT_EXIT1 =
4766
"{\"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 +154,13 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
135154

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

157+
Semaphore getCount = new Semaphore(2);
158+
Parameters getParams = new Parameters();
159+
getParams.put("semaphore", getCount);
160+
138161
wireMockRule.stubFor(
139162
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
163+
.withPostServeAction("semaphore", getParams)
140164
.willReturn(
141165
aResponse()
142166
.withStatus(404)
@@ -152,6 +176,11 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
152176
.setStderr(false)
153177
.execute()
154178
.waitFor();
179+
180+
// These will be released for each web call above.
181+
// There is a race between the above waitFor() and the request actually being recorded
182+
// by WireMock. This fixes it.
183+
getCount.acquire(2);
155184

156185
wireMockRule.verify(
157186
getRequestedFor(

0 commit comments

Comments
 (0)