19
19
import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
20
20
import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
21
21
import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
22
+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
22
23
import static org .junit .Assert .assertEquals ;
23
24
import static org .junit .Assert .assertNotNull ;
24
25
26
+ import com .github .tomakehurst .wiremock .core .Admin ;
27
+ import com .github .tomakehurst .wiremock .extension .Parameters ;
28
+ import com .github .tomakehurst .wiremock .extension .PostServeAction ;
25
29
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
30
+ import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
26
31
import com .google .gson .Gson ;
27
32
import io .kubernetes .client .informer .SharedIndexInformer ;
28
33
import io .kubernetes .client .informer .SharedInformerFactory ;
37
42
import io .kubernetes .client .util .ClientBuilder ;
38
43
import io .kubernetes .client .util .generic .GenericKubernetesApi ;
39
44
import java .util .Arrays ;
40
- import org .junit .Rule ;
45
+ import java .util .concurrent .Semaphore ;
46
+ import org .junit .ClassRule ;
41
47
import org .junit .Test ;
42
48
import org .junit .runner .RunWith ;
43
49
import org .springframework .beans .factory .annotation .Autowired ;
50
56
@ SpringBootTest
51
57
public class KubernetesInformerCreatorTest {
52
58
53
- @ Rule public WireMockRule wireMockRule = new WireMockRule (8188 );
59
+ public static class CountRequestAction extends PostServeAction {
60
+ @ Override
61
+ public String getName () {
62
+ return "semaphore" ;
63
+ }
64
+
65
+ @ Override
66
+ public void doAction (ServeEvent serveEvent , Admin admin , Parameters parameters ) {
67
+ Semaphore count = (Semaphore ) parameters .get ("semaphore" );
68
+ count .release ();
69
+ }
70
+ }
71
+
72
+ @ ClassRule
73
+ public static WireMockRule wireMockRule =
74
+ new WireMockRule (options ().dynamicPort ().extensions (new CountRequestAction ()));
54
75
55
76
@ SpringBootApplication
56
77
static class App {
57
78
58
79
@ Bean
59
80
public ApiClient testingApiClient () {
60
- ApiClient apiClient = new ClientBuilder ().setBasePath ("http://localhost:" + 8188 ).build ();
81
+ ApiClient apiClient =
82
+ new ClientBuilder ().setBasePath ("http://localhost:" + wireMockRule .port ()).build ();
61
83
return apiClient ;
62
84
}
63
85
@@ -91,6 +113,13 @@ public void testInformerInjection() throws InterruptedException {
91
113
assertNotNull (podInformer );
92
114
assertNotNull (configMapInformer );
93
115
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
+
94
123
V1Pod foo1 =
95
124
new V1Pod ().kind ("Pod" ).metadata (new V1ObjectMeta ().namespace ("default" ).name ("foo1" ));
96
125
V1ConfigMap bar1 =
@@ -100,6 +129,7 @@ public void testInformerInjection() throws InterruptedException {
100
129
101
130
wireMockRule .stubFor (
102
131
get (urlMatching ("^/api/v1/pods.*" ))
132
+ .withPostServeAction ("semaphore" , getParams )
103
133
.withQueryParam ("watch" , equalTo ("false" ))
104
134
.willReturn (
105
135
aResponse ()
@@ -112,11 +142,13 @@ public void testInformerInjection() throws InterruptedException {
112
142
.items (Arrays .asList (foo1 ))))));
113
143
wireMockRule .stubFor (
114
144
get (urlMatching ("^/api/v1/pods.*" ))
145
+ .withPostServeAction ("semaphore" , watchParams )
115
146
.withQueryParam ("watch" , equalTo ("true" ))
116
147
.willReturn (aResponse ().withStatus (200 ).withBody ("{}" )));
117
148
118
149
wireMockRule .stubFor (
119
150
get (urlMatching ("^/api/v1/namespaces/default/configmaps.*" ))
151
+ .withPostServeAction ("semaphore" , getParams )
120
152
.withQueryParam ("watch" , equalTo ("false" ))
121
153
.willReturn (
122
154
aResponse ()
@@ -129,12 +161,19 @@ public void testInformerInjection() throws InterruptedException {
129
161
.items (Arrays .asList (bar1 ))))));
130
162
wireMockRule .stubFor (
131
163
get (urlMatching ("^/api/v1/namespaces/default/configmaps.*" ))
164
+ .withPostServeAction ("semaphore" , watchParams )
132
165
.withQueryParam ("watch" , equalTo ("true" ))
133
166
.willReturn (aResponse ().withStatus (200 ).withBody ("{}" )));
134
167
168
+ // These will be released for each web call above.
169
+ getCount .acquire (2 );
170
+ watchCount .acquire (2 );
171
+
135
172
informerFactory .startAllRegisteredInformers ();
136
173
137
- Thread .sleep (200 );
174
+ // Wait for the GETs to complete and the watches to start.
175
+ getCount .acquire (2 );
176
+ watchCount .acquire (2 );
138
177
139
178
verify (
140
179
1 ,
0 commit comments