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 ;
31
+
26
32
import com .google .gson .Gson ;
27
33
import io .kubernetes .client .informer .SharedIndexInformer ;
28
34
import io .kubernetes .client .informer .SharedInformerFactory ;
37
43
import io .kubernetes .client .util .ClientBuilder ;
38
44
import io .kubernetes .client .util .generic .GenericKubernetesApi ;
39
45
import java .util .Arrays ;
40
- import org .junit .Rule ;
46
+ import java .util .concurrent .Semaphore ;
47
+ import org .junit .ClassRule ;
41
48
import org .junit .Test ;
42
49
import org .junit .runner .RunWith ;
43
50
import org .springframework .beans .factory .annotation .Autowired ;
50
57
@ SpringBootTest
51
58
public class KubernetesInformerCreatorTest {
52
59
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 ()));
54
76
55
77
@ SpringBootApplication
56
78
static class App {
57
79
58
80
@ Bean
59
81
public ApiClient testingApiClient () {
60
- ApiClient apiClient = new ClientBuilder ().setBasePath ("http://localhost:" + 8188 ).build ();
82
+ ApiClient apiClient = 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