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 () {
@@ -171,8 +192,13 @@ void copyFileToPod(@TempDir Path tempDir) throws Exception {
171192
172193 Copy copy = new Copy (client );
173194
195+ CountDownLatch latch = new CountDownLatch (1 );
196+ Parameters params = new Parameters ();
197+ params .put ("latch" , latch );
198+
174199 apiServer .stubFor (
175200 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
201+ .withPostServeAction ("countdown" , params )
176202 .willReturn (
177203 aResponse ()
178204 .withStatus (404 )
@@ -195,7 +221,7 @@ public void run() {
195221 }
196222 });
197223 t .start ();
198- Thread . sleep ( 2000 );
224+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
199225 t .interrupt ();
200226
201227 apiServer .verify (
@@ -217,8 +243,13 @@ void copyBinaryDataToPod() throws InterruptedException {
217243
218244 Copy copy = new Copy (client );
219245
246+ CountDownLatch latch = new CountDownLatch (1 );
247+ Parameters params = new Parameters ();
248+ params .put ("latch" , latch );
249+
220250 apiServer .stubFor (
221251 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
252+ .withPostServeAction ("countdown" , params )
222253 .willReturn (
223254 aResponse ()
224255 .withStatus (404 )
@@ -241,7 +272,7 @@ public void run() {
241272 }
242273 });
243274 t .start ();
244- Thread . sleep ( 2000 );
275+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
245276 t .interrupt ();
246277
247278 apiServer .verify (
@@ -260,8 +291,13 @@ public void run() {
260291 void testCopyDirectoryFromPod (@ TempDir Path tempDir ) throws Exception {
261292 Copy copy = new Copy (client );
262293
294+ CountDownLatch latch = new CountDownLatch (1 );
295+ Parameters params = new Parameters ();
296+ params .put ("latch" , latch );
297+
263298 apiServer .stubFor (
264299 get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
300+ .withPostServeAction ("countdown" , params )
265301 .willReturn (
266302 aResponse ()
267303 .withStatus (404 )
@@ -285,7 +321,7 @@ public void run() {
285321 }
286322 });
287323 t .start ();
288- Thread . sleep ( 2000 );
324+ assertTrue ( latch . await ( 30 , TimeUnit . SECONDS ) );
289325 t .interrupt ();
290326
291327 apiServer .verify (
0 commit comments