Skip to content

Commit f7e7ce3

Browse files
authored
Fix failing tests change logic (#1815)
* dirty * fix failing tests
1 parent e99a781 commit f7e7ce3

File tree

6 files changed

+184
-140
lines changed

6 files changed

+184
-140
lines changed

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.github.tomakehurst.wiremock.WireMockServer;
2626
import com.github.tomakehurst.wiremock.client.WireMock;
27+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
2728
import io.kubernetes.client.openapi.ApiClient;
2829
import io.kubernetes.client.openapi.Configuration;
2930
import io.kubernetes.client.openapi.JSON;
@@ -60,8 +61,10 @@
6061
import org.springframework.mock.env.MockEnvironment;
6162

6263
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
64+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
6365
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6466
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
67+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
6568
import 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

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.github.tomakehurst.wiremock.WireMockServer;
2828
import com.github.tomakehurst.wiremock.client.WireMock;
29+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
2930
import io.kubernetes.client.openapi.ApiClient;
3031
import io.kubernetes.client.openapi.Configuration;
3132
import io.kubernetes.client.openapi.JSON;
@@ -62,8 +63,10 @@
6263
import org.springframework.mock.env.MockEnvironment;
6364

6465
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
66+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
6567
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6668
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
69+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
6770
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6871

6972
/**
@@ -84,6 +87,10 @@ class EventReloadSecretTest {
8487

8588
private static final String NAMESPACE = "spring-k8s";
8689

90+
private static final String PATH = "/api/v1/namespaces/spring-k8s/secrets";
91+
92+
private static final String SCENARIO_NAME = "reload-test";
93+
8794
private static final AtomicBoolean STRATEGY_CALLED = new AtomicBoolean(false);
8895

8996
private static CoreV1Api coreV1Api;
@@ -101,6 +108,12 @@ static void setup() {
101108
wireMockServer.start();
102109
WireMock.configureFor("localhost", wireMockServer.port());
103110

111+
// something that the informer can work with. Since we do not care about this one
112+
// in the test, we mock it to return a 500 as it does not matter anyway.
113+
stubFor(get(urlPathMatching(PATH)).withQueryParam("resourceVersion", equalTo("0"))
114+
.withQueryParam("watch", equalTo("false"))
115+
.willReturn(aResponse().withStatus(500).withBody("Error From Informer")));
116+
104117
ApiClient client = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
105118
client.setDebugging(true);
106119
MOCK_STATIC.when(KubernetesClientUtils::createApiClientForInformerClient).thenReturn(client);
@@ -110,30 +123,6 @@ static void setup() {
110123
.thenReturn(NAMESPACE);
111124
Configuration.setDefaultApiClient(client);
112125
coreV1Api = new CoreV1Api();
113-
114-
String path = "/api/v1/namespaces/spring-k8s/secrets";
115-
V1Secret secretOne = secret(SECRET_NAME, Map.of());
116-
V1SecretList listOne = new V1SecretList().addItemsItem(secretOne);
117-
118-
// needed so that our environment is populated with 'something'
119-
// this call is done in the method that returns the AbstractEnvironment
120-
stubFor(get(path).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
121-
.inScenario("mine-test")
122-
.willSetStateTo("go-to-fail"));
123-
124-
// first call will fail
125-
stubFor(get(path).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
126-
.inScenario("mine-test")
127-
.whenScenarioStateIs("go-to-fail")
128-
.willSetStateTo("go-to-ok"));
129-
130-
// second call passes (change data so that reload is triggered)
131-
secretOne = secret(SECRET_NAME, Map.of("a", "b"));
132-
listOne = new V1SecretList().addItemsItem(secretOne);
133-
stubFor(get(path).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
134-
.inScenario("mine-test")
135-
.whenScenarioStateIs("go-to-ok")
136-
.willSetStateTo("done"));
137126
}
138127

139128
@AfterAll
@@ -152,6 +141,12 @@ static void after() {
152141
*/
153142
@Test
154143
void test(CapturedOutput output) {
144+
// first call will fail
145+
stubFor(get(PATH).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
146+
.inScenario(SCENARIO_NAME)
147+
.whenScenarioStateIs("go-to-fail")
148+
.willSetStateTo("go-to-ok"));
149+
155150
V1Secret secretNotMine = secret("not" + SECRET_NAME, Map.of());
156151
kubernetesClientEventBasedSecretsChangeDetector.onEvent(secretNotMine);
157152

@@ -165,6 +160,14 @@ void test(CapturedOutput output) {
165160
return one && two && three && updateStrategyNotCalled;
166161
});
167162

163+
// second call passes (change data so that reload is triggered)
164+
V1Secret secret = secret(SECRET_NAME, Map.of("a", "b"));
165+
V1SecretList secretList = new V1SecretList().addItemsItem(secret);
166+
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(secretList)))
167+
.inScenario(SCENARIO_NAME)
168+
.whenScenarioStateIs("go-to-ok")
169+
.willSetStateTo("done"));
170+
168171
// trigger the call again
169172
V1Secret secretMine = secret(SECRET_NAME, Map.of());
170173
kubernetesClientEventBasedSecretsChangeDetector.onEvent(secretMine);
@@ -200,6 +203,17 @@ VisibleKubernetesClientEventBasedSecretsChangeDetector kubernetesClientEventBase
200203
@Bean
201204
@Primary
202205
AbstractEnvironment environment() {
206+
207+
// needed so that our environment is populated with 'something'
208+
// this call is done in the method that returns the AbstractEnvironment
209+
V1Secret secret = secret(SECRET_NAME, Map.of());
210+
V1SecretList secretList = new V1SecretList().addItemsItem(secret);
211+
212+
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(secretList)))
213+
.inScenario(SCENARIO_NAME)
214+
.whenScenarioStateIs(Scenario.STARTED)
215+
.willSetStateTo("go-to-fail"));
216+
203217
MockEnvironment mockEnvironment = new MockEnvironment();
204218
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);
205219

