Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.configserver.configurations.MockConfig;
import org.springframework.context.ConfigurableApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -28,7 +29,7 @@
* @author Ryan Baxter
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "spring.profiles.include=kubernetes,kubernetesdisabled" },
properties = { "spring.profiles.include=kubernetes,kubernetesdisabled", "test.first.config.enabled=true" },
classes = { KubernetesConfigServerApplication.class, MockConfig.class })
class ConfigServerAutoConfigurationKubernetesDisabledTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.configserver.configurations.MockConfig;
import org.springframework.context.ConfigurableApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@

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.MockBean;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.cloud.kubernetes.configserver.configurations.ThirdConfig;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

@SpringJUnitConfig
@SpringBootTest
@SpringBootTest(classes = ThirdConfig.class, properties = "test.third.config.enabled=true")
class KubernetesEnvironmentRepositoryFactoryTests {

@MockBean
@Autowired
private KubernetesEnvironmentRepository mockRepository;

@Test
Expand All @@ -47,20 +43,4 @@ void testBuild() {
assertThat(repository).isSameAs(mockRepository);
}

@Configuration
static class TestConfig {

@Bean
public KubernetesEnvironmentRepository kubernetesEnvironmentRepository() {
return mock(KubernetesEnvironmentRepository.class);
}

@Bean
public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory(
KubernetesEnvironmentRepository kubernetesEnvironmentRepository) {
return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepository);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigContext;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapsCache;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsCache;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySource;
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
import org.springframework.cloud.kubernetes.commons.config.Constants;
Expand Down Expand Up @@ -89,39 +90,33 @@ class KubernetesEnvironmentRepositoryTests {

private static final V1ConfigMapList CONFIGMAP_DEV_LIST = new V1ConfigMapList()
.addItemsItem(new V1ConfigMapBuilder()
.withMetadata(
new V1ObjectMetaBuilder().withName("stores").withNamespace("dev").withResourceVersion("1").build())
.withMetadata(new V1ObjectMetaBuilder().withName("stores").withNamespace("dev").build())
.addToData(Constants.APPLICATION_YAML,
"dummy:\n property:\n string2: \"dev\"\n int2: 1\n bool2: true\n")
.build());

private static final V1SecretList SECRET_LIST = new V1SecretListBuilder()
.addToItems(
new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("application")
.withResourceVersion("0")
.withNamespace("default")
.build())
.addToData("password", "p455w0rd".getBytes())
.addToData("username", "user".getBytes())
.build())
.addToItems(new V1SecretBuilder().withMetadata(
new V1ObjectMetaBuilder().withName("stores").withResourceVersion("0").withNamespace("default").build())
.addToItems(new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("application").withNamespace("default").build())
.addToData("password", "p455w0rd".getBytes())
.addToData("username", "user".getBytes())
.build())
.addToItems(new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("stores").withNamespace("default").build())
.addToData("password", "password-from-stores".getBytes())
.addToData("username", "stores".getBytes())
.build())
.addToItems(new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("stores-dev")
.withResourceVersion("0")
.withNamespace("default")
.build())
.withMetadata(new V1ObjectMetaBuilder().withName("stores-dev").withNamespace("default").build())
.addToData("password", "password-from-stores-dev".getBytes())
.addToData("username", "stores-dev".getBytes())
.build())
.build();

private static final KubernetesConfigServerProperties PROPERTIES = properties();

@BeforeAll
public static void before() {
static void before() {
KUBERNETES_PROPERTY_SOURCE_SUPPLIER.add((coreApi, applicationName, namespace, springEnv) -> {
List<MapPropertySource> propertySources = new ArrayList<>();

Expand Down Expand Up @@ -152,8 +147,9 @@ public static void before() {
}

@AfterEach
public void after() {
void after() {
new KubernetesClientConfigMapsCache().discardAll();
new KubernetesClientSecretsCache().discardAll();
}

@Test
Expand All @@ -169,7 +165,7 @@ public void testApplicationCase() throws ApiException {
eq(null), eq(null), eq(null), eq(null), eq(null)))
.thenReturn(CONFIGMAP_DEV_LIST);
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
Environment environment = environmentRepository.findOne("application", "", "");
assertThat(environment.getPropertySources().size()).isEqualTo(2);
environment.getPropertySources().forEach(propertySource -> {
Expand Down Expand Up @@ -245,7 +241,7 @@ public void testStoresCase() throws ApiException {
eq(null), eq(null), eq(null), eq(null), eq(null)))
.thenReturn(SECRET_LIST);
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
Environment environment = environmentRepository.findOne("stores", "", "");
assertThat(environment.getPropertySources().size()).isEqualTo(4);
environment.getPropertySources().forEach(propertySource -> {
Expand Down Expand Up @@ -292,7 +288,7 @@ public void testStoresProfileCase() throws ApiException {
eq(null), eq(null), eq(null), eq(null), eq(null)))
.thenReturn(CONFIGMAP_DEV_LIST);
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
Environment environment = environmentRepository.findOne("stores", "dev", "");
assertThat(environment.getPropertySources().size()).isEqualTo(6);
environment.getPropertySources().forEach(propertySource -> {
Expand Down Expand Up @@ -356,7 +352,7 @@ public void testApplicationPropertiesAnSecretsOverride() throws ApiException {
eq(null), eq(null), eq(null), eq(null), eq(null)))
.thenReturn(CONFIGMAP_DEV_LIST);
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
Environment environment = environmentRepository.findOne("stores-dev", "", "");
environment.getPropertySources()
.stream()
Expand Down Expand Up @@ -404,7 +400,7 @@ public void testSingleConfigMapMultipleSources() throws ApiException {
return propertySources;
});
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi, suppliers,
"default");
"default", PROPERTIES);
Environment environment = environmentRepository.findOne("storessingle", "", "");
assertThat(environment.getPropertySources().size()).isEqualTo(1);
assertThat(environment.getPropertySources().get(0).getName())
Expand Down Expand Up @@ -434,4 +430,10 @@ public void testSingleConfigMapMultipleSources() throws ApiException {

}

private static KubernetesConfigServerProperties properties() {
KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties();
properties.setOrder(1);
return properties;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1SecretBuilder;
import io.kubernetes.client.openapi.models.V1SecretList;
import io.kubernetes.client.openapi.models.V1SecretListBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapsCache;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsCache;
import org.springframework.cloud.kubernetes.commons.config.Constants;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -47,7 +50,7 @@
*/
class KubernetesPropertySourceSupplierTests {

private static final CoreV1Api coreApi = mock(CoreV1Api.class);
private static final CoreV1Api CORE_V1_API = mock(CoreV1Api.class);

private static final V1ConfigMapList CONFIGMAP_DEFAULT_LIST = new V1ConfigMapList()
.addItemsItem(buildConfigMap("gateway", "default"));
Expand All @@ -58,41 +61,51 @@ class KubernetesPropertySourceSupplierTests {
private static final V1ConfigMapList CONFIGMAP_TEAM_B_LIST = new V1ConfigMapList()
.addItemsItem(buildConfigMap("orders", "team-b"));

private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretListBuilder()
.addToItems(buildSecret("gateway", "default"))
.build();
private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretList()
.addItemsItem(buildSecret("gateway", "default"));

private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretListBuilder()
.addToItems(buildSecret("stores", "team-a"))
.build();
private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretList()
.addItemsItem(buildSecret("stores", "team-a"));

private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretListBuilder()
.addToItems(buildSecret("orders", "team-b"))
.build();
private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretList()
.addItemsItem(buildSecret("orders", "team-b"));

private static final KubernetesConfigServerProperties PROPERTIES = properties();

@BeforeAll
public static void before() throws ApiException {
when(coreApi.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
static void beforeAll() throws ApiException {
when(CORE_V1_API.listNamespacedConfigMap("default", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(CONFIGMAP_DEFAULT_LIST);
when(coreApi.listNamespacedConfigMap(eq("team-a"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
when(CORE_V1_API.listNamespacedConfigMap("team-a", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(CONFIGMAP_TEAM_A_LIST);
when(coreApi.listNamespacedConfigMap(eq("team-b"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
when(CORE_V1_API.listNamespacedConfigMap("team-b", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(CONFIGMAP_TEAM_B_LIST);

when(coreApi.listNamespacedSecret(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
when(CORE_V1_API.listNamespacedSecret("default", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(SECRET_DEFAULT_LIST);
when(coreApi.listNamespacedSecret(eq("team-a"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
when(CORE_V1_API.listNamespacedSecret("team-a", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(SECRET_TEAM_A_LIST);
when(coreApi.listNamespacedSecret(eq("team-b"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
eq(null), eq(null), eq(null), eq(null), eq(null)))
when(CORE_V1_API.listNamespacedSecret("team-b", null, null, null, null, null, null, null, null, null, null,
null))
.thenReturn(SECRET_TEAM_B_LIST);
}

@AfterAll
static void afterAll() {
Mockito.reset(CORE_V1_API);
}

@AfterEach
void afterEach() {
new KubernetesClientConfigMapsCache().discardAll();
new KubernetesClientSecretsCache().discardAll();
}

@Test
void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
KubernetesConfigServerProperties kubernetesConfigServerProperties = new KubernetesConfigServerProperties();
Expand All @@ -101,8 +114,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);

KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);

Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
Expand All @@ -122,8 +135,8 @@ void whenExtraNamespacesAddedThenConfigMapsInCurrentNamespaceAreNotIncluded() {
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);

KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);

Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);
Expand All @@ -143,8 +156,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllSecretsAreIncluded() {
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);

KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);

Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
Expand All @@ -164,8 +177,8 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);

KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);

Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);
Expand All @@ -179,19 +192,25 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {

private static V1ConfigMap buildConfigMap(String name, String namespace) {
return new V1ConfigMapBuilder()
.withMetadata(
new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).withResourceVersion("1").build())
.addToData(Constants.APPLICATION_YAML, "dummy:\n property:\n string: \"" + name + "\"\n")
.withMetadata(new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).build())
.addToData(Constants.APPLICATION_PROPERTIES, """
dummy.property.string=%s
""".formatted(name))
.build();
}

private static V1Secret buildSecret(String name, String namespace) {
return new V1SecretBuilder()
.withMetadata(
new V1ObjectMetaBuilder().withName(name).withResourceVersion("0").withNamespace(namespace).build())
.withMetadata(new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).build())
.addToData("password", "p455w0rd".getBytes())
.addToData("username", "user".getBytes())
.build();
}

private static KubernetesConfigServerProperties properties() {
KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties();
properties.setOrder(1);
return properties;
}

}
Loading
Loading