25
25
import static org .mockito .Mockito .times ;
26
26
import static org .mockito .Mockito .verify ;
27
27
28
+ import com .github .tomakehurst .wiremock .core .Admin ;
28
29
import com .github .tomakehurst .wiremock .extension .Parameters ;
30
+ import com .github .tomakehurst .wiremock .extension .PostServeAction ;
29
31
import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
32
+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
30
33
import io .kubernetes .client .Exec .ExecProcess ;
31
34
import io .kubernetes .client .openapi .ApiClient ;
32
35
import io .kubernetes .client .openapi .ApiException ;
41
44
import java .io .OutputStream ;
42
45
import java .nio .charset .StandardCharsets ;
43
46
import java .util .concurrent .CountDownLatch ;
44
- import java .util .concurrent .Semaphore ;
45
47
import java .util .function .Consumer ;
46
48
import org .junit .jupiter .api .BeforeEach ;
47
49
import org .junit .jupiter .api .Test ;
50
52
/** Tests for the Exec helper class */
51
53
class ExecTest {
52
54
55
+ public static class CountDownLatchAction extends PostServeAction {
56
+ @ Override
57
+ public String getName () {
58
+ return "countdown" ;
59
+ }
60
+
61
+ @ Override
62
+ public void doAction (ServeEvent serveEvent , Admin admin , Parameters parameters ) {
63
+ CountDownLatch latch = (CountDownLatch ) parameters .get ("latch" );
64
+ latch .countDown ();
65
+ }
66
+ }
67
+
53
68
private static final String OUTPUT_EXIT0 = "{\" metadata\" :{},\" status\" :\" Success\" }" ;
54
69
private static final String OUTPUT_EXIT1 =
55
70
"{\" 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\" }]}}" ;
@@ -68,7 +83,9 @@ class ExecTest {
68
83
69
84
@ RegisterExtension
70
85
static WireMockExtension apiServer =
71
- WireMockExtension .newInstance ().options (options ().dynamicPort ()).build ();
86
+ WireMockExtension .newInstance ()
87
+ .options (options ().dynamicPort ().extensions (new CountDownLatchAction ()))
88
+ .build ();
72
89
73
90
@ BeforeEach
74
91
void setup () {
@@ -145,15 +162,10 @@ void terminalResize() throws IOException, InterruptedException {
145
162
final ExecProcess process = new ExecProcess (client );
146
163
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
147
164
148
- System .out .println ("Injecting output stream" );
149
165
process .getHandler ().injectOutputStream (4 , bos );
150
- System .out .println ("Resizing output stream" );
151
166
process .resize (100 , 100 );
152
- System .out .println ("Resizing output stream" );
153
167
process .destroy ();
154
168
155
- System .out .println ("Going to tests." );
156
-
157
169
String out = bos .toString ("UTF-8" );
158
170
assertThat (out ).isEqualTo ("{ \" width\" : 100, \" height\" : 100 }\n " );
159
171
}
@@ -196,13 +208,13 @@ void url() throws IOException, ApiException, InterruptedException {
196
208
197
209
V1Pod pod = new V1Pod ().metadata (new V1ObjectMeta ().name (podName ).namespace (namespace ));
198
210
199
- Semaphore getCount = new Semaphore (2 );
200
- Parameters getParams = new Parameters ();
201
- getParams .put ("semaphore " , getCount );
211
+ CountDownLatch latch = new CountDownLatch (2 );
212
+ Parameters params = new Parameters ();
213
+ params .put ("latch " , latch );
202
214
203
215
apiServer .stubFor (
204
216
get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
205
- .withPostServeAction ("semaphore " , getParams )
217
+ .withPostServeAction ("countdown " , params )
206
218
.willReturn (
207
219
aResponse ()
208
220
.withStatus (404 )
@@ -218,11 +230,7 @@ void url() throws IOException, ApiException, InterruptedException {
218
230
.setStderr (false )
219
231
.execute ()
220
232
.waitFor ();
221
-
222
- // These will be released for each web call above.
223
- // There is a race between the above waitFor() and the request actually being recorded
224
- // by WireMock. This fixes it.
225
- getCount .acquire (2 );
233
+ latch .await ();
226
234
227
235
apiServer .verify (
228
236
getRequestedFor (
0 commit comments