|
| 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.extended.kubectl; |
| 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.patch; |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 20 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | + |
| 23 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 24 | +import com.github.tomakehurst.wiremock.matching.EqualToPattern; |
| 25 | +import io.kubernetes.client.custom.V1Patch; |
| 26 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 27 | +import io.kubernetes.client.openapi.ApiClient; |
| 28 | +import io.kubernetes.client.openapi.models.V1ConfigMap; |
| 29 | +import io.kubernetes.client.util.ClientBuilder; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Paths; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class KubectlPatchTest { |
| 38 | + |
| 39 | + private static final String DISCOVERY_API = |
| 40 | + KubectlPatchTest.class.getClassLoader().getResource("discovery-api.json").getPath(); |
| 41 | + |
| 42 | + private static final String DISCOVERY_APIV1 = |
| 43 | + KubectlPatchTest.class.getClassLoader().getResource("discovery-api-v1.json").getPath(); |
| 44 | + |
| 45 | + private static final String DISCOVERY_APIS = |
| 46 | + KubectlPatchTest.class.getClassLoader().getResource("discovery-apis.json").getPath(); |
| 47 | + |
| 48 | + private ApiClient apiClient; |
| 49 | + |
| 50 | + @Rule public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort()); |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setup() throws IOException { |
| 54 | + apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testPatchConfigMap() throws KubectlException, IOException { |
| 59 | + wireMockRule.stubFor( |
| 60 | + patch(urlPathEqualTo("/api/v1/namespaces/foo/configmaps/bar")) |
| 61 | + .withHeader( |
| 62 | + "Content-Type", new EqualToPattern(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)) |
| 63 | + .willReturn( |
| 64 | + aResponse() |
| 65 | + .withStatus(200) |
| 66 | + .withBody("{\"metadata\":{\"name\":\"bar\",\"namespace\":\"foo\"}}"))); |
| 67 | + wireMockRule.stubFor( |
| 68 | + get(urlPathEqualTo("/api")) |
| 69 | + .willReturn( |
| 70 | + aResponse() |
| 71 | + .withStatus(200) |
| 72 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_API)))))); |
| 73 | + wireMockRule.stubFor( |
| 74 | + get(urlPathEqualTo("/apis")) |
| 75 | + .willReturn( |
| 76 | + aResponse() |
| 77 | + .withStatus(200) |
| 78 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIS)))))); |
| 79 | + wireMockRule.stubFor( |
| 80 | + get(urlPathEqualTo("/api/v1")) |
| 81 | + .willReturn( |
| 82 | + aResponse() |
| 83 | + .withStatus(200) |
| 84 | + .withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1)))))); |
| 85 | + |
| 86 | + V1ConfigMap configMap = |
| 87 | + Kubectl.patch(V1ConfigMap.class) |
| 88 | + .namespace("foo") |
| 89 | + .name("bar") |
| 90 | + .patchType(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH) |
| 91 | + .patchContent(new V1Patch("{\"data\":{\"key\":\"value\"}}")) |
| 92 | + .apiClient(apiClient) |
| 93 | + .execute(); |
| 94 | + wireMockRule.verify( |
| 95 | + 1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/foo/configmaps/bar"))); |
| 96 | + assertNotNull(configMap); |
| 97 | + } |
| 98 | +} |
0 commit comments