forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeploymentTest.java
374 lines (315 loc) · 13.7 KB
/
DeploymentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* 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.fabric8.kubernetes.client.mock;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.api.model.apps.DeploymentListBuilder;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder;
import io.fabric8.kubernetes.api.model.apps.ReplicaSetListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.Rule;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class DeploymentTest {
@Rule
public KubernetesServer server = new KubernetesServer();
@Test
public void testList() {
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments").andReturn(200, new DeploymentListBuilder().build()).once();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments").andReturn(200, new DeploymentListBuilder()
.addNewItem().and()
.addNewItem().and().build()).once();
server.expect().withPath("/apis/apps/v1/deployments").andReturn(200, new DeploymentListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem()
.and().build()).once();
KubernetesClient client = server.getClient();
DeploymentList deploymentList = client.apps().deployments().list();
assertNotNull(deploymentList);
assertEquals(0, deploymentList.getItems().size());
deploymentList = client.apps().deployments().inNamespace("ns1").list();
assertNotNull(deploymentList);
assertEquals(2, deploymentList.getItems().size());
deploymentList = client.apps().deployments().inAnyNamespace().list();
assertNotNull(deploymentList);
assertEquals(3, deploymentList.getItems().size());
}
@Test
public void testListWithLabels() {
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3")).andReturn(200, new DeploymentListBuilder().build()).always();
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2")).andReturn(200, new DeploymentListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem().and()
.build()).once();
KubernetesClient client = server.getClient();
DeploymentList deploymentList = client.apps().deployments()
.withLabel("key1", "value1")
.withLabel("key2", "value2")
.withLabel("key3", "value3")
.list();
assertNotNull(deploymentList);
assertEquals(0, deploymentList.getItems().size());
deploymentList = client.apps().deployments()
.withLabel("key1", "value1")
.withLabel("key2", "value2")
.list();
assertNotNull(deploymentList);
assertEquals(3, deploymentList.getItems().size());
}
@Test
public void testGet() {
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments/deployment1").andReturn(200, new DeploymentBuilder().build()).once();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment2").andReturn(200, new DeploymentBuilder().build()).once();
KubernetesClient client = server.getClient();
Deployment deployment = client.apps().deployments().withName("deployment1").get();
assertNotNull(deployment);
deployment = client.apps().deployments().withName("deployment2").get();
assertNull(deployment);
deployment = client.apps().deployments().inNamespace("ns1").withName("deployment2").get();
assertNotNull(deployment);
}
@Test
public void testDelete() {
Deployment deployment1 = new DeploymentBuilder().withNewMetadata()
.withName("deployment1")
.addToLabels("key1", "value1")
.withResourceVersion("1")
.withGeneration(1L)
.endMetadata()
.withNewSpec()
.withNewSelector()
.addToMatchLabels("key1", "value1")
.endSelector()
.withReplicas(0)
.endSpec()
.withNewStatus()
.withReplicas(1)
.withObservedGeneration(1l)
.endStatus()
.build();
ReplicaSet replicaSet1 = new ReplicaSetBuilder().withNewMetadata()
.withName("rs1")
.addToLabels("key1", "value1")
.withResourceVersion("1")
.withGeneration(1L)
.endMetadata()
.withNewSpec()
.withNewSelector()
.addToMatchLabels("key1", "value1")
.endSelector()
.withReplicas(0)
.endSpec()
.withNewStatus()
.withReplicas(1)
.withObservedGeneration(1l)
.endStatus()
.build();
Deployment deployment2 = new DeploymentBuilder().withNewMetadata()
.withName("deployment2")
.addToLabels("key2", "value2")
.withResourceVersion("1")
.withGeneration(1L)
.endMetadata()
.withNewSpec()
.withNewSelector()
.addToMatchLabels("key2", "value2")
.endSelector()
.withReplicas(0)
.endSpec()
.withNewStatus()
.withReplicas(1)
.withObservedGeneration(1l)
.endStatus()
.build();
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments/deployment1").andReturn(200, deployment1).once();
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments/deployment1").andReturn(200, new DeploymentBuilder(deployment1).editSpec().withReplicas(0).endSpec().build()).times(5);
server.expect().withPath("/apis/apps/v1/namespaces/test/replicasets?labelSelector=key1%3Dvalue1").andReturn(200, new ReplicaSetListBuilder().addToItems(replicaSet1).build()).once();
server.expect().withPath("/apis/apps/v1/namespaces/test/replicasets/rs1").andReturn(200, replicaSet1).once();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment2").andReturn(200, deployment2).once();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment2").andReturn(200, new DeploymentBuilder(deployment2).editSpec().withReplicas(0).endSpec().build()).times(5);
KubernetesClient client = server.getClient();
Boolean deleted = client.apps().deployments().withName("deployment1").delete();
assertNotNull(deleted);
deleted = client.apps().deployments().withName("deployment2").delete();
assertFalse(deleted);
deleted = client.apps().deployments().inNamespace("ns1").withName("deployment2").delete();
assertTrue(deleted);
}
@Test
public void testDeleteMulti() {
Deployment deployment1 = new DeploymentBuilder().withNewMetadata()
.withNamespace("test")
.withName("deployment1")
.withResourceVersion("1")
.withGeneration(2L)
.endMetadata()
.withNewSpec()
.withReplicas(0)
.endSpec()
.withNewStatus()
.withReplicas(1)
.withObservedGeneration(1l)
.endStatus()
.build();
Deployment deployment2 = new DeploymentBuilder().withNewMetadata()
.withNamespace("ns1")
.withName("deployment2")
.withResourceVersion("1")
.withGeneration(2L)
.endMetadata()
.withNewSpec()
.withReplicas(0)
.endSpec()
.withNewStatus()
.withReplicas(1)
.withObservedGeneration(1l)
.endStatus()
.build();
Deployment deployment3 = new DeploymentBuilder().withNewMetadata().withName("deployment3").withNamespace("any").and().build();
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments/deployment1").andReturn(200, deployment1).once();
server.expect().withPath("/apis/apps/v1/namespaces/test/deployments/deployment1").andReturn(200, new DeploymentBuilder(deployment1)
.editStatus()
.withReplicas(0)
.withObservedGeneration(2l)
.endStatus()
.build()).times(5);
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment2").andReturn(200, deployment2).once();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment2").andReturn(200, new DeploymentBuilder(deployment2)
.editStatus()
.withReplicas(0)
.withObservedGeneration(2l)
.endStatus()
.build()).times(5);
KubernetesClient client = server.getClient();
Boolean deleted = client.apps().deployments().inAnyNamespace().delete(deployment1, deployment2);
assertTrue(deleted);
deleted = client.apps().deployments().inAnyNamespace().delete(deployment3);
assertFalse(deleted);
}
@Test(expected = KubernetesClientException.class)
public void testDeleteWithNamespaceMismatch() {
Deployment deployment1 = new DeploymentBuilder().withNewMetadata().withName("deployment1").withNamespace("test").and().build();
KubernetesClient client = server.getClient();
Boolean deleted = client.apps().deployments().inNamespace("test1").delete(deployment1);
assertFalse(deleted);
}
@Test(expected = KubernetesClientException.class)
public void testCreateWithNameMismatch() {
Deployment deployment1 = new DeploymentBuilder().withNewMetadata().withName("deployment1").withNamespace("test").and().build();
Deployment deployment2 = new DeploymentBuilder().withNewMetadata().withName("deployment2").withNamespace("ns1").and().build();
KubernetesClient client = server.getClient();
client.apps().deployments().inNamespace("test1").withName("mydeployment1").create(deployment1);
}
@Test
public void testRollingUpdate() {
Deployment deployment = new DeploymentBuilder()
.withNewMetadata()
.withName("deployment1")
.withNamespace("ns1")
.endMetadata()
.withNewSpec()
.withReplicas(1)
.withNewSelector()
.withMatchLabels(Collections.singletonMap("service", "http-server"))
.endSelector()
.withNewStrategy()
.withType("RollingUpdate")
.withNewRollingUpdate()
.withNewMaxSurge().withIntVal(1).endMaxSurge()
.withNewMaxUnavailable().withIntVal(1).endMaxUnavailable()
.endRollingUpdate()
.endStrategy()
.withMinReadySeconds(5)
.withNewTemplate()
.withNewMetadata().withLabels(Collections.singletonMap("service", "http-server")).endMetadata()
.withNewSpec()
.addToContainers(new ContainerBuilder()
.withName("nginx")
.withImage("nginx:1.10.2")
.withImagePullPolicy("IfNotPresent")
.withPorts(Arrays.asList(new ContainerPortBuilder().withContainerPort(80).build()))
.build())
.endSpec()
.endTemplate()
.endSpec()
.build();
server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment1").andReturn(200, deployment).always();
server.expect().withPath("/api/v1/namespaces/ns1/pods?labelSelector=service%3Dhttp-server").andReturn(200, new KubernetesListBuilder().build()).once();
server.expect().post().withPath("/apis/apps/v1/namespaces/ns1/deployments").andReturn(201, deployment).times(2);
KubernetesClient client = server.getClient();
client.apps().deployments().inNamespace("ns1")
.withName("deployment1")
.rolling()
.withTimeout(5, TimeUnit.MINUTES)
.updateImage("");
}
@Test
public void testListFromServer() {
DeploymentBuilder deploymentBuilder = new DeploymentBuilder()
.withNewMetadata()
.withNamespace("test")
.withName("deployment1")
.endMetadata();
Deployment clientDeployment = deploymentBuilder.build();
Deployment serverDeployment = deploymentBuilder
.editMetadata()
.withResourceVersion("1")
.endMetadata()
.withNewStatus()
.addNewCondition()
.withType("Ready")
.withStatus("True")
.endCondition()
.endStatus()
.build();
server.expect()
.withPath("/apis/apps/v1/namespaces/test/deployments/deployment1")
.andReturn(200, serverDeployment).once();
KubernetesClient client = server.getClient();
List<HasMetadata> resources = client.resourceList(clientDeployment).fromServer().get();
assertNotNull(resources);
assertEquals(1, resources.size());
assertNotNull(resources.get(0));
assertTrue(resources.get(0) instanceof Deployment);
Deployment fromServerDeployment = (Deployment) resources.get(0);
assertNotNull(fromServerDeployment.getMetadata());
assertEquals("1", fromServerDeployment.getMetadata().getResourceVersion());
assertNotNull(fromServerDeployment.getStatus());
assertNotNull(fromServerDeployment.getStatus().getConditions());
assertEquals(1, fromServerDeployment.getStatus().getConditions().size());
assertEquals("Ready", fromServerDeployment.getStatus().getConditions().get(0).getType());
assertEquals("True", fromServerDeployment.getStatus().getConditions().get(0).getStatus());
}
}