From 74a1c9ba2a3ede48333b63b67b24f7a293c73305 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 18 May 2022 15:48:36 -0400 Subject: [PATCH] Use ConfigData To Load Configuration From Kubernetes Instead Of Bootstrap (#1002) * Use ConfigData to load configuration instead of bootstrap Co-authored-by: wind57 --- .../main/asciidoc/kubernetes-awareness.adoc | 9 +- .../main/asciidoc/property-source-config.adoc | 17 +- spring-cloud-kubernetes-client-config/pom.xml | 4 - ...netesClientConfigDataLocationResolver.java | 148 +++++++++++++ .../main/resources/META-INF/spring.factories | 3 + ...rapIncludeProfileSpecificSourcesTests.java | 43 ++++ ...ntConfigMapBootstrapNameAsPrefixTests.java | 36 +++ ...ataIncludeProfileSpecificSourcesTests.java | 70 ++++++ ...tConfigMapConfigDataNameAsPrefixTests.java | 66 ++++++ ...MapIncludeProfileSpecificSourcesTests.java | 15 +- ...netesClientConfigMapNameAsPrefixTests.java | 12 +- ...onfigMapNameAsPrefixConfigurationStub.java | 2 +- ...ofileSpecificSourcesConfigurationStub.java | 4 +- ...ClientBootstrapConfigurationInsideK8s.java | 3 +- .../KubernetesDisabled.java | 3 +- .../KubernetesEnabled.java | 3 +- .../KubernetesEnabledConfigDisabled.java | 3 +- .../KubernetesEnabledOnPurpose.java | 3 +- .../KubernetesEnabledSecretsDisabled.java | 3 +- .../BootstrapConfigFailFastDisabled.java | 30 +++ ...ConfigFailFastEnabledButRetryDisabled.java | 31 +++ ...igRetryDisabledButSecretsRetryEnabled.java | 32 +++ .../BootstrapConfigRetryEnabled.java | 31 +++ .../ConfigDataConfigFailFastDisabled.java | 30 +++ ...ConfigFailFastEnabledButRetryDisabled.java | 31 +++ ...igRetryDisabledButSecretsRetryEnabled.java | 32 +++ .../ConfigDataConfigRetryEnabled.java | 31 +++ .../ConfigFailFastDisabled.java | 13 +- ...ConfigFailFastEnabledButRetryDisabled.java | 14 +- ...igRetryDisabledButSecretsRetryEnabled.java | 18 +- .../configmap_retry/ConfigRetryEnabled.java | 36 ++- .../SecretsFailFastDisabled.java | 11 +- ...ecretsFailFastEnabledButRetryDisabled.java | 10 +- ...etsRetryDisabledButConfigRetryEnabled.java | 15 +- .../secrets_retry/SecretsRetryEnabled.java | 40 ++-- .../commons/KubernetesClientProperties.java | 9 +- .../config/AbstractConfigProperties.java | 10 + ...tryableConfigMapPropertySourceLocator.java | 72 ++++++ ...RetryableSecretsPropertySourceLocator.java | 72 ++++++ .../KubernetesBootstrapConfiguration.java | 4 +- .../config/KubernetesConfigDataLoader.java | 83 +++++++ .../KubernetesConfigDataLocationResolver.java | 207 ++++++++++++++++++ .../config/KubernetesConfigDataResource.java | 129 +++++++++++ .../main/resources/META-INF/spring.factories | 4 + .../pom.xml | 12 + .../BootstrapConfigServerIntegrationTest.java | 31 +++ ...ConfigDataConfigServerIntegrationTest.java | 69 ++++++ ...erAutoConfigurationKubernetesDisabled.java | 3 +- ...ConfigurationKubernetesProfileMissing.java | 3 +- .../ConfigServerIntegrationTest.java | 7 +- .../src/main/resources/application.yml | 2 + .../pom.xml | 4 - .../config/Fabric8BootstrapConfiguration.java | 2 + .../Fabric8ConfigDataLocationResolver.java | 141 ++++++++++++ .../Fabric8RetryBootstrapConfiguration.java | 2 + .../main/resources/META-INF/spring.factories | 3 + .../config/BoostrapConfigMapsTests.java | 47 ++++ ...strapFabric8SecretsPropertySourceTest.java | 43 ++++ .../BoostrapMultipleConfigMapsTests.java | 47 ++++ ...ithIncludeProfileSpecificSourcesTests.java | 50 +++++ .../BootstrapConfigMapWithPrefixTests.java | 47 ++++ ...BootstrapConfigMapsFromFilePathsTests.java | 47 ++++ .../config/BootstrapConfigMapsMixedTests.java | 46 ++++ ...ConfigMapsWithActiveProfilesNameTests.java | 52 +++++ ...pConfigMapsWithProfileExpressionTests.java | 51 +++++ ...gMapsWithProfilesNoActiveProfileTests.java | 48 ++++ .../BootstrapConfigMapsWithProfilesTests.java | 50 +++++ ...otstrapConfigMapsWithoutProfilesTests.java | 47 ++++ .../fabric8/config/BootstrapCoreTest.java | 43 ++++ ...trapCoreTestClientViaSystemProperties.java | 32 +++ .../config/BootstrapMultipleSecretsTests.java | 48 ++++ ...ithIncludeProfileSpecificSourcesTests.java | 52 +++++ .../ConfigDataConfigMapWithPrefixTests.java | 47 ++++ ...onfigDataConfigMapsFromFilePathsTests.java | 47 ++++ .../ConfigDataConfigMapsMixedTests.java | 46 ++++ .../config/ConfigDataConfigMapsTests.java | 47 ++++ ...ConfigMapsWithActiveProfilesNameTests.java | 52 +++++ ...aConfigMapsWithProfileExpressionTests.java | 51 +++++ ...gMapsWithProfilesNoActiveProfileTests.java | 48 ++++ ...ConfigDataConfigMapsWithProfilesTests.java | 50 +++++ ...figDataConfigMapsWithoutProfilesTests.java | 47 ++++ .../fabric8/config/ConfigDataCoreTest.java | 43 ++++ ...DataCoreTestClientViaSystemProperties.java | 32 +++ ...gDataFabric8SecretsPropertySourceTest.java | 43 ++++ .../ConfigDataMultipleConfigMapsTests.java | 47 ++++ .../ConfigDataMultipleSecretsTests.java | 48 ++++ ...ithIncludeProfileSpecificSourcesTests.java | 23 +- .../config/ConfigMapWithPrefixTests.java | 10 +- .../config/ConfigMapsFromFilePathsTests.java | 10 +- .../fabric8/config/ConfigMapsMixedTests.java | 20 +- .../fabric8/config/ConfigMapsTests.java | 20 +- ...ConfigMapsWithActiveProfilesNameTests.java | 24 +- .../ConfigMapsWithProfileExpressionTests.java | 22 +- ...gMapsWithProfilesNoActiveProfileTests.java | 20 +- .../config/ConfigMapsWithProfilesTests.java | 22 +- .../ConfigMapsWithoutProfilesTests.java | 22 +- .../kubernetes/fabric8/config/CoreTest.java | 18 +- .../CoreTestClientViaSystemProperties.java | 10 +- .../Fabric8SecretsPropertySourceTest.java | 19 +- .../KubernetesConfigConfigurationTest.java | 52 ++++- .../config/KubernetesConfigTestBase.java | 9 +- .../config/MultipleConfigMapsTests.java | 8 +- .../fabric8/config/MultipleSecretsTests.java | 20 +- ...ostrapKubernetesEnabledConfigDisabled.java | 30 +++ .../BootstrapKubernetesDisabled.java | 29 +++ .../bootstrap/BootstrapKubernetesEnabled.java | 30 +++ .../BootstrapKubernetesEnabledOnPurpose.java | 31 +++ ...rnetesEnabledSecretsAndConfigDisabled.java | 27 +++ ...strapKubernetesEnabledSecretsDisabled.java | 31 +++ .../ConfigDataKubernetesDisabled.java | 29 +++ .../ConfigDataKubernetesEnabled.java | 30 +++ ...igDataKubernetesEnabledConfigDisabled.java | 30 +++ .../ConfigDataKubernetesEnabledOnPurpose.java | 31 +++ ...rnetesEnabledSecretsAndConfigDisabled.java | 27 +++ ...gDataKubernetesEnabledSecretsDisabled.java | 31 +++ ...abric8BootstrapConfigurationInsideK8s.java | 3 +- .../config/bootstrap/KubernetesDisabled.java | 6 +- .../config/bootstrap/KubernetesEnabled.java | 6 +- .../KubernetesEnabledConfigDisabled.java | 6 +- .../bootstrap/KubernetesEnabledOnPurpose.java | 7 +- ...rnetesEnabledSecretsAndConfigDisabled.java | 6 +- .../KubernetesEnabledSecretsDisabled.java | 7 +- .../BoostrapConfigFailFastDisabled.java | 46 ++++ ...igRetryDisabledButSecretsRetryEnabled.java | 56 +++++ .../BoostrapConfigRetryEnabled.java | 59 +++++ ...ConfigFailFastEnabledButRetryDisabled.java | 54 +++++ .../ConfigDataConfigFailFastDisabled.java | 46 ++++ ...ConfigFailFastEnabledButRetryDisabled.java | 59 +++++ ...igRetryDisabledButSecretsRetryEnabled.java | 57 +++++ .../ConfigDataConfigRetryEnabled.java | 67 ++++++ .../locator_retry/ConfigFailFastDisabled.java | 29 +-- ...ConfigFailFastEnabledButRetryDisabled.java | 18 +- ...igRetryDisabledButSecretsRetryEnabled.java | 37 ++-- .../locator_retry/ConfigRetryEnabled.java | 42 ++-- .../ConfigReloadAutoConfigurationTest.java | 6 + .../BootstrapSecretsFailFastDisabled.java | 59 +++++ ...ecretsFailFastEnabledButRetryDisabled.java | 60 +++++ ...etsRetryDisabledButConfigRetryEnabled.java | 69 ++++++ .../retry/BootstrapSecretsRetryEnabled.java | 61 ++++++ .../ConfigDataSecretsFailFastDisabled.java | 61 ++++++ ...ecretsFailFastEnabledButRetryDisabled.java | 62 ++++++ ...etsRetryDisabledButConfigRetryEnabled.java | 72 ++++++ .../retry/ConfigDataSecretsRetryEnabled.java | 64 ++++++ .../config/retry/SecretsFailFastDisabled.java | 31 +-- ...ecretsFailFastEnabledButRetryDisabled.java | 33 +-- ...etsRetryDisabledButConfigRetryEnabled.java | 39 ++-- .../config/retry/SecretsRetryEnabled.java | 42 ++-- .../BootstrapSecretsWithLabelsTests.java | 41 ++++ .../ConfigDataSecretsWithLabelsTests.java | 41 ++++ .../SecretsWithLabelsTests.java | 14 +- ...application-with-active-profiles-name.yaml | 4 +- .../resources/application-with-profiles.yaml | 12 +- ...lientNotPresentWhenKubernetesDisabled.java | 3 +- 153 files changed, 4570 insertions(+), 619 deletions(-) create mode 100644 spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolver.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapIncludeProfileSpecificSourcesTests.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapNameAsPrefixTests.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataIncludeProfileSpecificSourcesTests.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataNameAsPrefixTests.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryDisabledButSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableConfigMapPropertySourceLocator.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLoader.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLocationResolver.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataResource.java create mode 100644 spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/BootstrapConfigServerIntegrationTest.java create mode 100644 spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/ConfigDataConfigServerIntegrationTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigDataLocationResolver.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapConfigMapsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapFabric8SecretsPropertySourceTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapMultipleConfigMapsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithIncludeProfileSpecificSourcesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithPrefixTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsFromFilePathsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsMixedTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithActiveProfilesNameTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfileExpressionTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesNoActiveProfileTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithoutProfilesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTestClientViaSystemProperties.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapMultipleSecretsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithIncludeProfileSpecificSourcesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithPrefixTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsFromFilePathsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsMixedTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithActiveProfilesNameTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfileExpressionTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesNoActiveProfileTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithoutProfilesTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTestClientViaSystemProperties.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataFabric8SecretsPropertySourceTest.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleConfigMapsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleSecretsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BoostrapKubernetesEnabledConfigDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledOnPurpose.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsAndConfigDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledConfigDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledOnPurpose.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsAndConfigDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryDisabledButSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryDisabledButConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastEnabledButRetryDisabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryDisabledButConfigRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryEnabled.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/BootstrapSecretsWithLabelsTests.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/ConfigDataSecretsWithLabelsTests.java diff --git a/docs/src/main/asciidoc/kubernetes-awareness.adoc b/docs/src/main/asciidoc/kubernetes-awareness.adoc index ea7ee325..f64d0123 100644 --- a/docs/src/main/asciidoc/kubernetes-awareness.adoc +++ b/docs/src/main/asciidoc/kubernetes-awareness.adoc @@ -14,10 +14,11 @@ that setting via `spring.main.cloud-platform`. For example, if you need to test some features, but do not want to deploy to a cluster, it is enough to set the: `spring.main.cloud-platform=KUBERNETES`. This will make `spring-cloud-kubernetes` act as-if it is deployed in a real cluster. -Be aware that when `spring-cloud-kubernetes-config` is on the classpath, `spring.main.cloud-platform` should be set in `bootstrap.{properties|yml}` -(or the profile specific one), otherwise it should be in `application.{properties|yml}` (or the profile specific one). -Also note that these properties: `spring.cloud.kubernetes.config.enabled` and `spring.cloud.kubernetes.secrets.enabled` -only take effect when set in `bootstrap.{properties|yml}`. + +NOTE: If you have `spring-cloud-bootstrap-starter` on your classpath or are setting `spring.cloud.bootstrap.enabled=true` then +you will have to set `spring.main.cloud-platform` should be set in `bootstrap.{properties|yml}` +(or the profile specific one). Also note that these properties: `spring.cloud.kubernetes.config.enabled` and `spring.cloud.kubernetes.secrets.enabled` +will only take effect when set in `bootstrap.{properties|yml}` when you have `spring-cloud-bootstrap-starter` on your classpath or are setting `spring.cloud.bootstrap.enabled=true`. === Breaking Changes In 3.0.x diff --git a/docs/src/main/asciidoc/property-source-config.adoc b/docs/src/main/asciidoc/property-source-config.adoc index 62eeef34..cbd9ca3a 100644 --- a/docs/src/main/asciidoc/property-source-config.adoc +++ b/docs/src/main/asciidoc/property-source-config.adoc @@ -5,18 +5,27 @@ an `application-profile.properties` or `application-profile.yaml` file that cont application or Spring Boot starters. You can override these properties by specifying system properties or environment variables. +To enable this functionality you need to set `spring.config.import=kubernetes:` in your application's configuration properties. +Currently you can not specify a ConfigMap or Secret to load using `spring.config.import`, by default Spring Cloud Kubernetes +will load a ConfigMap and/or Secret based on the `spring.application.name` property. If `spring.application.name` is not set it will +load a ConfigMap and/or Secret with the name `application`. + +If you would like to load Kubernetes `PropertySource`s during the bootstrap phase like it worked prior to the 3.0.x release +you can either add `spring-cloud-starter-bootstrap` to your application's classpath or set `spring.cloud.bootstrap.enabled=true` +as an environment variable. + [[configmap-propertysource]] === Using a `ConfigMap` `PropertySource` Kubernetes provides a resource named https://kubernetes.io/docs/user-guide/configmap/[`ConfigMap`] to externalize the parameters to pass to your application in the form of key-value pairs or embedded `application.properties` or `application.yaml` files. The link:https://github.com/spring-cloud/spring-cloud-kubernetes/tree/master/spring-cloud-kubernetes-fabric8-config[Spring Cloud Kubernetes Config] project makes Kubernetes `ConfigMap` instances available -during application bootstrapping and triggers hot reloading of beans or Spring context when changes are detected on +during application startup and triggers hot reloading of beans or Spring context when changes are detected on observed `ConfigMap` instances. The default behavior is to create a `Fabric8ConfigMapPropertySource` based on a Kubernetes `ConfigMap` that has a `metadata.name` value of either the name of your Spring application (as defined by its `spring.application.name` property) or a custom name defined within the -`bootstrap.properties` file under the following key: `spring.cloud.kubernetes.config.name`. +`application.properties` file under the following key: `spring.cloud.kubernetes.config.name`. However, more advanced configuration is possible where you can use multiple `ConfigMap` instances. The `spring.cloud.kubernetes.config.sources` list makes this possible. @@ -239,8 +248,8 @@ data: ==== -To tell Spring Boot which `profile` should be enabled at bootstrap, you can pass `SPRING_PROFILES_ACTIVE` environment variable. - To do so, you can launch your Spring Boot application with an environment variable that you can define it in the PodSpec at the container specification. +To tell Spring Boot which `profile` should be enabled see the https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles[Spring Boot documentation]. +One option for activating a specific profile when deploying to Kubernetes is to launch your Spring Boot application with an environment variable that you can define in the PodSpec at the container specification. Deployment resource file, as follows: ==== diff --git a/spring-cloud-kubernetes-client-config/pom.xml b/spring-cloud-kubernetes-client-config/pom.xml index f54aa48a..eba0fe4e 100644 --- a/spring-cloud-kubernetes-client-config/pom.xml +++ b/spring-cloud-kubernetes-client-config/pom.xml @@ -53,10 +53,6 @@ spring-cloud-starter - - org.springframework.cloud - spring-cloud-starter-bootstrap - org.springframework.retry spring-retry diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolver.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolver.java new file mode 100644 index 00000000..a9fb96ea --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolver.java @@ -0,0 +1,148 @@ +/* + * 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; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.apis.CoreV1Api; + +import org.springframework.boot.BootstrapRegistry.InstanceSupplier; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataLocationResolverContext; +import org.springframework.boot.context.config.Profiles; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.cloud.kubernetes.client.KubernetesClientPodUtils; +import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigDataLocationResolver; +import org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; +import org.springframework.core.env.Environment; + +import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.kubernetesApiClient; + +/** + * @author Ryan Baxter + */ +public class KubernetesClientConfigDataLocationResolver extends KubernetesConfigDataLocationResolver { + + public KubernetesClientConfigDataLocationResolver(DeferredLogFactory factory) { + super(factory); + } + + @Override + protected void registerBeans(ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, + Profiles profiles, KubernetesConfigDataLocationResolver.PropertyHolder propertyHolder, + KubernetesNamespaceProvider namespaceProvider) { + ConfigMapConfigProperties configMapProperties = propertyHolder.configMapConfigProperties(); + SecretsConfigProperties secretsProperties = propertyHolder.secretsProperties(); + + ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); + ApiClient apiClient = kubernetesApiClient(); + bootstrapContext.registerIfAbsent(ApiClient.class, InstanceSupplier.of(apiClient)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configDataApiClient", event.getBootstrapContext().get(ApiClient.class))); + + CoreV1Api coreV1Api = coreApi(apiClient); + bootstrapContext.registerIfAbsent(CoreV1Api.class, InstanceSupplier.of(coreV1Api)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configCoreV1Api", event.getBootstrapContext().get(CoreV1Api.class))); + + if (isRetryEnabled(configMapProperties, secretsProperties)) { + registerRetryBeans(configMapProperties, secretsProperties, bootstrapContext, coreV1Api, namespaceProvider); + } + else { + if (configMapProperties.isEnabled()) { + KubernetesClientConfigMapPropertySourceLocator configMapPropertySourceLocator = new KubernetesClientConfigMapPropertySourceLocator( + coreV1Api, configMapProperties, namespaceProvider); + bootstrapContext.registerIfAbsent(ConfigMapPropertySourceLocator.class, + InstanceSupplier.of(configMapPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configDataConfigMapPropertySourceLocator", + event.getBootstrapContext().get(ConfigMapPropertySourceLocator.class))); + } + + if (secretsProperties.isEnabled()) { + KubernetesClientSecretsPropertySourceLocator secretsPropertySourceLocator = new KubernetesClientSecretsPropertySourceLocator( + coreV1Api, namespaceProvider, secretsProperties); + bootstrapContext.registerIfAbsent(SecretsPropertySourceLocator.class, + InstanceSupplier.of(secretsPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configDataSecretsPropertySourceLocator", + event.getBootstrapContext().get(SecretsPropertySourceLocator.class))); + } + } + } + + private void registerRetryBeans(ConfigMapConfigProperties configMapProperties, + SecretsConfigProperties secretsProperties, ConfigurableBootstrapContext bootstrapContext, + CoreV1Api coreV1Api, KubernetesNamespaceProvider namespaceProvider) { + if (configMapProperties.isEnabled()) { + ConfigMapPropertySourceLocator configMapPropertySourceLocator = new KubernetesClientConfigMapPropertySourceLocator( + coreV1Api, configMapProperties, namespaceProvider); + if (isRetryEnabledForConfigMap(configMapProperties)) { + configMapPropertySourceLocator = new ConfigDataRetryableConfigMapPropertySourceLocator( + configMapPropertySourceLocator, configMapProperties); + } + + bootstrapContext.registerIfAbsent(ConfigMapPropertySourceLocator.class, + InstanceSupplier.of(configMapPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory().registerSingleton( + "configDataConfigMapPropertySourceLocator", + event.getBootstrapContext().get(ConfigMapPropertySourceLocator.class))); + } + + if (secretsProperties.isEnabled()) { + SecretsPropertySourceLocator secretsPropertySourceLocator = new KubernetesClientSecretsPropertySourceLocator( + coreV1Api, namespaceProvider, secretsProperties); + if (isRetryEnabledForSecrets(secretsProperties)) { + secretsPropertySourceLocator = new ConfigDataRetryableSecretsPropertySourceLocator( + secretsPropertySourceLocator, secretsProperties); + } + + bootstrapContext.registerIfAbsent(SecretsPropertySourceLocator.class, + InstanceSupplier.of(secretsPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory().registerSingleton( + "configDataSecretsPropertySourceLocator", + event.getBootstrapContext().get(SecretsPropertySourceLocator.class))); + } + } + + protected ApiClient apiClient(KubernetesClientProperties properties) { + ApiClient apiClient = kubernetesApiClient(); + apiClient.setUserAgent(properties.getUserAgent()); + return apiClient; + } + + protected CoreV1Api coreApi(ApiClient apiClient) { + return new CoreV1Api(apiClient); + } + + protected KubernetesNamespaceProvider kubernetesNamespaceProvider(Environment environment) { + return new KubernetesNamespaceProvider(environment); + } + + protected KubernetesClientPodUtils kubernetesPodUtils(CoreV1Api client, + KubernetesNamespaceProvider kubernetesNamespaceProvider) { + return new KubernetesClientPodUtils(client, kubernetesNamespaceProvider.getNamespace()); + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-client-config/src/main/resources/META-INF/spring.factories index 2efd0550..107ff95e 100644 --- a/spring-cloud-kubernetes-client-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-kubernetes-client-config/src/main/resources/META-INF/spring.factories @@ -3,3 +3,6 @@ org.springframework.cloud.kubernetes.client.config.reload.KubernetesClientConfig org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.kubernetes.client.config.KubernetesClientBootstrapConfiguration,\ org.springframework.cloud.kubernetes.client.config.KubernetesClientRetryBootstrapConfiguration +# ConfigData Location Resolvers +org.springframework.boot.context.config.ConfigDataLocationResolver=\ +org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigDataLocationResolver diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapIncludeProfileSpecificSourcesTests.java new file mode 100644 index 00000000..27bae0c1 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapIncludeProfileSpecificSourcesTests.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2020 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; + +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.client.config.applications.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * The stub data for this test is in : IncludeProfileSpecificSourcesConfigurationStub + * + * @author wind57 + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = IncludeProfileSpecificSourcesApp.class, + properties = { "spring.cloud.bootstrap.name=include-profile-specific-sources", + "include.profile.specific.sources=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@ActiveProfiles("dev") +class KubernetesClientConfigMapBootstrapIncludeProfileSpecificSourcesTests + extends KubernetesClientConfigMapIncludeProfileSpecificSourcesTests { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapNameAsPrefixTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapNameAsPrefixTests.java new file mode 100644 index 00000000..674caaff --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapBootstrapNameAsPrefixTests.java @@ -0,0 +1,36 @@ +/* + * 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; + +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.client.config.applications.config_map_name_as_prefix.WithPrefixApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Ryan Baxter + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WithPrefixApp.class, + properties = { "spring.cloud.bootstrap.name=config-map-name-as-prefix", "config.map.name.as.prefix.stub=true", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +public class KubernetesClientConfigMapBootstrapNameAsPrefixTests extends KubernetesClientConfigMapNameAsPrefixTests { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataIncludeProfileSpecificSourcesTests.java new file mode 100644 index 00000000..c4032040 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataIncludeProfileSpecificSourcesTests.java @@ -0,0 +1,70 @@ +/* + * 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; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.kubernetes.client.util.ClientBuilder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; +import org.springframework.cloud.kubernetes.client.config.applications.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.mockito.Mockito.mockStatic; +import static org.springframework.cloud.kubernetes.client.config.boostrap.stubs.IncludeProfileSpecificSourcesConfigurationStub.stubData; + +/** + * @author Ryan Baxter + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = IncludeProfileSpecificSourcesApp.class, + properties = { "spring.application.name=include-profile-specific-sources", + "include.profile.specific.sources=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./include-profile-specific-sources.yaml" }) +@AutoConfigureWebTestClient +@ActiveProfiles("dev") +public class KubernetesClientConfigMapConfigDataIncludeProfileSpecificSourcesTests + extends KubernetesClientConfigMapIncludeProfileSpecificSourcesTests { + + private static MockedStatic clientUtilsMock; + + @BeforeAll + public static void wireMock() { + WireMockServer server = new WireMockServer(options().dynamicPort()); + server.start(); + WireMock.configureFor("localhost", server.port()); + clientUtilsMock = mockStatic(KubernetesClientUtils.class); + clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient) + .thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build()); + stubData(); + } + + @AfterAll + static void teardown() { + clientUtilsMock.close(); + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataNameAsPrefixTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataNameAsPrefixTests.java new file mode 100644 index 00000000..3ff5dbdc --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapConfigDataNameAsPrefixTests.java @@ -0,0 +1,66 @@ +/* + * 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; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.kubernetes.client.util.ClientBuilder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; +import org.springframework.cloud.kubernetes.client.config.applications.config_map_name_as_prefix.WithPrefixApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.mockito.Mockito.mockStatic; +import static org.springframework.cloud.kubernetes.client.config.boostrap.stubs.ConfigMapNameAsPrefixConfigurationStub.stubData; + +/** + * @author Ryan Baxter + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WithPrefixApp.class, + properties = { "spring.cloud.application.name=config-map-name-as-prefix", "config.map.name.as.prefix.stub=true", + "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./config-map-name-as-prefix.yaml" }) +@AutoConfigureWebTestClient +public class KubernetesClientConfigMapConfigDataNameAsPrefixTests extends KubernetesClientConfigMapNameAsPrefixTests { + + private static MockedStatic clientUtilsMock; + + @BeforeAll + public static void wireMock() { + WireMockServer server = new WireMockServer(options().dynamicPort()); + server.start(); + WireMock.configureFor("localhost", server.port()); + clientUtilsMock = mockStatic(KubernetesClientUtils.class); + clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient) + .thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build()); + stubData(); + } + + @AfterAll + static void teardown() { + clientUtilsMock.close(); + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapIncludeProfileSpecificSourcesTests.java index a1fb50f1..c8a43cb3 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapIncludeProfileSpecificSourcesTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapIncludeProfileSpecificSourcesTests.java @@ -21,14 +21,8 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.client.config.applications.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** @@ -36,14 +30,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; * * @author wind57 */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes = IncludeProfileSpecificSourcesApp.class, - properties = { "spring.cloud.bootstrap.name=include-profile-specific-sources", - "include.profile.specific.sources=true", "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -@ActiveProfiles("dev") -class KubernetesClientConfigMapIncludeProfileSpecificSourcesTests { +abstract class KubernetesClientConfigMapIncludeProfileSpecificSourcesTests { @Autowired private WebTestClient webClient; diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapNameAsPrefixTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapNameAsPrefixTests.java index 77ffd97f..4d526761 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapNameAsPrefixTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapNameAsPrefixTests.java @@ -21,13 +21,8 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.client.config.applications.config_map_name_as_prefix.WithPrefixApp; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** @@ -35,12 +30,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; * * @author wind57 */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WithPrefixApp.class, - properties = { "spring.cloud.bootstrap.name=config-map-name-as-prefix", "config.map.name.as.prefix.stub=true", - "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -public class KubernetesClientConfigMapNameAsPrefixTests { +abstract class KubernetesClientConfigMapNameAsPrefixTests { @Autowired private WebTestClient webClient; diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/ConfigMapNameAsPrefixConfigurationStub.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/ConfigMapNameAsPrefixConfigurationStub.java index a639d713..a008f946 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/ConfigMapNameAsPrefixConfigurationStub.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/ConfigMapNameAsPrefixConfigurationStub.java @@ -64,7 +64,7 @@ public class ConfigMapNameAsPrefixConfigurationStub { return apiClient; } - private void stubData() { + public static void stubData() { V1ConfigMap one = new V1ConfigMapBuilder() .withMetadata(new V1ObjectMetaBuilder().withName("config-map-one").withNamespace("spring-k8s") .withResourceVersion("1").build()) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/IncludeProfileSpecificSourcesConfigurationStub.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/IncludeProfileSpecificSourcesConfigurationStub.java index ee37d971..eac5df20 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/IncludeProfileSpecificSourcesConfigurationStub.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap/stubs/IncludeProfileSpecificSourcesConfigurationStub.java @@ -45,7 +45,7 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options @Order(0) @Configuration @ConditionalOnProperty("include.profile.specific.sources") -class IncludeProfileSpecificSourcesConfigurationStub { +public class IncludeProfileSpecificSourcesConfigurationStub { @Bean public WireMockServer wireMock() { @@ -64,7 +64,7 @@ class IncludeProfileSpecificSourcesConfigurationStub { return apiClient; } - private void stubData() { + public static void stubData() { V1ConfigMap one = new V1ConfigMapBuilder() .withMetadata(new V1ObjectMetaBuilder().withName("config-map-one-dev").withNamespace("spring-k8s") .withResourceVersion("1").build()) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesClientBootstrapConfigurationInsideK8s.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesClientBootstrapConfigurationInsideK8s.java index eaa61f0f..898d664f 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesClientBootstrapConfigurationInsideK8s.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesClientBootstrapConfigurationInsideK8s.java @@ -31,7 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; * @author wind57 */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=abc" }) + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=abc", + "spring.cloud.bootstrap.enabled=true" }) class KubernetesClientBootstrapConfigurationInsideK8s { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesDisabled.java index c930a7bc..bfe295c4 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesDisabled.java @@ -31,8 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author wind57 */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.enabled=false", "kubernetes.informer.enabled=false", - "kubernetes.manifests.enabled=false" }) + properties = { "kubernetes.informer.enabled=false", "kubernetes.manifests.enabled=false" }) class KubernetesDisabled { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabled.java index 0006a5b6..fc43330f 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabled.java @@ -28,7 +28,8 @@ import org.springframework.context.ConfigurableApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) class KubernetesEnabled { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledConfigDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledConfigDisabled.java index 36dd39a9..e8ee40b5 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledConfigDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledConfigDisabled.java @@ -31,7 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; * @author wind57 */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) + properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) class KubernetesEnabledConfigDisabled { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledOnPurpose.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledOnPurpose.java index bae93bdb..821d15e2 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledOnPurpose.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledOnPurpose.java @@ -32,7 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties = { "spring.cloud.kubernetes.secrets.enabled=true", - "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) class KubernetesEnabledOnPurpose { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledSecretsDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledSecretsDisabled.java index 5e7e8470..db268a96 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledSecretsDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/boostrap_configuration/KubernetesEnabledSecretsDisabled.java @@ -32,7 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties = { "spring.cloud.kubernetes.secrets.enabled=false", - "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) class KubernetesEnabledSecretsDisabled { @Autowired diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastDisabled.java new file mode 100644 index 00000000..f94ff5ec --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastDisabled.java @@ -0,0 +1,30 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }, + classes = App.class) +class BootstrapConfigFailFastDisabled extends ConfigFailFastDisabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..6ce5113f --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java @@ -0,0 +1,31 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = App.class) +class BootstrapConfigFailFastEnabledButRetryDisabled extends ConfigFailFastEnabledButRetryDisabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryDisabledButSecretsRetryEnabled.java new file mode 100644 index 00000000..68ecd787 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryDisabledButSecretsRetryEnabled.java @@ -0,0 +1,32 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }, + classes = App.class) +class BootstrapConfigRetryDisabledButSecretsRetryEnabled extends ConfigRetryDisabledButSecretsRetryEnabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryEnabled.java new file mode 100644 index 00000000..e59631cd --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/BootstrapConfigRetryEnabled.java @@ -0,0 +1,31 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = App.class) +class BootstrapConfigRetryEnabled extends ConfigRetryEnabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastDisabled.java new file mode 100644 index 00000000..33051f02 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastDisabled.java @@ -0,0 +1,30 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, + classes = App.class) +class ConfigDataConfigFailFastDisabled extends ConfigFailFastDisabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..5fc1b2d3 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java @@ -0,0 +1,31 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = App.class) +class ConfigDataConfigFailFastEnabledButRetryDisabled extends ConfigFailFastEnabledButRetryDisabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java new file mode 100644 index 00000000..60ced494 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java @@ -0,0 +1,32 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, + classes = App.class) +class ConfigDataConfigRetryDisabledButSecretsRetryEnabled extends ConfigRetryDisabledButSecretsRetryEnabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryEnabled.java new file mode 100644 index 00000000..db2a2c96 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigDataConfigRetryEnabled.java @@ -0,0 +1,31 @@ +/* + * 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 org.springframework.boot.test.context.SpringBootTest; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = App.class) +class ConfigDataConfigRetryEnabled extends ConfigRetryEnabled { + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastDisabled.java index 7bdab621..ddb6ecef 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastDisabled.java @@ -28,8 +28,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySourceLocator; import org.springframework.mock.env.MockEnvironment; @@ -40,16 +39,14 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }, - classes = App.class) -class ConfigFailFastDisabled { +abstract class ConfigFailFastDisabled { private static final String API = "/api/v1/namespaces/default/configmaps"; @@ -86,12 +83,12 @@ class ConfigFailFastDisabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean + @Autowired private KubernetesClientConfigMapPropertySourceLocator propertySourceLocator; @Test void locateShouldNotRetry() { - + propertySourceLocator = spy(propertySourceLocator); stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastEnabledButRetryDisabled.java index e40d2c96..67d19781 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastEnabledButRetryDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigFailFastEnabledButRetryDisabled.java @@ -28,8 +28,6 @@ import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.springframework.beans.factory.annotation.Autowired; -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.context.ApplicationContext; @@ -43,18 +41,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", - "spring.main.cloud-platform=KUBERNETES" }, - classes = App.class) -class ConfigFailFastEnabledButRetryDisabled { +abstract class ConfigFailFastEnabledButRetryDisabled { private static final String API = "/api/v1/namespaces/default/configmaps"; @@ -91,7 +85,7 @@ class ConfigFailFastEnabledButRetryDisabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean + @Autowired private KubernetesClientConfigMapPropertySourceLocator propertySourceLocator; @Autowired @@ -99,7 +93,7 @@ class ConfigFailFastEnabledButRetryDisabled { @Test void locateShouldFailWithoutRetrying() { - + propertySourceLocator = spy(propertySourceLocator); stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isFalse(); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryDisabledButSecretsRetryEnabled.java index 497e3f86..bedffd00 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryDisabledButSecretsRetryEnabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/configmap_retry/ConfigRetryDisabledButSecretsRetryEnabled.java @@ -29,8 +29,6 @@ import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.springframework.beans.factory.annotation.Autowired; -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.context.ApplicationContext; @@ -40,22 +38,17 @@ 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.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", - "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES" }, - classes = App.class) -class ConfigRetryDisabledButSecretsRetryEnabled { +abstract class ConfigRetryDisabledButSecretsRetryEnabled { private static final String API = "/api/v1/namespaces/default/configmaps"; @@ -96,7 +89,7 @@ class ConfigRetryDisabledButSecretsRetryEnabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean + @Autowired private KubernetesClientConfigMapPropertySourceLocator propertySourceLocator; @Autowired @@ -104,7 +97,7 @@ class ConfigRetryDisabledButSecretsRetryEnabled { @Test void locateShouldFailWithoutRetrying() { - + propertySourceLocator = spy(propertySourceLocator); /* * Enabling secrets retry causes Spring Retry to be enabled and a * RetryOperationsInterceptor bean with NeverRetryPolicy for config maps to be @@ -114,7 +107,8 @@ class ConfigRetryDisabledButSecretsRetryEnabled { stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isTrue(); + // TODO not in bootstrap + // assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isTrue(); assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) .isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read ConfigMap(s) in namespace 'default'"); 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 7bfa4e31..dee9aed4 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 @@ -33,34 +33,28 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.kubernetes.client.KubernetesClientUtils; -import org.springframework.cloud.kubernetes.client.config.RetryableKubernetesClientConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; 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.getRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.spy; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", - "spring.main.cloud-platform=KUBERNETES" }, - classes = App.class) -class ConfigRetryEnabled { +abstract class ConfigRetryEnabled { private static final String API = "/api/v1/namespaces/default/configmaps"; @@ -97,12 +91,12 @@ class ConfigRetryEnabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean - private RetryableKubernetesClientConfigMapPropertySourceLocator propertySourceLocator; + @Autowired + private ConfigMapPropertySourceLocator retryablePl; @Test void locateShouldNotRetryWhenThereIsNoFailure() { - + ConfigMapPropertySourceLocator propertySourceLocator = spy(retryablePl); Map data = new HashMap<>(); data.put("some.prop", "theValue"); data.put("some.number", "0"); @@ -116,7 +110,7 @@ class ConfigRetryEnabled { .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); // verify locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + WireMock.verify(1, getRequestedFor(urlEqualTo(API))); // validate the contents of the property source assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue"); @@ -125,6 +119,7 @@ class ConfigRetryEnabled { @Test void locateShouldRetryAndRecover() { + ConfigMapPropertySourceLocator propertySourceLocator = spy(retryablePl); Map data = new HashMap<>(); data.put("some.prop", "theValue"); data.put("some.number", "0"); @@ -149,8 +144,8 @@ class ConfigRetryEnabled { PropertySource propertySource = Assertions .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); - // verify retried 4 times - verify(propertySourceLocator, times(4)).locate(any()); + // verify the request was retried 4 times, 5 total request + WireMock.verify(5, getRequestedFor(urlEqualTo(API))); // validate the contents of the property source assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue"); @@ -159,6 +154,7 @@ class ConfigRetryEnabled { @Test void locateShouldRetryAndFail() { + ConfigMapPropertySourceLocator propertySourceLocator = spy(retryablePl); // fail all the 5 requests stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); @@ -166,8 +162,8 @@ class ConfigRetryEnabled { .isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read ConfigMap(s) in namespace 'default'"); - // verify retried 5 times until failure - verify(propertySourceLocator, times(5)).locate(any()); + // verify the request was retried 5 times + WireMock.verify(5, getRequestedFor(urlEqualTo(API))); } } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastDisabled.java index 24e9e20a..e83623b8 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastDisabled.java @@ -28,8 +28,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; +import org.springframework.beans.factory.annotation.Autowired; 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.mock.env.MockEnvironment; @@ -40,6 +40,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -49,7 +50,7 @@ import static org.mockito.Mockito.verify; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", - "spring.main.cloud-platform=KUBERNETES" }, + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, classes = Application.class) class SecretsFailFastDisabled { @@ -88,12 +89,12 @@ class SecretsFailFastDisabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean - private KubernetesClientSecretsPropertySourceLocator propertySourceLocator; + @Autowired + private KubernetesClientSecretsPropertySourceLocator psl; @Test void locateShouldNotRetry() { - + KubernetesClientSecretsPropertySourceLocator propertySourceLocator = spy(psl); stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastEnabledButRetryDisabled.java index 929a4ea3..88c6a7ff 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastEnabledButRetryDisabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsFailFastEnabledButRetryDisabled.java @@ -30,7 +30,6 @@ import org.mockito.MockedStatic; import org.springframework.beans.factory.annotation.Autowired; 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.context.ApplicationContext; @@ -44,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -54,7 +54,7 @@ import static org.mockito.Mockito.verify; properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", - "spring.main.cloud-platform=KUBERNETES" }, + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, classes = Application.class) class SecretsFailFastEnabledButRetryDisabled { @@ -97,15 +97,15 @@ class SecretsFailFastEnabledButRetryDisabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean - private KubernetesClientSecretsPropertySourceLocator propertySourceLocator; + @Autowired + private KubernetesClientSecretsPropertySourceLocator psl; @Autowired private ApplicationContext context; @Test void locateShouldFailWithoutRetrying() { - + KubernetesClientSecretsPropertySourceLocator propertySourceLocator = spy(psl); stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isFalse(); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryDisabledButConfigRetryEnabled.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryDisabledButConfigRetryEnabled.java index d1bafcbe..6636cac9 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryDisabledButConfigRetryEnabled.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/secrets_retry/SecretsRetryDisabledButConfigRetryEnabled.java @@ -30,7 +30,6 @@ import org.mockito.MockedStatic; import org.springframework.beans.factory.annotation.Autowired; 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.context.ApplicationContext; @@ -40,10 +39,10 @@ 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.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -54,7 +53,8 @@ import static org.mockito.Mockito.verify; properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.secrets.name=my-secret", - "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES" }, + "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, classes = Application.class) class SecretsRetryDisabledButConfigRetryEnabled { @@ -97,15 +97,15 @@ class SecretsRetryDisabledButConfigRetryEnabled { stubConfigMapAndSecretsDefaults(); } - @SpyBean - private KubernetesClientSecretsPropertySourceLocator propertySourceLocator; + @Autowired + private KubernetesClientSecretsPropertySourceLocator psl; @Autowired private ApplicationContext context; @Test void locateShouldFailWithoutRetrying() { - + KubernetesClientSecretsPropertySourceLocator propertySourceLocator = spy(psl); /* * Enabling config retry causes Spring Retry to be enabled and a * RetryOperationsInterceptor bean with NeverRetryPolicy for secrets to be @@ -115,7 +115,8 @@ class SecretsRetryDisabledButConfigRetryEnabled { stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isTrue(); + // TODO not in bootstrap + // assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isTrue(); assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) .isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read Secret with name 'my-secret' in namespace 'default'"); 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 3431ff1e..16687cd7 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 @@ -27,38 +27,40 @@ import io.kubernetes.client.openapi.models.V1Secret; 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.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; +import org.springframework.beans.factory.annotation.Autowired; 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.RetryableKubernetesClientSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; 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.getRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.spy; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { - "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true", - "spring.cloud.kubernetes.secrets.retry.max-attempts=5", "spring.cloud.kubernetes.secrets.name=my-secret", - "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES" }, +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", + "spring.cloud.kubernetes.secrets.retry.max-attempts=5", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, classes = Application.class) class SecretsRetryEnabled { @@ -91,18 +93,18 @@ class SecretsRetryEnabled { clientUtilsMock.close(); } - @AfterEach + @Autowired + private SecretsPropertySourceLocator psl; + + @BeforeEach void afterEach() { WireMock.reset(); stubConfigMapAndSecretsDefaults(); } - @SpyBean - private RetryableKubernetesClientSecretsPropertySourceLocator propertySourceLocator; - @Test void locateShouldNotRetryWhenThereIsNoFailure() { - + SecretsPropertySourceLocator propertySourceLocator = spy(psl); Map data = new HashMap<>(); data.put("some.sensitive.prop", "theSensitiveValue".getBytes()); data.put("some.sensitive.number", "1".getBytes()); @@ -116,7 +118,7 @@ class SecretsRetryEnabled { .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); // verify locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + WireMock.verify(1, getRequestedFor(urlEqualTo(API))); // validate the contents of the property source assertThat(propertySource.getProperty("some.sensitive.prop")).isEqualTo("theSensitiveValue"); @@ -125,6 +127,7 @@ class SecretsRetryEnabled { @Test void locateShouldRetryAndRecover() { + SecretsPropertySourceLocator propertySourceLocator = spy(psl); Map data = new HashMap<>(); data.put("some.sensitive.prop", "theSensitiveValue".getBytes()); data.put("some.sensitive.number", "1".getBytes()); @@ -150,7 +153,7 @@ class SecretsRetryEnabled { .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); // verify retried 4 times - verify(propertySourceLocator, times(4)).locate(any()); + WireMock.verify(4, getRequestedFor(urlEqualTo(API))); // validate the contents of the property source assertThat(propertySource.getProperty("some.sensitive.prop")).isEqualTo("theSensitiveValue"); @@ -159,6 +162,7 @@ class SecretsRetryEnabled { @Test void locateShouldRetryAndFail() { + SecretsPropertySourceLocator propertySourceLocator = spy(psl); // fail all the 5 requests stubFor(get(API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); @@ -167,7 +171,7 @@ class SecretsRetryEnabled { .hasMessage("Unable to read Secret with name 'my-secret' in namespace 'default'"); // verify retried 5 times until failure - verify(propertySourceLocator, times(5)).locate(any()); + WireMock.verify(5, getRequestedFor(urlEqualTo(API))); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/KubernetesClientProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/KubernetesClientProperties.java index b31a7c1d..ee62df6d 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/KubernetesClientProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/KubernetesClientProperties.java @@ -20,14 +20,21 @@ import java.time.Duration; import org.springframework.boot.context.properties.ConfigurationProperties; +import static org.springframework.cloud.kubernetes.commons.KubernetesClientProperties.PREFIX; + /** * Kubernetes client properties. * * @author Ioannis Canellos */ -@ConfigurationProperties("spring.cloud.kubernetes.client") +@ConfigurationProperties(PREFIX) public class KubernetesClientProperties { + /** + * Configuration properties prefix. + */ + public static final String PREFIX = "spring.cloud.kubernetes.client"; + /** * Default user-agent for kubernetes client. */ diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/AbstractConfigProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/AbstractConfigProperties.java index 82e2bf59..14756bb8 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/AbstractConfigProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/AbstractConfigProperties.java @@ -121,6 +121,8 @@ public abstract class AbstractConfigProperties { */ private int maxAttempts = 6; + private boolean enabled = true; + public long getInitialInterval() { return this.initialInterval; } @@ -153,6 +155,14 @@ public abstract class AbstractConfigProperties { this.maxAttempts = maxAttempts; } + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableConfigMapPropertySourceLocator.java new file mode 100644 index 00000000..75f383c7 --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableConfigMapPropertySourceLocator.java @@ -0,0 +1,72 @@ +/* + * Copyright 2013-2020 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.commons.config; + +import java.util.Collection; + +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.retry.support.RetryTemplate; + +/** + * ConfigMapPropertySourceLocator for when retry is enabled. + * + * @author Ryan Baxter + */ +public class ConfigDataRetryableConfigMapPropertySourceLocator extends ConfigMapPropertySourceLocator { + + private RetryTemplate retryTemplate; + + private ConfigMapPropertySourceLocator configMapPropertySourceLocator; + + public ConfigDataRetryableConfigMapPropertySourceLocator( + ConfigMapPropertySourceLocator configMapPropertySourceLocator, ConfigMapConfigProperties properties) { + super(properties); + this.configMapPropertySourceLocator = configMapPropertySourceLocator; + this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().getMaxAttempts()) + .exponentialBackoff(properties.getRetry().getInitialInterval(), properties.getRetry().getMultiplier(), + properties.getRetry().getMaxInterval()) + .build(); + } + + @Override + protected MapPropertySource getMapPropertySource(NormalizedSource normalizedSource, + ConfigurableEnvironment environment) { + return configMapPropertySourceLocator.getMapPropertySource(normalizedSource, environment); + } + + @Override + public PropertySource locate(Environment environment) { + return retryTemplate.execute(retryContext -> configMapPropertySourceLocator.locate(environment)); + } + + @Override + public Collection> locateCollection(Environment environment) { + return retryTemplate.execute(retryContext -> configMapPropertySourceLocator.locateCollection(environment)); + } + + public void setConfigMapPropertySourceLocator(ConfigMapPropertySourceLocator configMapPropertySourceLocator) { + this.configMapPropertySourceLocator = configMapPropertySourceLocator; + } + + public ConfigMapPropertySourceLocator getConfigMapPropertySourceLocator() { + return configMapPropertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java new file mode 100644 index 00000000..2f78f306 --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java @@ -0,0 +1,72 @@ +/* + * 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.commons.config; + +import java.util.Collection; + +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.retry.support.RetryTemplate; + +/** + * SecretsPropertySourceLocator for when retry is enabled. + * + * @author Ryan Baxter + */ +public class ConfigDataRetryableSecretsPropertySourceLocator extends SecretsPropertySourceLocator { + + private RetryTemplate retryTemplate; + + private SecretsPropertySourceLocator secretsPropertySourceLocator; + + public ConfigDataRetryableSecretsPropertySourceLocator(SecretsPropertySourceLocator propertySourceLocator, + SecretsConfigProperties secretsConfigProperties) { + super(secretsConfigProperties); + this.secretsPropertySourceLocator = propertySourceLocator; + this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().getMaxAttempts()) + .exponentialBackoff(properties.getRetry().getInitialInterval(), properties.getRetry().getMultiplier(), + properties.getRetry().getMaxInterval()) + .build(); + } + + @Override + public PropertySource locate(Environment environment) { + return retryTemplate.execute(retryContext -> secretsPropertySourceLocator.locate(environment)); + } + + @Override + public Collection> locateCollection(Environment environment) { + return retryTemplate.execute(retryContext -> secretsPropertySourceLocator.locateCollection(environment)); + } + + @Override + protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, + NormalizedSource normalizedSource) { + return this.secretsPropertySourceLocator.getPropertySource(environment, normalizedSource); + } + + public SecretsPropertySourceLocator getSecretsPropertySourceLocator() { + return secretsPropertySourceLocator; + } + + public void setSecretsPropertySourceLocator(SecretsPropertySourceLocator secretsPropertySourceLocator) { + this.secretsPropertySourceLocator = secretsPropertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesBootstrapConfiguration.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesBootstrapConfiguration.java index 477562fd..9c06059b 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesBootstrapConfiguration.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesBootstrapConfiguration.java @@ -46,9 +46,9 @@ public class KubernetesBootstrapConfiguration { @Configuration(proxyBeanMethods = false) @EnableRetry(proxyTargetClass = true) @Import(AopAutoConfiguration.class) - static class RetryConfiguration { + public static class RetryConfiguration { - private static RetryOperationsInterceptor retryOperationsInterceptor( + public static RetryOperationsInterceptor retryOperationsInterceptor( AbstractConfigProperties.RetryProperties retryProperties) { return RetryInterceptorBuilder.stateless().backOffOptions(retryProperties.getInitialInterval(), retryProperties.getMultiplier(), retryProperties.getMaxInterval()) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLoader.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLoader.java new file mode 100644 index 00000000..624dbdae --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLoader.java @@ -0,0 +1,83 @@ +/* + * 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.commons.config; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.context.config.ConfigData; +import org.springframework.boot.context.config.ConfigData.Option; +import org.springframework.boot.context.config.ConfigDataLoader; +import org.springframework.boot.context.config.ConfigDataLoaderContext; +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; +import org.springframework.core.Ordered; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; + +/** + * @author Ryan Baxter + */ +public class KubernetesConfigDataLoader implements ConfigDataLoader, Ordered { + + @Override + public ConfigData load(ConfigDataLoaderContext context, KubernetesConfigDataResource resource) + throws IOException, ConfigDataResourceNotFoundException { + + List> propertySources = new ArrayList<>(2); + ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext(); + Environment env = resource.getEnvironment(); + + if (bootstrapContext.isRegistered(ConfigMapPropertySourceLocator.class)) { + propertySources.add(bootstrapContext.get(ConfigMapPropertySourceLocator.class).locate(env)); + } + if (bootstrapContext.isRegistered(SecretsPropertySourceLocator.class)) { + propertySources.add(bootstrapContext.get(SecretsPropertySourceLocator.class).locate(env)); + } + + // boot 2.4.5+ + return new ConfigData(propertySources, propertySource -> { + String propertySourceName = propertySource.getName(); + List - - org.springframework.cloud - spring-cloud-starter-bootstrap - org.springframework.cloud spring-cloud-commons diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java index 6cb0a698..6ab8aa15 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java @@ -23,6 +23,7 @@ import io.fabric8.kubernetes.client.KubernetesClient; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesConfigEnabled; import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesSecretsEnabled; @@ -49,6 +50,7 @@ import org.springframework.core.env.Environment; @ConditionalOnClass({ ConfigMap.class, Secret.class }) @AutoConfigureAfter(KubernetesBootstrapConfiguration.class) @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) +@ConditionalOnProperty(value = "spring.cloud.bootstrap.enabled", havingValue = "true") public class Fabric8BootstrapConfiguration { @Bean diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigDataLocationResolver.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigDataLocationResolver.java new file mode 100644 index 00000000..0861ddd4 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigDataLocationResolver.java @@ -0,0 +1,141 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.KubernetesClient; + +import org.springframework.boot.BootstrapRegistry; +import org.springframework.boot.BootstrapRegistry.InstanceSupplier; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataLocationResolverContext; +import org.springframework.boot.context.config.Profiles; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigDataLocationResolver; +import org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; +import org.springframework.core.env.Environment; + +/** + * @author Ryan Baxter + */ +public class Fabric8ConfigDataLocationResolver extends KubernetesConfigDataLocationResolver { + + public Fabric8ConfigDataLocationResolver(DeferredLogFactory factory) { + super(factory); + } + + @Override + protected void registerBeans(ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, + Profiles profiles, KubernetesConfigDataLocationResolver.PropertyHolder propertyHolder, + KubernetesNamespaceProvider namespaceProvider) { + KubernetesClientProperties properties = propertyHolder.kubernetesClientProperties(); + ConfigMapConfigProperties configMapProperties = propertyHolder.configMapConfigProperties(); + SecretsConfigProperties secretsProperties = propertyHolder.secretsProperties(); + + ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); + Config config = config(properties); + bootstrapContext.registerIfAbsent(Config.class, InstanceSupplier.of(config)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("fabric8Config", event.getBootstrapContext().get(Config.class))); + + KubernetesClient kubernetesClient = kubernetesClient(config); + bootstrapContext.registerIfAbsent(KubernetesClient.class, + BootstrapRegistry.InstanceSupplier.of(kubernetesClient)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configKubernetesClient", event.getBootstrapContext().get(KubernetesClient.class))); + + if (isRetryEnabled(configMapProperties, secretsProperties)) { + registerRetryBeans(configMapProperties, secretsProperties, bootstrapContext, kubernetesClient, + namespaceProvider); + } + else { + if (configMapProperties.isEnabled()) { + Fabric8ConfigMapPropertySourceLocator configMapPropertySourceLocator = new Fabric8ConfigMapPropertySourceLocator( + kubernetesClient, configMapProperties, namespaceProvider); + bootstrapContext.registerIfAbsent(ConfigMapPropertySourceLocator.class, + InstanceSupplier.of(configMapPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configDataConfigMapPropertySourceLocator", + event.getBootstrapContext().get(ConfigMapPropertySourceLocator.class))); + } + if (secretsProperties.isEnabled()) { + Fabric8SecretsPropertySourceLocator secretsPropertySourceLocator = new Fabric8SecretsPropertySourceLocator( + kubernetesClient, secretsProperties, namespaceProvider); + bootstrapContext.registerIfAbsent(SecretsPropertySourceLocator.class, + InstanceSupplier.of(secretsPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory() + .registerSingleton("configDataSecretsPropertySourceLocator", + event.getBootstrapContext().get(SecretsPropertySourceLocator.class))); + } + } + } + + private void registerRetryBeans(ConfigMapConfigProperties configMapProperties, + SecretsConfigProperties secretsProperties, ConfigurableBootstrapContext bootstrapContext, + KubernetesClient kubernetesClient, KubernetesNamespaceProvider namespaceProvider) { + if (configMapProperties.isEnabled()) { + ConfigMapPropertySourceLocator configMapPropertySourceLocator = new Fabric8ConfigMapPropertySourceLocator( + kubernetesClient, configMapProperties, namespaceProvider); + if (isRetryEnabledForConfigMap(configMapProperties)) { + configMapPropertySourceLocator = new ConfigDataRetryableConfigMapPropertySourceLocator( + configMapPropertySourceLocator, configMapProperties); + } + + bootstrapContext.registerIfAbsent(ConfigMapPropertySourceLocator.class, + InstanceSupplier.of(configMapPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory().registerSingleton( + "configDataConfigMapPropertySourceLocator", + event.getBootstrapContext().get(ConfigMapPropertySourceLocator.class))); + } + + if (secretsProperties.isEnabled()) { + SecretsPropertySourceLocator secretsPropertySourceLocator = new Fabric8SecretsPropertySourceLocator( + kubernetesClient, secretsProperties, namespaceProvider); + if (isRetryEnabledForSecrets(secretsProperties)) { + secretsPropertySourceLocator = new ConfigDataRetryableSecretsPropertySourceLocator( + secretsPropertySourceLocator, secretsProperties); + } + bootstrapContext.registerIfAbsent(SecretsPropertySourceLocator.class, + InstanceSupplier.of(secretsPropertySourceLocator)); + bootstrapContext.addCloseListener(event -> event.getApplicationContext().getBeanFactory().registerSingleton( + "configDataSecretsPropertySourceLocator", + event.getBootstrapContext().get(SecretsPropertySourceLocator.class))); + } + } + + protected KubernetesNamespaceProvider kubernetesNamespaceProvider(Environment environment) { + return new KubernetesNamespaceProvider(environment); + } + + private Config config(KubernetesClientProperties properties) { + return new Fabric8AutoConfiguration().kubernetesClientConfig(properties); + } + + private KubernetesClient kubernetesClient(Config config) { + return new Fabric8AutoConfiguration().kubernetesClient(config); + } + +} 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 159d9c26..9c58bb5d 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 @@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; @@ -48,6 +49,7 @@ import org.springframework.context.annotation.Import; @AutoConfigureAfter(KubernetesBootstrapConfiguration.class) @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) @ConditionalOnKubernetesConfigOrSecretsRetryEnabled +@ConditionalOnProperty(value = "spring.cloud.bootstrap.enabled", havingValue = "true") public class Fabric8RetryBootstrapConfiguration { @Bean diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-fabric8-config/src/main/resources/META-INF/spring.factories index cdd2220a..04934d5b 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-kubernetes-fabric8-config/src/main/resources/META-INF/spring.factories @@ -3,3 +3,6 @@ org.springframework.cloud.kubernetes.fabric8.config.reload.ConfigReloadAutoConfi org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.kubernetes.fabric8.config.Fabric8BootstrapConfiguration,\ org.springframework.cloud.kubernetes.fabric8.config.Fabric8RetryBootstrapConfiguration +# ConfigData Location Resolvers +org.springframework.boot.context.config.ConfigDataLocationResolver=\ +org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigDataLocationResolver diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapConfigMapsTests.java new file mode 100644 index 00000000..0f8757fa --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapConfigMapsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-example", "spring.cloud.kubernetes.reload.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BoostrapConfigMapsTests extends ConfigMapsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapFabric8SecretsPropertySourceTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapFabric8SecretsPropertySourceTest.java new file mode 100644 index 00000000..d57fd42e --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapFabric8SecretsPropertySourceTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@TestPropertySource("classpath:/application-secrets.properties") +@EnableKubernetesMockClient(crud = true, https = false) +class BoostrapFabric8SecretsPropertySourceTest extends Fabric8SecretsPropertySourceTest { + + private static KubernetesClient mockClient; + + @BeforeAll + static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapMultipleConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapMultipleConfigMapsTests.java new file mode 100644 index 00000000..d9efdbcd --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BoostrapMultipleConfigMapsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example2.ExampleApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ExampleApp.class, + properties = { "spring.cloud.bootstrap.name=multiplecms", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BoostrapMultipleConfigMapsTests extends MultipleConfigMapsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithIncludeProfileSpecificSourcesTests.java new file mode 100644 index 00000000..6ed645bd --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithIncludeProfileSpecificSourcesTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author wind57 + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = IncludeProfileSpecificSourcesApp.class, + properties = { "spring.cloud.bootstrap.name=include-profile-specific-sources", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +@ActiveProfiles("dev") +class BootstrapConfigMapWithIncludeProfileSpecificSourcesTests extends ConfigMapWithIncludeProfileSpecificSourcesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithPrefixTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithPrefixTests.java new file mode 100644 index 00000000..1adf140a --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapWithPrefixTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.with_prefix.WithPrefixApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author wind57 + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WithPrefixApp.class, + properties = { "spring.cloud.bootstrap.name=config-map-name-as-prefix", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +class BootstrapConfigMapWithPrefixTests extends ConfigMapWithPrefixTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsFromFilePathsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsFromFilePathsTests.java new file mode 100644 index 00000000..d21ecd5a --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsFromFilePathsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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.io.IOException; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { + "spring.application.name=configmap-path-example", "spring.cloud.kubernetes.config.enableApi=false", + "spring.cloud.kubernetes.config.paths=" + BootstrapConfigMapsFromFilePathsTests.FIRST_FILE_NAME_FULL_PATH + "," + + BootstrapConfigMapsFromFilePathsTests.SECOND_FILE_NAME_FULL_PATH + "," + + BootstrapConfigMapsFromFilePathsTests.FIRST_FILE_NAME_DUPLICATED_FULL_PATH, + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsFromFilePathsTests extends ConfigMapsFromFilePathsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() throws IOException { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsMixedTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsMixedTests.java new file mode 100644 index 00000000..b550c939 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsMixedTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2013-2019 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.io.IOException; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=" + BootstrapConfigMapsMixedTests.APPLICATION_NAME, + "spring.cloud.kubernetes.config.enableApi=true", + "spring.cloud.kubernetes.config.paths=" + BootstrapConfigMapsMixedTests.FILE_NAME_FULL_PATH, + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsMixedTests extends ConfigMapsMixedTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() throws IOException { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithActiveProfilesNameTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithActiveProfilesNameTests.java new file mode 100644 index 00000000..2be71533 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithActiveProfilesNameTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Ali Shahbour + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-active-profile-name-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsWithActiveProfilesNameTests extends ConfigMapsWithActiveProfilesNameTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfileExpressionTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfileExpressionTests.java new file mode 100644 index 00000000..85109710 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfileExpressionTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2018 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * Tests reading property from YAML document specified by profile expression. + */ + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@ActiveProfiles({ "production", "us-east" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsWithProfileExpressionTests extends ConfigMapsWithProfileExpressionTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesNoActiveProfileTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesNoActiveProfileTests.java new file mode 100644 index 00000000..ab6304dd --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesNoActiveProfileTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-no-active-profiles-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsWithProfilesNoActiveProfileTests extends ConfigMapsWithProfilesNoActiveProfileTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesTests.java new file mode 100644 index 00000000..b3b957d0 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithProfilesTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsWithProfilesTests extends ConfigMapsWithProfilesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithoutProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithoutProfilesTests.java new file mode 100644 index 00000000..83d053ff --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapConfigMapsWithoutProfilesTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-without-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapConfigMapsWithoutProfilesTests extends ConfigMapsWithoutProfilesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTest.java new file mode 100644 index 00000000..6f796ddc --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = TestApplication.class, + properties = { "spring.application.name=testapp", "spring.cloud.kubernetes.client.namespace=testns", + "spring.cloud.kubernetes.client.trustCerts=true", "spring.cloud.kubernetes.config.namespace=testns", + "spring.cloud.kubernetes.secrets.enableApi=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapCoreTest extends CoreTest { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTestClientViaSystemProperties.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTestClientViaSystemProperties.java new file mode 100644 index 00000000..3b50cb2b --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapCoreTestClientViaSystemProperties.java @@ -0,0 +1,32 @@ +/* + * Copyright 2013-2021 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 org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = TestApplication.class, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.application.name=testapp", + "spring.cloud.kubernetes.client.namespace=testns", "spring.cloud.kubernetes.client.trustCerts=true", + "spring.cloud.kubernetes.config.namespace=testns", "spring.cloud.kubernetes.secrets.enableApi=true", + "spring.cloud.bootstrap.enabled=true" }) +public class BootstrapCoreTestClientViaSystemProperties extends CoreTestClientViaSystemProperties { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapMultipleSecretsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapMultipleSecretsTests.java new file mode 100644 index 00000000..351df840 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/BootstrapMultipleSecretsTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example3.MultiSecretsApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Haytham Mohamed + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MultiSecretsApp.class, + properties = { "spring.cloud.bootstrap.name=multiple-secrets", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class BootstrapMultipleSecretsTests extends MultipleSecretsTests { + + // will be injected by KubernetesMockServerExtension + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithIncludeProfileSpecificSourcesTests.java new file mode 100644 index 00000000..06b932a8 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithIncludeProfileSpecificSourcesTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author wind57 + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = IncludeProfileSpecificSourcesApp.class, + properties = { "spring.application.name=include-profile-specific-sources", + "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./include-profile-specific-sources.yaml" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +@ActiveProfiles("dev") +class ConfigDataConfigMapWithIncludeProfileSpecificSourcesTests + extends ConfigMapWithIncludeProfileSpecificSourcesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithPrefixTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithPrefixTests.java new file mode 100644 index 00000000..ed48d695 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapWithPrefixTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.with_prefix.WithPrefixApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author wind57 + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WithPrefixApp.class, + properties = { "spring.application.name=config-map-name-as-prefix", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./config-map-name-as-prefix.yaml" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +class ConfigDataConfigMapWithPrefixTests extends ConfigMapWithPrefixTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsFromFilePathsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsFromFilePathsTests.java new file mode 100644 index 00000000..4f21d7b3 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsFromFilePathsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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.io.IOException; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { + "spring.application.name=configmap-path-example", "spring.cloud.kubernetes.config.enableApi=false", + "spring.cloud.kubernetes.config.paths=" + ConfigDataConfigMapsFromFilePathsTests.FIRST_FILE_NAME_FULL_PATH + "," + + ConfigDataConfigMapsFromFilePathsTests.SECOND_FILE_NAME_FULL_PATH + "," + + ConfigDataConfigMapsFromFilePathsTests.FIRST_FILE_NAME_DUPLICATED_FULL_PATH, + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsFromFilePathsTests extends ConfigMapsFromFilePathsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() throws IOException { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsMixedTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsMixedTests.java new file mode 100644 index 00000000..ccfe0123 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsMixedTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2013-2019 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.io.IOException; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=" + ConfigDataConfigMapsMixedTests.APPLICATION_NAME, + "spring.cloud.kubernetes.config.enableApi=true", + "spring.cloud.kubernetes.config.paths=" + ConfigDataConfigMapsMixedTests.FILE_NAME_FULL_PATH, + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsMixedTests extends ConfigMapsMixedTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() throws IOException { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsTests.java new file mode 100644 index 00000000..83abf5d8 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-example", "spring.cloud.kubernetes.reload.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsTests extends ConfigMapsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithActiveProfilesNameTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithActiveProfilesNameTests.java new file mode 100644 index 00000000..ec6f6793 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithActiveProfilesNameTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Ali Shahbour + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-active-profile-name-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsWithActiveProfilesNameTests extends ConfigMapsWithActiveProfilesNameTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfileExpressionTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfileExpressionTests.java new file mode 100644 index 00000000..5505c44d --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfileExpressionTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2018 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * Tests reading property from YAML document specified by profile expression. + */ + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@ActiveProfiles({ "production", "us-east" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsWithProfileExpressionTests extends ConfigMapsWithProfileExpressionTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesNoActiveProfileTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesNoActiveProfileTests.java new file mode 100644 index 00000000..b48eabb2 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesNoActiveProfileTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-no-active-profiles-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsWithProfilesNoActiveProfileTests extends ConfigMapsWithProfilesNoActiveProfileTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesTests.java new file mode 100644 index 00000000..e890b6ab --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithProfilesTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsWithProfilesTests extends ConfigMapsWithProfilesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithoutProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithoutProfilesTests.java new file mode 100644 index 00000000..a35ecbfc --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataConfigMapsWithoutProfilesTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.application.name=configmap-without-profile-example", + "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@ActiveProfiles("development") +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataConfigMapsWithoutProfilesTests extends ConfigMapsWithoutProfilesTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTest.java new file mode 100644 index 00000000..209d7dd4 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = TestApplication.class, + properties = { "spring.application.name=testapp", "spring.cloud.kubernetes.client.namespace=testns", + "spring.cloud.kubernetes.client.trustCerts=true", "spring.cloud.kubernetes.config.namespace=testns", + "spring.cloud.kubernetes.secrets.enableApi=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataCoreTest extends CoreTest { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTestClientViaSystemProperties.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTestClientViaSystemProperties.java new file mode 100644 index 00000000..ed344d81 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataCoreTestClientViaSystemProperties.java @@ -0,0 +1,32 @@ +/* + * Copyright 2013-2021 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 org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = TestApplication.class, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.application.name=testapp", + "spring.cloud.kubernetes.client.namespace=testns", "spring.cloud.kubernetes.client.trustCerts=true", + "spring.cloud.kubernetes.config.namespace=testns", "spring.cloud.kubernetes.secrets.enableApi=true", + "spring.config.import=kubernetes:" }) +public class ConfigDataCoreTestClientViaSystemProperties extends CoreTestClientViaSystemProperties { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataFabric8SecretsPropertySourceTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataFabric8SecretsPropertySourceTest.java new file mode 100644 index 00000000..acc28679 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataFabric8SecretsPropertySourceTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2021 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example.App; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }) +@TestPropertySource("classpath:/application-secrets.properties") +@EnableKubernetesMockClient(crud = true, https = false) +class ConfigDataFabric8SecretsPropertySourceTest extends Fabric8SecretsPropertySourceTest { + + private static KubernetesClient mockClient; + + @BeforeAll + static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleConfigMapsTests.java new file mode 100644 index 00000000..1d381b0d --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleConfigMapsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example2.ExampleApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Charles Moulliard + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ExampleApp.class, + properties = { "spring.application.name=multiplecms", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./multiplecms.yml" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataMultipleConfigMapsTests extends MultipleConfigMapsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleSecretsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleSecretsTests.java new file mode 100644 index 00000000..441b2c3e --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigDataMultipleSecretsTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.example3.MultiSecretsApp; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Haytham Mohamed + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MultiSecretsApp.class, + properties = { "spring.cloud.bootstrap.name=multiple-secrets", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./multiple-secrets.yml" }) +@AutoConfigureWebTestClient +@EnableKubernetesMockClient(crud = true, https = false) +public class ConfigDataMultipleSecretsTests extends MultipleSecretsTests { + + // will be injected by KubernetesMockServerExtension + private static KubernetesClient mockClient; + + @BeforeAll + public static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithIncludeProfileSpecificSourcesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithIncludeProfileSpecificSourcesTests.java index 8a1b7aa6..dfaa5e37 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithIncludeProfileSpecificSourcesTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithIncludeProfileSpecificSourcesTests.java @@ -22,41 +22,24 @@ import java.util.Map; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.include_profile_specific_sources.IncludeProfileSpecificSourcesApp; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** * @author wind57 */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes = IncludeProfileSpecificSourcesApp.class, - properties = { "spring.cloud.bootstrap.name=include-profile-specific-sources", - "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -@ActiveProfiles("dev") -class ConfigMapWithIncludeProfileSpecificSourcesTests { +abstract class ConfigMapWithIncludeProfileSpecificSourcesTests { private static KubernetesClient mockClient; @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { - + public static void setUpBeforeClass(KubernetesClient mockClient) { + ConfigMapWithIncludeProfileSpecificSourcesTests.mockClient = mockClient; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithPrefixTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithPrefixTests.java index 86d86987..c0e1c083 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithPrefixTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapWithPrefixTests.java @@ -22,9 +22,7 @@ import java.util.Map; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -43,17 +41,15 @@ import org.springframework.test.web.reactive.server.WebTestClient; properties = { "spring.cloud.bootstrap.name=config-map-name-as-prefix", "spring.main.cloud-platform=KUBERNETES" }) @AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -class ConfigMapWithPrefixTests { +abstract class ConfigMapWithPrefixTests { private static KubernetesClient mockClient; @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { - + public static void setUpBeforeClass(KubernetesClient mockClient) { + ConfigMapWithPrefixTests.mockClient = mockClient; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsFromFilePathsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsFromFilePathsTests.java index afb245c3..0161a06d 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsFromFilePathsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsFromFilePathsTests.java @@ -22,9 +22,7 @@ import java.nio.file.Paths; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -44,8 +42,7 @@ import static org.assertj.core.util.Lists.newArrayList; + ConfigMapsFromFilePathsTests.SECOND_FILE_NAME_FULL_PATH + "," + ConfigMapsFromFilePathsTests.FIRST_FILE_NAME_DUPLICATED_FULL_PATH, "spring.main.cloud-platform=KUBERNETES" }) -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsFromFilePathsTests { +abstract class ConfigMapsFromFilePathsTests { protected static final String FILES_ROOT_PATH = "/tmp/scktests"; @@ -66,13 +63,10 @@ public class ConfigMapsFromFilePathsTests { protected static final String FIRST_FILE_NAME_DUPLICATED_FULL_PATH = FILES_ROOT_PATH + "/" + FILES_SUB_PATH + "/" + FIRST_FILE_NAME; - private static KubernetesClient mockClient; - @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() throws IOException { + public static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsMixedTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsMixedTests.java index 11ec267d..0e967afb 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsMixedTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsMixedTests.java @@ -25,28 +25,15 @@ import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import static org.assertj.core.util.Lists.newArrayList; -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=" + ConfigMapsMixedTests.APPLICATION_NAME, - "spring.cloud.kubernetes.config.enableApi=true", - "spring.cloud.kubernetes.config.paths=" + ConfigMapsMixedTests.FILE_NAME_FULL_PATH, - "spring.main.cloud-platform=KUBERNETES" }) -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsMixedTests { +abstract class ConfigMapsMixedTests { protected static final String FILES_ROOT_PATH = "/tmp/scktests"; @@ -56,13 +43,10 @@ public class ConfigMapsMixedTests { protected static final String APPLICATION_NAME = "configmap-mixed-example"; - private static KubernetesClient mockClient; - @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() throws IOException { + public static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsTests.java index e40ad1a0..ad769409 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsTests.java @@ -22,16 +22,9 @@ import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -39,13 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Charles Moulliard */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-example", "spring.cloud.kubernetes.reload.enabled=false", - "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsTests { +abstract class ConfigMapsTests { private static final String APPLICATION_NAME = "configmap-example"; @@ -57,9 +44,8 @@ public class ConfigMapsTests { @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { - + public static void setUpBeforeClass(KubernetesClient mockClient) { + ConfigMapsTests.mockClient = mockClient; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithActiveProfilesNameTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithActiveProfilesNameTests.java index b5b1716f..23f40544 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithActiveProfilesNameTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithActiveProfilesNameTests.java @@ -21,47 +21,27 @@ import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil.readResourceFile; /** * @author Ali Shahbour */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-with-active-profile-name-example", - "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -@ActiveProfiles("development") -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsWithActiveProfilesNameTests { +abstract class ConfigMapsWithActiveProfilesNameTests { private static final String APPLICATION_NAME = "configmap-with-active-profile-name-example"; - private static KubernetesClient mockClient; - @Autowired(required = false) Config config; @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { - + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfileExpressionTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfileExpressionTests.java index 46d7c525..e0605cf2 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfileExpressionTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfileExpressionTests.java @@ -21,41 +21,23 @@ import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** * Tests reading property from YAML document specified by profile expression. */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-with-profile-example", - "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -@ActiveProfiles({ "production", "us-east" }) -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsWithProfileExpressionTests { - - private static KubernetesClient mockClient; +abstract class ConfigMapsWithProfileExpressionTests { private static final String APPLICATION_NAME = "configmap-with-profile-example"; @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesNoActiveProfileTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesNoActiveProfileTests.java index e81a6fa0..d7afd453 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesNoActiveProfileTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesNoActiveProfileTests.java @@ -21,16 +21,9 @@ import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import static org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil.readResourceFile; @@ -38,23 +31,14 @@ import static org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestU /** * @author Charles Moulliard */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-with-profile-no-active-profiles-example", - "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsWithProfilesNoActiveProfileTests { +abstract class ConfigMapsWithProfilesNoActiveProfileTests { private static final String APPLICATION_NAME = "configmap-with-profile-no-active-profiles-example"; - private static KubernetesClient mockClient; - @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesTests.java index 0ac709d3..56a8d000 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithProfilesTests.java @@ -21,43 +21,25 @@ import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** * @author Charles Moulliard */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-with-profile-example", - "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -@ActiveProfiles("development") -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsWithProfilesTests { +abstract class ConfigMapsWithProfilesTests { private static final String APPLICATION_NAME = "configmap-with-profile-example"; - private static KubernetesClient mockClient; - @Autowired(required = false) Config config; @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithoutProfilesTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithoutProfilesTests.java index 3c26bb93..9f8849ab 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithoutProfilesTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapsWithoutProfilesTests.java @@ -21,37 +21,19 @@ import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.application.name=configmap-without-profile-example", - "spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -@ActiveProfiles("development") -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class ConfigMapsWithoutProfilesTests { +abstract class ConfigMapsWithoutProfilesTests { private static final String APPLICATION_NAME = "configmap-without-profile-example"; - private static KubernetesClient mockClient; - @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTest.java index 677c4bc4..be0c9b98 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTest.java @@ -23,25 +23,14 @@ import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.Environment; -import org.springframework.test.context.junit.jupiter.SpringExtension; import static org.assertj.core.api.Assertions.assertThat; -@ExtendWith(SpringExtension.class) -@SpringBootTest(classes = TestApplication.class, - properties = { "spring.application.name=testapp", "spring.cloud.kubernetes.client.namespace=testns", - "spring.cloud.kubernetes.client.trustCerts=true", "spring.cloud.kubernetes.config.namespace=testns", - "spring.cloud.kubernetes.secrets.enableApi=true", "spring.main.cloud-platform=KUBERNETES" }) -@EnableKubernetesMockClient(crud = true, https = false) -public class CoreTest { +abstract class CoreTest { private static KubernetesClient mockClient; @@ -51,9 +40,8 @@ public class CoreTest { @Autowired private Config config; - @BeforeAll - public static void setUpBeforeClass() { - + public static void setUpBeforeClass(KubernetesClient mockClient) { + CoreTest.mockClient = mockClient; Map data1 = new HashMap<>(); data1.put("spring.kubernetes.test.value", "value1"); mockClient.configMaps().inNamespace("testns").create( diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTestClientViaSystemProperties.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTestClientViaSystemProperties.java index 6ebdbd83..2b922b65 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTestClientViaSystemProperties.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/CoreTestClientViaSystemProperties.java @@ -20,20 +20,12 @@ import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit.jupiter.SpringExtension; import static org.assertj.core.api.Assertions.assertThat; -@ExtendWith(SpringExtension.class) -@SpringBootTest(classes = TestApplication.class, - properties = { "spring.main.cloud-platform=KUBERNETES", "spring.application.name=testapp", - "spring.cloud.kubernetes.client.namespace=testns", "spring.cloud.kubernetes.client.trustCerts=true", - "spring.cloud.kubernetes.config.namespace=testns", "spring.cloud.kubernetes.secrets.enableApi=true" }) -public class CoreTestClientViaSystemProperties { +abstract class CoreTestClientViaSystemProperties { @Autowired private KubernetesClient client; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceTest.java index d6816bc5..767ec547 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceTest.java @@ -22,33 +22,19 @@ import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example.App; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; import static java.util.Collections.singletonMap; import static org.assertj.core.api.Assertions.assertThat; -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = "spring.main.cloud-platform=KUBERNETES") -@TestPropertySource("classpath:/application-secrets.properties") -@EnableKubernetesMockClient(crud = true, https = false) -class Fabric8SecretsPropertySourceTest { +abstract class Fabric8SecretsPropertySourceTest { private static final String NAMESPACE = "test"; - private static KubernetesClient mockClient; - private static final String SECRET_VALUE = "secretValue"; @Autowired @@ -57,8 +43,7 @@ class Fabric8SecretsPropertySourceTest { @Autowired private Environment environment; - @BeforeAll - static void setUpBeforeClass() { + static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigConfigurationTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigConfigurationTest.java index eafda75f..6d27eb2c 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigConfigurationTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigConfigurationTest.java @@ -41,13 +41,21 @@ public class KubernetesConfigConfigurationTest extends KubernetesConfigTestBase private static KubernetesClient mockClient; @Test - public void kubernetesWhenKubernetesDefaultEnabled() { + public void kubernetesBootstrapWhenKubernetesDefaultEnabled() { setup(KubernetesClientTestConfiguration.class, "spring.main.cloud-platform=KUBERNETES", - "spring.cloud.kubernetes.client.namespace=default"); + "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.bootstrap.enabled=true"); assertThat(getContext().containsBean("configMapPropertySourceLocator")).isTrue(); assertThat(getContext().containsBean("secretsPropertySourceLocator")).isTrue(); } + @Test + public void kubernetesConfigDataWhenKubernetesDefaultEnabled() { + setup(KubernetesClientTestConfiguration.class, "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.kubernetes.client.namespace=default", "spring.config.import=kubernetes:"); + assertThat(getContext().containsBean("configDataConfigMapPropertySourceLocator")).isTrue(); + assertThat(getContext().containsBean("configDataSecretsPropertySourceLocator")).isTrue(); + } + @Test public void kubernetesWhenKubernetesDisabled() { setup(KubernetesClientTestConfiguration.class); @@ -64,31 +72,59 @@ public class KubernetesConfigConfigurationTest extends KubernetesConfigTestBase } @Test - public void kubernetesWhenKubernetesConfigEnabledButSecretDisabled() { + public void kubernetesBootstrapWhenKubernetesConfigEnabledButSecretDisabled() { setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=true", "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.client.namespace=default", - "spring.main.cloud-platform=KUBERNETES"); + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true"); assertThat(getContext().containsBean("configMapPropertySourceLocator")).isTrue(); assertThat(getContext().containsBean("secretsPropertySourceLocator")).isFalse(); } @Test - public void kubernetesWhenKubernetesConfigDisabledButSecretEnabled() { + public void kubernetesConfigDataWhenKubernetesConfigEnabledButSecretDisabled() { + setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=true", + "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.client.namespace=default", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:"); + assertThat(getContext().containsBean("configDataConfigMapPropertySourceLocator")).isTrue(); + assertThat(getContext().containsBean("configDataSecretsPropertySourceLocator")).isFalse(); + } + + @Test + public void kubernetesBootstrapWhenKubernetesConfigDisabledButSecretEnabled() { setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=false", - "spring.cloud.kubernetes.secrets.enabled=true", "spring.main.cloud-platform=KUBERNETES"); + "spring.cloud.kubernetes.secrets.enabled=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true"); assertThat(getContext().containsBean("configMapPropertySourceLocator")).isFalse(); assertThat(getContext().containsBean("secretsPropertySourceLocator")).isTrue(); } @Test - public void kubernetesConfigWhenKubernetesEnabledAndKubernetesConfigEnabled() { + public void kubernetesConfigDataWhenKubernetesConfigDisabledButSecretEnabled() { + setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=false", + "spring.cloud.kubernetes.secrets.enabled=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:"); + assertThat(getContext().containsBean("configDataConfigMapPropertySourceLocator")).isFalse(); + assertThat(getContext().containsBean("configDataSecretsPropertySourceLocator")).isTrue(); + } + + @Test + public void kubernetesBootstrapConfigWhenKubernetesEnabledAndKubernetesConfigEnabled() { setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=true", "spring.cloud.kubernetes.secrets.enabled=true", "spring.cloud.kubernetes.client.namespace=default", - "spring.main.cloud-platform=KUBERNETES"); + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true"); assertThat(getContext().containsBean("configMapPropertySourceLocator")).isTrue(); assertThat(getContext().containsBean("secretsPropertySourceLocator")).isTrue(); } + @Test + public void kubernetesConfigDataConfigWhenKubernetesEnabledAndKubernetesConfigEnabled() { + setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=true", + "spring.cloud.kubernetes.secrets.enabled=true", "spring.cloud.kubernetes.client.namespace=default", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:"); + assertThat(getContext().containsBean("configDataConfigMapPropertySourceLocator")).isTrue(); + assertThat(getContext().containsBean("configDataSecretsPropertySourceLocator")).isTrue(); + } + @Test public void kubernetesConfigWhenKubernetesEnabledAndKubernetesConfigDisabled() { setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.config.enabled=false"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigTestBase.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigTestBase.java index 9d3075bb..fdfea069 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigTestBase.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/KubernetesConfigTestBase.java @@ -16,6 +16,9 @@ package org.springframework.cloud.kubernetes.fabric8.config; +import java.util.Arrays; +import java.util.stream.Stream; + import org.junit.jupiter.api.AfterEach; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; @@ -32,14 +35,18 @@ public class KubernetesConfigTestBase { private ConfigurableApplicationContext context; + protected String[] commonProperties = new String[0]; + protected ConfigurableApplicationContext getContext() { return context; } protected void setup(Class mockClientConfiguration, String... env) { + String[] properties = Stream.concat(Arrays.stream(commonProperties), Arrays.stream(env)) + .toArray(size -> new String[size]); context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class, mockClientConfiguration, BootstrapConfiguration.class, ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class) - .web(org.springframework.boot.WebApplicationType.NONE).properties(env).run(); + .web(org.springframework.boot.WebApplicationType.NONE).properties(properties).run(); } @AfterEach diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleConfigMapsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleConfigMapsTests.java index 6b54f403..535cab7e 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleConfigMapsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleConfigMapsTests.java @@ -23,7 +23,6 @@ import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -42,15 +41,12 @@ import org.springframework.test.web.reactive.server.WebTestClient; properties = { "spring.cloud.bootstrap.name=multiplecms", "spring.main.cloud-platform=KUBERNETES" }) @AutoConfigureWebTestClient @EnableKubernetesMockClient(crud = true, https = false) -public class MultipleConfigMapsTests { - - private static KubernetesClient mockClient; +abstract class MultipleConfigMapsTests { @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleSecretsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleSecretsTests.java index 7e254bcc..26b25923 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleSecretsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/MultipleSecretsTests.java @@ -24,27 +24,15 @@ import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.example3.MultiSecretsApp; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; /** * @author Haytham Mohamed */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MultiSecretsApp.class, - properties = { "spring.cloud.bootstrap.name=multiple-secrets", "spring.main.cloud-platform=KUBERNETES" }) -@AutoConfigureWebTestClient -@EnableKubernetesMockClient(crud = true, https = false) -public class MultipleSecretsTests { +abstract class MultipleSecretsTests { private static final String DEFAULT_NAMESPACE = "ns1"; @@ -54,14 +42,10 @@ public class MultipleSecretsTests { private static final String SECRET_VALUE_2 = "secretValue-2"; - // will be injected by KubernetesMockServerExtension - private static KubernetesClient mockClient; - @Autowired private WebTestClient webClient; - @BeforeAll - public static void setUpBeforeClass() { + public static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BoostrapKubernetesEnabledConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BoostrapKubernetesEnabledConfigDisabled.java new file mode 100644 index 00000000..2b82fcf1 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BoostrapKubernetesEnabledConfigDisabled.java @@ -0,0 +1,30 @@ +/* + * Copyright 2013-2021 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +class BoostrapKubernetesEnabledConfigDisabled extends KubernetesEnabledConfigDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesDisabled.java new file mode 100644 index 00000000..21a4d384 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesDisabled.java @@ -0,0 +1,29 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.bootstrap.enabled=true" }) +class BootstrapKubernetesDisabled extends KubernetesDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabled.java new file mode 100644 index 00000000..42fe2aa9 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabled.java @@ -0,0 +1,30 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +class BootstrapKubernetesEnabled extends KubernetesEnabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledOnPurpose.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledOnPurpose.java new file mode 100644 index 00000000..9091f578 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledOnPurpose.java @@ -0,0 +1,31 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=true", + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +class BootstrapKubernetesEnabledOnPurpose extends KubernetesEnabledOnPurpose { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsAndConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsAndConfigDisabled.java new file mode 100644 index 00000000..dcb58982 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsAndConfigDisabled.java @@ -0,0 +1,27 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.config.enabled=false", + "spring.cloud.bootstrap.enabled=true" }) +class BootstrapKubernetesEnabledSecretsAndConfigDisabled extends KubernetesEnabledSecretsAndConfigDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsDisabled.java new file mode 100644 index 00000000..c8f369f0 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/BootstrapKubernetesEnabledSecretsDisabled.java @@ -0,0 +1,31 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=false", + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +class BootstrapKubernetesEnabledSecretsDisabled extends KubernetesEnabledSecretsDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesDisabled.java new file mode 100644 index 00000000..e71c9462 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesDisabled.java @@ -0,0 +1,29 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.main.cloud-platform=KUBERNETES" }) +class ConfigDataKubernetesDisabled extends KubernetesDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabled.java new file mode 100644 index 00000000..b385dc0b --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabled.java @@ -0,0 +1,30 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +class ConfigDataKubernetesEnabled extends KubernetesEnabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledConfigDisabled.java new file mode 100644 index 00000000..01a23b94 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledConfigDisabled.java @@ -0,0 +1,30 @@ +/* + * Copyright 2013-2021 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +class ConfigDataKubernetesEnabledConfigDisabled extends KubernetesEnabledConfigDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledOnPurpose.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledOnPurpose.java new file mode 100644 index 00000000..98c019d3 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledOnPurpose.java @@ -0,0 +1,31 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=true", + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +class ConfigDataKubernetesEnabledOnPurpose extends KubernetesEnabledOnPurpose { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsAndConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsAndConfigDisabled.java new file mode 100644 index 00000000..25575eca --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsAndConfigDisabled.java @@ -0,0 +1,27 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.config.enabled=false", + "spring.config.import=kubernetes:", "spring.main.cloud-platform=KUBERNETES" }) +class ConfigDataKubernetesEnabledSecretsAndConfigDisabled extends KubernetesEnabledSecretsAndConfigDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsDisabled.java new file mode 100644 index 00000000..e375762c --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/ConfigDataKubernetesEnabledSecretsDisabled.java @@ -0,0 +1,31 @@ +/* + * 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.bootstrap; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = { "spring.cloud.kubernetes.secrets.enabled=false", + "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }) +class ConfigDataKubernetesEnabledSecretsDisabled extends KubernetesEnabledSecretsDisabled { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/Fabric8BootstrapConfigurationInsideK8s.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/Fabric8BootstrapConfigurationInsideK8s.java index e0014630..2f8265cd 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/Fabric8BootstrapConfigurationInsideK8s.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/Fabric8BootstrapConfigurationInsideK8s.java @@ -31,7 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; * @author wind57 */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=abc" }) + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=abc", + "spring.cloud.bootstrap.enabled=true" }) class Fabric8BootstrapConfigurationInsideK8s { @Autowired diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesDisabled.java index 08b21f19..eb757e4b 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesDisabled.java @@ -19,8 +19,6 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; @@ -30,9 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = "spring.cloud.kubernetes.enabled=false") -class KubernetesDisabled { +abstract class KubernetesDisabled { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabled.java index c8ba70e0..8fd7f739 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabled.java @@ -19,8 +19,6 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; @@ -30,9 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) -class KubernetesEnabled { +abstract class KubernetesEnabled { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledConfigDisabled.java index d438ed1e..a9a2e697 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledConfigDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledConfigDisabled.java @@ -19,8 +19,6 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; @@ -30,9 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.main.cloud-platform=KUBERNETES" }) -class KubernetesEnabledConfigDisabled { +abstract class KubernetesEnabledConfigDisabled { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledOnPurpose.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledOnPurpose.java index 70b75f93..bdc2776a 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledOnPurpose.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledOnPurpose.java @@ -19,8 +19,6 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; @@ -30,10 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.secrets.enabled=true", - "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) -class KubernetesEnabledOnPurpose { +abstract class KubernetesEnabledOnPurpose { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsAndConfigDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsAndConfigDisabled.java index 7a50d8e3..2c9519e6 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsAndConfigDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsAndConfigDisabled.java @@ -19,17 +19,13 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties = { - "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.config.enabled=false" }) -class KubernetesEnabledSecretsAndConfigDisabled { +abstract class KubernetesEnabledSecretsAndConfigDisabled { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsDisabled.java index ae4763d5..9fa115a9 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/bootstrap/KubernetesEnabledSecretsDisabled.java @@ -19,8 +19,6 @@ package org.springframework.cloud.kubernetes.fabric8.config.bootstrap; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; @@ -30,10 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, - properties = { "spring.cloud.kubernetes.secrets.enabled=false", - "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }) -class KubernetesEnabledSecretsDisabled { +abstract class KubernetesEnabledSecretsDisabled { @Autowired private ConfigurableApplicationContext context; diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigFailFastDisabled.java new file mode 100644 index 00000000..d9f4071a --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigFailFastDisabled.java @@ -0,0 +1,46 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BoostrapConfigFailFastDisabled extends ConfigFailFastDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryDisabledButSecretsRetryEnabled.java new file mode 100644 index 00000000..cb482ea6 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryDisabledButSecretsRetryEnabled.java @@ -0,0 +1,56 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.context.ApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BoostrapConfigRetryDisabledButSecretsRetryEnabled extends ConfigRetryDisabledButSecretsRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @Override + protected void assertRetryBean(ApplicationContext context) { + assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isTrue(); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryEnabled.java new file mode 100644 index 00000000..a29eef0f --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BoostrapConfigRetryEnabled.java @@ -0,0 +1,59 @@ +/* + * 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.locator_retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BoostrapConfigRetryEnabled extends ConfigRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @SpyBean + Fabric8ConfigMapPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = propertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..4ac5c75c --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/BootstrapConfigFailFastEnabledButRetryDisabled.java @@ -0,0 +1,54 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * we call Fabric8ConfigMapPropertySourceLocator::locate directly, thus no need for + * bootstrap phase to kick in. As such two flags that might look a bit un-expected: + * "spring.cloud.kubernetes.config.enabled=false" + * "spring.cloud.kubernetes.secrets.enabled=false" + * + * @author Isik Erhan + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.enabled=false", + "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BootstrapConfigFailFastEnabledButRetryDisabled extends ConfigFailFastEnabledButRetryDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastDisabled.java new file mode 100644 index 00000000..becc6a23 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastDisabled.java @@ -0,0 +1,46 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataConfigFailFastDisabled extends ConfigFailFastDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..faffb9cb --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigFailFastEnabledButRetryDisabled.java @@ -0,0 +1,59 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +/** + * we call Fabric8ConfigMapPropertySourceLocator::locate directly, thus no need for + * bootstrap phase to kick in. As such two flags that might look a bit un-expected: + * "spring.cloud.kubernetes.config.enabled=false" + * "spring.cloud.kubernetes.secrets.enabled=false" + * + * @author Isik Erhan + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.enabled=false", + "spring.cloud.kubernetes.secrets.enabled=false", "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataConfigFailFastEnabledButRetryDisabled extends ConfigFailFastEnabledButRetryDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @MockBean + KubernetesNamespaceProvider kubernetesNamespaceProvider; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java new file mode 100644 index 00000000..0832b42f --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryDisabledButSecretsRetryEnabled.java @@ -0,0 +1,57 @@ +/* + * 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.locator_retry; + +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.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.context.ApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataConfigRetryDisabledButSecretsRetryEnabled extends ConfigRetryDisabledButSecretsRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @Override + protected void assertRetryBean(ApplicationContext context) { + assertThat(context.getBean(ConfigDataRetryableSecretsPropertySourceLocator.class)).isNotNull(); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryEnabled.java new file mode 100644 index 00000000..6390e213 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigDataConfigRetryEnabled.java @@ -0,0 +1,67 @@ +/* + * 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.locator_retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +import static org.mockito.Mockito.spy; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataConfigRetryEnabled extends ConfigRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @MockBean + private KubernetesNamespaceProvider namespaceProvider; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @Autowired + ConfigDataRetryableConfigMapPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = spy(propertySourceLocator.getConfigMapPropertySourceLocator()); + propertySourceLocator.setConfigMapPropertySourceLocator(verifiablePsl); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastDisabled.java index 37b57207..2e1c754b 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastDisabled.java @@ -18,30 +18,23 @@ package org.springframework.cloud.kubernetes.fabric8.config.locator_retry; import io.fabric8.kubernetes.client.Config; 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.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.mock.env.MockEnvironment; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class ConfigFailFastDisabled { +abstract class ConfigFailFastDisabled { private static final String API = "/api/v1/namespaces/default/configmaps/application"; @@ -49,8 +42,12 @@ class ConfigFailFastDisabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + @Autowired + private Fabric8ConfigMapPropertySourceLocator propertySourceLocator; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + ConfigFailFastDisabled.mockClient = mockClient; + ConfigFailFastDisabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -59,17 +56,15 @@ class ConfigFailFastDisabled { System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); } - @SpyBean - private Fabric8ConfigMapPropertySourceLocator propertySourceLocator; - @Test void locateShouldNotRetry() { + Fabric8ConfigMapPropertySourceLocator psl = spy(propertySourceLocator); mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); - Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify that propertySourceLocator.locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(psl, times(1)).locate(any()); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastEnabledButRetryDisabled.java index 7cf2b960..26a2a98c 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastEnabledButRetryDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigFailFastEnabledButRetryDisabled.java @@ -18,15 +18,11 @@ package org.springframework.cloud.kubernetes.fabric8.config.locator_retry; import io.fabric8.kubernetes.client.Config; 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.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.context.ApplicationContext; import org.springframework.mock.env.MockEnvironment; @@ -46,14 +42,7 @@ import static org.mockito.Mockito.verify; * @author Isik Erhan * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", - "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.enabled=false", - "spring.cloud.kubernetes.secrets.enabled=false" }, - classes = Application.class) -@EnableKubernetesMockClient -class ConfigFailFastEnabledButRetryDisabled { +abstract class ConfigFailFastEnabledButRetryDisabled { private static final String API = "/api/v1/namespaces/default/configmaps/application"; @@ -61,8 +50,9 @@ class ConfigFailFastEnabledButRetryDisabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + ConfigFailFastEnabledButRetryDisabled.mockClient = mockClient; + ConfigFailFastEnabledButRetryDisabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryDisabledButSecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryDisabledButSecretsRetryEnabled.java index a19a6e37..30f1d57c 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryDisabledButSecretsRetryEnabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryDisabledButSecretsRetryEnabled.java @@ -18,35 +18,24 @@ package org.springframework.cloud.kubernetes.fabric8.config.locator_retry; import io.fabric8.kubernetes.client.Config; 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.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; import org.springframework.context.ApplicationContext; import org.springframework.mock.env.MockEnvironment; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.enabled=false", - "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class ConfigRetryDisabledButSecretsRetryEnabled { +abstract class ConfigRetryDisabledButSecretsRetryEnabled { private static final String API = "/api/v1/namespaces/default/configmaps/application"; @@ -54,8 +43,12 @@ class ConfigRetryDisabledButSecretsRetryEnabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + @Autowired + private Fabric8ConfigMapPropertySourceLocator propertySourceLocator; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + ConfigRetryDisabledButSecretsRetryEnabled.mockClient = mockClient; + ConfigRetryDisabledButSecretsRetryEnabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -64,15 +57,12 @@ class ConfigRetryDisabledButSecretsRetryEnabled { System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); } - @SpyBean - private Fabric8ConfigMapPropertySourceLocator propertySourceLocator; - @Autowired private ApplicationContext context; @Test void locateShouldFailWithoutRetrying() { - + Fabric8ConfigMapPropertySourceLocator psl = spy(propertySourceLocator); /* * Enabling secrets retry causes Spring Retry to be enabled and a * RetryOperationsInterceptor bean with NeverRetryPolicy for config maps to be @@ -82,13 +72,14 @@ class ConfigRetryDisabledButSecretsRetryEnabled { mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); - assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isTrue(); - assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) - .isInstanceOf(IllegalStateException.class) + assertRetryBean(context); + assertThatThrownBy(() -> psl.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read ConfigMap with name 'application' in namespace 'default'"); // verify that propertySourceLocator.locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(psl, times(1)).locate(any()); } + protected abstract void assertRetryBean(ApplicationContext context); + } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryEnabled.java index caea6275..b347f75f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryEnabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/ConfigRetryEnabled.java @@ -22,16 +22,11 @@ import java.util.Map; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; 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.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; -import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; @@ -44,13 +39,7 @@ import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5", - "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class ConfigRetryEnabled { +abstract class ConfigRetryEnabled { private static final String API = "/api/v1/namespaces/default/configmaps/application"; @@ -58,8 +47,13 @@ class ConfigRetryEnabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + protected ConfigMapPropertySourceLocator psl; + + protected ConfigMapPropertySourceLocator verifiablePsl; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + ConfigRetryEnabled.mockClient = mockClient; + ConfigRetryEnabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -68,9 +62,6 @@ class ConfigRetryEnabled { System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); } - @SpyBean - private Fabric8ConfigMapPropertySourceLocator propertySourceLocator; - @Test void locateShouldNotRetryWhenThereIsNoFailure() { Map data = new HashMap<>(); @@ -82,11 +73,10 @@ class ConfigRetryEnabled { new ConfigMapBuilder().withNewMetadata().withName("application").endMetadata().addToData(data).build()) .once(); - PropertySource propertySource = Assertions - .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + PropertySource propertySource = Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(verifiablePsl, times(1)).locate(any()); // validate the contents of the property source assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue"); @@ -105,11 +95,10 @@ class ConfigRetryEnabled { new ConfigMapBuilder().withNewMetadata().withName("application").endMetadata().addToData(data).build()) .once(); - PropertySource propertySource = Assertions - .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + PropertySource propertySource = Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify retried 4 times - verify(propertySourceLocator, times(4)).locate(any()); + verify(verifiablePsl, times(4)).locate(any()); // validate the contents of the property source assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue"); @@ -121,12 +110,11 @@ class ConfigRetryEnabled { // fail all the 5 requests mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").times(5); - assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) - .isInstanceOf(IllegalStateException.class) + assertThatThrownBy(() -> psl.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read ConfigMap with name 'application' in namespace 'default'"); // verify retried 5 times until failure - verify(propertySourceLocator, times(5)).locate(any()); + verify(verifiablePsl, times(5)).locate(any()); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload/ConfigReloadAutoConfigurationTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload/ConfigReloadAutoConfigurationTest.java index 5b090d6a..e350a274 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload/ConfigReloadAutoConfigurationTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload/ConfigReloadAutoConfigurationTest.java @@ -24,6 +24,7 @@ import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -74,6 +75,11 @@ public class ConfigReloadAutoConfigurationTest extends KubernetesConfigTestBase mockClient.configMaps().inNamespace("spring").create(configMap2); } + @BeforeEach + public void beforeEach() { + commonProperties = new String[] { "spring.cloud.bootstrap.enabled=true" }; + } + @Test public void kubernetesConfigReloadDisabled() { setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.reload.enabled=false"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastDisabled.java new file mode 100644 index 00000000..f42dc5b3 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastDisabled.java @@ -0,0 +1,59 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BootstrapSecretsFailFastDisabled extends SecretsFailFastDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @SpyBean + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = propertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..387eef35 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsFailFastEnabledButRetryDisabled.java @@ -0,0 +1,60 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BootstrapSecretsFailFastEnabledButRetryDisabled extends SecretsFailFastEnabledButRetryDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @SpyBean + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = propertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryDisabledButConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryDisabledButConfigRetryEnabled.java new file mode 100644 index 00000000..8c11049d --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryDisabledButConfigRetryEnabled.java @@ -0,0 +1,69 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.context.ApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.secrets.name=my-secret", + "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BootstrapSecretsRetryDisabledButConfigRetryEnabled extends SecretsRetryDisabledButConfigRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @SpyBean + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = propertySourceLocator; + } + + @Override + protected void assertRetryBean(ApplicationContext context) { + assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isTrue(); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryEnabled.java new file mode 100644 index 00000000..89cabd90 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/BootstrapSecretsRetryEnabled.java @@ -0,0 +1,61 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", + "spring.cloud.kubernetes.secrets.retry.max-attempts=5", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" }, + classes = Application.class) +@EnableKubernetesMockClient +class BootstrapSecretsRetryEnabled extends SecretsRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @SpyBean + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = propertySourceLocator; + verifiablePsl = propertySourceLocator; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastDisabled.java new file mode 100644 index 00000000..64a25d7d --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastDisabled.java @@ -0,0 +1,61 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; + +import static org.mockito.Mockito.spy; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataSecretsFailFastDisabled extends SecretsFailFastDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @Autowired + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = spy(propertySourceLocator); + verifiablePsl = psl; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastEnabledButRetryDisabled.java new file mode 100644 index 00000000..9b4140a1 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsFailFastEnabledButRetryDisabled.java @@ -0,0 +1,62 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; + +import static org.mockito.Mockito.spy; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataSecretsFailFastEnabledButRetryDisabled extends SecretsFailFastEnabledButRetryDisabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @Autowired + private Fabric8SecretsPropertySourceLocator propertySourceLocator; + + @BeforeEach + public void beforeEach() { + psl = spy(propertySourceLocator); + verifiablePsl = psl; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryDisabledButConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryDisabledButConfigRetryEnabled.java new file mode 100644 index 00000000..829f355a --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryDisabledButConfigRetryEnabled.java @@ -0,0 +1,72 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.Application; +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.context.ApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", + "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.secrets.name=my-secret", + "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataSecretsRetryDisabledButConfigRetryEnabled extends SecretsRetryDisabledButConfigRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @Autowired + private Fabric8SecretsPropertySourceLocator secretsPropertySourceLocator; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @BeforeEach + public void beforeEach() { + psl = spy(secretsPropertySourceLocator); + verifiablePsl = psl; + } + + @Override + protected void assertRetryBean(ApplicationContext context) { + assertThat(context.containsBean("configDataSecretsPropertySourceLocator")).isTrue(); + assertThat(context.getBean("configDataSecretsPropertySourceLocator")) + .isInstanceOf(Fabric8SecretsPropertySourceLocator.class); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryEnabled.java new file mode 100644 index 00000000..d15eff9f --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/ConfigDataSecretsRetryEnabled.java @@ -0,0 +1,64 @@ +/* + * 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.retry; + +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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableSecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.fabric8.config.Application; + +import static org.mockito.Mockito.spy; + +/** + * @author Isik Erhan + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { "spring.cloud.kubernetes.client.namespace=default", + "spring.cloud.kubernetes.secrets.fail-fast=true", + "spring.cloud.kubernetes.secrets.retry.max-attempts=5", + "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", + "spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" }, + classes = Application.class) +@EnableKubernetesMockClient +class ConfigDataSecretsRetryEnabled extends SecretsRetryEnabled { + + private static KubernetesMockServer mockServer; + + private static KubernetesClient mockClient; + + @Autowired + private ConfigDataRetryableSecretsPropertySourceLocator configDataRetryableSecretsPropertySourceLocator; + + @BeforeAll + static void setup() { + setup(mockClient, mockServer); + } + + @BeforeEach + public void beforeEach() { + psl = configDataRetryableSecretsPropertySourceLocator; + verifiablePsl = spy(configDataRetryableSecretsPropertySourceLocator.getSecretsPropertySourceLocator()); + configDataRetryableSecretsPropertySourceLocator.setSecretsPropertySourceLocator(verifiablePsl); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastDisabled.java index 162722f1..1ce88b41 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastDisabled.java @@ -19,16 +19,11 @@ package org.springframework.cloud.kubernetes.fabric8.config.retry; import io.fabric8.kubernetes.api.model.SecretListBuilder; import io.fabric8.kubernetes.client.Config; 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.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; -import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.mock.env.MockEnvironment; import static org.mockito.ArgumentMatchers.any; @@ -38,13 +33,7 @@ import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", - "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class SecretsFailFastDisabled { +abstract class SecretsFailFastDisabled { private static final String API = "/api/v1/namespaces/default/secrets/my-secret"; @@ -54,8 +43,13 @@ class SecretsFailFastDisabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + protected SecretsPropertySourceLocator psl; + + protected SecretsPropertySourceLocator verifiablePsl; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + SecretsFailFastDisabled.mockClient = mockClient; + SecretsFailFastDisabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -67,17 +61,14 @@ class SecretsFailFastDisabled { mockServer.expect().withPath(LIST_API).andReturn(200, new SecretListBuilder().build()).always(); } - @SpyBean - private Fabric8SecretsPropertySourceLocator propertySourceLocator; - @Test void locateShouldNotRetry() { mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); - Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(verifiablePsl, times(1)).locate(any()); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastEnabledButRetryDisabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastEnabledButRetryDisabled.java index 2d00828d..3a3c63b1 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastEnabledButRetryDisabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsFailFastEnabledButRetryDisabled.java @@ -19,16 +19,11 @@ package org.springframework.cloud.kubernetes.fabric8.config.retry; import io.fabric8.kubernetes.api.model.SecretListBuilder; import io.fabric8.kubernetes.client.Config; 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.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; -import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.context.ApplicationContext; import org.springframework.mock.env.MockEnvironment; @@ -41,14 +36,7 @@ import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", - "spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true", - "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class SecretsFailFastEnabledButRetryDisabled { +abstract class SecretsFailFastEnabledButRetryDisabled { private static final String API = "/api/v1/namespaces/default/secrets/my-secret"; @@ -58,8 +46,13 @@ class SecretsFailFastEnabledButRetryDisabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + protected SecretsPropertySourceLocator psl; + + protected SecretsPropertySourceLocator verifiablePsl; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + SecretsFailFastEnabledButRetryDisabled.mockClient = mockClient; + SecretsFailFastEnabledButRetryDisabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -71,9 +64,6 @@ class SecretsFailFastEnabledButRetryDisabled { mockServer.expect().withPath(LIST_API).andReturn(200, new SecretListBuilder().build()).always(); } - @SpyBean - private Fabric8SecretsPropertySourceLocator propertySourceLocator; - @Autowired private ApplicationContext context; @@ -83,12 +73,11 @@ class SecretsFailFastEnabledButRetryDisabled { mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isFalse(); - assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) - .isInstanceOf(IllegalStateException.class) + assertThatThrownBy(() -> psl.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read Secret with name 'my-secret' in namespace 'default'"); // verify that propertySourceLocator.locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(verifiablePsl, times(1)).locate(any()); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryDisabledButConfigRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryDisabledButConfigRetryEnabled.java index 3b084716..70ccd2d0 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryDisabledButConfigRetryEnabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryDisabledButConfigRetryEnabled.java @@ -19,20 +19,14 @@ package org.springframework.cloud.kubernetes.fabric8.config.retry; import io.fabric8.kubernetes.api.model.SecretListBuilder; import io.fabric8.kubernetes.client.Config; 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.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; -import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.context.ApplicationContext; import org.springframework.mock.env.MockEnvironment; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; @@ -41,14 +35,7 @@ import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = { "spring.cloud.kubernetes.client.namespace=default", - "spring.cloud.kubernetes.secrets.fail-fast=true", "spring.cloud.kubernetes.secrets.retry.enabled=false", - "spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.secrets.name=my-secret", - "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class SecretsRetryDisabledButConfigRetryEnabled { +abstract class SecretsRetryDisabledButConfigRetryEnabled { private static final String API = "/api/v1/namespaces/default/secrets/my-secret"; @@ -58,8 +45,13 @@ class SecretsRetryDisabledButConfigRetryEnabled { private static KubernetesClient mockClient; - @BeforeAll - static void setup() { + protected SecretsPropertySourceLocator psl; + + protected SecretsPropertySourceLocator verifiablePsl; + + protected static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + SecretsRetryDisabledButConfigRetryEnabled.mockClient = mockClient; + SecretsRetryDisabledButConfigRetryEnabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -71,15 +63,11 @@ class SecretsRetryDisabledButConfigRetryEnabled { mockServer.expect().withPath(LIST_API).andReturn(200, new SecretListBuilder().build()).always(); } - @SpyBean - private Fabric8SecretsPropertySourceLocator propertySourceLocator; - @Autowired private ApplicationContext context; @Test void locateShouldFailWithoutRetrying() { - /* * Enabling config retry causes Spring Retry to be enabled and a * RetryOperationsInterceptor bean with NeverRetryPolicy for secrets to be @@ -89,13 +77,14 @@ class SecretsRetryDisabledButConfigRetryEnabled { mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once(); - assertThat(context.containsBean("kubernetesSecretsRetryInterceptor")).isTrue(); - assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) - .isInstanceOf(IllegalStateException.class) + assertRetryBean(context); + assertThatThrownBy(() -> psl.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read Secret with name 'my-secret' in namespace 'default'"); // verify that propertySourceLocator.locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(verifiablePsl, times(1)).locate(any()); } + protected abstract void assertRetryBean(ApplicationContext context); + } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryEnabled.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryEnabled.java index 75ee58de..8e1cd9e9 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryEnabled.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/retry/SecretsRetryEnabled.java @@ -24,16 +24,11 @@ import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.api.model.SecretListBuilder; import io.fabric8.kubernetes.client.Config; 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.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.cloud.kubernetes.fabric8.config.Application; -import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySourceLocator; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.core.env.PropertySource; import org.springframework.mock.env.MockEnvironment; @@ -46,13 +41,7 @@ import static org.mockito.Mockito.verify; /** * @author Isik Erhan */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { - "spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true", - "spring.cloud.kubernetes.secrets.retry.max-attempts=5", "spring.cloud.kubernetes.secrets.name=my-secret", - "spring.cloud.kubernetes.secrets.enable-api=true", "spring.main.cloud-platform=KUBERNETES" }, - classes = Application.class) -@EnableKubernetesMockClient -class SecretsRetryEnabled { +abstract class SecretsRetryEnabled { private static final String API = "/api/v1/namespaces/default/secrets/my-secret"; @@ -60,10 +49,12 @@ class SecretsRetryEnabled { private static KubernetesMockServer mockServer; - private static KubernetesClient mockClient; + protected SecretsPropertySourceLocator psl; - @BeforeAll - static void setup() { + protected SecretsPropertySourceLocator verifiablePsl; + + static void setup(KubernetesClient mockClient, KubernetesMockServer mockServer) { + SecretsRetryEnabled.mockServer = mockServer; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); @@ -75,9 +66,6 @@ class SecretsRetryEnabled { mockServer.expect().withPath(LIST_API).andReturn(200, new SecretListBuilder().build()).always(); } - @SpyBean - private Fabric8SecretsPropertySourceLocator propertySourceLocator; - @Test void locateShouldNotRetryWhenThereIsNoFailure() { Map data = new HashMap<>(); @@ -89,11 +77,10 @@ class SecretsRetryEnabled { new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData(data).build()) .once(); - PropertySource propertySource = Assertions - .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + PropertySource propertySource = Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify locate is called only once - verify(propertySourceLocator, times(1)).locate(any()); + verify(verifiablePsl, times(1)).locate(any()); // validate the contents of the property source assertThat(propertySource.getProperty("some.sensitive.prop")).isEqualTo("theSensitiveValue"); @@ -112,11 +99,10 @@ class SecretsRetryEnabled { new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData(data).build()) .once(); - PropertySource propertySource = Assertions - .assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment())); + PropertySource propertySource = Assertions.assertDoesNotThrow(() -> psl.locate(new MockEnvironment())); // verify retried 4 times - verify(propertySourceLocator, times(4)).locate(any()); + verify(verifiablePsl, times(4)).locate(any()); // validate the contents of the property source assertThat(propertySource.getProperty("some.sensitive.prop")).isEqualTo("theSensitiveValue"); @@ -128,12 +114,12 @@ class SecretsRetryEnabled { // fail all the 5 requests mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").times(5); - assertThatThrownBy(() -> propertySourceLocator.locate(new MockEnvironment())) - .isInstanceOf(IllegalStateException.class) + assertThatThrownBy(() -> psl.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class) .hasMessage("Unable to read Secret with name 'my-secret' in namespace 'default'"); // verify retried 5 times until failure - verify(propertySourceLocator, times(5)).locate(any()); + verify(verifiablePsl, times(5)).locate(any()); + } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/BootstrapSecretsWithLabelsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/BootstrapSecretsWithLabelsTests.java new file mode 100644 index 00000000..c56fd7db --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/BootstrapSecretsWithLabelsTests.java @@ -0,0 +1,41 @@ +/* + * 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.secrets_with_labels; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SecretsWithLabelsApp.class, + properties = { "spring.cloud.bootstrap.name=secret-with-labels-config", "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.bootstrap.enabled=true" }) +@EnableKubernetesMockClient(crud = true, https = false) +class BootstrapSecretsWithLabelsTests extends SecretsWithLabelsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/ConfigDataSecretsWithLabelsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/ConfigDataSecretsWithLabelsTests.java new file mode 100644 index 00000000..ccadf90f --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/ConfigDataSecretsWithLabelsTests.java @@ -0,0 +1,41 @@ +/* + * 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.secrets_with_labels; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.BeforeAll; + +import org.springframework.boot.test.context.SpringBootTest; + +/** + * @author wind57 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SecretsWithLabelsApp.class, + properties = { "spring.application.name=secret-with-labels-config", "spring.main.cloud-platform=KUBERNETES", + "spring.config.import=kubernetes:,classpath:./secret-with-labels-config.yaml" }) +@EnableKubernetesMockClient(crud = true, https = false) +class ConfigDataSecretsWithLabelsTests extends SecretsWithLabelsTests { + + private static KubernetesClient mockClient; + + @BeforeAll + static void setUpBeforeClass() { + setUpBeforeClass(mockClient); + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/SecretsWithLabelsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/SecretsWithLabelsTests.java index 61adb1b4..838355a5 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/SecretsWithLabelsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/secrets_with_labels/SecretsWithLabelsTests.java @@ -24,32 +24,24 @@ import java.util.Map; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.reactive.server.WebTestClient; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SecretsWithLabelsApp.class, - properties = { "spring.cloud.bootstrap.name=secret-with-labels-config", - "spring.main.cloud-platform=KUBERNETES" }) -@EnableKubernetesMockClient(crud = true, https = false) -class SecretsWithLabelsTests { +abstract class SecretsWithLabelsTests { private static KubernetesClient mockClient; @Autowired private WebTestClient webClient; - @BeforeAll - static void setUpBeforeClass() { - + static void setUpBeforeClass(KubernetesClient mockClient) { + SecretsWithLabelsTests.mockClient = mockClient; // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-active-profiles-name.yaml b/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-active-profiles-name.yaml index 98357fa6..7f99eb50 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-active-profiles-name.yaml +++ b/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-active-profiles-name.yaml @@ -1,4 +1,6 @@ spring: - profiles: development + config: + activate: + on-profile: development bean: greeting: "Hello ConfigMap Active Profile Name, %s!" diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-profiles.yaml b/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-profiles.yaml index 5e296b8a..a0ea2d28 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-profiles.yaml +++ b/spring-cloud-kubernetes-fabric8-config/src/test/resources/application-with-profiles.yaml @@ -3,16 +3,22 @@ bean: farewell: "Goodbye ConfigMap default, %s!" --- spring: - profiles: development + config: + activate: + on-profile: development bean: greeting: "Hello ConfigMap dev, %s!" --- spring: - profiles: production + config: + activate: + on-profile: production bean: greeting: "Hello ConfigMap prod, %s!" --- spring: - profiles: production & us-east + config: + activate: + on-profile: production & us-east bean: greeting: "Hello ConfigMap production and us-east, %s!" diff --git a/spring-cloud-kubernetes-fabric8-istio/src/test/java/org/springframework/cloud/kubernetes/fabric8/istio/IstioAutoConfigurationClientNotPresentWhenKubernetesDisabled.java b/spring-cloud-kubernetes-fabric8-istio/src/test/java/org/springframework/cloud/kubernetes/fabric8/istio/IstioAutoConfigurationClientNotPresentWhenKubernetesDisabled.java index 369923d4..5b15e9ae 100644 --- a/spring-cloud-kubernetes-fabric8-istio/src/test/java/org/springframework/cloud/kubernetes/fabric8/istio/IstioAutoConfigurationClientNotPresentWhenKubernetesDisabled.java +++ b/spring-cloud-kubernetes-fabric8-istio/src/test/java/org/springframework/cloud/kubernetes/fabric8/istio/IstioAutoConfigurationClientNotPresentWhenKubernetesDisabled.java @@ -28,8 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, - properties = { "spring.cloud.kubernetes.enabled=false" }) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class) class IstioAutoConfigurationClientNotPresentWhenKubernetesDisabled { @Autowired