|
| 1 | +/* |
| 2 | +Copyright 2021 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.*; |
| 16 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 17 | +import static org.junit.Assert.assertThrows; |
| 18 | + |
| 19 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 20 | +import com.github.tomakehurst.wiremock.matching.EqualToPattern; |
| 21 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 22 | +import io.kubernetes.client.openapi.ApiClient; |
| 23 | +import io.kubernetes.client.openapi.models.V1Deployment; |
| 24 | +import io.kubernetes.client.util.ClientBuilder; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class KubectlRolloutHistoryTest { |
| 34 | + |
| 35 | + private ApiClient apiClient; |
| 36 | + |
| 37 | + @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); |
| 38 | + |
| 39 | + private static final String DEPLOYMENT = |
| 40 | + KubectlRolloutHistoryTest.class.getClassLoader().getResource("deployment.json").getPath(); |
| 41 | + |
| 42 | + private static final String REPLICASET_LIST = |
| 43 | + KubectlRolloutHistoryTest.class |
| 44 | + .getClassLoader() |
| 45 | + .getResource("replicaset-list.json") |
| 46 | + .getPath(); |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setup() throws IOException { |
| 50 | + apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testKubectlRolloutHistoryDeploymentShouldWork() throws KubectlException, IOException { |
| 55 | + wireMockRule.stubFor( |
| 56 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")) |
| 57 | + .willReturn( |
| 58 | + aResponse() |
| 59 | + .withStatus(200) |
| 60 | + .withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT)))))); |
| 61 | + wireMockRule.stubFor( |
| 62 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")) |
| 63 | + .willReturn( |
| 64 | + aResponse() |
| 65 | + .withStatus(200) |
| 66 | + .withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST)))))); |
| 67 | + KubectlRolloutHistory<V1Deployment> rolloutHistory = |
| 68 | + Kubectl.rolloutHistory(V1Deployment.class) |
| 69 | + .apiClient(apiClient) |
| 70 | + .name("foo") |
| 71 | + .namespace("default"); |
| 72 | + rolloutHistory.execute(); |
| 73 | + wireMockRule.verify( |
| 74 | + 1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")))); |
| 75 | + wireMockRule.verify( |
| 76 | + 1, |
| 77 | + getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))) |
| 78 | + .withQueryParam("labelSelector", new EqualToPattern("app = bar"))); |
| 79 | + Assert.assertEquals(3, rolloutHistory.getHistories().size()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testKubectlRolloutHistoryDeploymentWithRevisionShouldWork() |
| 84 | + throws KubectlException, IOException { |
| 85 | + wireMockRule.stubFor( |
| 86 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")) |
| 87 | + .willReturn( |
| 88 | + aResponse() |
| 89 | + .withStatus(200) |
| 90 | + .withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT)))))); |
| 91 | + wireMockRule.stubFor( |
| 92 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")) |
| 93 | + .willReturn( |
| 94 | + aResponse() |
| 95 | + .withStatus(200) |
| 96 | + .withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST)))))); |
| 97 | + KubectlRolloutHistory<V1Deployment> rolloutHistory = |
| 98 | + Kubectl.rolloutHistory(V1Deployment.class) |
| 99 | + .apiClient(apiClient) |
| 100 | + .name("foo") |
| 101 | + .namespace("default") |
| 102 | + .revision(3); |
| 103 | + rolloutHistory.execute(); |
| 104 | + wireMockRule.verify( |
| 105 | + 1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")))); |
| 106 | + wireMockRule.verify( |
| 107 | + 1, |
| 108 | + getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))) |
| 109 | + .withQueryParam("labelSelector", new EqualToPattern("app = bar"))); |
| 110 | + Assert.assertNotNull(rolloutHistory.getTemplate()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testKubectlRolloutHistoryWithInvalidRevisionShouldThrow() throws IOException { |
| 115 | + wireMockRule.stubFor( |
| 116 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")) |
| 117 | + .willReturn( |
| 118 | + aResponse() |
| 119 | + .withStatus(200) |
| 120 | + .withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT)))))); |
| 121 | + wireMockRule.stubFor( |
| 122 | + get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")) |
| 123 | + .willReturn( |
| 124 | + aResponse() |
| 125 | + .withStatus(200) |
| 126 | + .withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST)))))); |
| 127 | + KubectlRolloutHistory<V1Deployment> rolloutHistory = |
| 128 | + Kubectl.rolloutHistory(V1Deployment.class) |
| 129 | + .apiClient(apiClient) |
| 130 | + .name("foo") |
| 131 | + .namespace("default") |
| 132 | + .revision(999); |
| 133 | + assertThrows(KubectlException.class, rolloutHistory::execute); |
| 134 | + rolloutHistory.revision(-1); |
| 135 | + assertThrows(KubectlException.class, rolloutHistory::execute); |
| 136 | + } |
| 137 | +} |
0 commit comments