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 ;
2428import io .kubernetes .client .Exec .ExecProcess ;
2529import io .kubernetes .client .openapi .ApiClient ;
2630import io .kubernetes .client .openapi .ApiException ;
3539import java .io .OutputStream ;
3640import java .nio .charset .StandardCharsets ;
3741import java .util .concurrent .CountDownLatch ;
42+ import java .util .concurrent .Semaphore ;
3843import org .junit .Before ;
3944import org .junit .Rule ;
4045import org .junit .Test ;
4146
4247/** Tests for the Exec helper class */
4348public 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