|
| 1 | +/* |
| 2 | + * Copyright 2013-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.kubernetes.config; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | + |
| 21 | +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; |
| 22 | +import io.fabric8.kubernetes.client.Config; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.fabric8.kubernetes.client.server.mock.KubernetesServer; |
| 25 | +import org.junit.BeforeClass; |
| 26 | +import org.junit.ClassRule; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | + |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.cloud.kubernetes.config.example.App; |
| 34 | +import org.springframework.test.context.ActiveProfiles; |
| 35 | +import org.springframework.test.context.junit4.SpringRunner; |
| 36 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 37 | + |
| 38 | +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Ali Shahbour |
| 42 | + */ |
| 43 | +@RunWith(SpringRunner.class) |
| 44 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { |
| 45 | + "spring.application.name=configmap-with-active-profile-name-example", |
| 46 | + "spring.cloud.kubernetes.reload.enabled=false" }) |
| 47 | +@ActiveProfiles("development") |
| 48 | +@AutoConfigureWebTestClient |
| 49 | +public class ConfigMapsWithActiveProfilesNameSpringBootTest { |
| 50 | + |
| 51 | + private static final String APPLICATION_NAME = "configmap-with-active-profile-name-example"; |
| 52 | + |
| 53 | + @ClassRule |
| 54 | + public static KubernetesServer server = new KubernetesServer(); |
| 55 | + |
| 56 | + private static KubernetesClient mockClient; |
| 57 | + |
| 58 | + @Autowired(required = false) |
| 59 | + Config config; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private WebTestClient webClient; |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void setUpBeforeClass() { |
| 66 | + mockClient = server.getClient(); |
| 67 | + |
| 68 | + // Configure the kubernetes master url to point to the mock server |
| 69 | + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, |
| 70 | + mockClient.getConfiguration().getMasterUrl()); |
| 71 | + System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); |
| 72 | + System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); |
| 73 | + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, |
| 74 | + "false"); |
| 75 | + System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); |
| 76 | + |
| 77 | + HashMap<String, String> data = new HashMap<>(); |
| 78 | + data.put("application.yml", readResourceFile("application-with-profiles.yaml")); |
| 79 | + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) |
| 80 | + .andReturn(200, new ConfigMapBuilder().withNewMetadata() |
| 81 | + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) |
| 82 | + .always(); |
| 83 | + |
| 84 | + HashMap<String, String> dataWithName = new HashMap<>(); |
| 85 | + dataWithName.put("application.yml", |
| 86 | + readResourceFile("application-with-active-profiles-name.yaml")); |
| 87 | + server.expect() |
| 88 | + .withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME |
| 89 | + + "-development") |
| 90 | + .andReturn(200, |
| 91 | + new ConfigMapBuilder().withNewMetadata() |
| 92 | + .withName(APPLICATION_NAME + "-development").endMetadata() |
| 93 | + .addToData(dataWithName).build()) |
| 94 | + .always(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testGreetingEndpoint() { |
| 99 | + this.webClient.get().uri("/api/greeting").exchange().expectStatus().isOk() |
| 100 | + .expectBody().jsonPath("content") |
| 101 | + .isEqualTo("Hello ConfigMap Active Profile Name, World!"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testFarewellEndpoint() { |
| 106 | + this.webClient.get().uri("/api/farewell").exchange().expectStatus().isOk() |
| 107 | + .expectBody().jsonPath("content") |
| 108 | + .isEqualTo("Goodbye ConfigMap default, World!"); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments