Skip to content

Commit 4b6ab08

Browse files
committed
added first draft of non-working kubectl.delete test
1 parent 592d2a1 commit 4b6ab08

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
14+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
15+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
20+
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
21+
import io.kubernetes.client.custom.V1Patch;
22+
import io.kubernetes.client.extended.kubectl.Kubectl;
23+
import io.kubernetes.client.extended.kubectl.KubectlDelete;
24+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
25+
import io.kubernetes.client.openapi.ApiClient;
26+
import io.kubernetes.client.openapi.ApiException;
27+
import io.kubernetes.client.openapi.JSON;
28+
import io.kubernetes.client.openapi.apis.BatchV1Api;
29+
import io.kubernetes.client.openapi.models.*;
30+
import io.kubernetes.client.util.ClientBuilder;
31+
import io.kubernetes.client.util.ModelMapper;
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.nio.file.Files;
35+
import java.nio.file.Paths;
36+
37+
import io.kubernetes.client.util.generic.GenericKubernetesApi;
38+
import io.kubernetes.client.util.generic.KubernetesApiResponse;
39+
import org.junit.Before;
40+
import org.junit.Rule;
41+
import org.junit.Test;
42+
43+
public class KubectlDeleteTest {
44+
45+
private ApiClient apiClient;
46+
47+
private JSON json = new JSON();
48+
private GenericKubernetesApi<V1Job, V1JobList> jobClient;
49+
50+
private static final String DISCOVERY_API =
51+
new File(KubectlDeleteTest.class.getClassLoader().getResource("discovery-api.json").getPath())
52+
.toString();
53+
54+
private static final String DISCOVERY_APIV1 =
55+
new File(
56+
KubectlDeleteTest.class
57+
.getClassLoader()
58+
.getResource("discovery-api-v1.json")
59+
.getPath())
60+
.toString();
61+
62+
private static final String DISCOVERY_APIS =
63+
new File(KubectlDeleteTest.class.getClassLoader().getResource("discovery-apis.json").getPath())
64+
.toString();
65+
66+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
67+
68+
@Before
69+
public void setup() throws IOException {
70+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
71+
jobClient =
72+
new GenericKubernetesApi<>(V1Job.class, V1JobList.class, "batch", "v1", "jobs", apiClient);
73+
}
74+
75+
@Test
76+
public void testKubeDelete() throws KubectlException, IOException, ApiException {
77+
// wireMockRule.stubFor(
78+
// patch(urlPathEqualTo("/apis/batch/v1/namespaces/foo/jobs/bar"))
79+
// .withHeader(
80+
// "Content-Type", new EqualToPattern(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH))
81+
// .willReturn(
82+
// aResponse()
83+
// .withStatus(200)
84+
// .withBody("{\"metadata\":{\"name\":\"bar\",\"namespace\":\"foo\"}}")));
85+
// V1Status status = new V1Status().kind("Status").code(200).message("good!");
86+
// stubFor(
87+
// delete(urlEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1"))
88+
// .willReturn(aResponse().withStatus(200).withBody(json.serialize(status))));
89+
wireMockRule.stubFor(
90+
post(urlPathEqualTo("/apis/batch/v1/namespaces/foo/jobs"))
91+
// .withHeader(
92+
// "Content-Type", new EqualToPattern("application-json"))
93+
.willReturn(
94+
aResponse()
95+
.withStatus(201)
96+
.withBody("{\"kind\":\"Job\",\"apiVersion\":\"batch/v1\",\"metadata\":{\"name\":\"bar\",\"namespace\":\"foo\",\"uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\",\"resourceVersion\":\"46205\",\"generation\":1,\"creationTimestamp\":\"2023-11-23T15:38:18Z\",\"labels\":{\"batch.kubernetes.io/controller-uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\",\"batch.kubernetes.io/job-name\":\"bar\",\"controller-uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\",\"job-name\":\"bar\"},\"annotations\":{\"batch.kubernetes.io/job-tracking\":\"\"},\"managedFields\":[{\"manager\":\"Kubernetes Java Client\",\"operation\":\"Update\",\"apiVersion\":\"batch/v1\",\"time\":\"2023-11-23T15:38:18Z\",\"fieldsType\":\"FieldsV1\",\"fieldsV1\":{\"f:spec\":{\"f:backoffLimit\":{},\"f:completionMode\":{},\"f:completions\":{},\"f:parallelism\":{},\"f:suspend\":{},\"f:template\":{\"f:spec\":{\"f:containers\":{\"k:{\\\"name\\\":\\\"bar2\\\"}\":{\".\":{},\"f:command\":{},\"f:image\":{},\"f:imagePullPolicy\":{},\"f:name\":{},\"f:resources\":{},\"f:terminationMessagePath\":{},\"f:terminationMessagePolicy\":{}}},\"f:dnsPolicy\":{},\"f:restartPolicy\":{},\"f:schedulerName\":{},\"f:securityContext\":{},\"f:terminationGracePeriodSeconds\":{}}}}}}]},\"spec\":{\"parallelism\":1,\"completions\":1,\"backoffLimit\":6,\"selector\":{\"matchLabels\":{\"batch.kubernetes.io/controller-uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\"}},\"template\":{\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"batch.kubernetes.io/controller-uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\",\"batch.kubernetes.io/job-name\":\"bar\",\"controller-uid\":\"7f64e06e-d6a6-4598-b375-7c8773f3b0e7\",\"job-name\":\"bar\"}},\"spec\":{\"containers\":[{\"name\":\"bar2\",\"image\":\"busybox\",\"command\":[\"sh\",\"-c\",\"echo Hello World!\"],\"resources\":{},\"terminationMessagePath\":\"/dev/termination-log\",\"terminationMessagePolicy\":\"File\",\"imagePullPolicy\":\"Always\"}],\"restartPolicy\":\"Never\",\"terminationGracePeriodSeconds\":30,\"dnsPolicy\":\"ClusterFirst\",\"securityContext\":{},\"schedulerName\":\"default-scheduler\"}},\"completionMode\":\"NonIndexed\",\"suspend\":false},\"status\":{}}")));
97+
// String localVarPath = "/apis/batch/v1/namespaces/{namespace}/jobs"
98+
// .replaceAll("\\{" + "namespace" + "\\}", localVarApiClient.escapeString(namespace.toString()));
99+
100+
wireMockRule.stubFor(
101+
get(urlPathEqualTo("/api"))
102+
.willReturn(
103+
aResponse()
104+
.withStatus(200)
105+
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_API))))));
106+
wireMockRule.stubFor(
107+
get(urlPathEqualTo("/apis"))
108+
.willReturn(
109+
aResponse()
110+
.withStatus(200)
111+
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIS))))));
112+
wireMockRule.stubFor(
113+
get(urlPathEqualTo("/api/v1"))
114+
.willReturn(
115+
aResponse()
116+
.withStatus(200)
117+
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1))))));
118+
119+
V1JobSpec v1JobSpec = new V1JobSpec()
120+
.template(new V1PodTemplateSpec()
121+
.spec(new V1PodSpec()
122+
.containers(java.util.Arrays.asList(new V1Container()
123+
.name("hello")
124+
.image("busybox")
125+
.command(java.util.Arrays.asList("sh", "-c", "echo Hello World!"))))
126+
.restartPolicy("Never")));
127+
V1Job job = new V1Job()
128+
.apiVersion("batch/v1")
129+
.kind("Job")
130+
.metadata(new V1ObjectMeta().name("bar"))
131+
.spec(v1JobSpec);
132+
////
133+
BatchV1Api api = new BatchV1Api();
134+
api.setApiClient(apiClient);
135+
api.createNamespacedJob("foo", job, null, null, null, "Strict");
136+
// wireMockRule.stubFor(
137+
// patch(urlPathEqualTo("/apis/batch/v1/namespaces/foo/jobs/bar"))
138+
// .withHeader(
139+
// "Content-Type", new EqualToPattern(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH))
140+
// .willReturn(
141+
// aResponse()
142+
// .withStatus(200)
143+
// .withBody("{\"metadata\":{\"name\":\"bar\",\"namespace\":\"foo\"}}")))
144+
145+
// V1Job is not namespaced in apiClient, so I have to add it to model map??
146+
ModelMapper.addModelMap(V1Job.class, true);
147+
148+
KubectlDelete<V1Job> kubectlDelete = Kubectl.delete(V1Job.class);
149+
kubectlDelete.apiClient(apiClient);
150+
kubectlDelete.namespace("foo").name("bar");
151+
kubectlDelete.execute();
152+
153+
// jobClient.delete("foo", "bar");
154+
155+
156+
// V1Job foo1 =
157+
// new V1Job().kind("Job").metadata(new V1ObjectMeta().namespace("foo").name("bar"));
158+
//
159+
// stubFor(
160+
// post(urlEqualTo("/apis/batch/v1/namespaces/foo/jobs"))
161+
// .willReturn(aResponse().withStatus(200).withBody(json.serialize(foo1))));
162+
// KubernetesApiResponse<V1Job> jobListResp = jobClient.create(foo1);
163+
// V1Status status = new V1Status().kind("Status").code(200).message("good!");
164+
// stubFor(
165+
// delete(urlEqualTo("/apis/batch/v1/namespaces/foo/jobs/bar"))
166+
// .willReturn(aResponse().withStatus(200).withBody(json.serialize(status))));
167+
//
168+
// KubernetesApiResponse<V1Job> deleteJobResp = jobClient.delete("foo", "bar", null);
169+
// System.out.println(deleteJobResp.isSuccess());
170+
// System.out.println(deleteJobResp.getStatus());
171+
}
172+
}

0 commit comments

Comments
 (0)