1919import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
2020import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
2121import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
22+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
2223import static org .junit .Assert .assertEquals ;
2324import static org .junit .Assert .assertNotNull ;
2425
26+ import com .github .tomakehurst .wiremock .core .Admin ;
27+ import com .github .tomakehurst .wiremock .extension .Parameters ;
28+ import com .github .tomakehurst .wiremock .extension .PostServeAction ;
2529import com .github .tomakehurst .wiremock .junit .WireMockRule ;
30+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
31+
2632import com .google .gson .Gson ;
2733import io .kubernetes .client .informer .SharedIndexInformer ;
2834import io .kubernetes .client .informer .SharedInformerFactory ;
3743import io .kubernetes .client .util .ClientBuilder ;
3844import io .kubernetes .client .util .generic .GenericKubernetesApi ;
3945import java .util .Arrays ;
40- import org .junit .Rule ;
46+ import java .util .concurrent .Semaphore ;
47+ import org .junit .ClassRule ;
4148import org .junit .Test ;
4249import org .junit .runner .RunWith ;
4350import org .springframework .beans .factory .annotation .Autowired ;
5057@ SpringBootTest
5158public class KubernetesInformerCreatorTest {
5259
53- @ Rule public WireMockRule wireMockRule = new WireMockRule (8188 );
60+ public static class CountRequestAction extends PostServeAction {
61+ @ Override
62+ public String getName () {
63+ return "semaphore" ;
64+ }
65+
66+ @ Override
67+ public void doAction (ServeEvent serveEvent , Admin admin , Parameters parameters ) {
68+ Semaphore count = (Semaphore ) parameters .get ("semaphore" );
69+ count .release ();
70+ }
71+ }
72+
73+
74+
75+ @ ClassRule public static WireMockRule wireMockRule = new WireMockRule (options ().dynamicPort ().extensions (new CountRequestAction ()));
5476
5577 @ SpringBootApplication
5678 static class App {
5779
5880 @ Bean
5981 public ApiClient testingApiClient () {
60- ApiClient apiClient = new ClientBuilder ().setBasePath ("http://localhost:" + 8188 ).build ();
82+ ApiClient apiClient = new ClientBuilder ().setBasePath ("http://localhost:" + wireMockRule . port () ).build ();
6183 return apiClient ;
6284 }
6385
@@ -91,6 +113,13 @@ public void testInformerInjection() throws InterruptedException {
91113 assertNotNull (podInformer );
92114 assertNotNull (configMapInformer );
93115
116+ Semaphore getCount = new Semaphore (2 );
117+ Semaphore watchCount = new Semaphore (2 );
118+ Parameters getParams = new Parameters ();
119+ Parameters watchParams = new Parameters ();
120+ getParams .put ("semaphore" , getCount );
121+ watchParams .put ("semaphore" , watchCount );
122+
94123 V1Pod foo1 =
95124 new V1Pod ().kind ("Pod" ).metadata (new V1ObjectMeta ().namespace ("default" ).name ("foo1" ));
96125 V1ConfigMap bar1 =
@@ -100,6 +129,7 @@ public void testInformerInjection() throws InterruptedException {
100129
101130 wireMockRule .stubFor (
102131 get (urlMatching ("^/api/v1/pods.*" ))
132+ .withPostServeAction ("semaphore" , getParams )
103133 .withQueryParam ("watch" , equalTo ("false" ))
104134 .willReturn (
105135 aResponse ()
@@ -112,11 +142,13 @@ public void testInformerInjection() throws InterruptedException {
112142 .items (Arrays .asList (foo1 ))))));
113143 wireMockRule .stubFor (
114144 get (urlMatching ("^/api/v1/pods.*" ))
145+ .withPostServeAction ("semaphore" , watchParams )
115146 .withQueryParam ("watch" , equalTo ("true" ))
116147 .willReturn (aResponse ().withStatus (200 ).withBody ("{}" )));
117148
118149 wireMockRule .stubFor (
119150 get (urlMatching ("^/api/v1/namespaces/default/configmaps.*" ))
151+ .withPostServeAction ("semaphore" , getParams )
120152 .withQueryParam ("watch" , equalTo ("false" ))
121153 .willReturn (
122154 aResponse ()
@@ -129,12 +161,19 @@ public void testInformerInjection() throws InterruptedException {
129161 .items (Arrays .asList (bar1 ))))));
130162 wireMockRule .stubFor (
131163 get (urlMatching ("^/api/v1/namespaces/default/configmaps.*" ))
164+ .withPostServeAction ("semaphore" , watchParams )
132165 .withQueryParam ("watch" , equalTo ("true" ))
133166 .willReturn (aResponse ().withStatus (200 ).withBody ("{}" )));
134167
168+ // These will be released for each web call above.
169+ getCount .acquire (2 );
170+ watchCount .acquire (2 );
171+
135172 informerFactory .startAllRegisteredInformers ();
136173
137- Thread .sleep (200 );
174+ // Wait for the GETs to complete and the watches to start.
175+ getCount .acquire (2 );
176+ watchCount .acquire (2 );
138177
139178 verify (
140179 1 ,
0 commit comments