Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Dec 5, 2024
1 parent 65b83f5 commit d563cec
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
import org.springframework.mock.env.MockEnvironment;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

/**
Expand Down Expand Up @@ -104,6 +106,12 @@ static void setup() {
wireMockServer.start();
WireMock.configureFor("localhost", wireMockServer.port());

// something that the informer can work with. Since we do not care about this one
// in the test, we mock it to return a 500 as it does not matter anyway.
stubFor(get(urlPathMatching(PATH)).withQueryParam("resourceVersion", equalTo("0"))
.withQueryParam("watch", equalTo("false"))
.willReturn(aResponse().withStatus(500).withBody("Error From Informer")));

ApiClient client = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
client.setDebugging(true);
MOCK_STATIC.when(KubernetesClientUtils::createApiClientForInformerClient).thenReturn(client);
Expand All @@ -113,16 +121,6 @@ static void setup() {
.thenReturn(NAMESPACE);
Configuration.setDefaultApiClient(client);
coreV1Api = new CoreV1Api();

V1ConfigMap configMap = configMap(CONFIG_MAP_NAME, Map.of());
V1ConfigMapList configMapList = new V1ConfigMapList().addItemsItem(configMap);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(configMapList)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));
}

@AfterAll
Expand All @@ -141,15 +139,16 @@ static void after() {
*/
@Test
void test(CapturedOutput output) {
V1ConfigMap configMapNotMine = configMap("not" + CONFIG_MAP_NAME, Map.of());
kubernetesClientEventBasedConfigMapChangeDetector.onEvent(configMapNotMine);

// first call will fail
stubFor(get(PATH).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs("go-to-fail")
.willSetStateTo("go-to-ok"));

V1ConfigMap configMapNotMine = configMap("not" + CONFIG_MAP_NAME, Map.of());
kubernetesClientEventBasedConfigMapChangeDetector.onEvent(configMapNotMine);

// we fail while reading 'configMapOne'
Awaitility.await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofSeconds(1)).until(() -> {
boolean one = output.getOut().contains("Failure in reading named sources");
Expand Down Expand Up @@ -199,6 +198,17 @@ VisibleKubernetesClientEventBasedConfigMapChangeDetector kubernetesClientEventBa
@Bean
@Primary
AbstractEnvironment environment() {

V1ConfigMap configMap = configMap(CONFIG_MAP_NAME, Map.of());
V1ConfigMapList configMapList = new V1ConfigMapList().addItemsItem(configMap);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(configMapList)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);

Expand All @@ -222,7 +232,7 @@ AbstractEnvironment environment() {
@Primary
ConfigReloadProperties configReloadProperties() {
return new ConfigReloadProperties(true, true, false, ConfigReloadProperties.ReloadStrategy.REFRESH,
ConfigReloadProperties.ReloadDetectionMode.POLLING, Duration.ofMillis(2000), Set.of("non-default"),
ConfigReloadProperties.ReloadDetectionMode.POLLING, Duration.ofMillis(2000), Set.of("spring-k8s"),
false, Duration.ofSeconds(2));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

/**
Expand All @@ -81,6 +81,8 @@ class EventReloadSecretTest {

private static final boolean FAIL_FAST = false;

private static WireMockServer wireMockServer;

private static final String SECRET_NAME = "mine";

private static final String NAMESPACE = "spring-k8s";
Expand All @@ -93,8 +95,6 @@ class EventReloadSecretTest {

private static CoreV1Api coreV1Api;

private static WireMockServer wireMockServer;

private static final MockedStatic<KubernetesClientUtils> MOCK_STATIC = Mockito
.mockStatic(KubernetesClientUtils.class);

Expand All @@ -108,6 +108,12 @@ static void setup() {
wireMockServer.start();
WireMock.configureFor("localhost", wireMockServer.port());

// something that the informer can work with. Since we do not care about this one
// in the test, we mock it to return a 500 as it does not matter anyway.
stubFor(get(urlPathMatching(PATH)).withQueryParam("resourceVersion", equalTo("0"))
.withQueryParam("watch", equalTo("false"))
.willReturn(aResponse().withStatus(500).withBody("Error From Informer")));

ApiClient client = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
client.setDebugging(true);
MOCK_STATIC.when(KubernetesClientUtils::createApiClientForInformerClient).thenReturn(client);
Expand All @@ -117,22 +123,6 @@ static void setup() {
.thenReturn(NAMESPACE);
Configuration.setDefaultApiClient(client);
coreV1Api = new CoreV1Api();

V1Secret secret = secret(SECRET_NAME, Map.of());
V1SecretList secretList = new V1SecretList().addItemsItem(secret);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH)
.willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(secretList)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

stubFor(get(urlPathEqualTo(PATH)).withQueryParam("resourceVersion", equalTo("0"))
.withQueryParam("watch", equalTo("false"))
.willReturn(aResponse().withStatus(500).withBody(" he he he")));

}

@AfterAll
Expand All @@ -151,15 +141,15 @@ static void after() {
*/
@Test
void test(CapturedOutput output) {
V1Secret secretNotMine = secret("not" + SECRET_NAME, Map.of());
kubernetesClientEventBasedSecretsChangeDetector.onEvent(secretNotMine);

// first call will fail
stubFor(get(PATH).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs("go-to-fail")
.willSetStateTo("go-to-ok"));

V1Secret secretNotMine = secret("not" + SECRET_NAME, Map.of());
kubernetesClientEventBasedSecretsChangeDetector.onEvent(secretNotMine);

// we fail while reading 'configMapOne'
Awaitility.await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofSeconds(1)).until(() -> {
boolean one = output.getOut().contains("Failure in reading named sources");
Expand All @@ -179,7 +169,7 @@ void test(CapturedOutput output) {
.willSetStateTo("done"));

// trigger the call again
V1Secret secretMine = secret(SECRET_NAME, Map.of("a", "b"));
V1Secret secretMine = secret(SECRET_NAME, Map.of());
kubernetesClientEventBasedSecretsChangeDetector.onEvent(secretMine);
Awaitility.await()
.atMost(Duration.ofSeconds(10))
Expand Down Expand Up @@ -213,6 +203,17 @@ VisibleKubernetesClientEventBasedSecretsChangeDetector kubernetesClientEventBase
@Bean
@Primary
AbstractEnvironment environment() {

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
V1Secret secret = secret(SECRET_NAME, Map.of());
V1SecretList secretList = new V1SecretList().addItemsItem(secret);

stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(secretList)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,6 @@ static void setup() {
client.setDebugging(true);
Configuration.setDefaultApiClient(client);
coreV1Api = new CoreV1Api();

V1ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
V1ConfigMapList listOne = new V1ConfigMapList().addItemsItem(configMapOne);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));
}

@AfterAll
Expand Down Expand Up @@ -141,7 +131,7 @@ void test(CapturedOutput output) {
.contains("Reloadable condition was not satisfied, reload will not be triggered");
boolean updateStrategyNotCalled = !STRATEGY_CALLED.get();
System.out.println("one: " + one + " two: " + two + " three: " + three + " updateStrategyNotCalled: "
+ updateStrategyNotCalled);
+ updateStrategyNotCalled);
return one && two && three && updateStrategyNotCalled;
});

Expand Down Expand Up @@ -184,6 +174,17 @@ PollingConfigMapChangeDetector pollingConfigMapChangeDetector(AbstractEnvironmen
@Bean
@Primary
AbstractEnvironment environment() {

V1ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
V1ConfigMapList listOne = new V1ConfigMapList().addItemsItem(configMapOne);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ static void setup() {
client.setDebugging(true);
Configuration.setDefaultApiClient(client);
coreV1Api = new CoreV1Api();

V1Secret secretOne = secret(SECRET_NAME, Map.of());
V1SecretList listOne = new V1SecretList().addItemsItem(secretOne);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

}

@AfterAll
Expand Down Expand Up @@ -187,6 +176,17 @@ PollingSecretsChangeDetector pollingSecretsChangeDetector(AbstractEnvironment en
@Bean
@Primary
AbstractEnvironment environment() {

V1Secret secretOne = secret(SECRET_NAME, Map.of());
V1SecretList listOne = new V1SecretList().addItemsItem(secretOne);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
stubFor(get(PATH).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(listOne)))
.inScenario(SCENARIO_NAME)
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("go-to-fail"));

MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class PollingReloadConfigMapTest {

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

private static final String PATH = "/api/v1/namespaces/spring-k8s/configmaps";

private static KubernetesMockServer kubernetesMockServer;

private static KubernetesClient kubernetesClient;
Expand All @@ -78,25 +80,7 @@ class PollingReloadConfigMapTest {

@BeforeAll
static void beforeAll() {

kubernetesClient.getConfiguration().setRequestRetryBackoffLimit(0);

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
ConfigMap configMapTwo = configMap(CONFIG_MAP_NAME, Map.of("a", "b"));
String path = "/api/v1/namespaces/spring-k8s/configmaps";
kubernetesMockServer.expect()
.withPath(path)
.andReturn(200, new ConfigMapListBuilder().withItems(configMapOne).build())
.once();

kubernetesMockServer.expect().withPath(path).andReturn(500, "Internal Server Error").once();

kubernetesMockServer.expect()
.withPath(path)
.andReturn(200, new ConfigMapListBuilder().withItems(configMapTwo).build())
.once();
}

/**
Expand All @@ -109,6 +93,7 @@ static void beforeAll() {
@Test
void test(CapturedOutput output) {
// we fail while reading 'configMapOne'
kubernetesMockServer.expect().withPath(PATH).andReturn(500, "Internal Server Error").once();
Awaitility.await().atMost(Duration.ofSeconds(20)).pollInterval(Duration.ofSeconds(1)).until(() -> {
boolean one = output.getOut().contains("Failure in reading named sources");
boolean two = output.getOut().contains("Failed to load source");
Expand All @@ -118,6 +103,12 @@ void test(CapturedOutput output) {
return one && two && three && updateStrategyNotCalled;
});

ConfigMap configMapTwo = configMap(CONFIG_MAP_NAME, Map.of("a", "b"));
kubernetesMockServer.expect()
.withPath(PATH)
.andReturn(200, new ConfigMapListBuilder().withItems(configMapTwo).build())
.once();

// it passes while reading 'configMapTwo'
Awaitility.await()
.atMost(Duration.ofSeconds(20))
Expand Down Expand Up @@ -146,6 +137,15 @@ PollingConfigMapChangeDetector pollingConfigMapChangeDetector(AbstractEnvironmen
@Bean
@Primary
AbstractEnvironment environment() {

// needed so that our environment is populated with 'something'
// this call is done in the method that returns the AbstractEnvironment
ConfigMap configMapOne = configMap(CONFIG_MAP_NAME, Map.of());
kubernetesMockServer.expect()
.withPath(PATH)
.andReturn(200, new ConfigMapListBuilder().withItems(configMapOne).build())
.once();

MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);

Expand Down
Loading

0 comments on commit d563cec

Please sign in to comment.