This commit is contained in:
erabii
2022-02-05 12:28:46 +02:00
committed by GitHub
parent bd8656c5e5
commit 9a564289f2
3 changed files with 0 additions and 608 deletions

View File

@@ -1,95 +0,0 @@
/*
* 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.client;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.client.example.App;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* @author wind57
*/
public class KubernetesClientActuatorTests {
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "management.health.kubernetes.enabled=false",
"management.endpoint.health.show-details=always",
"management.endpoint.health.show-components=always",
"management.endpoints.web.exposure.include=health" })
public class DisabledHealthTest {
@Autowired
private ReactiveHealthContributorRegistry registry;
@Autowired
private WebTestClient webClient;
@Value("${local.server.port}")
private int port;
@Test
public void healthEndpointShouldContainKubernetes() {
this.webClient.get().uri("http://localhost:{port}/actuator/health", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectBody(String.class)
.value(Matchers.not(Matchers.containsString("kubernetes")));
Assertions.assertNull(registry.getContributor("kubernetes"),
"reactive kubernetes contributor must NOT be present when 'management.health.kubernetes.enabled=false'");
}
}
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "management.health.kubernetes.enabled=true",
"management.endpoint.health.show-details=always",
"management.endpoint.health.show-components=always",
"management.endpoints.web.exposure.include=health" })
public class EnabledHealthTest {
@Autowired
private WebTestClient webClient;
@Autowired
private ReactiveHealthContributorRegistry registry;
@Value("${local.server.port}")
private int port;
@Test
public void healthEndpointShouldContainKubernetes() {
this.webClient.get().uri("http://localhost:{port}/actuator/health", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectBody(String.class)
.value(Matchers.containsString("kubernetes"));
Assertions.assertNotNull(registry.getContributor("kubernetes"),
"reactive kubernetes contributor must be present when 'management.health.kubernetes.enabled=true'");
}
}
}

View File

