forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceListTest.java
348 lines (296 loc) · 15.2 KB
/
ResourceListTest.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
/**
* 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.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.api.model.WatchEvent;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.KubernetesClientTimeoutException;
import io.fabric8.kubernetes.client.dsl.ListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.utils.Serialization;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import static java.net.HttpURLConnection.HTTP_CONFLICT;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_GONE;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnableKubernetesMockClient
public class ResourceListTest {
KubernetesMockServer server;
KubernetesClient client;
private Service service;
private Service updatedService;
private ConfigMap configMap;
private ConfigMap updatedConfigMap;
private KubernetesList resourcesToUpdate;
@BeforeEach
void setUp() {
service = mockService().build();
configMap = mockConfigMap().build();
updatedService = mockService().editSpec().editFirstPort()
.withTargetPort(new IntOrString(9999)).endPort().endSpec()
.build();
updatedConfigMap = mockConfigMap().addToData(Collections.singletonMap("foo", "bar")).build();
resourcesToUpdate = new KubernetesListBuilder()
.withItems(updatedService, updatedConfigMap).build();
}
@Test
void testCreateOrReplace() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HTTP_CREATED, pod1).once();
List<HasMetadata> response = client.resourceList(new PodListBuilder().addToItems(pod1).build()).createOrReplace();
assertTrue(response.contains(pod1));
}
@Test
void testCreateOrReplaceFailedCreate() {
// Given
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HTTP_UNAVAILABLE, pod1).once();
NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> listOp = client
.resourceList(new PodListBuilder().addToItems(pod1).build());
// When
assertThrows(KubernetesClientException.class, listOp::createOrReplace);
}
@Test
void testCreateWithExplicitNamespace() throws InterruptedException {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HTTP_CREATED, pod1).once();
client.resourceList(new PodListBuilder().addToItems(pod1).build()).inNamespace("ns1").createOrReplace();
Pod sent = Serialization.unmarshal(server.getLastRequest().getBody().inputStream());
// ensure the namespace was modified
assertEquals("ns1", sent.getMetadata().getNamespace());
}
@Test
void testDelete() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").withNamespace("ns1").and().build();
Pod pod3 = new PodBuilder().withNewMetadata().withName("pod3").withNamespace("any").and().build();
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(HTTP_OK, pod1).times(2);
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(HTTP_OK, pod2).times(2);
server.expect().withPath("/api/v1/namespaces/any/pods/pod3").andReturn(HTTP_OK, pod3).times(1);
//First time all items should be deleted.
Boolean deleted = client.resourceList(new PodListBuilder().withItems(pod1, pod2, pod3).build()).delete().size() == 3;
assertTrue(deleted);
//Now we expect pod3 to fail.
deleted = client.resourceList(new PodListBuilder().withItems(pod1, pod2, pod3).build()).delete().size() == 2;
assertTrue(deleted);
}
@Test
void testCreateOrReplaceWithoutDeleteExisting() throws Exception {
server.expect().post().withPath("/api/v1/namespaces/ns1/services").andReturn(HTTP_CONFLICT, service).once();
server.expect().post().withPath("/api/v1/namespaces/ns1/configmaps").andReturn(HTTP_CONFLICT, configMap).once();
// once for the ip and once for the resource version - this should be consolidated
server.expect().get().withPath("/api/v1/namespaces/ns1/services/my-service").andReturn(HTTP_OK, service).times(2);
server.expect().get().withPath("/api/v1/namespaces/ns1/configmaps/my-configmap").andReturn(HTTP_OK, configMap).once();
server.expect().put().withPath("/api/v1/namespaces/ns1/services/my-service").andReturn(HTTP_OK, updatedService).once();
server.expect().put().withPath("/api/v1/namespaces/ns1/configmaps/my-configmap").andReturn(HTTP_OK, updatedConfigMap)
.once();
client.resourceList(resourcesToUpdate).inNamespace("ns1").createOrReplace();
assertEquals(7, server.getRequestCount());
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/ns1/configmaps/my-configmap", request.getPath());
assertEquals("PUT", request.getMethod());
}
@Test
void testCreateOrReplaceWithDeleteExisting() throws Exception {
server.expect().delete().withPath("/api/v1/namespaces/ns1/services/my-service").andReturn(HTTP_OK, service).once();
server.expect().delete().withPath("/api/v1/namespaces/ns1/configmaps/my-configmap").andReturn(HTTP_OK, configMap).once();
server.expect().get().withPath("/api/v1/namespaces/ns1/services?fieldSelector=metadata.name%3Dmy-service")
.andReturn(HTTP_OK,
new KubernetesListBuilder().withNewMetadata().endMetadata().build())
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/configmaps?fieldSelector=metadata.name%3Dmy-configmap")
.andReturn(HTTP_OK,
new KubernetesListBuilder().withNewMetadata().endMetadata().build())
.once();
server.expect().post().withPath("/api/v1/namespaces/ns1/services").andReturn(HTTP_OK, updatedService).once();
server.expect().post().withPath("/api/v1/namespaces/ns1/configmaps").andReturn(HTTP_OK, updatedConfigMap).once();
ListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList = client.resourceList(resourcesToUpdate)
.inNamespace("ns1");
resourceList.delete();
resourceList.waitUntilCondition(Objects::isNull, 10, TimeUnit.SECONDS);
resourceList.createOrReplace();
assertEquals(6, server.getRequestCount());
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/ns1/configmaps", request.getPath());
assertEquals("POST", request.getMethod());
}
@Test
void testSuccessfulWaitUntilCondition() throws InterruptedException {
Pod pod1 = new PodBuilder().withNewMetadata()
.withName("pod1")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady1 = ResourceTest.createReadyFrom(pod1, "False", "2");
Pod ready1 = ResourceTest.createReadyFrom(pod1, "True", "3");
Pod pod2 = new PodBuilder().withNewMetadata()
.withName("pod2")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady2 = ResourceTest.createReadyFrom(pod2, "False", "4");
Pod ready2 = ResourceTest.createReadyFrom(pod2, "True", "5");
Predicate<HasMetadata> isReady = p -> "Pod".equals(p.getKind()) && ((Pod) p).getStatus().getConditions().stream()
.anyMatch(c -> "True".equals(c.getStatus()));
// The pods are never ready if you request them directly.
ResourceTest.list(server, noReady1);
ResourceTest.list(server, noReady2);
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(ready1, "MODIFIED"))
.done()
.once();
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod2&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(ready2, "MODIFIED"))
.done()
.once();
KubernetesList list = new KubernetesListBuilder().withItems(pod1, pod2).build();
List<HasMetadata> results = client.resourceList(list).inNamespace("ns1")
.waitUntilCondition(isReady, 10, SECONDS);
assertThat(results)
.containsExactlyInAnyOrder(ready1, ready2);
}
@Test
void testPartialSuccessfulWaitUntilCondition() {
Pod pod1 = new PodBuilder().withNewMetadata()
.withName("pod1")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady1 = ResourceTest.createReadyFrom(pod1, "False", "2");
Pod pod2 = new PodBuilder().withNewMetadata()
.withName("pod2")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady2 = ResourceTest.createReadyFrom(pod2, "False", "3");
Pod ready2 = ResourceTest.createReadyFrom(pod2, "True", "4");
Predicate<HasMetadata> isReady = p -> "Pod".equals(p.getKind()) && ((Pod) p).getStatus().getConditions().stream()
.anyMatch(c -> "True".equals(c.getStatus()));
// The pods are never ready if you request them directly.
ResourceTest.list(server, noReady1);
ResourceTest.list(server, noReady2);
Status gone = new StatusBuilder()
.withCode(HTTP_GONE)
.build();
// This pod has a non-retryable error.
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(gone, "ERROR"))
.done()
.once();
// This pod succeeds.
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod2&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(ready2, "MODIFIED"))
.done()
.once();
KubernetesList list = new KubernetesListBuilder().withItems(pod1, pod2).build();
final ListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> ops = client.resourceList(list).inNamespace("ns1");
KubernetesClientTimeoutException ex = assertThrows(KubernetesClientTimeoutException.class,
() -> ops.waitUntilCondition(isReady, 10, SECONDS));
assertThat(ex.getResourcesNotReady())
.containsExactly(pod1);
}
@Test
void testAllFailedWaitUntilCondition() {
Pod pod1 = new PodBuilder().withNewMetadata()
.withName("pod1")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady1 = ResourceTest.createReadyFrom(pod1, "False", "2");
Pod pod2 = new PodBuilder().withNewMetadata()
.withName("pod2")
.withResourceVersion("1")
.withNamespace("ns1").and().build();
Pod noReady2 = ResourceTest.createReadyFrom(pod2, "False", "3");
Predicate<HasMetadata> isReady = p -> "Pod".equals(p.getKind()) && ((Pod) p).getStatus().getConditions().stream()
.anyMatch(c -> "True".equals(c.getStatus()));
// The pods are never ready if you request them directly.
ResourceTest.list(server, noReady1);
ResourceTest.list(server, noReady2);
Status gone = new StatusBuilder()
.withCode(HTTP_GONE)
.build();
// Both pods have a non-retryable error.
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(gone, "ERROR"))
.done()
.once();
server.expect().get().withPath(
"/api/v1/namespaces/ns1/pods?fieldSelector=metadata.name%3Dpod2&resourceVersion=1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(500).andEmit(new WatchEvent(gone, "ERROR"))
.done()
.once();
KubernetesList list = new KubernetesListBuilder().withItems(pod1, pod2).build();
final ListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> ops = client.resourceList(list).inNamespace("ns1");
KubernetesClientTimeoutException ex = assertThrows(KubernetesClientTimeoutException.class,
() -> ops.waitUntilCondition(isReady, 10, SECONDS));
assertThat(ex.getResourcesNotReady())
.containsExactlyInAnyOrder(pod1, pod2);
}
private static ServiceBuilder mockService() {
return new ServiceBuilder()
.withNewMetadata().withName("my-service").endMetadata()
.withNewSpec()
.addToSelector("app", "my-app")
.addNewPort().withProtocol("TCP").withPort(80).withTargetPort(new IntOrString(9376)).endPort()
.endSpec();
}
private static ConfigMapBuilder mockConfigMap() {
return new ConfigMapBuilder()
.withNewMetadata().withName("my-configmap").endMetadata()
.addToData(Collections.singletonMap("app", "my-app"));
}
}