From d947f623c8184fb7bfd75f89e098c4352cc798ed Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Fri, 11 Feb 2022 10:42:17 -0500 Subject: [PATCH] Fixing merge --- ...etesClientRetryBootstrapConfiguration.java | 2 - ...sClientConfigMapPropertySourceLocator.java | 3 +- ...tesClientSecretsPropertySourceLocator.java | 3 +- ...nfigMapEnableRetryWithoutFailFastTest.java | 129 +++++++++++++++++ .../configmap_retry/ConfigRetryEnabled.java | 4 +- ...SecretsEnableRetryWithoutFailFastTest.java | 130 ++++++++++++++++++ .../secrets_retry/SecretsRetryEnabled.java | 4 +- .../Fabric8RetryBootstrapConfiguration.java | 2 - ...nfigMapEnableRetryWithoutFailFastTest.java | 87 ++++++++++++ ...SecretsEnableRetryWithoutFailFastTest.java | 85 ++++++++++++ 10 files changed, 439 insertions(+), 10 deletions(-) create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigMapEnableRetryWithoutFailFastTest.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsEnableRetryWithoutFailFastTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapEnableRetryWithoutFailFastTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/SecretsEnableRetryWithoutFailFastTest.java diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientRetryBootstrapConfiguration.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientRetryBootstrapConfiguration.java index 0dff4e67..c1ea424d 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientRetryBootstrapConfiguration.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientRetryBootstrapConfiguration.java @@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration; -import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled; import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.config.ConditionalOnKubernetesConfigOrSecretsRetryEnabled; @@ -40,7 +39,6 @@ import org.springframework.context.annotation.Import; * @author Ryan Baxter */ @Configuration(proxyBeanMethods = false) -@ConditionalOnKubernetesEnabled @AutoConfigureAfter(KubernetesBootstrapConfiguration.class) @AutoConfigureBefore(KubernetesClientBootstrapConfiguration.class) @Import({ KubernetesCommonsAutoConfiguration.class, KubernetesClientAutoConfiguration.class }) diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientConfigMapPropertySourceLocator.java index 1dc602ed..cb4d4021 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientConfigMapPropertySourceLocator.java @@ -31,7 +31,8 @@ import org.springframework.retry.annotation.Retryable; * * @author Ryan Baxter */ -class RetryableKubernetesClientConfigMapPropertySourceLocator extends KubernetesClientConfigMapPropertySourceLocator { +public class RetryableKubernetesClientConfigMapPropertySourceLocator + extends KubernetesClientConfigMapPropertySourceLocator { RetryableKubernetesClientConfigMapPropertySourceLocator(CoreV1Api coreV1Api, ConfigMapConfigProperties properties, KubernetesNamespaceProvider kubernetesNamespaceProvider) { diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientSecretsPropertySourceLocator.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientSecretsPropertySourceLocator.java index ab922d66..a711b590 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientSecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/RetryableKubernetesClientSecretsPropertySourceLocator.java @@ -31,7 +31,8 @@ import org.springframework.retry.annotation.Retryable; * * @author Ryan Baxter */ -class RetryableKubernetesClientSecretsPropertySourceLocator extends KubernetesClientSecretsPropertySourceLocator { +public class RetryableKubernetesClientSecretsPropertySourceLocator + extends KubernetesClientSecretsPropertySourceLocator { RetryableKubernetesClientSecretsPropertySourceLocator(CoreV1Api coreV1Api, KubernetesNamespaceProvider kubernetesNamespaceProvider, SecretsConfigProperties secretsConfigProperties) { diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigMapEnableRetryWithoutFailFastTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigMapEnableRetryWithoutFailFastTest.java new file mode 100644 index 00000000..ffac8045 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigMapEnableRetryWithoutFailFastTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.configmap_retry; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.kubernetes.client.openapi.JSON; +import io.kubernetes.client.openapi.models.V1ConfigMapList; +import io.kubernetes.client.openapi.models.V1SecretList; +import io.kubernetes.client.util.ClientBuilder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; +import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; +import org.springframework.cloud.kubernetes.client.config.KubernetesClientBootstrapConfiguration; +import org.springframework.cloud.kubernetes.client.config.KubernetesClientRetryBootstrapConfiguration; +import org.springframework.cloud.kubernetes.client.config.reload.KubernetesClientConfigReloadAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration; +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.retry.annotation.RetryConfiguration; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mockStatic; + +/** + * @author Ryan Baxter + */ +public class ConfigMapEnableRetryWithoutFailFastTest { + + private static final String API = "/api/v1/namespaces/default/configmaps"; + + private static final String SECRETS_API = "/api/v1/namespaces/default/secrets"; + + private ConfigurableApplicationContext context; + + private static WireMockServer wireMockServer; + + private static MockedStatic clientUtilsMock; + + @BeforeAll + public static void setup() { + wireMockServer = new WireMockServer(options().dynamicPort()); + wireMockServer.start(); + WireMock.configureFor(wireMockServer.port()); + + clientUtilsMock = mockStatic(KubernetesClientUtils.class); + clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient) + .thenReturn(new ClientBuilder().setBasePath(wireMockServer.baseUrl()).build()); + stubConfigMapAndSecretsDefaults(); + } + + private static void stubConfigMapAndSecretsDefaults() { + // return empty config map / secret list to not fail context creation + stubFor(get(API).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1ConfigMapList())))); + stubFor(get(SECRETS_API) + .willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1SecretList())))); + } + + @AfterAll + public static void teardown() { + wireMockServer.stop(); + clientUtilsMock.close(); + } + + protected void setup(String... env) { + List envList = (env != null) ? new ArrayList<>(Arrays.asList(env)) : new ArrayList<>(); + envList.add("spring.cloud.kubernetes.client.namespace=default"); + String[] envArray = envList.toArray(new String[0]); + + context = new SpringApplicationBuilder(RetryConfiguration.class, PropertyPlaceholderAutoConfiguration.class, + ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class, EndpointAutoConfiguration.class, + InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class, + ConfigurationPropertiesBindingPostProcessor.class, + ConfigurationPropertiesRebinderAutoConfiguration.class, KubernetesClientBootstrapConfiguration.class, + KubernetesClientRetryBootstrapConfiguration.class, KubernetesBootstrapConfiguration.class, + KubernetesClientConfigReloadAutoConfiguration.class) + .web(org.springframework.boot.WebApplicationType.NONE).properties(envArray).run(); + } + + @AfterEach + public void afterEach() { + if (this.context != null) { + this.context.close(); + this.context = null; + } + } + + @Test + public void doesNotContainRetryableConfigMapPropertySourceLocator() throws Exception { + stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); + setup("debug=true", "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.test.enable-retry=true"); + assertThat(context.containsBean("retryableConfigMapPropertySourceLocator")).isFalse(); + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryEnabled.java index 4c3339c9..9d475a5a 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryEnabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryEnabled.java @@ -36,7 +36,7 @@ import org.mockito.MockedStatic; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; -import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.client.config.RetryableKubernetesClientConfigMapPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; @@ -98,7 +98,7 @@ class ConfigRetryEnabled { } @SpyBean - private KubernetesClientConfigMapPropertySourceLocator propertySourceLocator; + private RetryableKubernetesClientConfigMapPropertySourceLocator propertySourceLocator; @Test void locateShouldNotRetryWhenThereIsNoFailure() { diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsEnableRetryWithoutFailFastTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsEnableRetryWithoutFailFastTest.java new file mode 100644 index 00000000..e0312b48 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsEnableRetryWithoutFailFastTest.java @@ -0,0 +1,130 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.secrets_retry; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.kubernetes.client.openapi.JSON; +import io.kubernetes.client.openapi.models.V1ConfigMapList; +import io.kubernetes.client.openapi.models.V1SecretList; +import io.kubernetes.client.util.ClientBuilder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; +import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; +import org.springframework.cloud.kubernetes.client.config.KubernetesClientBootstrapConfiguration; +import org.springframework.cloud.kubernetes.client.config.KubernetesClientRetryBootstrapConfiguration; +import org.springframework.cloud.kubernetes.client.config.reload.KubernetesClientConfigReloadAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration; +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.retry.annotation.RetryConfiguration; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mockStatic; + +/** + * @author Ryan Baxter + */ +public class SecretsEnableRetryWithoutFailFastTest { + + private static final String API = "/api/v1/namespaces/default/configmaps"; + + private static final String SECRETS_API = "/api/v1/namespaces/default/secrets"; + + private ConfigurableApplicationContext context; + + private static WireMockServer wireMockServer; + + private static MockedStatic clientUtilsMock; + + @BeforeAll + public static void setup() { + wireMockServer = new WireMockServer(options().dynamicPort()); + wireMockServer.start(); + WireMock.configureFor(wireMockServer.port()); + + clientUtilsMock = mockStatic(KubernetesClientUtils.class); + clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient) + .thenReturn(new ClientBuilder().setBasePath(wireMockServer.baseUrl()).build()); + stubConfigMapAndSecretsDefaults(); + } + + private static void stubConfigMapAndSecretsDefaults() { + // return empty config map / secret list to not fail context creation + stubFor(get(API).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1ConfigMapList())))); + stubFor(get(SECRETS_API) + .willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1SecretList())))); + } + + @AfterAll + public static void teardown() { + wireMockServer.stop(); + clientUtilsMock.close(); + } + + protected void setup(String... env) { + List envList = (env != null) ? new ArrayList<>(Arrays.asList(env)) : new ArrayList<>(); + envList.add("spring.cloud.kubernetes.client.namespace=default"); + String[] envArray = envList.toArray(new String[0]); + + context = new SpringApplicationBuilder(RetryConfiguration.class, PropertyPlaceholderAutoConfiguration.class, + ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class, EndpointAutoConfiguration.class, + InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class, + ConfigurationPropertiesBindingPostProcessor.class, + ConfigurationPropertiesRebinderAutoConfiguration.class, KubernetesClientBootstrapConfiguration.class, + KubernetesClientRetryBootstrapConfiguration.class, KubernetesBootstrapConfiguration.class, + KubernetesClientConfigReloadAutoConfiguration.class) + .web(org.springframework.boot.WebApplicationType.NONE).properties(envArray).run(); + } + + @AfterEach + public void afterEach() { + if (this.context != null) { + this.context.close(); + this.context = null; + } + } + + @Test + public void doesNotContainRetryableSecretsPropertySourceLocator() throws Exception { + stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); + setup("debug=true", "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.test.enable-retry=true", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true"); + assertThat(context.containsBean("retryableSecretsPropertySourceLocator")).isFalse(); + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryEnabled.java index 7a9aa46a..a6f160c1 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryEnabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryEnabled.java @@ -36,7 +36,7 @@ import org.mockito.MockedStatic; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; -import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.client.config.RetryableKubernetesClientSecretsPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; @@ -98,7 +98,7 @@ class SecretsRetryEnabled { } @SpyBean - private KubernetesClientSecretsPropertySourceLocator propertySourceLocator; + private RetryableKubernetesClientSecretsPropertySourceLocator propertySourceLocator; @Test void locateShouldNotRetryWhenThereIsNoFailure() { diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8RetryBootstrapConfiguration.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8RetryBootstrapConfiguration.java index 5e2009d1..159d9c26 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8RetryBootstrapConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8RetryBootstrapConfiguration.java @@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; import org.springframework.boot.cloud.CloudPlatform; -import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled; import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.config.ConditionalOnKubernetesConfigOrSecretsRetryEnabled; @@ -43,7 +42,6 @@ import org.springframework.context.annotation.Import; * @author Ryan Baxter */ @Configuration(proxyBeanMethods = false) -@ConditionalOnKubernetesEnabled @AutoConfigureBefore(Fabric8BootstrapConfiguration.class) @Import({ KubernetesCommonsAutoConfiguration.class, Fabric8AutoConfiguration.class }) @ConditionalOnClass({ ConfigMap.class, Secret.class }) diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapEnableRetryWithoutFailFastTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapEnableRetryWithoutFailFastTest.java new file mode 100644 index 00000000..4456b0fe --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapEnableRetryWithoutFailFastTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration; +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.retry.annotation.RetryConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Ryan Baxter + */ +@EnableKubernetesMockClient +public class ConfigMapEnableRetryWithoutFailFastTest { + + private static final String API = "/api/v1/namespaces/default/configmaps/application"; + + static KubernetesMockServer mockServer; + + static KubernetesClient mockClient; + + private ConfigurableApplicationContext context; + + protected void setup(String... env) { + List envList = (env != null) ? new ArrayList<>(Arrays.asList(env)) : new ArrayList<>(); + envList.add("spring.cloud.kubernetes.client.namespace=default"); + String[] envArray = envList.toArray(new String[0]); + + context = new SpringApplicationBuilder(RetryConfiguration.class, PropertyPlaceholderAutoConfiguration.class, + ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class, EndpointAutoConfiguration.class, + InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class, + ConfigurationPropertiesBindingPostProcessor.class, + ConfigurationPropertiesRebinderAutoConfiguration.class, Fabric8BootstrapConfiguration.class, + Fabric8RetryBootstrapConfiguration.class, KubernetesBootstrapConfiguration.class) + .web(org.springframework.boot.WebApplicationType.NONE).properties(envArray).run(); + } + + @AfterEach + public void afterEach() { + if (this.context != null) { + this.context.close(); + this.context = null; + } + } + + @Test + public void doesNotContainRetryableConfigMapPropertySourceLocator() throws Exception { + mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); + setup("debug=true", "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.test.enable-retry=true"); + assertThat(context.containsBean("retryableConfigMapPropertySourceLocator")).isFalse(); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/SecretsEnableRetryWithoutFailFastTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/SecretsEnableRetryWithoutFailFastTest.java new file mode 100644 index 00000000..55564adb --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/SecretsEnableRetryWithoutFailFastTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration; +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.retry.annotation.RetryConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Ryan Baxter + */ +@EnableKubernetesMockClient +public class SecretsEnableRetryWithoutFailFastTest { + + private static final String API = "/api/v1/namespaces/default/configmaps/application"; + + static KubernetesMockServer mockServer; + + private ConfigurableApplicationContext context; + + protected void setup(String... env) { + List envList = (env != null) ? new ArrayList<>(Arrays.asList(env)) : new ArrayList<>(); + envList.add("spring.cloud.kubernetes.client.namespace=default"); + String[] envArray = envList.toArray(new String[0]); + + context = new SpringApplicationBuilder(RetryConfiguration.class, PropertyPlaceholderAutoConfiguration.class, + ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class, EndpointAutoConfiguration.class, + InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class, + ConfigurationPropertiesBindingPostProcessor.class, + ConfigurationPropertiesRebinderAutoConfiguration.class, Fabric8BootstrapConfiguration.class, + Fabric8RetryBootstrapConfiguration.class, KubernetesBootstrapConfiguration.class) + .web(org.springframework.boot.WebApplicationType.NONE).properties(envArray).run(); + } + + @AfterEach + public void afterEach() { + if (this.context != null) { + this.context.close(); + this.context = null; + } + } + + @Test + public void doesNotContainRetryableSecretsPropertySourceLocator() throws Exception { + mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); + setup("debug=true", "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.test.enable-retry=true", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true"); + assertThat(context.containsBean("retryableSecretsPropertySourceLocator")).isFalse(); + } + +}