Skip to content

Commit ade222f

Browse files
committed
test : kubectl rollout history for daemonset
1 parent 3b1c4c4 commit ade222f

File tree

4 files changed

+864
-0
lines changed

4 files changed

+864
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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;
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.patch;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
21+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
22+
import static org.junit.Assert.assertThrows;
23+
24+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
25+
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
26+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
27+
import io.kubernetes.client.openapi.ApiClient;
28+
import io.kubernetes.client.openapi.models.V1DaemonSet;
29+
import io.kubernetes.client.openapi.models.V1DaemonSetList;
30+
import io.kubernetes.client.openapi.models.V1Deployment;
31+
import io.kubernetes.client.openapi.models.V1DeploymentList;
32+
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
33+
import io.kubernetes.client.util.ClientBuilder;
34+
import io.kubernetes.client.util.ModelMapper;
35+
import java.io.IOException;
36+
import java.nio.file.Files;
37+
import java.nio.file.Paths;
38+
import java.util.List;
39+
import org.junit.Assert;
40+
import org.junit.Before;
41+
import org.junit.Rule;
42+
import org.junit.Test;
43+
44+
public class KubectlRolloutTest {
45+
46+
private ApiClient apiClient;
47+
48+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
49+
50+
private static final String DEPLOYMENT =
51+
KubectlRolloutTest.class.getClassLoader().getResource("deployment.json").getPath();
52+
53+
private static final String REPLICASET_LIST =
54+
KubectlRolloutTest.class.getClassLoader().getResource("replicaset-list.json").getPath();
55+
56+
private static final String DAEMON_SET =
57+
KubectlRolloutTest.class.getClassLoader().getResource("daemonset.json").getPath();
58+
59+
private static final String PATCHED_DAEMON_SET =
60+
KubectlRolloutTest.class.getClassLoader().getResource("patched-daemonset.json").getPath();
61+
62+
private static final String DAEMON_SET_CONTROLLER_REVISION_LIST =
63+
KubectlRolloutTest.class
64+
.getClassLoader()
65+
.getResource("daemonset-controllerrevision-list.json")
66+
.getPath();
67+
68+
@Before
69+
public void setup() throws IOException {
70+
ModelMapper.addModelMap(
71+
"apps",
72+
"v1",
73+
"Deployment",
74+
"deployments",
75+
true,
76+
V1Deployment.class,
77+
V1DeploymentList.class);
78+
ModelMapper.addModelMap(
79+
"apps", "v1", "DaemonSet", "daemonsets", true, V1DaemonSet.class, V1DaemonSetList.class);
80+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
81+
}
82+
83+
@Test
84+
public void testKubectlRolloutHistoryDeploymentShouldWork() throws KubectlException, IOException {
85+
wireMockRule.stubFor(
86+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))
87+
.willReturn(
88+
aResponse()
89+
.withStatus(200)
90+
.withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))))));
91+
wireMockRule.stubFor(
92+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))
93+
.willReturn(
94+
aResponse()
95+
.withStatus(200)
96+
.withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
97+
List<History> histories =
98+
Kubectl.rollout(V1Deployment.class)
99+
.history()
100+
.apiClient(apiClient)
101+
.name("foo")
102+
.namespace("default")
103+
.skipDiscovery()
104+
.execute();
105+
106+
wireMockRule.verify(
107+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))));
108+
wireMockRule.verify(
109+
1,
110+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")))
111+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
112+
Assert.assertEquals(3, histories.size());
113+
}
114+
115+
@Test
116+
public void testKubectlRolloutHistoryDeploymentWithRevisionShouldWork()
117+
throws KubectlException, IOException {
118+
wireMockRule.stubFor(
119+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))
120+
.willReturn(
121+
aResponse()
122+
.withStatus(200)
123+
.withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))))));
124+
wireMockRule.stubFor(
125+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))
126+
.willReturn(
127+
aResponse()
128+
.withStatus(200)
129+
.withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
130+
V1PodTemplateSpec template =
131+
Kubectl.rollout(V1Deployment.class)
132+
.history()
133+
.apiClient(apiClient)
134+
.name("foo")
135+
.namespace("default")
136+
.revision(3)
137+
.skipDiscovery()
138+
.execute();
139+
wireMockRule.verify(
140+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))));
141+
wireMockRule.verify(
142+
1,
143+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")))
144+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
145+
Assert.assertNotNull(template);
146+
}
147+
148+
@Test
149+
public void testKubectlRolloutHistoryDaemonSetShouldWork() throws KubectlException, IOException {
150+
wireMockRule.stubFor(
151+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))
152+
.willReturn(
153+
aResponse()
154+
.withStatus(200)
155+
.withBody(new String(Files.readAllBytes(Paths.get(DAEMON_SET))))));
156+
wireMockRule.stubFor(
157+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
158+
.willReturn(
159+
aResponse()
160+
.withStatus(200)
161+
.withBody(
162+
new String(
163+
Files.readAllBytes(Paths.get(DAEMON_SET_CONTROLLER_REVISION_LIST))))));
164+
List<History> histories =
165+
Kubectl.rollout(V1DaemonSet.class)
166+
.history()
167+
.apiClient(apiClient)
168+
.name("foo")
169+
.namespace("default")
170+
.skipDiscovery()
171+
.execute();
172+
wireMockRule.verify(
173+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))));
174+
wireMockRule.verify(
175+
1,
176+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
177+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
178+
Assert.assertEquals(3, histories.size());
179+
}
180+
181+
@Test
182+
public void testKubectlRolloutHistoryDaemonSetWithRevisionShouldWork()
183+
throws KubectlException, IOException {
184+
wireMockRule.stubFor(
185+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))
186+
.willReturn(
187+
aResponse()
188+
.withStatus(200)
189+
.withBody(new String(Files.readAllBytes(Paths.get(DAEMON_SET))))));
190+
wireMockRule.stubFor(
191+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
192+
.willReturn(
193+
aResponse()
194+
.withStatus(200)
195+
.withBody(
196+
new String(
197+
Files.readAllBytes(Paths.get(DAEMON_SET_CONTROLLER_REVISION_LIST))))));
198+
wireMockRule.stubFor(
199+
patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))
200+
.willReturn(
201+
aResponse()
202+
.withStatus(200)
203+
.withBody(new String(Files.readAllBytes(Paths.get(PATCHED_DAEMON_SET))))));
204+
V1PodTemplateSpec template =
205+
Kubectl.rollout(V1DaemonSet.class)
206+
.history()
207+
.apiClient(apiClient)
208+
.name("foo")
209+
.namespace("default")
210+
.revision(2)
211+
.skipDiscovery()
212+
.execute();
213+
wireMockRule.verify(
214+
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))));
215+
wireMockRule.verify(
216+
1,
217+
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
218+
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
219+
wireMockRule.verify(
220+
1,
221+
patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo")))
222+
.withQueryParam("dryRun", new EqualToPattern("All")));
223+
Assert.assertNotNull(template);
224+
}
225+
226+
@Test
227+
public void testKubectlRolloutHistoryWithInvalidRevisionShouldThrow() throws IOException {
228+
wireMockRule.stubFor(
229+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))
230+
.willReturn(
231+
aResponse()
232+
.withStatus(200)
233+
.withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))))));
234+
wireMockRule.stubFor(
235+
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))
236+
.willReturn(
237+
aResponse()
238+
.withStatus(200)
239+
.withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
240+
241+
assertThrows(
242+
KubectlException.class,
243+
() ->
244+
Kubectl.rollout(V1Deployment.class)
245+
.history()
246+
.apiClient(apiClient)
247+
.name("foo")
248+
.namespace("default")
249+
.revision(999)
250+
.skipDiscovery()
251+
.execute());
252+
}
253+
}

0 commit comments

Comments
 (0)