X Tutup
Skip to content

Commit 5ff2209

Browse files
committed
test : test DeploymentHelper should work
1 parent f7d40c2 commit 5ff2209

File tree

3 files changed

+1024
-0
lines changed

3 files changed

+1024
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Copyright 2021 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.util.deployment;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
18+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
19+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
20+
21+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
22+
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
23+
import io.kubernetes.client.openapi.ApiClient;
24+
import io.kubernetes.client.openapi.ApiException;
25+
import io.kubernetes.client.openapi.JSON;
26+
import io.kubernetes.client.openapi.apis.AppsV1Api;
27+
import io.kubernetes.client.openapi.models.V1Deployment;
28+
import io.kubernetes.client.openapi.models.V1ReplicaSet;
29+
import io.kubernetes.client.openapi.models.V1ReplicaSetList;
30+
import io.kubernetes.client.util.ClientBuilder;
31+
import java.io.IOException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Paths;
34+
import java.util.ArrayList;
35+
import java.util.Arrays;
36+
import java.util.List;
37+
import org.junit.Assert;
38+
import org.junit.Before;
39+
import org.junit.Rule;
40+
import org.junit.Test;
41+
42+
public class DeploymentHelperTest {
43+
44+
private ApiClient apiClient;
45+
46+
@Rule public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort(), false);
47+
48+
private static final String DEPLOYMENT =
49+
DeploymentHelperTest.class.getClassLoader().getResource("deployment.json").getPath();
50+
51+
private static final String REPLICASET_LIST =
52+
DeploymentHelperTest.class.getClassLoader().getResource("replicaset-list.json").getPath();
53+
54+
@Before
55+
public void setup() throws IOException {
56+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
57+
}
58+
59+
@Test
60+
public void testGetAllReplicaSetsShouldWork() throws IOException, ApiException {
61+
wireMockRule.stubFor(
62+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))
63+
.willReturn(
64+
aResponse()
65+
.withStatus(200)
66+
.withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
67+
AppsV1Api api = new AppsV1Api(this.apiClient);
68+
69+
V1Deployment deployment =
70+
new JSON()
71+
.deserialize(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))), V1Deployment.class);
72+
List<V1ReplicaSet> oldRSes = new ArrayList<>();
73+
List<V1ReplicaSet> allOldRSes = new ArrayList<>();
74+
V1ReplicaSet newRs = DeploymentHelper.getAllReplicaSets(deployment, api, oldRSes, allOldRSes);
75+
wireMockRule.verify(
76+
1,
77+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")))
78+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
79+
Assert.assertNotNull(newRs);
80+
Assert.assertEquals(1, oldRSes.size());
81+
Assert.assertEquals(2, allOldRSes.size());
82+
}
83+
84+
@Test
85+
public void testRevisionShouldWork() throws IOException {
86+
V1ReplicaSetList replicaSetList =
87+
new JSON()
88+
.deserialize(
89+
new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))), V1ReplicaSetList.class);
90+
List<Long> revisions = new ArrayList<>();
91+
for (V1ReplicaSet rs : replicaSetList.getItems()) {
92+
revisions.add(DeploymentHelper.revision(rs.getMetadata()));
93+
}
94+
revisions.sort(Long::compareTo);
95+
List<Long> exceptRevisions = Arrays.asList(1L, 2L, 2L, 3L, 4L);
96+
Assert.assertEquals(exceptRevisions, revisions);
97+
}
98+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
{
2+
"apiVersion": "apps/v1",
3+
"kind": "Deployment",
4+
"metadata": {
5+
"annotations": {
6+
"deployment.kubernetes.io/revision": "3",
7+
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{\"kubernetes.io/change-cause\":\"kubectl.exe apply --filename=deployment.yaml --record=true\"},\"name\":\"foo\",\"namespace\":\"default\"},\"spec\":{\"replicas\":3,\"selector\":{\"matchLabels\":{\"app\":\"busybox-v1\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"busybox-v1\"}},\"spec\":{\"containers\":[{\"args\":[\"1000\"],\"command\":[\"sleep\"],\"image\":\"busybox:1.31.1\",\"name\":\"busybox-host\"}]}}}}\n",
8+
"kubernetes.io/change-cause": "kubectl.exe apply --filename=deployment.yaml --record=true"
9+
},
10+
"creationTimestamp": "2021-08-04T02:09:32Z",
11+
"generation": 6,
12+
"managedFields": [
13+
{
14+
"apiVersion": "apps/v1",
15+
"fieldsType": "FieldsV1",
16+
"fieldsV1": {
17+
"f:spec": {
18+
"f:progressDeadlineSeconds": {},
19+
"f:replicas": {},
20+
"f:revisionHistoryLimit": {},
21+
"f:selector": {},
22+
"f:strategy": {
23+
"f:rollingUpdate": {
24+
".": {},
25+
"f:maxSurge": {},
26+
"f:maxUnavailable": {}
27+
},
28+
"f:type": {}
29+
},
30+
"f:template": {
31+
"f:metadata": {
32+
"f:labels": {
33+
".": {},
34+
"f:app": {}
35+
}
36+
},
37+
"f:spec": {
38+
"f:containers": {
39+
"k:{\"name\":\"busybox-host\"}": {
40+
".": {},
41+
"f:args": {},
42+
"f:command": {},
43+
"f:image": {},
44+
"f:imagePullPolicy": {},
45+
"f:name": {},
46+
"f:resources": {},
47+
"f:terminationMessagePath": {},
48+
"f:terminationMessagePolicy": {}
49+
}
50+
},
51+
"f:dnsPolicy": {},
52+
"f:restartPolicy": {},
53+
"f:schedulerName": {},
54+
"f:securityContext": {},
55+
"f:terminationGracePeriodSeconds": {}
56+
}
57+
}
58+
}
59+
},
60+
"manager": "kubectl-create",
61+
"operation": "Update",
62+
"time": "2021-08-04T02:09:32Z"
63+
},
64+
{
65+
"apiVersion": "apps/v1",
66+
"fieldsType": "FieldsV1",
67+
"fieldsV1": {
68+
"f:metadata": {
69+
"f:annotations": {
70+
"f:kubectl.kubernetes.io/last-applied-configuration": {},
71+
"f:kubernetes.io/change-cause": {}
72+
}
73+
}
74+
},
75+
"manager": "kubectl-client-side-apply",
76+
"operation": "Update",
77+
"time": "2021-08-04T02:39:54Z"
78+
},
79+
{
80+
"apiVersion": "apps/v1",
81+
"fieldsType": "FieldsV1",
82+
"fieldsV1": {
83+
"f:metadata": {
84+
"f:annotations": {
85+
".": {},
86+
"f:deployment.kubernetes.io/revision": {}
87+
}
88+
},
89+
"f:status": {
90+
"f:availableReplicas": {},
91+
"f:conditions": {
92+
".": {},
93+
"k:{\"type\":\"Available\"}": {
94+
".": {},
95+
"f:lastTransitionTime": {},
96+
"f:lastUpdateTime": {},
97+
"f:message": {},
98+
"f:reason": {},
99+
"f:status": {},
100+
"f:type": {}
101+
},
102+
"k:{\"type\":\"Progressing\"}": {
103+
".": {},
104+
"f:lastTransitionTime": {},
105+
"f:lastUpdateTime": {},
106+
"f:message": {},
107+
"f:reason": {},
108+
"f:status": {},
109+
"f:type": {}
110+
}
111+
},
112+
"f:observedGeneration": {},
113+
"f:readyReplicas": {},
114+
"f:replicas": {},
115+
"f:updatedReplicas": {}
116+
}
117+
},
118+
"manager": "kube-controller-manager",
119+
"operation": "Update",
120+
"time": "2021-08-04T03:03Z"
121+
}
122+
],
123+
"name": "foo",
124+
"namespace": "default",
125+
"resourceVersion": "51675",
126+
"uid": "3e3ed62b-b868-45a7-aabb-462d421aa4ad"
127+
},
128+
"spec": {
129+
"progressDeadlineSeconds": 600,
130+
"replicas": 3,
131+
"revisionHistoryLimit": 10,
132+
"selector": {
133+
"matchLabels": {
134+
"app": "bar"
135+
}
136+
},
137+
"strategy": {
138+
"type": "RollingUpdate",
139+
"rollingUpdate": {
140+
"maxSurge": "25%",
141+
"maxUnavailable": "25%"
142+
}
143+
},
144+
"template": {
145+
"metadata": {
146+
"labels": {
147+
"app": "bar"
148+
}
149+
},
150+
"spec": {
151+
"containers": [
152+
{
153+
"args": [
154+
"1000"
155+
],
156+
"command": [
157+
"sleep"
158+
],
159+
"image": "busybox:1.31.1",
160+
"imagePullPolicy": "IfNotPresent",
161+
"name": "busybox-host",
162+
"resources": {},
163+
"terminationMessagePath": "/dev/termination-log",
164+
"terminationMessagePolicy": "File"
165+
}
166+
],
167+
"dnsPolicy": "ClusterFirst",
168+
"restartPolicy": "Always",
169+
"schedulerName": "default-scheduler",
170+
"securityContext": {},
171+
"terminationGracePeriodSeconds": 30
172+
}
173+
}
174+
},
175+
"status": {
176+
"availableReplicas": 3,
177+
"conditions": [
178+
{
179+
"type": "Progressing",
180+
"lastTransitionTime": "2021-08-04T02:24:02Z",
181+
"lastUpdateTime": "2021-08-04T02:29:38Z",
182+
"message": "ReplicaSet \"foo-64dff5fd7c\" has successfully progressed.",
183+
"reason": "NewReplicaSetAvailable",
184+
"status": "True"
185+
},
186+
{
187+
"type": "Available",
188+
"lastTransitionTime": "2021-08-04T03:03Z",
189+
"lastUpdateTime": "2021-08-04T03:03Z",
190+
"message": "Deployment has minimum availability.",
191+
"reason": "MinimumReplicasAvailable",
192+
"status": "True"
193+
}
194+
],
195+
"observedGeneration": 6,
196+
"readyReplicas": 3,
197+
"replicas": 3,
198+
"updatedReplicas": 3
199+
}
200+
}

0 commit comments

Comments
 (0)
X Tutup