20
20
import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
21
21
import static org .junit .Assert .assertEquals ;
22
22
23
+ import com .github .tomakehurst .wiremock .core .Admin ;
24
+ import com .github .tomakehurst .wiremock .extension .Parameters ;
25
+ import com .github .tomakehurst .wiremock .extension .PostServeAction ;
23
26
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
27
+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
24
28
import io .kubernetes .client .Exec .ExecProcess ;
25
29
import io .kubernetes .client .openapi .ApiClient ;
26
30
import io .kubernetes .client .openapi .ApiException ;
35
39
import java .io .OutputStream ;
36
40
import java .nio .charset .StandardCharsets ;
37
41
import java .util .concurrent .CountDownLatch ;
42
+ import java .util .concurrent .Semaphore ;
38
43
import org .junit .Before ;
39
44
import org .junit .Rule ;
40
45
import org .junit .Test ;
41
46
42
47
/** Tests for the Exec helper class */
43
48
public class ExecTest {
44
49
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
+
45
63
private static final String OUTPUT_EXIT0 = "{\" metadata\" :{},\" status\" :\" Success\" }" ;
46
64
private static final String OUTPUT_EXIT1 =
47
65
"{\" 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 {
135
153
136
154
V1Pod pod = new V1Pod ().metadata (new V1ObjectMeta ().name (podName ).namespace (namespace ));
137
155
156
+ Semaphore getCount = new Semaphore (2 );
157
+ Parameters getParams = new Parameters ();
158
+ getParams .put ("semaphore" , getCount );
159
+
138
160
wireMockRule .stubFor (
139
161
get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
162
+ .withPostServeAction ("semaphore" , getParams )
140
163
.willReturn (
141
164
aResponse ()
142
165
.withStatus (404 )
@@ -153,6 +176,11 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
153
176
.execute ()
154
177
.waitFor ();
155
178
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
+
156
184
wireMockRule .verify (
157
185
getRequestedFor (
158
186
urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
0 commit comments