@@ -222,7 +236,7 @@ AbstractEnvironment environment() {
222236
@Primary
223237
ConfigReloadProperties configReloadProperties() {
224238
return new ConfigReloadProperties(true, true, false, ConfigReloadProperties.ReloadStrategy.REFRESH,
225-
ConfigReloadProperties.ReloadDetectionMode.POLLING, Duration.ofMillis(2000), Set.of("non-default"),
239+
ConfigReloadProperties.ReloadDetectionMode.POLLING, Duration.ofMillis(2000), Set.of("spring-k8s"),
226240
false, Duration.ofSeconds(2));
227241
}
228242

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.github.tomakehurst.wiremock.WireMockServer;
2626
import com.github.tomakehurst.wiremock.client.WireMock;
27+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
2728
import io.kubernetes.client.openapi.ApiClient;
2829
import io.kubernetes.client.openapi.Configuration;
2930
import io.kubernetes.client.openapi.JSON;
@@ -72,18 +73,22 @@
7273
@ExtendWith(OutputCaptureExtension.class)
7374
class PollingReloadConfigMapTest {
7475

75-
private static WireMockServer wireMockServer;
76-
7776
private static final boolean FAIL_FAST = false;
7877

7978
private static final String CONFIG_MAP_NAME = "mine";
8079

80+
private static final String PATH = "/api/v1/namespaces/spring-k8s/configmaps";
81+
8182
private static final String NAMESPACE = "spring-k8s";
8283

84+
private static final String SCENARIO_NAME = "reload-test";
85+
8386
private static final AtomicBoolean STRATEGY_CALLED = new AtomicBoolean(false);
8487

8588
private static CoreV1Api coreV1Api;
8689

90+
private static WireMockServer wireMockServer;
91+
8792
@BeforeAll
8893
static void setup() {
8994
wireMockServer = new WireMockServer(options().dynamicPort());
@@ -95,30 +100,6 @@ static void setup() {
95100
client.setDebugging(true);
96101
Configuration.setDefaultApiClient(client);
97102
coreV1Api = new CoreV1Api();
98-
99-
String path = "/api/v1/namespaces/spring-k8s/configmaps";
100-
V1ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
101-
V1ConfigMapList listOne = new V1ConfigMapList().addItemsItem(configMapOne);
102-
103-
// needed so that our environment is populated with 'something'
104-
// this call is done in the method that returns the AbstractEnvironment
105-
stubFor(get(path).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
106-
.inScenario("my-test")
107-
.willSetStateTo("go-to-fail"));
108-
109-
// first reload call fails
110-
stubFor(get(path).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
111-
.inScenario("my-test")
112-
.whenScenarioStateIs("go-to-fail")
113-
.willSetStateTo("go-to-ok"));
114-
115-
// second reload call passes
116-
V1ConfigMap configMapTwo = configMap(CONFIG_MAP_NAME, Map.of("a", "b"));
117-
V1ConfigMapList listTwo = new V1ConfigMapList().addItemsItem(configMapTwo);
118-
stubFor(get(path).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listTwo)))
119-
.inScenario("my-test")
120-
.whenScenarioStateIs("go-to-ok"));
121-
122103
}
123104

