forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPodTest.java
1144 lines (995 loc) · 37.4 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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.CopyOrReadable;
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.ErrorStreamMessage;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.server.mock.OutputStreamMessage;
import io.fabric8.kubernetes.client.server.mock.StatusMessage;
import io.fabric8.kubernetes.client.server.mock.StatusStreamMessage;
import io.fabric8.kubernetes.client.utils.InputStreamPumper;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.mockwebserver.internal.WebSocketMessage;
import okio.ByteString;
import org.awaitility.Awaitility;
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.io.PipedInputStream;
import java.io.PipedOutputStream;
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.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
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 podOp = client.pods().withName("pod1");
// Then
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().size() == 1;
assertTrue(deleted);
deleted = client.pods().withName("pod2").delete().size() == 1;
assertFalse(deleted);
deleted = client.pods().inNamespace("ns1").withName("pod2").cascading(false).delete().size() == 1;
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);
assertEquals(0, client.pods().inAnyNamespace().delete(pod3).size());
}
@Test
void testDeleteWithNamespaceMismatch() {
// Given
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
// When + Then
NonNamespaceOperation<Pod, PodList, PodResource> podOp = client.pods().inNamespace("test1");
assertEquals(0, podOp.delete(pod1).size());
}
@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();
assertEquals(1, client.pods()
.inNamespace("test")
.withName("pod1")
.withPropagationPolicy(DeletionPropagation.FOREGROUND)
.delete()
.size());
}
@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/pod-429/eviction")
.andReturn(PodOperationsImpl.HTTP_TOO_MANY_REQUESTS, new PodBuilder().build())
.once();
server.expect()
.withPath("/api/v1/namespaces/ns1/pods/pod-429/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
PodResource podResource = client.pods().withName("pod2");
assertThrows(KubernetesClientException.class, podResource::evict);
deleted = client.pods().inNamespace("ns1").withName("pod2").evict();
assertTrue(deleted);
// too many requests - automatically retries
deleted = client.pods().inNamespace("ns1").withName("pod-429").evict();
assertTrue(deleted);
// unhandled error
PodResource 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 podOp = client.pods().inNamespace("test1").withName("mypod1");
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&container=default&stdout=true")
.andUpgradeToWebSocket()
.open(new OutputStreamMessage(expectedOutput))
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
final CountDownLatch execLatch = new CountDownLatch(1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExecWatch watch = client.pods()
.withName("pod1")
.writingOutput(baos)
.usingListener(createCountDownLatchListener(execLatch))
.exec("ls");
assertTrue(execLatch.await(10, TimeUnit.MINUTES));
assertNotNull(watch);
assertEquals(expectedOutput, baos.toString());
watch.close();
}
@Test
void testExecWithErrorOutput() throws InterruptedException {
String expectedError = "ls: cannot open directory '/': Permission denied";
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&container=default&stderr=true")
.andUpgradeToWebSocket()
.open(new ErrorStreamMessage(expectedError))
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
final CountDownLatch execLatch = new CountDownLatch(1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExecWatch watch = client.pods()
.withName("pod1")
.writingError(baos)
.usingListener(createCountDownLatchListener(execLatch))
.exec("ls");
assertTrue(execLatch.await(10, TimeUnit.MINUTES));
assertNotNull(watch);
assertEquals(expectedError, baos.toString());
watch.close();
}
@Test
void testExecWithExitCode() throws Exception {
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&container=default&stdout=true")
.andUpgradeToWebSocket()
.open(new StatusStreamMessage(1))
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExecWatch watch = client.pods()
.withName("pod1")
.writingOutput(baos)
.exec("ls");
final Integer exitCode = watch.exitCode().get(10, TimeUnit.MINUTES);
assertEquals(1, exitCode);
assertNotNull(watch);
assertEquals(0, baos.size());
watch.close();
}
@Test
void testAttachWithWritingOutput() throws InterruptedException, IOException {
// Given
String validInput = "input";
String expectedOutput = "output";
String invalidInput = "invalid";
String expectedError = "error";
String shutdownInput = "shutdown";
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/attach?container=default&stdin=true&stdout=true&stderr=true")
.andUpgradeToWebSocket()
.open()
.expect("\u0000" + validInput) // \u0000 is the file descriptor for stdin
.andEmit(new WebSocketMessage(0L, "\u0001" + expectedOutput, false, true)) // \u0001 is the file descriptor for stdout
.always()
.expect("\u0000" + invalidInput)
.andEmit(new WebSocketMessage(0L, "\u0002" + expectedError, false, true)) // \u0002 is the file descriptor for stderr
.always()
.expect("\u0000" + shutdownInput)
.andEmit(new WebSocketMessage(0L, "\u0003shutdown", false, true))
.always()
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.addToAnnotations(PodOperationsImpl.DEFAULT_CONTAINER_ANNOTATION_NAME, "default")
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("first")
.endContainer()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
CountDownLatch latch = new CountDownLatch(1);
// When
ExecWatch watch = client.pods()
.withName("pod1")
.redirectingInput()
.writingOutput(stdout)
.writingError(stderr)
.usingListener(createCountDownLatchListener(latch))
.attach();
watch.getInput().write(validInput.getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
watch.getInput().write(invalidInput.getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
watch.getInput().write(shutdownInput.getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
assertTrue(latch.await(1, TimeUnit.MINUTES));
// Then
assertEquals(expectedOutput, stdout.toString());
assertEquals(expectedError, stderr.toString());
watch.close();
}
@Test
void testExecExplicitDefaultContainerMissing() {
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&container=first&stderr=true")
.andUpgradeToWebSocket()
.open()
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.addToAnnotations(PodOperationsImpl.DEFAULT_CONTAINER_ANNOTATION_NAME, "default")
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("first")
.endContainer()
.endSpec()
.build())
.once();
// When
ExecWatch watch = client.pods()
.withName("pod1")
.terminateOnError()
.exec("ls");
watch.close();
}
@Test
void testAttachWithRedirectOutput() throws InterruptedException, IOException {
// Given
String validInput = "input";
String expectedOutput = "output";
String invalidInput = "invalid";
String expectedError = "error";
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/attach?container=first&stdin=true&stdout=true&stderr=true")
.andUpgradeToWebSocket()
.open()
.expect("\u0000" + validInput) // \u0000 is the file descriptor for stdin
.andEmit(new WebSocketMessage(0L, "\u0001" + expectedOutput, false, true)) // \u0001 is the file descriptor for stdout
.always()
.expect("\u0000" + invalidInput)
.andEmit(new WebSocketMessage(0L, "\u0002" + expectedError, false, true)) // \u0002 is the file descriptor for stderr
.always()
.done()
.always();
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1")
.andReturn(200,
new PodBuilder().withNewMetadata()
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("first")
.endContainer()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
CountDownLatch latch = new CountDownLatch(1);
// When
ExecWatch watch = client.pods()
.withName("pod1")
.redirectingInput()
.redirectingOutput()
.redirectingError()
.usingListener(createCountDownLatchListener(latch))
.attach();
watch.getInput().write(validInput.getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
watch.getInput().write(invalidInput.getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
InputStreamPumper.pump(watch.getOutput(), stdout::write, Executors.newSingleThreadExecutor());
InputStreamPumper.pump(watch.getError(), stderr::write, Executors.newSingleThreadExecutor());
// Then
Awaitility.await()
.atMost(30, TimeUnit.SECONDS)
.until(() -> stdout.toString().equals(expectedOutput) && stderr.toString().equals(expectedError));
watch.close();
assertTrue(latch.await(1, TimeUnit.MINUTES));
}
private ExecListener createCountDownLatchListener(CountDownLatch latch) {
return new ExecListener() {
@Override
public void onFailure(Throwable t, Response failureResponse) {
latch.countDown();
}
@Override
public void onClose(int code, String reason) {
latch.countDown();
}
};
}
@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?allowWatchBookmarks=true&fieldSelector=metadata.name%3Dpod1&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 podOp = client.pods().withName("pod5");
// When + Then
assertThrows(KubernetesClientException.class, () -> podOp.getLog(true));
}
@Test
void testLoad() {
Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod.yml")).item();
assertEquals("nginx", pod.getMetadata().getName());
}
@Test
void testWait() {
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?allowWatchBookmarks=true&fieldSelector=metadata.name%3Dpod1&resourceVersion=1&timeoutSeconds=600&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(50)
.andEmit(new WatchEvent(ready, "MODIFIED"))
.done()
.always();
Pod result = client.pods().withName("pod1").waitUntilReady(10, TimeUnit.SECONDS);
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") &&
!io.getMessage().startsWith("Connection reset")) {
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 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)) {
Awaitility.await().atMost(Duration.ofSeconds(60)).until(() -> !portForward.isAlive());
}
String got = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello World!", got);
}
@Test
void testOptionalUpload() {
final CopyOrReadable dir = client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts/dir");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> dir.upload(absolutePath));
}
@Test
void testOptionalCopy() {
server.expect()
.withPath("/api/v1/namespaces/ns1/pods/pod2")
.andReturn(200,
new PodBuilder().withNewMetadata()
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("first")
.endContainer()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec()
.build())
.once();
final CopyOrReadable file = client.pods().inNamespace("ns1").withName("pod2").file("/etc/hosts");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> file.copy(absolutePath));
}
@Test
void testOptionalCopyDir() {
final CopyOrReadable dir = client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> dir.copy(absolutePath));
}
@Test
void testPipesNotAllowed() throws IOException {
try (PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream()) {
PodResource podOp = client.pods().inNamespace("ns1").withName("pod2");
assertThrows(KubernetesClientException.class, () -> podOp.watchLog(out));
assertThrows(KubernetesClientException.class, () -> podOp.writingError(out));
assertThrows(KubernetesClientException.class, () -> podOp.writingErrorChannel(out));
assertThrows(KubernetesClientException.class, () -> podOp.writingOutput(out));
assertThrows(KubernetesClientException.class, () -> podOp.readingInput(in));
}
}
@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).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());
}
@Test
void testPatchEphemeralContainer() {
PodBuilder podBuilder = new PodBuilder()
.withNewMetadata()
.withNamespace("test")
.withName("pod1")
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("default")
.endContainer()
.endSpec();
Pod patchedResponse = podBuilder.editSpec()
.addNewEphemeralContainer()
.withName("debug")
.endEphemeralContainer()
.endSpec()
.withNewStatus()
.addNewEphemeralContainerStatus()
.withName("debug")
.endEphemeralContainerStatus()
.endStatus()
.build();
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, podBuilder.build()).once();
server.expect().patch().withPath("/api/v1/namespaces/test/pods/pod1/ephemeralcontainers").andReturn(201, patchedResponse)
.once();
Pod patched = client.pods().withName("pod1")
.ephemeralContainers()