diff --git a/spring-cloud-kubernetes-config/pom.xml b/spring-cloud-kubernetes-config/pom.xml index 7a55b131..dfcf1b0e 100644 --- a/spring-cloud-kubernetes-config/pom.xml +++ b/spring-cloud-kubernetes-config/pom.xml @@ -76,6 +76,11 @@ spock-spring test + + io.rest-assured + rest-assured + test + org.springframework.boot diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java new file mode 100644 index 00000000..a50be32f --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config; + +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.server.mock.KubernetesServer; +import io.restassured.RestAssured; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.config.example.App; +import org.springframework.test.context.junit4.SpringRunner; + +import static io.restassured.RestAssured.when; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; + +/** + * @author Charles Moulliard + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = App.class, + properties = { "spring.application.name=configmap-example", + "spring.cloud.kubernetes.reload.enabled=false"}) +public class ConfigMapsSpringBootTest { + + @ClassRule + public static KubernetesServer server = new KubernetesServer(); + + public static KubernetesClient mockClient; + + @Autowired(required = false) + Config config; + + private static String APPLICATION_NAME = "configmap-example"; + + @Value("${local.server.port}") + private int port; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + mockClient = server.getClient(); + + //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_NAMESPACE_SYSTEM_PROPERTY, "test"); + + HashMap data = new HashMap<>(); + data.put("bean.message","Hello ConfigMap, %s!"); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME).andReturn(200, new ConfigMapBuilder() + .withNewMetadata().withName(APPLICATION_NAME).endMetadata() + .addToData(data) + .build()) + .always(); + } + + + @Before + public void setUp() { + RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port); + } + + @Test + public void testConfig() { + assertEquals(config.getMasterUrl(),mockClient.getConfiguration().getMasterUrl()); + assertEquals(config.getNamespace(),mockClient.getNamespace()); + } + + @Test + public void testGreetingEndpoint() { + when().get() + .then() + .statusCode(200) + .body("content", is("Hello ConfigMap, World!")); + } + + @Test + public void testConfigMap() { + ConfigMap configmap = mockClient.configMaps().inNamespace("test").withName(APPLICATION_NAME).get(); + HashMap keys = (HashMap) configmap.getData(); + assertEquals(keys.get("bean.message"),"Hello ConfigMap, %s!"); + } + +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java new file mode 100644 index 00000000..653406bb --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config; + +import java.util.HashMap; +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.api.model.ListMeta; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.server.mock.KubernetesServer; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Charles Moulliard + */ +public class ConfigMapsTest { + + @Rule + public KubernetesServer server = new KubernetesServer(); + + @Test + public void testConfigMapList() { + server.expect().withPath("/api/v1/namespaces/ns1/configmaps").andReturn(200, new ConfigMapListBuilder().build()).once(); + + KubernetesClient client = server.getClient(); + + ConfigMapList configMapList = client.configMaps().inNamespace("ns1").list(); + assertNotNull(configMapList); + assertEquals(0, configMapList.getItems().size()); + } + + @Test + public void testConfigMapGet() { + server.expect().withPath("/api/v1/namespaces/ns2/configmaps").andReturn(200, new ConfigMapBuilder() + .withNewMetadata().withName("reload-example").endMetadata() + .addToData("KEY","123") + .build()) + .once(); + + KubernetesClient client = server.getClient(); + ConfigMapList configMapList = client.configMaps().inNamespace("ns2").list(); + assertNotNull(configMapList); + assertEquals(1, configMapList.getAdditionalProperties().size()); + HashMap data = (HashMap) configMapList.getAdditionalProperties().get("data"); + assertEquals("123",data.get("KEY")); + + } + +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java new file mode 100644 index 00000000..078a68df --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * + */ +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java new file mode 100644 index 00000000..e34b818c --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config.example; + +/** + * @author Charles Moulliard + */ +public class Greeting { + + private final String content; + + public Greeting() { + this.content = null; + } + + public Greeting(String content) { + this.content = content; + } + + public String getContent() { + return content; + } +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java new file mode 100644 index 00000000..8c771908 --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Charles Moulliard + */ +@RestController +public class GreetingController { + + private final GreetingProperties properties; + + @Autowired + public GreetingController(GreetingProperties properties) { + this.properties = properties; + } + + @RequestMapping("/api/greeting") + public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { + String message = String.format(properties.getMessage(), name); + return new Greeting(message); + } +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java new file mode 100644 index 00000000..c0222488 --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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.config.example; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "bean") +public class GreetingProperties { + + private String message = "Hello, %s!"; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/spring-cloud-kubernetes-config/src/test/resources/logback-test.xml b/spring-cloud-kubernetes-config/src/test/resources/logback-test.xml new file mode 100644 index 00000000..75d20a0a --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/resources/logback-test.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/spring-cloud-kubernetes-dependencies/pom.xml b/spring-cloud-kubernetes-dependencies/pom.xml index ab44c3f1..021f8ece 100644 --- a/spring-cloud-kubernetes-dependencies/pom.xml +++ b/spring-cloud-kubernetes-dependencies/pom.xml @@ -18,6 +18,7 @@ 1.0.0.Alpha20 2.2.0 0.0.12 + 3.0.2 1.0-groovy-2.3 @@ -146,6 +147,13 @@ test + + io.rest-assured + rest-assured + ${restassured.version} + test + + org.spockframework spock-spring