|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.spring.extended.manifests; |
| 14 | + |
| 15 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 16 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 17 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | + |
| 23 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 24 | +import com.google.common.io.Resources; |
| 25 | +import io.kubernetes.client.openapi.ApiClient; |
| 26 | +import io.kubernetes.client.openapi.Configuration; |
| 27 | +import io.kubernetes.client.openapi.models.V1Namespace; |
| 28 | +import io.kubernetes.client.openapi.models.V1ObjectMeta; |
| 29 | +import io.kubernetes.client.openapi.models.V1ServiceAccount; |
| 30 | +import io.kubernetes.client.spring.extended.manifests.annotation.KubectlCreate; |
| 31 | +import io.kubernetes.client.util.ClientBuilder; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import org.junit.ClassRule; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.springframework.beans.factory.annotation.Autowired; |
| 39 | +import org.springframework.boot.test.context.SpringBootTest; |
| 40 | +import org.springframework.boot.test.context.TestConfiguration; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.context.annotation.Import; |
| 43 | +import org.springframework.test.context.junit4.SpringRunner; |
| 44 | + |
| 45 | +@RunWith(SpringRunner.class) |
| 46 | +@SpringBootTest |
| 47 | +@Import(KubernetesManifestTest.TestConfig.class) |
| 48 | +public class KubernetesManifestTest { |
| 49 | + |
| 50 | + private static final String DISCOVERY_API = Resources.getResource("discovery-api.json").getPath(); |
| 51 | + |
| 52 | + private static final String DISCOVERY_APIV1 = |
| 53 | + Resources.getResource("discovery-api-v1.json").getPath(); |
| 54 | + |
| 55 | + private static final String DISCOVERY_APIS = |
| 56 | + Resources.getResource("discovery-apis.json").getPath(); |
| 57 | + |
| 58 | + @ClassRule public static WireMockRule wireMockRule = new WireMockRule(8288); |
| 59 | + |
| 60 | + @TestConfiguration |
| 61 | + static class TestConfig { |
| 62 | + |
| 63 | + @Bean |
| 64 | + public ApiClient testingApiClient() { |
| 65 | + ApiClient apiClient = new ClientBuilder().setBasePath(wireMockRule.baseUrl()).build(); |
| 66 | + return apiClient; |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public KubernetesKubectlCreateProcessor kubernetesManifestsProcessor() { |
| 71 | + return new KubernetesKubectlCreateProcessor(); |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + @KubectlCreate |
| 76 | + public V1Namespace testNamespace() { |
| 77 | + return new V1Namespace().metadata(new V1ObjectMeta().name("spring-boot-test-namespace")); |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + @KubectlCreate |
| 82 | + public V1ServiceAccount testServiceAccount( |
| 83 | + V1Namespace testNamespace) { // depends on testNamespace |
| 84 | + return new V1ServiceAccount() |
| 85 | + .metadata( |
| 86 | + new V1ObjectMeta() |
| 87 | + .namespace(testNamespace.getMetadata().getName()) |
| 88 | + .name("spring-boot-test-serviceaccount")); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Autowired private ApiClient client; |
| 93 | + |
| 94 | + @Autowired private V1Namespace createdNamespace; |
| 95 | + |
| 96 | + @Autowired private V1ServiceAccount createdServiceAccount; |
| 97 | + |
| 98 | + private static final V1Namespace returningCreatedNamespace = |
| 99 | + new V1Namespace() |
| 100 | + .metadata( |
| 101 | + new V1ObjectMeta() |
| 102 | + .name("spring-boot-test-namespace") |
| 103 | + .putLabelsItem("created", "true")); |
| 104 | + |
| 105 | + private static final V1ServiceAccount returningCreatedServiceAccount = |
| 106 | + new V1ServiceAccount() |
| 107 | + .metadata( |
| 108 | + new V1ObjectMeta() |
| 109 | + .name("spring-boot-test-serviceaccount") |
| 110 | + .putLabelsItem("created", "true")); |
| 111 | + |
| 112 | + static { |
| 113 | + wireMockRule.resetMappings(); |
| 114 | + wireMockRule.resetScenarios(); |
| 115 | + wireMockRule.resetRequests(); |
| 116 | + wireMockRule.stubFor( |
| 117 | + post(urlEqualTo("/api/v1/namespaces")) |
| 118 | + .willReturn( |
| 119 | + aResponse() |
| 120 | + .withHeader("Content-Type", "application/json") |
| 121 | + .withBody( |
| 122 | + Configuration.getDefaultApiClient() |
| 123 | + .getJSON() |
| 124 | + .serialize(returningCreatedNamespace)))); |
| 125 | + wireMockRule.stubFor( |
| 126 | + post(urlEqualTo("/api/v1/namespaces/spring-boot-test-namespace/serviceaccounts")) |
| 127 | + .willReturn( |
| 128 | + aResponse() |
| 129 | + .withHeader("Content-Type", "application/json") |
| 130 | + .withBody( |
| 131 | + Configuration.getDefaultApiClient() |
| 132 | + .getJSON() |
| 133 | + .serialize(returningCreatedServiceAccount)))); |
| 134 | + |
| 135 | + try { |
| 136 | + wireMockRule.stubFor( |
| 137 | + get(urlPathEqualTo("/api")) |
| 138 | + .willReturn( |
| 139 | + aResponse() |
| 140 | + .withStatus(200) |
| 141 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_API)))))); |
| 142 | + wireMockRule.stubFor( |
| 143 | + get(urlPathEqualTo("/apis")) |
| 144 | + .willReturn( |
| 145 | + aResponse() |
| 146 | + .withStatus(200) |
| 147 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIS)))))); |
| 148 | + wireMockRule.stubFor( |
| 149 | + get(urlPathEqualTo("/api/v1")) |
| 150 | + .willReturn( |
| 151 | + aResponse() |
| 152 | + .withStatus(200) |
| 153 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1)))))); |
| 154 | + } catch (IOException e) { |
| 155 | + e.printStackTrace(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void test() { |
| 161 | + assertNotNull(createdNamespace); |
| 162 | + assertNotNull(createdServiceAccount); |
| 163 | + |
| 164 | + assertEquals("true", createdNamespace.getMetadata().getLabels().get("created")); |
| 165 | + assertEquals("true", createdServiceAccount.getMetadata().getLabels().get("created")); |
| 166 | + } |
| 167 | +} |
0 commit comments