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 @@ -22,28 +22,28 @@

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.assertj.core.util.Lists.newArrayList;

@RunWith(SpringRunner.class)
@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=" + ConfigMapsFromFilePathsTests.FIRST_FILE_NAME_FULL_PATH + ","
+ ConfigMapsFromFilePathsTests.SECOND_FILE_NAME_FULL_PATH + ","
+ ConfigMapsFromFilePathsTests.FIRST_FILE_NAME_DUPLICATED_FULL_PATH })
@EnableKubernetesMockClient(crud = true, https = false)
public class ConfigMapsFromFilePathsTests {

protected static final String FILES_ROOT_PATH = "/tmp/scktests";
Expand All @@ -65,17 +65,13 @@ public class ConfigMapsFromFilePathsTests {
protected static final String FIRST_FILE_NAME_DUPLICATED_FULL_PATH = FILES_ROOT_PATH + "/" + FILES_SUB_PATH + "/"
+ FIRST_FILE_NAME;

@ClassRule
Copy link
Contributor Author

@wind57 wind57 May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now injected by @EnableKubernetesMockClient(crud = true, https = false) so no need for the server. the rest of the changes are trivial, imho, here.

public static KubernetesServer server = new KubernetesServer(false);

private static KubernetesClient mockClient;

@Autowired
private WebTestClient webClient;

@BeforeClass
@BeforeAll
public static void setUpBeforeClass() throws IOException {
mockClient = server.getClient();

// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
Expand All @@ -92,7 +88,7 @@ public static void setUpBeforeClass() throws IOException {
"bean.bonjour=Bonjour from path!");
}

@AfterClass
@AfterAll
public static void teardownAfterClass() {
newArrayList(FIRST_FILE_NAME_FULL_PATH, SECOND_FILE_NAME_FULL_PATH, SECOND_FILE_NAME_FULL_PATH, FILES_ROOT_PATH)
.forEach(fn -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,30 @@
import java.nio.file.Paths;
import java.util.HashMap;

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.KubernetesServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.assertj.core.util.Lists.newArrayList;

@RunWith(SpringRunner.class)
@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 })
@EnableKubernetesMockClient(crud = true, https = false)
public class ConfigMapsMixedTests {

protected static final String FILES_ROOT_PATH = "/tmp/scktests";
Expand All @@ -54,17 +55,13 @@ public class ConfigMapsMixedTests {

protected static final String APPLICATION_NAME = "configmap-mixed-example";

@ClassRule
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now injected by @EnableKubernetesMockClient(crud = true, https = false) so no need for the server. the rest of the changes are trivial, imho, here.

public static KubernetesServer server = new KubernetesServer(false);

private static KubernetesClient mockClient;

@Autowired
private WebTestClient webClient;

@BeforeClass
@BeforeAll
public static void setUpBeforeClass() throws IOException {
mockClient = server.getClient();

// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
Expand All @@ -80,13 +77,14 @@ public static void setUpBeforeClass() throws IOException {

HashMap<String, String> data = new HashMap<>();
data.put("bean.morning", "Buenos Dias ConfigMap, %s");
server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to mock paths anymore

.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(APPLICATION_NAME).endMetadata()
.addToData(data).build())
.always();

ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(APPLICATION_NAME).endMetadata()
.addToData(data).build();

mockClient.configMaps().inNamespace("test").create(configMap);
}

@AfterClass
@AfterAll
public static void teardownAfterClass() {
newArrayList(FILE_NAME_FULL_PATH, FILES_ROOT_PATH).forEach(fn -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,58 @@

import java.util.Map;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.api.model.ConfigMapListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.Rule;
import org.junit.Test;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil.readResourceFile;

/**
* @author Charles Moulliard
*/
@EnableKubernetesMockClient(crud = true, https = false)
public class ConfigMapsTest {

@Rule
public KubernetesServer server = new KubernetesServer(false);
private static KubernetesClient mockClient;

@Test
public void testConfigMapList() {
this.server.expect().withPath("/api/v1/namespaces/ns1/configmaps")
.andReturn(200, new ConfigMapListBuilder().build()).once();
mockClient.configMaps().inNamespace("ns1").createNew();

KubernetesClient client = this.server.getClient();

ConfigMapList configMapList = client.configMaps().inNamespace("ns1").list();
ConfigMapList configMapList = mockClient.configMaps().inNamespace("ns1").list();
assertThat(configMapList).isNotNull();
assertThat(configMapList.getItems().size()).isEqualTo(0);
}

@Test
public void testConfigMapGet() {
this.server
.expect().withPath("/api/v1/namespaces/ns2/configmaps").andReturn(200, new ConfigMapBuilder()
.withNewMetadata().withName("reload-example").endMetadata().addToData("KEY", "123").build())
.once();

KubernetesClient client = this.server.getClient();
ConfigMapList configMapList = client.configMaps().inNamespace("ns2").list();
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName("reload-example").endMetadata()
.addToData("KEY", "123").build();

mockClient.configMaps().inNamespace("ns2").create(configMap);

ConfigMapList configMapList = mockClient.configMaps().inNamespace("ns2").list();
assertThat(configMapList).isNotNull();
assertThat(configMapList.getAdditionalProperties()).containsKey("data");
@SuppressWarnings("unchecked")
Map<String, String> data = (Map<String, String>) configMapList.getAdditionalProperties().get("data");
assertThat(data.get("KEY")).isEqualTo("123");
assertThat(configMapList.getItems().size()).isEqualTo(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it tests the same thing, but where to find the "thing" to assert against is in a different place

assertThat(configMapList.getItems().get(0).getData().size()).isEqualTo(1);
Map<String, String> resultData = configMapList.getItems().get(0).getData();
assertThat(resultData.get("KEY")).isEqualTo("123");
}

@Test
public void testConfigMapFromSingleApplicationProperties() {
String configMapName = "app-properties-test";
String namespace = "app-props";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another interesting change is that KubernetesServer used to return a NamespacedKubernetesClient, so you could do : this.server.getClient().inNamespace(namespace), specifically call inNamespace with a certain namespace. KubernetesClient that is injected can't do that so I used it's default namespace test.

As such, I deleted String namespace = "app-props";. Fundamentally, the test does the same assertions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same change is across all tests in this class

this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", readResourceFile("application.properties")).build())
.once();
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", ConfigMapTestUtil.readResourceFile("application.properties"))
.build();

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(
this.server.getClient().inNamespace(namespace), configMapName);
mockClient.configMaps().inNamespace("test").create(configMap);

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

assertThat(cmps.getProperty("dummy.property.string1")).isEqualTo("a");
assertThat(cmps.getProperty("dummy.property.int1")).isEqualTo("1");
Expand All @@ -84,15 +78,13 @@ public void testConfigMapFromSingleApplicationProperties() {

@Test
public void testConfigMapFromSingleApplicationYaml() {
String configMapName = "app-yaml-test";
String namespace = "app-props";
this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.yaml", readResourceFile("application.yaml")).build())
.once();
String configMapName = "app-properties-test";
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.yaml", ConfigMapTestUtil.readResourceFile("application.yaml")).build();

mockClient.configMaps().inNamespace("test").create(configMap);

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(
this.server.getClient().inNamespace(namespace), configMapName);
Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

assertThat(cmps.getProperty("dummy.property.string2")).isEqualTo("a");
assertThat(cmps.getProperty("dummy.property.int2")).isEqualTo(1);
Expand All @@ -102,14 +94,12 @@ public void testConfigMapFromSingleApplicationYaml() {
@Test
public void testConfigMapFromSingleNonStandardFileName() {
String configMapName = "single-non-standard-test";
String namespace = "app-props";
this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("adhoc.yml", readResourceFile("adhoc.yml")).build())
.once();
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("adhoc.yml", ConfigMapTestUtil.readResourceFile("adhoc.yml")).build();

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(
this.server.getClient().inNamespace(namespace), configMapName);
mockClient.configMaps().inNamespace("test").create(configMap);

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

assertThat(cmps.getProperty("dummy.property.string3")).isEqualTo("a");
assertThat(cmps.getProperty("dummy.property.int3")).isEqualTo(1);
Expand All @@ -119,44 +109,39 @@ public void testConfigMapFromSingleNonStandardFileName() {
@Test
public void testConfigMapFromSingleInvalidPropertiesContent() {
String configMapName = "single-unparseable-properties-test";
String namespace = "app-props";
this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", "somevalue").build())
.once();
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", "somevalue").build();

mockClient.configMaps().inNamespace("test").create(configMap);

new Fabric8ConfigMapPropertySource(this.server.getClient().inNamespace(namespace), configMapName);
Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

// no exception is thrown for unparseable content
}

@Test
public void testConfigMapFromSingleInvalidYamlContent() {
String configMapName = "single-unparseable-yaml-test";
String namespace = "app-props";
this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200, new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.yaml", "somevalue").build())
.once();
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.yaml", "somevalue").build();

mockClient.configMaps().inNamespace("test").create(configMap);

new Fabric8ConfigMapPropertySource(this.server.getClient().inNamespace(namespace), configMapName);
Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

// no exception is thrown for unparseable content
}

@Test
public void testConfigMapFromMultipleApplicationProperties() {
String configMapName = "app-multiple-properties-test";
String namespace = "app-props";
this.server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
.andReturn(200,
new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", readResourceFile("application.properties"))
.addToData("adhoc.properties", readResourceFile("adhoc.properties")).build())
.once();

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(
this.server.getClient().inNamespace(namespace), configMapName);
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(configMapName).endMetadata()
.addToData("application.properties", ConfigMapTestUtil.readResourceFile("application.properties"))
.addToData("adhoc.properties", ConfigMapTestUtil.readResourceFile("adhoc.properties")).build();

mockClient.configMaps().inNamespace("test").create(configMap);

Fabric8ConfigMapPropertySource cmps = new Fabric8ConfigMapPropertySource(mockClient, configMapName);

// application.properties should be read correctly
assertThat(cmps.getProperty("dummy.property.string1")).isEqualTo("a");
Expand Down
Loading