2020import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
2121import 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 ;
2326import com .github .tomakehurst .wiremock .junit .WireMockRule ;
27+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
28+ import com .google .common .io .ByteStreams ;
2429import io .kubernetes .client .Exec .ExecProcess ;
2530import io .kubernetes .client .openapi .ApiClient ;
2631import io .kubernetes .client .openapi .ApiException ;
3540import java .io .OutputStream ;
3641import java .nio .charset .StandardCharsets ;
3742import java .util .concurrent .CountDownLatch ;
43+ import java .util .concurrent .Semaphore ;
3844import org .junit .Before ;
3945import org .junit .Rule ;
4046import org .junit .Test ;
4147
4248/** Tests for the Exec helper class */
4349public 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