1919import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
2020import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
2121
22+ import com .github .tomakehurst .wiremock .core .Admin ;
23+ import com .github .tomakehurst .wiremock .extension .Parameters ;
24+ import com .github .tomakehurst .wiremock .extension .PostServeAction ;
2225import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
2326import com .github .tomakehurst .wiremock .matching .AnythingPattern ;
27+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
2428import io .kubernetes .client .openapi .ApiClient ;
2529import io .kubernetes .client .openapi .ApiException ;
2630import io .kubernetes .client .openapi .models .V1ObjectMeta ;
3640import java .nio .file .Paths ;
3741import java .util .HashMap ;
3842import java .util .Map ;
43+ import java .util .concurrent .CountDownLatch ;
44+ import java .util .concurrent .TimeUnit ;
3945import org .junit .jupiter .api .BeforeEach ;
4046import org .junit .jupiter .api .Test ;
4147import org .junit .jupiter .api .extension .RegisterExtension ;
@@ -119,9 +125,24 @@ public InputStream copyFileFromPod(String namespace, String pod, String srcPath)
119125 }
120126 }
121127
128+ public static class CountDownLatchAction extends PostServeAction {
129+ @ Override
130+ public String getName () {
131+ return "countdown" ;
132+ }
133+
134+ @ Override
135+ public void doAction (ServeEvent serveEvent , Admin admin , Parameters parameters ) {
136+ CountDownLatch latch = (CountDownLatch ) parameters .get ("latch" );
137+ latch .countDown ();
138+ }
139+ }
140+
122141 @ RegisterExtension
123142 static WireMockExtension apiServer =
124- WireMockExtension .newInstance ().options (wireMockConfig ().dynamicPort ()).build ();
143+ WireMockExtension .newInstance ()
144+ .options (wireMockConfig ().dynamicPort ().extensions (new CountDownLatchAction ()))
145+ .build ();
125146
126147 @ BeforeEach
127148 void setup () {
@@ -131,21 +152,6 @@ void setup() {
131152 podName = "apod" ;
132153 }
133154
134- // Wait (bounded) until the exec request reaches the mock server, instead of a fixed sleep
135- // which is flaky on slow CI runners.
136- private void waitForExecRequest () throws InterruptedException {
137- long deadline = System .currentTimeMillis () + 30000 ;
138- while (System .currentTimeMillis () < deadline
139- && apiServer
140- .findAll (
141- getRequestedFor (
142- urlPathEqualTo (
143- "/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" )))
144- .isEmpty ()) {
145- Thread .sleep (100 );
146- }
147- }
148-
149155 @ Test
150156 void url () {
151157 Copy copy = new Copy (client );
@@ -186,8 +192,13 @@ void copyFileToPod(@TempDir Path tempDir) throws Exception {
186192
187193 Copy copy = new Copy (client );
188194
195+ CountDownLatch latch = new CountDownLatch (1 );
196+ Parameters params = new Parameters ();
197+ params .put ("latch" , latch );
198+
189199 apiServer .stubFor (
190200 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
201+ .withPostServeAction ("countdown" , params )
191202 .willReturn (
192203 aResponse ()
193204 .withStatus (404 )
@@ -210,7 +221,7 @@ public void run() {
210221 }
211222 });
212223 t .start ();
213- waitForExecRequest ( );
224+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
214225 t .interrupt ();
215226
216227 apiServer .verify (
@@ -232,8 +243,13 @@ void copyBinaryDataToPod() throws InterruptedException {
232243
233244 Copy copy = new Copy (client );
234245
246+ CountDownLatch latch = new CountDownLatch (1 );
247+ Parameters params = new Parameters ();
248+ params .put ("latch" , latch );
249+
235250 apiServer .stubFor (
236251 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
252+ .withPostServeAction ("countdown" , params )
237253 .willReturn (
238254 aResponse ()
239255 .withStatus (404 )
@@ -256,7 +272,7 @@ public void run() {
256272 }
257273 });
258274 t .start ();
259- waitForExecRequest ( );
275+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
260276 t .interrupt ();
261277
262278 apiServer .verify (
@@ -275,8 +291,13 @@ public void run() {
275291 void testCopyDirectoryFromPod (@ TempDir Path tempDir ) throws Exception {
276292 Copy copy = new Copy (client );
277293
294+ CountDownLatch latch = new CountDownLatch (1 );
295+ Parameters params = new Parameters ();
296+ params .put ("latch" , latch );
297+
278298 apiServer .stubFor (
279299 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
300+ .withPostServeAction ("countdown" , params )
280301 .willReturn (
281302 aResponse ()
282303 .withStatus (404 )
@@ -300,7 +321,7 @@ public void run() {
300321 }
301322 });
302323 t .start ();
303- waitForExecRequest ( );
324+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
304325 t .interrupt ();
305326
306327 apiServer .verify (
0 commit comments