From d563cece8f1b647fa3d02f88999068fa50a3ebbf Mon Sep 17 00:00:00 2001 From: wind57 Date: Thu, 5 Dec 2024 22:58:44 +0200 Subject: [PATCH] fix failing tests --- .../reload_it/EventReloadConfigMapTest.java | 36 +++++++++----- .../reload_it/EventReloadSecretTest.java | 47 ++++++++++--------- .../reload_it/PollingReloadConfigMapTest.java | 23 ++++----- .../reload_it/PollingReloadSecretTest.java | 22 ++++----- .../reload_it/PollingReloadConfigMapTest.java | 36 +++++++------- .../reload_it/PollingReloadSecretTest.java | 36 +++++++------- 6 files changed, 106 insertions(+), 94 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java index d01eface1..7d30a2a6c 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java @@ -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; /** @@ -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); @@ -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 @@ -141,8 +139,6 @@ 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")) @@ -150,6 +146,9 @@ void test(CapturedOutput output) { .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"); @@ -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); @@ -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)); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java index 7687efb59..5cc87004b 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java @@ -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; /** @@ -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"; @@ -93,8 +95,6 @@ class EventReloadSecretTest { private static CoreV1Api coreV1Api; - private static WireMockServer wireMockServer; - private static final MockedStatic MOCK_STATIC = Mockito .mockStatic(KubernetesClientUtils.class); @@ -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); @@ -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 @@ -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"); @@ -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)) @@ -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); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java index ef956dcdb..53467860c 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java @@ -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 @@ -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; }); @@ -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); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java index c623165ad..8a3019271 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java @@ -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 @@ -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); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java index 1a355cf7c..d59e4cb85 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java @@ -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; @@ -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(); } /** @@ -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"); @@ -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)) @@ -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); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java index 02a4ab566..c9bb1eba4 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java @@ -73,6 +73,8 @@ public class PollingReloadSecretTest { private static final String NAMESPACE = "spring-k8s"; + private static final String PATH = "/api/v1/namespaces/spring-k8s/secrets"; + private static KubernetesMockServer kubernetesMockServer; private static KubernetesClient kubernetesClient; @@ -81,25 +83,7 @@ public class PollingReloadSecretTest { @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 - Secret secretOne = secret(SECRET_NAME, Map.of()); - Secret secretTwo = secret(SECRET_NAME, Map.of("a", "b")); - String path = "/api/v1/namespaces/spring-k8s/secrets"; - kubernetesMockServer.expect() - .withPath(path) - .andReturn(200, new SecretListBuilder().withItems(secretOne).build()) - .once(); - - kubernetesMockServer.expect().withPath(path).andReturn(500, "Internal Server Error").once(); - - kubernetesMockServer.expect() - .withPath(path) - .andReturn(200, new SecretListBuilder().withItems(secretTwo).build()) - .once(); } /** @@ -112,6 +96,7 @@ static void beforeAll() { @Test void test(CapturedOutput output) { // we fail while reading 'secretOne' + 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"); @@ -121,6 +106,12 @@ void test(CapturedOutput output) { return one && two && three && updateStrategyNotCalled; }); + Secret secretTwo = secret(SECRET_NAME, Map.of("a", "b")); + kubernetesMockServer.expect() + .withPath(PATH) + .andReturn(200, new SecretListBuilder().withItems(secretTwo).build()) + .once(); + // it passes while reading 'secretTwo' Awaitility.await() .atMost(Duration.ofSeconds(20)) @@ -153,6 +144,15 @@ PollingSecretsChangeDetector pollingSecretsChangeDetector(AbstractEnvironment en @Bean @Primary AbstractEnvironment environment() { + + // needed so that our environment is populated with 'something' + // this call is done in the method that returns the AbstractEnvironment + Secret secretOne = secret(SECRET_NAME, Map.of()); + kubernetesMockServer.expect() + .withPath(PATH) + .andReturn(200, new SecretListBuilder().withItems(secretOne).build()) + .once(); + MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", NAMESPACE);