124105
@AfterAll
@@ -135,17 +116,33 @@ static void after() {
135116
*/
136117
@Test
137118
void test(CapturedOutput output) {
119+
120+
// first reload call fails
121+
stubFor(get(PATH).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
122+
.inScenario(SCENARIO_NAME)
123+
.whenScenarioStateIs("go-to-fail")
124+
.willSetStateTo("go-to-ok"));
125+
138126
// we fail while reading 'configMapOne'
139-
Awaitility.await().atMost(Duration.ofSeconds(20)).pollInterval(Duration.ofSeconds(1)).until(() -> {
127+
Awaitility.await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofSeconds(1)).until(() -> {
140128
boolean one = output.getOut().contains("Failure in reading named sources");
141129
boolean two = output.getOut().contains("Failed to load source");
142130
boolean three = output.getOut()
143131
.contains("Reloadable condition was not satisfied, reload will not be triggered");
144132
boolean updateStrategyNotCalled = !STRATEGY_CALLED.get();
145-
System.out.println("one: " + one + " two: " + two + " three: " + three + " updateStrategyNotCalled: " + updateStrategyNotCalled);
133+
System.out.println("one: " + one + " two: " + two + " three: " + three + " updateStrategyNotCalled: "
134+
+ updateStrategyNotCalled);
146135
return one && two && three && updateStrategyNotCalled;
147136
});
148137

138+
// second reload call passes
139+
V1ConfigMap configMapTwo = configMap(CONFIG_MAP_NAME, Map.of("a", "b"));
140+
V1ConfigMapList listTwo = new V1ConfigMapList().addItemsItem(configMapTwo);
141+
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listTwo)))
142+
.inScenario(SCENARIO_NAME)
143+
.whenScenarioStateIs("go-to-ok")
144+
.willSetStateTo("done"));
145+
149146
System.out.println("first assertion passed");
150147

151148
// it passes while reading 'configMapTwo'
@@ -177,6 +174,17 @@ PollingConfigMapChangeDetector pollingConfigMapChangeDetector(AbstractEnvironmen
177174
@Bean
178175
@Primary
179176
AbstractEnvironment environment() {
177+
178+
V1ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
179+
V1ConfigMapList listOne = new V1ConfigMapList().addItemsItem(configMapOne);
180+
181+
// needed so that our environment is populated with 'something'
182+
// this call is done in the method that returns the AbstractEnvironment
183+
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
184+
.inScenario(SCENARIO_NAME)
185+
.whenScenarioStateIs(Scenario.STARTED)
186+
.willSetStateTo("go-to-fail"));
187+
180188
MockEnvironment mockEnvironment = new MockEnvironment();
181189
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);
182190

0 commit comments

Comments
 (0)