-
Notifications
You must be signed in to change notification settings - Fork 1k
move fabric8-config to junit-5 #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -54,17 +55,13 @@ public class ConfigMapsMixedTests { | |
|
||
protected static final String APPLICATION_NAME = "configmap-mixed-example"; | ||
|
||
@ClassRule | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is now injected by |
||
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()); | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another interesting change is that As such, I deleted There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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"); | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.