2424
2525import com .github .tomakehurst .wiremock .WireMockServer ;
2626import com .github .tomakehurst .wiremock .client .WireMock ;
27+ import com .github .tomakehurst .wiremock .stubbing .Scenario ;
2728import io .kubernetes .client .openapi .ApiClient ;
2829import io .kubernetes .client .openapi .Configuration ;
2930import io .kubernetes .client .openapi .JSON ;
6061import org .springframework .mock .env .MockEnvironment ;
6162
6263import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
64+ import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
6365import static com .github .tomakehurst .wiremock .client .WireMock .get ;
6466import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
67+ import static com .github .tomakehurst .wiremock .client .WireMock .urlPathMatching ;
6568import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
6669
6770/**
@@ -82,6 +85,10 @@ class EventReloadConfigMapTest {
8285
8386 private static final String NAMESPACE = "spring-k8s" ;
8487
88+ private static final String SCENARIO_NAME = "reload-test" ;
89+
90+ private static final String PATH = "/api/v1/namespaces/spring-k8s/configmaps" ;
91+
8592 private static final AtomicBoolean STRATEGY_CALLED = new AtomicBoolean (false );
8693
8794 private static CoreV1Api coreV1Api ;
@@ -99,6 +106,12 @@ static void setup() {
99106 wireMockServer .start ();
100107 WireMock .configureFor ("localhost" , wireMockServer .port ());
101108
109+ // something that the informer can work with. Since we do not care about this one
110+ // in the test, we mock it to return a 500 as it does not matter anyway.
111+ stubFor (get (urlPathMatching (PATH )).withQueryParam ("resourceVersion" , equalTo ("0" ))
112+ .withQueryParam ("watch" , equalTo ("false" ))
113+ .willReturn (aResponse ().withStatus (500 ).withBody ("Error From Informer" )));
114+
102115 ApiClient client = new ClientBuilder ().setBasePath ("http://localhost:" + wireMockServer .port ()).build ();
103116 client .setDebugging (true );
104117 MOCK_STATIC .when (KubernetesClientUtils ::createApiClientForInformerClient ).thenReturn (client );
@@ -108,30 +121,6 @@ static void setup() {
108121 .thenReturn (NAMESPACE );
109122 Configuration .setDefaultApiClient (client );
110123 coreV1Api = new CoreV1Api ();
111-
112- String path = "/api/v1/namespaces/spring-k8s/configmaps" ;
113- V1ConfigMap configMapOne = configMap (CONFIG_MAP_NAME , Map .of ());
114- V1ConfigMapList listOne = new V1ConfigMapList ().addItemsItem (configMapOne );
115-
116- // needed so that our environment is populated with 'something'
117- // this call is done in the method that returns the AbstractEnvironment
118- stubFor (get (path ).willReturn (aResponse ().withStatus (200 ).withBody (new JSON ().serialize (listOne )))
119- .inScenario ("mine-test" )
120- .willSetStateTo ("go-to-fail" ));
121-
122- // first call will fail
123- stubFor (get (path ).willReturn (aResponse ().withStatus (500 ).withBody ("Internal Server Error" ))
124- .inScenario ("mine-test" )
125- .whenScenarioStateIs ("go-to-fail" )
126- .willSetStateTo ("go-to-ok" ));
127-
128- // second call passes (change data so that reload is triggered)
129- configMapOne = configMap (CONFIG_MAP_NAME , Map .of ("a" , "b" ));
130- listOne = new V1ConfigMapList ().addItemsItem (configMapOne );
131- stubFor (get (path ).willReturn (aResponse ().withStatus (200 ).withBody (new JSON ().serialize (listOne )))
132- .inScenario ("mine-test" )
133- .whenScenarioStateIs ("go-to-ok" )
134- .willSetStateTo ("done" ));
135124 }
136125
137126 @ AfterAll
@@ -150,6 +139,13 @@ static void after() {
150139 */
151140 @ Test
152141 void test (CapturedOutput output ) {
142+
143+ // first call will fail
144+ stubFor (get (PATH ).willReturn (aResponse ().withStatus (500 ).withBody ("Internal Server Error" ))
145+ .inScenario (SCENARIO_NAME )
146+ .whenScenarioStateIs ("go-to-fail" )
147+ .willSetStateTo ("go-to-ok" ));
148+
153149 V1ConfigMap configMapNotMine = configMap ("not" + CONFIG_MAP_NAME , Map .of ());
154150 kubernetesClientEventBasedConfigMapChangeDetector .onEvent (configMapNotMine );
155151
@@ -163,6 +159,14 @@ void test(CapturedOutput output) {
163159 return one && two && three && updateStrategyNotCalled ;
164160 });
165161
162+ // second call passes (change data so that reload is triggered)
163+ V1ConfigMap configMap = configMap (CONFIG_MAP_NAME , Map .of ("a" , "b" ));
164+ V1ConfigMapList configMapList = new V1ConfigMapList ().addItemsItem (configMap );
165+ stubFor (get (PATH ).willReturn (aResponse ().withStatus (200 ).withBody (new JSON ().serialize (configMapList )))
166+ .inScenario (SCENARIO_NAME )
167+ .whenScenarioStateIs ("go-to-ok" )
168+ .willSetStateTo ("done" ));
169+
166170 // trigger the call again
167171 V1ConfigMap configMapMine = configMap (CONFIG_MAP_NAME , Map .of ());
168172 kubernetesClientEventBasedConfigMapChangeDetector .onEvent (configMapMine );
@@ -194,6 +198,17 @@ VisibleKubernetesClientEventBasedConfigMapChangeDetector kubernetesClientEventBa
194198 @ Bean
195199 @ Primary
196200 AbstractEnvironment environment () {
201+
202+ V1ConfigMap configMap = configMap (CONFIG_MAP_NAME , Map .of ());
203+ V1ConfigMapList configMapList = new V1ConfigMapList ().addItemsItem (configMap );
204+
205+ // needed so that our environment is populated with 'something'
206+ // this call is done in the method that returns the AbstractEnvironment
207+ stubFor (get (PATH ).willReturn (aResponse ().withStatus (200 ).withBody (new JSON ().serialize (configMapList )))
208+ .inScenario (SCENARIO_NAME )
209+ .whenScenarioStateIs (Scenario .STARTED )
210+ .willSetStateTo ("go-to-fail" ));
211+
197212 MockEnvironment mockEnvironment = new MockEnvironment ();
198213 mockEnvironment .setProperty ("spring.cloud.kubernetes.client.namespace" , NAMESPACE );
199214
@@ -217,7 +232,7 @@ AbstractEnvironment environment() {
217232 @ Primary
218233 ConfigReloadProperties configReloadProperties () {
219234 return new ConfigReloadProperties (true , true , false , ConfigReloadProperties .ReloadStrategy .REFRESH ,
220- ConfigReloadProperties .ReloadDetectionMode .POLLING , Duration .ofMillis (2000 ), Set .of ("non-default " ),
235+ ConfigReloadProperties .ReloadDetectionMode .POLLING , Duration .ofMillis (2000 ), Set .of ("spring-k8s " ),
221236 false , Duration .ofSeconds (2 ));
222237 }
223238
0 commit comments