Skip to content

Commit 1fc1f4f

Browse files
committed
test : kubectl rollout history for stafefulset
1 parent d6103da commit 1fc1f4f

File tree

4 files changed

+944
-0
lines changed

4 files changed

+944
-0
lines changed

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlRolloutTest.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import io.kubernetes.client.openapi.models.V1DaemonSetList;
3030
import io.kubernetes.client.openapi.models.V1Deployment;
3131
import io.kubernetes.client.openapi.models.V1DeploymentList;
32+
import io.kubernetes.client.openapi.models.V1PodTemplate;
3233
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
34+
import io.kubernetes.client.openapi.models.V1StatefulSet;
35+
import io.kubernetes.client.openapi.models.V1StatefulSetList;
3336
import io.kubernetes.client.util.ClientBuilder;
3437
import io.kubernetes.client.util.ModelMapper;
3538
import java.io.IOException;
@@ -65,6 +68,18 @@ public class KubectlRolloutTest {
6568
.getResource("daemonset-controllerrevision-list.json")
6669
.getPath();
6770

71+
private static final String STATEFUL_SET =
72+
KubectlRolloutTest.class.getClassLoader().getResource("statefulset.json").getPath();
73+
74+
private static final String PATCHED_STATEFUL_SET =
75+
KubectlRolloutTest.class.getClassLoader().getResource("patched-statefulset.json").getPath();
76+
77+
private static final String STATEFUL_SET_CONTROLLER_REVISION_LIST =
78+
KubectlRolloutTest.class
79+
.getClassLoader()
80+
.getResource("statefulset-controllerrevision-list.json")
81+
.getPath();
82+
6883
@Before
6984
public void setup() throws IOException {
7085
ModelMapper.addModelMap(
@@ -77,6 +92,14 @@ public void setup() throws IOException {
7792
V1DeploymentList.class);
7893
ModelMapper.addModelMap(
7994
"apps", "v1", "DaemonSet", "daemonsets", true, V1DaemonSet.class, V1DaemonSetList.class);
95+
ModelMapper.addModelMap(
96+
"apps",
97+
"v1",
98+
"StatefulSet",
99+
"statefulsets",
100+
true,
101+
V1StatefulSet.class,
102+
V1StatefulSetList.class);
80103
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
81104
}
82105

@@ -223,6 +246,87 @@ public void testKubectlRolloutHistoryDaemonSetWithRevisionShouldWork()
223246
Assert.assertNotNull(template);
224247
}
225248

249+
@Test
250+
public void testKubectlRolloutHistoryStatefulSetShouldWork()
251+
throws KubectlException, IOException {
252+
wireMockRule.stubFor(
253+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
254+
.willReturn(
255+
aResponse()
256+
.withStatus(200)
257+
.withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
258+
wireMockRule.stubFor(
259+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
260+
.willReturn(
261+
aResponse()
262+
.withStatus(200)
263+
.withBody(
264+
new String(
265+
Files.readAllBytes(
266+
Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
267+
List<History> histories =
268+
Kubectl.rollout(V1StatefulSet.class)
269+
.history()
270+
.apiClient(apiClient)
271+
.name("foo")
272+
.namespace("default")
273+
.skipDiscovery()
274+
.execute();
275+
wireMockRule.verify(
276+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
277+
wireMockRule.verify(
278+
1,
279+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
280+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
281+
Assert.assertEquals(3, histories.size());
282+
}
283+
284+
@Test
285+
public void testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork()
286+
throws KubectlException, IOException {
287+
wireMockRule.stubFor(
288+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
289+
.willReturn(
290+
aResponse()
291+
.withStatus(200)
292+
.withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
293+
wireMockRule.stubFor(
294+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
295+
.willReturn(
296+
aResponse()
297+
.withStatus(200)
298+
.withBody(
299+
new String(
300+
Files.readAllBytes(
301+
Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
302+
wireMockRule.stubFor(
303+
patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
304+
.willReturn(
305+
aResponse()
306+
.withStatus(200)
307+
.withBody(new String(Files.readAllBytes(Paths.get(PATCHED_STATEFUL_SET))))));
308+
V1PodTemplateSpec template =
309+
Kubectl.rollout(V1StatefulSet.class)
310+
.history()
311+
.apiClient(apiClient)
312+
.name("foo")
313+
.namespace("default")
314+
.revision(2)
315+
.skipDiscovery()
316+
.execute();
317+
wireMockRule.verify(
318+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
319+
wireMockRule.verify(
320+
1,
321+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
322+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
323+
wireMockRule.verify(
324+
1,
325+
patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")))
326+
.withQueryParam("dryRun", new EqualToPattern("All")));
327+
Assert.assertNotNull(template);
328+
}
329+
226330
@Test
227331
public void testKubectlRolloutHistoryWithInvalidRevisionShouldThrow() throws IOException {
228332
wireMockRule.stubFor(
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"apiVersion": "apps/v1",
3+
"kind": "StatefulSet",
4+
"metadata": {
5+
"annotations": {
6+
"kubernetes.io/change-cause": "kubectl.exe create --filename\u003dstatefulset.yaml --record\u003dtrue"
7+
},
8+
"creationTimestamp": "2021-08-13T09:44:24.000000Z",
9+
"generation": 4,
10+
"managedFields": [{
11+
"apiVersion": "apps/v1",
12+
"fieldsType": "FieldsV1",
13+
"fieldsV1": {
14+
"f:metadata": {
15+
"f:annotations": {
16+
".": {},
17+
"f:kubernetes.io/change-cause": {}
18+
}
19+
},
20+
"f:spec": {
21+
"f:podManagementPolicy": {},
22+
"f:replicas": {},
23+
"f:revisionHistoryLimit": {},
24+
"f:selector": {},
25+
"f:serviceName": {},
26+
"f:template": {
27+
"f:metadata": {
28+
"f:labels": {
29+
".": {},
30+
"f:app": {}
31+
}
32+
},
33+
"f:spec": {
34+
"f:containers": {
35+
"k:{\"name\":\"busybox-host\"}": {
36+
".": {},
37+
"f:args": {},
38+
"f:command": {},
39+
"f:image": {},
40+
"f:imagePullPolicy": {},
41+
"f:name": {},
42+
"f:resources": {},
43+
"f:terminationMessagePath": {},
44+
"f:terminationMessagePolicy": {}
45+
}
46+
},
47+
"f:dnsPolicy": {},
48+
"f:restartPolicy": {},
49+
"f:schedulerName": {},
50+
"f:securityContext": {},
51+
"f:terminationGracePeriodSeconds": {}
52+
}
53+
},
54+
"f:updateStrategy": {
55+
"f:rollingUpdate": {
56+
".": {},
57+
"f:partition": {}
58+
},
59+
"f:type": {}
60+
}
61+
}
62+
},
63+
"manager": "kubectl-create",
64+
"operation": "Update",
65+
"time": "2021-08-13T09:44:24.000000Z"
66+
}, {
67+
"apiVersion": "apps/v1",
68+
"fieldsType": "FieldsV1",
69+
"fieldsV1": {
70+
"f:spec": {
71+
"f:template": {
72+
"f:metadata": {
73+
"f:annotations": {}
74+
}
75+
}
76+
}
77+
},
78+
"manager": "kubectl-rollout",
79+
"operation": "Update",
80+
"time": "2021-08-13T09:47:22.000000Z"
81+
}, {
82+
"apiVersion": "apps/v1",
83+
"fieldsType": "FieldsV1",
84+
"fieldsV1": {
85+
"f:spec": {
86+
"f:template": {
87+
"f:metadata": {
88+
"f:annotations": {
89+
"f:kubectl.kubernetes.io/restartedAt": {}
90+
}
91+
}
92+
}
93+
}
94+
},
95+
"manager": "Kubernetes Java Client",
96+
"operation": "Update",
97+
"time": "2021-08-17T11:13:42.000000Z"
98+
}],
99+
"name": "foo",
100+
"namespace": "default",
101+
"resourceVersion": "103707",
102+
"uid": "9a968c95-4b78-4b72-baf9-fa18bab00155"
103+
},
104+
"spec": {
105+
"podManagementPolicy": "OrderedReady",
106+
"replicas": 2,
107+
"revisionHistoryLimit": 10,
108+
"selector": {
109+
"matchLabels": {
110+
"app": "bar"
111+
}
112+
},
113+
"serviceName": "busybox-service\"",
114+
"template": {
115+
"metadata": {
116+
"annotations": {
117+
"kubectl.kubernetes.io/restartedAt": "2021-08-13T17:47:22+08:00"
118+
},
119+
"labels": {
120+
"app": "bar"
121+
}
122+
},
123+
"spec": {
124+
"containers": [{
125+
"args": ["1000"],
126+
"command": ["sleep"],
127+
"image": "busybox:1.31.1",
128+
"imagePullPolicy": "IfNotPresent",
129+
"name": "busybox-host",
130+
"resources": {},
131+
"terminationMessagePath": "/dev/termination-log",
132+
"terminationMessagePolicy": "File"
133+
}],
134+
"dnsPolicy": "ClusterFirst",
135+
"restartPolicy": "Always",
136+
"schedulerName": "default-scheduler",
137+
"securityContext": {},
138+
"terminationGracePeriodSeconds": 30
139+
}
140+
},
141+
"updateStrategy": {
142+
"rollingUpdate": {
143+
"partition": 0
144+
},
145+
"type": "RollingUpdate"
146+
}
147+
},
148+
"status": {
149+
"replicas": 0
150+
}
151+
}

0 commit comments

Comments
 (0)