-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Kubectl delete tests #2906
Merged
k8s-ci-robot
merged 11 commits into
kubernetes-client:master
from
awesominat:add-kubectl-delete-tests
Nov 28, 2023
Merged
Add Kubectl delete tests #2906
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4b6ab08
added first draft of non-working kubectl.delete test
awesominat bac561c
Merge branch 'kubernetes-client:master' into add-kubectl-delete-tests
awesominat 7fe6637
hard coded http returns again
awesominat 7c5b81c
fixed test
awesominat 74d4cb1
cleaned file
awesominat ba7d54c
ran spotless
awesominat 0671ef8
refactored http returns to separate resource files
awesominat 12d1b55
renamed resource files
awesominat fc32b50
switched file usage to getResourceAsStream
awesominat 207243f
Merge remote-tracking branch 'upstream/master' into add-kubectl-delet…
awesominat 137ccfe
switched to IOUtils to read IS byte array
awesominat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlDeleteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.extended.kubectl; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.github.tomakehurst.wiremock.stubbing.Scenario; | ||
import io.kubernetes.client.extended.kubectl.Kubectl; | ||
import io.kubernetes.client.extended.kubectl.KubectlDelete; | ||
import io.kubernetes.client.extended.kubectl.exception.KubectlException; | ||
import io.kubernetes.client.openapi.ApiClient; | ||
import io.kubernetes.client.openapi.ApiException; | ||
import io.kubernetes.client.openapi.apis.BatchV1Api; | ||
import io.kubernetes.client.openapi.models.*; | ||
import io.kubernetes.client.util.ClientBuilder; | ||
import io.kubernetes.client.util.ModelMapper; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class KubectlDeleteTest { | ||
|
||
private ApiClient apiClient; | ||
|
||
private static final String DISCOVERY_API = | ||
new File(KubectlDeleteTest.class.getClassLoader().getResource("discovery-api.json").getPath()) | ||
.toString(); | ||
|
||
private static final String DISCOVERY_APIV1 = | ||
new File( | ||
KubectlDeleteTest.class | ||
.getClassLoader() | ||
.getResource("discovery-api-v1.json") | ||
.getPath()) | ||
.toString(); | ||
|
||
|
||
private static final String ADD_JOB = | ||
new File( | ||
KubectlDeleteTest.class | ||
.getClassLoader() | ||
.getResource("deleted-add-job.json") | ||
.getPath()) | ||
.toString(); | ||
private static final String GET_BATCH = | ||
new File( | ||
KubectlDeleteTest.class | ||
.getClassLoader() | ||
.getResource("deleted-get-batch.json") | ||
.getPath()) | ||
.toString(); | ||
private static final String DELETED_FIRST = | ||
new File( | ||
KubectlDeleteTest.class | ||
.getClassLoader() | ||
.getResource("deleted-success.json") | ||
.getPath()) | ||
.toString(); | ||
private static final String DELETED_SECOND = | ||
new File( | ||
KubectlDeleteTest.class | ||
.getClassLoader() | ||
.getResource("deleted-not-found.json") | ||
.getPath()) | ||
.toString(); | ||
|
||
private static final String DISCOVERY_APIS = | ||
new File(KubectlDeleteTest.class.getClassLoader().getResource("discovery-apis.json").getPath()) | ||
.toString(); | ||
|
||
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); | ||
|
||
@Before | ||
public void setup() { | ||
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build(); | ||
} | ||
|
||
@Test | ||
public void testKubectlDelete() throws KubectlException, IOException, ApiException { | ||
wireMockRule.stubFor( | ||
post(urlPathEqualTo("/apis/batch/v1/namespaces/foo/jobs")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(201) | ||
.withBody(new String(Files.readAllBytes(Paths.get(ADD_JOB)))))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once you convert to byte array/string above, you can simplify this code. |
||
wireMockRule.stubFor( | ||
delete(urlPathEqualTo("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar")) | ||
.inScenario("JobDeletionScenario") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(new String(Files.readAllBytes(Paths.get(DELETED_FIRST)))) | ||
) | ||
.willSetStateTo("SecondCall") | ||
); | ||
|
||
wireMockRule.stubFor( | ||
delete(urlPathEqualTo("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar")) | ||
.inScenario("JobDeletionScenario") | ||
.whenScenarioStateIs("SecondCall") | ||
.willReturn(aResponse() | ||
.withStatus(404) | ||
.withBody(new String(Files.readAllBytes(Paths.get(DELETED_SECOND)))) | ||
) | ||
); | ||
|
||
wireMockRule.stubFor( | ||
get(urlPathEqualTo("/api")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_API)))))); | ||
wireMockRule.stubFor( | ||
get(urlPathEqualTo("/apis")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIS)))))); | ||
wireMockRule.stubFor( | ||
get(urlPathEqualTo("/api/v1")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1)))))); | ||
wireMockRule.stubFor( | ||
get(urlPathEqualTo("/apis/batch/v1/")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withBody(new String(Files.readAllBytes(Paths.get(GET_BATCH)))))); | ||
|
||
V1JobSpec v1JobSpec = new V1JobSpec() | ||
.template(new V1PodTemplateSpec() | ||
.spec(new V1PodSpec() | ||
.containers(java.util.Arrays.asList(new V1Container() | ||
.name("hello") | ||
.image("busybox") | ||
.command(java.util.Arrays.asList("sh", "-c", "echo Hello World!")))) | ||
.restartPolicy("Never"))); | ||
V1Job job = new V1Job() | ||
.apiVersion("batch/v1") | ||
.kind("Job") | ||
.metadata(new V1ObjectMeta().name("bar")) | ||
.spec(v1JobSpec); | ||
BatchV1Api api = new BatchV1Api(); | ||
api.setApiClient(apiClient); | ||
api.createNamespacedJob("foo", job, null, null, null, "Strict"); | ||
ModelMapper.addModelMap(api.getAPIResources().getGroupVersion(), job.getApiVersion(), job.getKind(), "jobs", true, V1Job.class); | ||
|
||
KubectlDelete<V1Job> kubectlDelete = Kubectl.delete(V1Job.class); | ||
kubectlDelete.apiClient(apiClient); | ||
kubectlDelete.namespace("foo").name("bar"); | ||
kubectlDelete.execute(); | ||
|
||
assertThrows(KubectlException.class, () -> { | ||
KubectlDelete<V1Job> kubectlDelete2 = Kubectl.delete(V1Job.class); | ||
kubectlDelete2.apiClient(apiClient); | ||
kubectlDelete2.namespace("foo").name("bar"); | ||
kubectlDelete2.execute(); | ||
}); | ||
|
||
KubectlDelete<V1Job> kubectlDelete2 = Kubectl.delete(V1Job.class); | ||
kubectlDelete2.apiClient(apiClient); | ||
kubectlDelete2.namespace("foo").name("bar").ignoreNotFound(true); | ||
kubectlDelete2.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{ | ||
"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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"kind": "APIResourceList", | ||
"apiVersion": "v1", | ||
"groupVersion": "batch/v1", | ||
"resources": [ | ||
{ | ||
"name": "cronjobs", | ||
"singularName": "cronjob", | ||
"namespaced": true, | ||
"kind": "CronJob", | ||
"verbs": [ | ||
"create", | ||
"delete", | ||
"deletecollection", | ||
"get", | ||
"list", | ||
"patch", | ||
"update", | ||
"watch" | ||
], | ||
"shortNames": [ | ||
"cj" | ||
], | ||
"categories": [ | ||
"all" | ||
], | ||
"storageVersionHash": "sd5LIXh4Fjs=" | ||
}, | ||
{ | ||
"name": "cronjobs/status", | ||
"singularName": "", | ||
"namespaced": true, | ||
"kind": "CronJob", | ||
"verbs": [ | ||
"get", | ||
"patch", | ||
"update" | ||
] | ||
}, | ||
{ | ||
"name": "jobs", | ||
"singularName": "job", | ||
"namespaced": true, | ||
"kind": "Job", | ||
"verbs": [ | ||
"create", | ||
"delete", | ||
"deletecollection", | ||
"get", | ||
"list", | ||
"patch", | ||
"update", | ||
"watch" | ||
], | ||
"categories": [ | ||
"all" | ||
], | ||
"storageVersionHash": "mudhfqk/qZY=" | ||
}, | ||
{ | ||
"name": "jobs/status", | ||
"singularName": "", | ||
"namespaced": true, | ||
"kind": "Job", | ||
"verbs": [ | ||
"get", | ||
"patch", | ||
"update" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"kind": "Status", | ||
"apiVersion": "v1", | ||
"metadata": {}, | ||
"status": "Failure", | ||
"message": "jobs.batch \"bar\" not found", | ||
"reason": "NotFound", | ||
"details": { | ||
"name": "bar", | ||
"group": "batch", | ||
"kind": "jobs" | ||
}, | ||
"code": 404 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use
getResourceAsStream()
and load this into a byte array instead of theFile
stuff, I find it confusing to read.