|
| 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.*; |
| 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.stubbing.Scenario; |
| 21 | +import io.kubernetes.client.extended.kubectl.Kubectl; |
| 22 | +import io.kubernetes.client.extended.kubectl.KubectlDelete; |
| 23 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 24 | +import io.kubernetes.client.openapi.ApiClient; |
| 25 | +import io.kubernetes.client.openapi.ApiException; |
| 26 | +import io.kubernetes.client.openapi.apis.BatchV1Api; |
| 27 | +import io.kubernetes.client.openapi.models.*; |
| 28 | +import io.kubernetes.client.util.ClientBuilder; |
| 29 | +import io.kubernetes.client.util.ModelMapper; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +import org.apache.commons.io.IOUtils; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +public class KubectlDeleteTest { |
| 39 | + |
| 40 | + private ApiClient apiClient; |
| 41 | + |
| 42 | + private final byte[] DISCOVERY_API; |
| 43 | + |
| 44 | + { |
| 45 | + try { |
| 46 | + DISCOVERY_API = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 47 | + .getClassLoader() |
| 48 | + .getResourceAsStream("discovery-api.json"))); |
| 49 | + } catch (IOException e) { |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private final byte[] DISCOVERY_APIV1; |
| 55 | + |
| 56 | + { |
| 57 | + try { |
| 58 | + DISCOVERY_APIV1 = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 59 | + .getClassLoader() |
| 60 | + .getResourceAsStream("discovery-api-v1.json"))); |
| 61 | + } catch (IOException e) { |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private final byte[] ADD_JOB; |
| 68 | + |
| 69 | + { |
| 70 | + try { |
| 71 | + ADD_JOB = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 72 | + .getClassLoader() |
| 73 | + .getResourceAsStream("deleted-add-job.json"))); |
| 74 | + } catch (IOException e) { |
| 75 | + throw new RuntimeException(e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private final byte[] GET_BATCH; |
| 80 | + |
| 81 | + { |
| 82 | + try { |
| 83 | + GET_BATCH = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 84 | + .getClassLoader() |
| 85 | + .getResourceAsStream("deleted-get-batch.json"))); |
| 86 | + } catch (IOException e) { |
| 87 | + throw new RuntimeException(e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private final byte[] DELETED_FIRST; |
| 92 | + |
| 93 | + { |
| 94 | + try { |
| 95 | + DELETED_FIRST = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 96 | + .getClassLoader() |
| 97 | + .getResourceAsStream("deleted-success.json"))); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new RuntimeException(e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private final byte[] DELETED_SECOND; |
| 104 | + |
| 105 | + { |
| 106 | + try { |
| 107 | + DELETED_SECOND = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 108 | + .getClassLoader() |
| 109 | + .getResourceAsStream("deleted-not-found.json"))); |
| 110 | + } catch (IOException e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private final byte[] DISCOVERY_APIS; |
| 116 | + |
| 117 | + { |
| 118 | + try { |
| 119 | + DISCOVERY_APIS = IOUtils.toByteArray(Objects.requireNonNull(KubectlDeleteTest.class |
| 120 | + .getClassLoader() |
| 121 | + .getResourceAsStream("discovery-apis.json"))); |
| 122 | + } catch (IOException e) { |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); |
| 128 | + |
| 129 | + @Before |
| 130 | + public void setup() { |
| 131 | + apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testKubectlDelete() throws KubectlException, IOException, ApiException { |
| 136 | + wireMockRule.stubFor( |
| 137 | + post(urlPathEqualTo("/apis/batch/v1/namespaces/foo/jobs")) |
| 138 | + .willReturn( |
| 139 | + aResponse() |
| 140 | + .withStatus(201) |
| 141 | + .withBody(ADD_JOB))); |
| 142 | + wireMockRule.stubFor( |
| 143 | + delete(urlPathEqualTo("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar")) |
| 144 | + .inScenario("JobDeletionScenario") |
| 145 | + .whenScenarioStateIs(Scenario.STARTED) |
| 146 | + .willReturn(aResponse() |
| 147 | + .withStatus(200) |
| 148 | + .withBody(DELETED_FIRST) |
| 149 | + ) |
| 150 | + .willSetStateTo("SecondCall") |
| 151 | + ); |
| 152 | + |
| 153 | + wireMockRule.stubFor( |
| 154 | + delete(urlPathEqualTo("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar")) |
| 155 | + .inScenario("JobDeletionScenario") |
| 156 | + .whenScenarioStateIs("SecondCall") |
| 157 | + .willReturn(aResponse() |
| 158 | + .withStatus(404) |
| 159 | + .withBody(DELETED_SECOND) |
| 160 | + ) |
| 161 | + ); |
| 162 | + |
| 163 | + |
| 164 | + wireMockRule.stubFor( |
| 165 | + get(urlPathEqualTo("/api")) |
| 166 | + .willReturn( |
| 167 | + aResponse() |
| 168 | + .withStatus(200) |
| 169 | + .withBody(DISCOVERY_API))); |
| 170 | + wireMockRule.stubFor( |
| 171 | + get(urlPathEqualTo("/apis")) |
| 172 | + .willReturn( |
| 173 | + aResponse() |
| 174 | + .withStatus(200) |
| 175 | + .withBody(DISCOVERY_APIS))); |
| 176 | + wireMockRule.stubFor( |
| 177 | + get(urlPathEqualTo("/api/v1")) |
| 178 | + .willReturn( |
| 179 | + aResponse() |
| 180 | + .withStatus(200) |
| 181 | + .withBody(DISCOVERY_APIV1))); |
| 182 | + wireMockRule.stubFor( |
| 183 | + get(urlPathEqualTo("/apis/batch/v1/")) |
| 184 | + .willReturn( |
| 185 | + aResponse() |
| 186 | + .withStatus(200) |
| 187 | + .withBody(GET_BATCH))); |
| 188 | + |
| 189 | + V1JobSpec v1JobSpec = new V1JobSpec() |
| 190 | + .template(new V1PodTemplateSpec() |
| 191 | + .spec(new V1PodSpec() |
| 192 | + .containers(java.util.Arrays.asList(new V1Container() |
| 193 | + .name("hello") |
| 194 | + .image("busybox") |
| 195 | + .command(java.util.Arrays.asList("sh", "-c", "echo Hello World!")))) |
| 196 | + .restartPolicy("Never"))); |
| 197 | + V1Job job = new V1Job() |
| 198 | + .apiVersion("batch/v1") |
| 199 | + .kind("Job") |
| 200 | + .metadata(new V1ObjectMeta().name("bar")) |
| 201 | + .spec(v1JobSpec); |
| 202 | + BatchV1Api api = new BatchV1Api(); |
| 203 | + api.setApiClient(apiClient); |
| 204 | + api.createNamespacedJob("foo", job, null, null, null, "Strict"); |
| 205 | + ModelMapper.addModelMap(api.getAPIResources().getGroupVersion(), job.getApiVersion(), job.getKind(), "jobs", true, V1Job.class); |
| 206 | + |
| 207 | + KubectlDelete<V1Job> kubectlDelete = Kubectl.delete(V1Job.class); |
| 208 | + kubectlDelete.apiClient(apiClient); |
| 209 | + kubectlDelete.namespace("foo").name("bar"); |
| 210 | + kubectlDelete.execute(); |
| 211 | + |
| 212 | + assertThrows(KubectlException.class, () -> { |
| 213 | + KubectlDelete<V1Job> kubectlDelete2 = Kubectl.delete(V1Job.class); |
| 214 | + kubectlDelete2.apiClient(apiClient); |
| 215 | + kubectlDelete2.namespace("foo").name("bar"); |
| 216 | + kubectlDelete2.execute(); |
| 217 | + }); |
| 218 | + |
| 219 | + KubectlDelete<V1Job> kubectlDelete2 = Kubectl.delete(V1Job.class); |
| 220 | + kubectlDelete2.apiClient(apiClient); |
| 221 | + kubectlDelete2.namespace("foo").name("bar").ignoreNotFound(true); |
| 222 | + kubectlDelete2.execute(); |
| 223 | + } |
| 224 | +} |
0 commit comments