23
23
import io .kubernetes .client .extended .kubectl .exception .KubectlException ;
24
24
import io .kubernetes .client .openapi .ApiClient ;
25
25
import io .kubernetes .client .openapi .ApiException ;
26
- import io .kubernetes .client .openapi .JSON ;
27
26
import io .kubernetes .client .openapi .apis .BatchV1Api ;
28
27
import io .kubernetes .client .openapi .models .*;
29
28
import io .kubernetes .client .util .ClientBuilder ;
33
32
import java .nio .file .Files ;
34
33
import java .nio .file .Paths ;
35
34
36
- import io .kubernetes .client .util .generic .GenericKubernetesApi ;
37
35
import org .junit .Before ;
38
36
import org .junit .Rule ;
39
37
import org .junit .Test ;
@@ -54,32 +52,62 @@ public class KubectlDeleteTest {
54
52
.getPath ())
55
53
.toString ();
56
54
55
+
56
+ private static final String ADD_JOB =
57
+ new File (
58
+ KubectlDeleteTest .class
59
+ .getClassLoader ()
60
+ .getResource ("deleted-add-job.json" )
61
+ .getPath ())
62
+ .toString ();
63
+ private static final String GET_BATCH =
64
+ new File (
65
+ KubectlDeleteTest .class
66
+ .getClassLoader ()
67
+ .getResource ("deleted-get-batch.json" )
68
+ .getPath ())
69
+ .toString ();
70
+ private static final String DELETED_FIRST =
71
+ new File (
72
+ KubectlDeleteTest .class
73
+ .getClassLoader ()
74
+ .getResource ("deleted-first-time.json" )
75
+ .getPath ())
76
+ .toString ();
77
+ private static final String DELETED_SECOND =
78
+ new File (
79
+ KubectlDeleteTest .class
80
+ .getClassLoader ()
81
+ .getResource ("deleted-second-time.json" )
82
+ .getPath ())
83
+ .toString ();
84
+
57
85
private static final String DISCOVERY_APIS =
58
86
new File (KubectlDeleteTest .class .getClassLoader ().getResource ("discovery-apis.json" ).getPath ())
59
87
.toString ();
60
88
61
89
@ Rule public WireMockRule wireMockRule = new WireMockRule (wireMockConfig ().dynamicPort ());
62
90
63
91
@ Before
64
- public void setup () throws IOException {
92
+ public void setup () {
65
93
apiClient = new ClientBuilder ().setBasePath ("http://localhost:" + wireMockRule .port ()).build ();
66
94
}
67
95
68
96
@ Test
69
- public void testKubectlDelete () throws KubectlException , IOException , ApiException , InterruptedException {
97
+ public void testKubectlDelete () throws KubectlException , IOException , ApiException {
70
98
wireMockRule .stubFor (
71
99
post (urlPathEqualTo ("/apis/batch/v1/namespaces/foo/jobs" ))
72
100
.willReturn (
73
101
aResponse ()
74
102
.withStatus (201 )
75
- .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\" :{}}" )));
103
+ .withBody (new String (Files .readAllBytes (Paths .get (ADD_JOB ))))));
76
104
wireMockRule .stubFor (
77
105
delete (urlPathEqualTo ("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar" ))
78
106
.inScenario ("JobDeletionScenario" )
79
107
.whenScenarioStateIs (Scenario .STARTED )
80
108
.willReturn (aResponse ()
81
- .withStatus (200 )
82
- .withBody ("{\" kind\" :\" Job\" ,\" apiVersion\" :\" batch/v1\" ,\" metadata\" :{\" name\" :\" bar\" ,\" namespace\" :\" foo\" ,\" uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" ,\" resourceVersion\" :\" 82015\" ,\" generation\" :2,\" creationTimestamp\" :\" 2023-11-24T06:00:49Z\" ,\" deletionTimestamp\" :\" 2023-11-24T06:07:44Z\" ,\" deletionGracePeriodSeconds\" :0,\" labels\" :{\" batch.kubernetes.io/controller-uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" ,\" batch.kubernetes.io/job-name\" :\" bar\" ,\" controller-uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" ,\" job-name\" :\" bar\" },\" annotations\" :{\" batch.kubernetes.io/job-tracking\" :\" \" },\" finalizers\" :[\" orphan\" ],\" managedFields\" :[{\" manager\" :\" Kubernetes Java Client\" ,\" operation\" :\" Update\" ,\" apiVersion\" :\" batch/v1\" ,\" time\" :\" 2023-11-24T06:00:49Z\" ,\" 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\" :{}}}}}},{\" manager\" :\" kube-controller-manager\" ,\" operation\" :\" Update\" ,\" apiVersion\" :\" batch/v1\" ,\" time\" :\" 2023-11-24T06:00:53Z\" ,\" fieldsType\" :\" FieldsV1\" ,\" fieldsV1\" :{\" f:status\" :{\" f:completionTime\" :{},\" f:conditions\" :{},\" f:ready\" :{},\" f:startTime\" :{},\" f:succeeded\" :{},\" f:uncountedTerminatedPods\" :{}}},\" subresource\" :\" status\" }]},\" spec\" :{\" parallelism\" :1,\" completions\" :1,\" backoffLimit\" :6,\" selector\" :{\" matchLabels\" :{\" batch.kubernetes.io/controller-uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" }},\" template\" :{\" metadata\" :{\" creationTimestamp\" :null,\" labels\" :{\" batch.kubernetes.io/controller-uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" ,\" batch.kubernetes.io/job-name\" :\" bar\" ,\" controller-uid\" :\" b862e993-3828-4108-a38f-c19a602d9af6\" ,\" 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\" :{\" conditions\" :[{\" type\" :\" Complete\" ,\" status\" :\" True\" ,\" lastProbeTime\" :\" 2023-11-24T06:00:53Z\" ,\" lastTransitionTime\" :\" 2023-11-24T06:00:53Z\" }],\" startTime\" :\" 2023-11-24T06:00:49Z\" ,\" completionTime\" :\" 2023-11-24T06:00:53Z\" ,\" succeeded\" :1,\" uncountedTerminatedPods\" :{},\" ready\" :0}}\n " )
109
+ .withStatus (200 )
110
+ .withBody (new String (Files .readAllBytes (Paths .get (DELETED_FIRST ))))
83
111
)
84
112
.willSetStateTo ("SecondCall" )
85
113
);
@@ -90,7 +118,7 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
90
118
.whenScenarioStateIs ("SecondCall" )
91
119
.willReturn (aResponse ()
92
120
.withStatus (404 )
93
- .withBody ("{ \" kind \" : \" Status \" , \" apiVersion \" : \" v1 \" , \" metadata \" :{}, \" status \" : \" Failure \" , \" message \" : \" jobs.batch \\ \" bar \\ \" not found \" , \" reason \" : \" NotFound \" , \" details \" :{ \" name \" : \" bar \" , \" group \" : \" batch \" , \" kind \" : \" jobs \" }, \" code \" :404}" )
121
+ .withBody (new String ( Files . readAllBytes ( Paths . get ( DELETED_SECOND ))) )
94
122
)
95
123
);
96
124
@@ -117,7 +145,7 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
117
145
.willReturn (
118
146
aResponse ()
119
147
.withStatus (200 )
120
- .withBody ("{ \" 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 \" ]}]} \n " )));
148
+ .withBody (new String ( Files . readAllBytes ( Paths . get ( GET_BATCH ))) )));
121
149
122
150
V1JobSpec v1JobSpec = new V1JobSpec ()
123
151
.template (new V1PodTemplateSpec ()
0 commit comments