forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PodTest.java
595 lines (498 loc) · 20.8 KB
/
PodTest.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* 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.DeleteOptionsBuilder;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.WatchEvent;
import io.fabric8.kubernetes.api.model.policy.v1.EvictionBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.LocalPortForward;
import io.fabric8.kubernetes.client.PortForward;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.server.mock.OutputStreamMessage;
import io.fabric8.kubernetes.client.utils.Utils;
import okio.ByteString;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@EnableKubernetesMockClient
class PodTest {
KubernetesMockServer server;
@TempDir
Path tempDir;
private KubernetesClient client;
@BeforeEach
void setUp() {
client = server.createClient().inNamespace("test");
}
@Test
void testList() {
server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods").andReturn(200, new PodListBuilder()
.addNewItem().and()
.addNewItem().and().build()).once();
server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem()
.and().build()).once();
PodList podList = client.pods().list();
assertNotNull(podList);
assertEquals(0, podList.getItems().size());
podList = client.pods().inNamespace("ns1").list();
assertNotNull(podList);
assertEquals(2, podList.getItems().size());
podList = client.pods().inAnyNamespace().list();
assertNotNull(podList);
assertEquals(3, podList.getItems().size());
}
@Test
void testListWithLabels() {
server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3")).andReturn(200, new PodListBuilder().build()).always();
server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2")).andReturn(200, new PodListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem().and()
.build()).once();
PodList podList = client.pods()
.withLabel("key1", "value1")
.withLabel("key2","value2")
.withLabel("key3","value3")
.list();
assertNotNull(podList);
assertEquals(0, podList.getItems().size());
podList = client.pods()
.withLabel("key1", "value1")
.withLabel("key2","value2")
.list();
assertNotNull(podList);
assertEquals(3, podList.getItems().size());
}
@Test
void testListWithFields() {
server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3!=value3,key3!=value4")).andReturn(200, new PodListBuilder()
.addNewItem().and()
.addNewItem().and()
.build()).once();
PodList podList = client.pods()
.withField("key1", "value1")
.withField("key2","value2")
.withoutField("key3","value3")
.withoutField("key3", "value4")
.list();
assertNotNull(podList);
assertEquals(2, podList.getItems().size());
}
@Test
void testEditMissing() {
// Given
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(404, "error message from kubernetes").always();
// When
PodResource<Pod> podOp = client.pods().withName("pod1");
// Then
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.edit(p -> p));
}
@Test
void testDelete() {
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, new PodBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, new PodBuilder().build()).once();
Boolean deleted = client.pods().withName("pod1").delete();
assertTrue(deleted);
deleted = client.pods().withName("pod2").delete();
assertFalse(deleted);
deleted = client.pods().inNamespace("ns1").withName("pod2").cascading(false).delete();
assertTrue(deleted);
}
@Test
void testDeleteMulti() {
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(200, pod1).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, pod2).once();
Boolean deleted = client.pods().inAnyNamespace().delete(pod1, pod2);
assertTrue(deleted);
deleted = client.pods().inAnyNamespace().delete(pod3);
assertFalse(deleted);
}
@Test
void testDeleteWithNamespaceMismatch() {
// Given
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
// When + Then
NonNamespaceOperation<Pod, PodList, PodResource<Pod>> podOp = client.pods().inNamespace("test1");
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.delete(pod1));
}
@Test
void testDeleteWithPropagationPolicy() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once();
Boolean deleted = client.pods().inNamespace("test").withName("pod1").withPropagationPolicy(DeletionPropagation.FOREGROUND).delete();
assertTrue(deleted);
}
@Test
void testEvict() {
server.expect().withPath("/api/v1/namespaces/test/pods/pod1/eviction").andReturn(200, new PodBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2/eviction").andReturn(200, new PodBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod3/eviction").andReturn(PodOperationsImpl.HTTP_TOO_MANY_REQUESTS, new PodBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod3/eviction").andReturn(200, new PodBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod4/eviction").andReturn(500, new PodBuilder().build()).once();
Boolean deleted = client.pods().withName("pod1").evict();
assertTrue(deleted);
// not found
deleted = client.pods().withName("pod2").evict();
assertFalse(deleted);
deleted = client.pods().inNamespace("ns1").withName("pod2").evict();
assertTrue(deleted);
// too many requests
deleted = client.pods().inNamespace("ns1").withName("pod3").evict();
assertFalse(deleted);
deleted = client.pods().inNamespace("ns1").withName("pod3").evict();
assertTrue(deleted);
// unhandled error
PodResource<Pod> resource = client.pods().inNamespace("ns1").withName("pod4");
assertThrows(KubernetesClientException.class, resource::evict);
}
@Test
void testEvictWithPolicyV1Eviction() {
// Given
server.expect().post()
.withPath("/api/v1/namespaces/ns1/pods/foo/eviction")
.andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build())
.once();
// When
boolean evicted = client.pods().inNamespace("ns1").withName("foo").evict(new EvictionBuilder()
.withNewMetadata()
.withName("foo")
.withNamespace("ns1")
.endMetadata()
.withDeleteOptions(new DeleteOptionsBuilder().build())
.build());
// Then
assertTrue(evicted);
}
@Test
void testCreateWithNameMismatch() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
PodResource<Pod> podOp = client.pods().inNamespace("test1").withName("mypod1");
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.create(pod1));
}
@Test
void testGetLog() {
String pod1Log = "pod1Log";
String pod2Log = "pod2Log";
String pod3Log = "pod3Log";
String pod4Log = "pod4Log";
server.expect().withPath("/api/v1/namespaces/test/pods/pod1/log?pretty=true").andReturn(200, pod1Log).once();
server.expect().withPath("/api/v1/namespaces/test/pods/pod2/log?pretty=false").andReturn(200, pod2Log).once();
server.expect().withPath("/api/v1/namespaces/test/pods/pod3/log?pretty=false&container=cnt3").andReturn(200, pod3Log).once();
server.expect().withPath("/api/v1/namespaces/test4/pods/pod4/log?pretty=true&container=cnt4").andReturn(200, pod4Log).once();
server.expect().withPath("/api/v1/namespaces/test4/pods/pod1/log?pretty=false&limitBytes=100").andReturn(200, pod1Log).once();
server.expect().withPath("/api/v1/namespaces/test5/pods/pod1/log?pretty=false&tailLines=1×tamps=true").andReturn(200, pod1Log).once();
String log = client.pods().withName("pod1").getLog(true);
assertEquals(pod1Log, log);
log = client.pods().withName("pod2").getLog(false);
assertEquals(pod2Log, log);
log = client.pods().withName("pod3").inContainer("cnt3").getLog();
assertEquals(pod3Log, log);
log = client.pods().inNamespace("test4").withName("pod4").inContainer("cnt4").withPrettyOutput().getLog();
assertEquals(pod4Log, log);
log = client.pods().inNamespace("test4").withName("pod1").limitBytes(100).getLog();
assertEquals(pod1Log, log);
log = client.pods().inNamespace("test5").withName("pod1").usingTimestamps().tailingLines(1).getLog();
assertEquals(pod1Log, log);
}
@Test
void testExec() throws InterruptedException {
String expectedOutput = "file1 file2";
server.expect().withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&stdout=true")
.andUpgradeToWebSocket()
.open(new OutputStreamMessage(expectedOutput))
.done()
.always();
final CountDownLatch execLatch = new CountDownLatch(1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExecWatch watch = client.pods().withName("pod1").writingOutput(baos).usingListener(new ExecListener() {
@Override
public void onFailure(Throwable t, Response failureResponse) {
execLatch.countDown();
}
@Override
public void onClose(int code, String reason) {
execLatch.countDown();
}
}).exec("ls");
execLatch.await(10, TimeUnit.MINUTES);
assertNotNull(watch);
assertEquals(expectedOutput, baos.toString());
watch.close();
}
@Test
void testWatch() throws InterruptedException {
// Given
//We start with a list
Pod pod1 = new PodBuilder()
.withNewMetadata()
.withName("pod1")
.withResourceVersion("1")
.endMetadata()
.build();
server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder()
.withNewMetadata()
.withResourceVersion("1")
.endMetadata()
.addToItems(pod1)
.build()
).once();
server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(50).andEmit(new WatchEvent(pod1, "DELETED"))
.done()
.always();
final CountDownLatch deleteLatch = new CountDownLatch(1);
final Watcher<Pod> watcher = new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
if (action != Action.DELETED) {
fail();
}
deleteLatch.countDown();
}
@Override
public void onClose(WatcherException cause) {
}
};
// When
final Watch watch = client.pods().withName("pod1").watch(watcher);
// Then
assertTrue(deleteLatch.await(30, TimeUnit.SECONDS));
watch.close();
}
@Test
void testGetLogNotFound() {
// Given
PodResource<Pod> podOp = client.pods().withName("pod5");
// When + Then
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.getLog(true));
}
@Test
void testLoad() {
Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod.yml")).get();
assertEquals("nginx", pod.getMetadata().getName());
}
@Test
void testWait() throws InterruptedException {
Pod notReady = new PodBuilder()
.withNewMetadata()
.withName("pod1")
.withResourceVersion("1")
.withNamespace("test")
.endMetadata()
.withNewStatus()
.addNewCondition()
.withType("Ready")
.withStatus("False")
.endCondition()
.endStatus()
.build();
Pod ready = new PodBuilder(notReady)
.editMetadata()
.withResourceVersion("2")
.endMetadata()
.withNewStatus()
.addNewCondition()
.withType("Ready")
.withStatus("True")
.endCondition()
.endStatus()
.build();
server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1").andReturn(200, notReady).once();
server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket()
.open()
.waitFor(50).andEmit(new WatchEvent(ready, "MODIFIED"))
.done()
.always();
Pod result = client.pods().withName("pod1").waitUntilReady(5, TimeUnit.SECONDS);
Assert.assertEquals("2", result.getMetadata().getResourceVersion());
}
@Test
void testPortForward() throws IOException {
server.expect().withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123")
.andUpgradeToWebSocket()
.open()
.waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info
.waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info
.waitFor(10).andEmit(portForwardEncode(true, "Hell"))
.waitFor(10).andEmit(portForwardEncode(true, "o World"))
.done()
.once();
try(
LocalPortForward portForward = client.pods().withName("pod1").portForward(123);
SocketChannel channel = SocketChannel.open()
) {
int localPort = portForward.getLocalPort();
assertTrue(channel.connect(new InetSocketAddress("localhost", localPort)));
ByteBuffer buffer = ByteBuffer.allocate(1024);
int read;
do {
try {
read = channel.read(buffer);
} catch (IOException io) {
// On windows an exception is thrown when connection is reset during read
// It can only be distinguished via message, and that message is localized
// So let's at least handle English
if (!io.getMessage().startsWith("An existing connection was forcibly closed")) {
throw io;
}
read = -1;
}
} while(read >= 0);
buffer.flip();
channel.socket().close();
assertEquals("Hello World", ByteString.of(buffer).utf8());
assertFalse(portForward.errorOccurred());
assertEquals(0, portForward.getClientThrowables().size());
assertEquals(0, portForward.getServerThrowables().size());
}
}
@Test
void testPortForwardWithChannel() throws InterruptedException, IOException {
server.expect().withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123")
.andUpgradeToWebSocket()
.open()
.waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info
.waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info
.waitFor(10).andEmit(portForwardEncode(true, "Hell"))
.waitFor(10).andEmit(portForwardEncode(true, "o World!"))
.done()
.once();
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
ReadableByteChannel inChannel = Channels.newChannel(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
WritableByteChannel outChannel = Channels.newChannel(out);
try(PortForward portForward = client.pods().withName("pod1").portForward(123, inChannel, outChannel)) {
while(portForward.isAlive()) {
Thread.sleep(100);
}
}
String got = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello World!", got);
}
@Test
void testOptionalUpload() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts/dir").upload(tempDir.toAbsolutePath());
});
}
@Test
void testOptionalCopy() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").file("/etc/hosts").copy(tempDir.toAbsolutePath());
});
}
@Test
void testOptionalCopyDir() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts").copy(tempDir.toAbsolutePath());
});
}
@Test
void testListFromServer() {
PodBuilder podBuilder = new PodBuilder()
.withNewMetadata()
.withNamespace("test")
.withName("pod1")
.endMetadata();
Pod clientPod = podBuilder.build();
Pod serverPod = podBuilder
.editMetadata()
.withResourceVersion("1")
.endMetadata()
.withNewStatus()
.addNewCondition()
.withType("Ready")
.withStatus("True")
.endCondition()
.endStatus()
.build();
server.expect().get()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200, serverPod).once();
List<HasMetadata> resources = client.resourceList(clientPod).fromServer().get();
assertNotNull(resources);
assertEquals(1, resources.size());
assertNotNull(resources.get(0));
assertTrue(resources.get(0) instanceof Pod);
Pod fromServerPod = (Pod) resources.get(0);
assertNotNull(fromServerPod.getMetadata());
assertEquals("1", fromServerPod.getMetadata().getResourceVersion());
assertNotNull(fromServerPod.getStatus());
assertNotNull(fromServerPod.getStatus().getConditions());
assertEquals(1, fromServerPod.getStatus().getConditions().size());
assertEquals("Ready", fromServerPod.getStatus().getConditions().get(0).getType());
assertEquals("True", fromServerPod.getStatus().getConditions().get(0).getStatus());
}
private static String portForwardEncode(boolean dataChannel, String str) {
try {
byte[] data = str.getBytes(StandardCharsets.UTF_8);
byte[] msg = new byte[data.length + 1];
System.arraycopy(data, 0, msg, 1, data.length);
msg[0] = dataChannel ? (byte) 0 : (byte) 1;
return new String(msg, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}