@@ -1,283 +0,0 @@
/*
* 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.commons.config;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.kubernetes.commons.config.AbstractConfigProperties.RetryProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.interceptor.RetryOperationsInterceptor;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Isik Erhan
*/
public class KubernetesBootstrapConfigurationTests {
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class)
@Nested
public class FailFastDisabled {
@Autowired
ConfigurableApplicationContext context;
@Test
public void shouldNotDefineRetryBeans() {
assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty();
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.fail-fast=true" })
@Nested
public class ConfigFailFastEnabled {
@Autowired
ConfigurableApplicationContext context;
@Autowired
ConfigMapConfigProperties configMapConfigProperties;
@Test
public void shouldDefineRequiredBeans() {
Map<String, RetryOperationsInterceptor> retryInterceptors = context
.getBeansOfType(RetryOperationsInterceptor.class);
assertThat(retryInterceptors.containsKey("kubernetesConfigRetryInterceptor")).isTrue();
assertThat(retryInterceptors.containsKey("kubernetesSecretsRetryInterceptor")).isTrue();
}
@Test
public void retryConfigurationShouldBeDefault() {
RetryProperties retryProperties = configMapConfigProperties.getRetry();
RetryProperties defaultRetryProperties = new RetryProperties();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(retryProperties.getInitialInterval()).isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(retryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(retryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.secrets.fail-fast=true" })
@Nested
public class SecretsFailFastEnabled {
@Autowired
ConfigurableApplicationContext context;
@Autowired
SecretsConfigProperties secretsConfigProperties;
@Test
public void shouldDefineRequiredBeans() {
Map<String, RetryOperationsInterceptor> retryInterceptors = context
.getBeansOfType(RetryOperationsInterceptor.class);
assertThat(retryInterceptors.containsKey("kubernetesConfigRetryInterceptor")).isTrue();
assertThat(retryInterceptors.containsKey("kubernetesSecretsRetryInterceptor")).isTrue();
}
@Test
public void retryConfigurationShouldBeDefault() {
RetryProperties retryProperties = secretsConfigProperties.getRetry();
RetryProperties defaultRetryProperties = new RetryProperties();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(retryProperties.getInitialInterval()).isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(retryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(retryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.fail-fast=true",
"spring.cloud.kubernetes.secrets.fail-fast=true" })
@Nested
public class ConfigAndSecretsFailFastEnabledWithDefaultRetryConfiguration {
@Autowired
ConfigurableApplicationContext context;
@Autowired
ConfigMapConfigProperties configMapConfigProperties;
@Autowired
SecretsConfigProperties secretsConfigProperties;
@Test
public void shouldDefineRequiredBeans() {
Map<String, RetryOperationsInterceptor> retryInterceptors = context
.getBeansOfType(RetryOperationsInterceptor.class);
assertThat(retryInterceptors.containsKey("kubernetesConfigRetryInterceptor")).isTrue();
assertThat(retryInterceptors.containsKey("kubernetesSecretsRetryInterceptor")).isTrue();
}
@Test
public void retryConfigurationShouldBeDefault() {
RetryProperties defaultRetryProperties = new RetryProperties();
RetryProperties configMapRetryProperties = configMapConfigProperties.getRetry();
assertThat(configMapRetryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(configMapRetryProperties.getInitialInterval())
.isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(configMapRetryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(configMapRetryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
RetryProperties secretsRetryProperties = secretsConfigProperties.getRetry();
assertThat(secretsRetryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(secretsRetryProperties.getInitialInterval())
.isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(secretsRetryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(secretsRetryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.config.fail-fast=true",
"spring.cloud.kubernetes.config.retry.max-attempts=3",
"spring.cloud.kubernetes.config.retry.initial-interval=1500",
"spring.cloud.kubernetes.config.retry.max-interval=3000",
"spring.cloud.kubernetes.config.retry.multiplier=1.5" })
@Nested
public class ConfigFailFastEnabledWithCustomRetryConfiguration {
@Autowired
ConfigMapConfigProperties configMapConfigProperties;
@Test
public void retryConfigurationShouldBeCustomized() {
RetryProperties retryProperties = configMapConfigProperties.getRetry();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(3);
assertThat(retryProperties.getInitialInterval()).isEqualTo(1500L);
assertThat(retryProperties.getMaxInterval()).isEqualTo(3000L);
assertThat(retryProperties.getMultiplier()).isEqualTo(1.5D);
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.secrets.fail-fast=true",
"spring.cloud.kubernetes.secrets.retry.max-attempts=3",
"spring.cloud.kubernetes.secrets.retry.initial-interval=1500",
"spring.cloud.kubernetes.secrets.retry.max-interval=3000",
"spring.cloud.kubernetes.secrets.retry.multiplier=1.5" })
@Nested
public class SecretsFailFastEnabledWithCustomRetryConfiguration {
@Autowired
SecretsConfigProperties secretsConfigProperties;
@Test
public void retryConfigurationShouldBeCustomized() {
RetryProperties retryProperties = secretsConfigProperties.getRetry();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(3);
assertThat(retryProperties.getInitialInterval()).isEqualTo(1500L);
assertThat(retryProperties.getMaxInterval()).isEqualTo(3000L);
assertThat(retryProperties.getMultiplier()).isEqualTo(1.5D);
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.cloud.kubernetes.config.fail-fast=true",
"spring.cloud.kubernetes.config.retry.enabled=false" })
@Nested
public class ConfigFailFastEnabledButRetryDisabled {
@Autowired
ConfigurableApplicationContext context;
@Test
public void shouldNotDefineRetryBeans() {
assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty();
}
}
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = App.class,
properties = { "spring.cloud.kubernetes.secrets.fail-fast=true",
"spring.cloud.kubernetes.secrets.retry.enabled=false" })
@Nested
public class SecretsFailFastEnabledButRetryDisabled {
@Autowired
ConfigurableApplicationContext context;
@Test
public void shouldNotDefineRetryBeans() {
assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty();
}
}
@Nested
public class FailFastEnabledWithoutSpringRetryOnClasspath {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(KubernetesBootstrapConfiguration.class))
.withClassLoader(new FilteredClassLoader(Retryable.class, Aspect.class, AopAutoConfiguration.class));
@Test
public void shouldNotDefineRetryBeansWhenConfigMapFailFastEnabled() {
contextRunner.withPropertyValues("spring.cloud.kubernetes.config.fail-fast=true")
.run(context -> assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty());
}
@Test
public void shouldNotDefineRetryBeansWhenSecretsFailFastEnabled() {
contextRunner.withPropertyValues("spring.cloud.kubernetes.secrets.fail-fast=true")
.run(context -> assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty());
}
@Test
public void shouldNotDefineRetryBeansWhenConfigMapAndSecretsFailFastEnabled() {
contextRunner
.withPropertyValues("spring.cloud.kubernetes.config.fail-fast=true",
"spring.cloud.kubernetes.secrets.fail-fast=true")
.run(context -> assertThat(context.getBeansOfType(RetryOperationsInterceptor.class)).isEmpty());
}
}
@SpringBootApplication
static class App {
}
}

View File

@@ -1,230 +0,0 @@
/*
* 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 java.util.HashMap;
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.Nested;
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.context.ApplicationContext;
import org.springframework.core.env.PropertySource;
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.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Isik Erhan
*/
@EnableKubernetesMockClient
public class Fabric8ConfigMapPropertySourceLocatorRetryTests {
private static final String API = "/api/v1/namespaces/default/configmaps/application";
static KubernetesMockServer mockServer;
static KubernetesClient mockClient;
@BeforeAll
public static void setup() {
// 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");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
}
@Nested
@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 {
@SpyBean
private Fabric8ConfigMapPropertySourceLocator propertySourceLocator;
@Test
public void locateShouldNotRetryWhenThereIsNoFailure() {
Map<String, String> data = new HashMap<>();
data.put("some.prop", "theValue");
data.put("some.number", "0");
// return config map without failing
mockServer.expect().withPath(API).andReturn(200, new ConfigMapBuilder().withNewMetadata()
.withName("application").endMetadata().addToData(data).build()).once();
PropertySource<?> propertySource = Assertions
.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment()));
// verify locate is called only once
verify(propertySourceLocator, times(1)).locate(any());
// validate the contents of the property source
assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue");
assertThat(propertySource.getProperty("some.number")).isEqualTo("0");
}
@Test
public void locateShouldRetryAndRecover() {
Map<String, String> data = new HashMap<>();
data.put("some.prop", "theValue");
data.put("some.number", "0");
// fail 3 times then succeed at the 4th call
mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").times(3);
mockServer.expect().withPath(API).andReturn(200, new ConfigMapBuilder().withNewMetadata()
.withName("application").endMetadata().addToData(data).build()).once();
PropertySource<?> propertySource = Assertions
.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment()));
// verify retried 4 times
verify(propertySourceLocator, times(4)).locate(any());
// validate the contents of the property source
assertThat(propertySource.getProperty("some.prop")).isEqualTo("theValue");
assertThat(propertySource.getProperty("some.number")).isEqualTo("0");
}
@Test
public void locateShouldRetryAndFail() {
// fail all the 5 requests
mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").times(5);
assertThatThrownBy(() -> propertySourceLocator.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());
}
}
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = { "spring.cloud.kubernetes.client.namespace=default",
"spring.main.cloud-platform=KUBERNETES" },
classes = Application.class)
@EnableKubernetesMockClient
class ConfigFailFastDisabled {
@SpyBean
private Fabric8ConfigMapPropertySourceLocator propertySourceLocator;
@Test
public void locateShouldNotRetry() {
mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once();
Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment()));
// verify that propertySourceLocator.locate is called only once
verify(propertySourceLocator, times(1)).locate(any());
}
}
@Nested
@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 {
@SpyBean
private Fabric8ConfigMapPropertySourceLocator propertySourceLocator;
@Autowired
private ApplicationContext context;
@Test
public void locateShouldFailWithoutRetrying() {
/*
* Enabling secrets retry causes Spring Retry to be enabled and a
* RetryOperationsInterceptor bean with NeverRetryPolicy for config maps to be
* defined. ConfigMapPropertySourceLocator should not retry even Spring Retry
* is enabled.
*/
mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once();
assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isTrue();
assertThatThrownBy(() -> propertySourceLocator.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());
}
}
@Nested
@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 = Application.class)
@EnableKubernetesMockClient
class ConfigFailFastEnabledButRetryDisabled {
@SpyBean
private Fabric8ConfigMapPropertySourceLocator propertySourceLocator;
@Autowired
private ApplicationContext context;
@Test
public void locateShouldFailWithoutRetrying() {
mockServer.expect().withPath(API).andReturn(500, "Internal Server Error").once();
assertThat(context.containsBean("kubernetesConfigRetryInterceptor")).isFalse();
assertThatThrownBy(() -> propertySourceLocator.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());
}
}
}