-
Notifications
You must be signed in to change notification settings - Fork 149
/
e2e_test.go
986 lines (848 loc) · 34.2 KB
/
e2e_test.go
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
package e2e
import (
_ "embed"
"errors"
"fmt"
"strconv"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
"github.com/topolvm/topolvm"
topolvmv1 "github.com/topolvm/topolvm/api/v1"
"github.com/topolvm/topolvm/cmd/topolvm-controller/app"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
//go:embed testdata/e2e/pvc-template.yaml
var pvcTemplateYAML string
//go:embed testdata/e2e/pod-volume-mount-template.yaml
var podVolumeMountTemplateYAML string
//go:embed testdata/e2e/pod-volume-device-template.yaml
var podVolumeDeviceTemplateYAML string
//go:embed testdata/e2e/node-capacity-pvc.yaml
var nodeCapacityPVCYAML []byte
//go:embed testdata/e2e/node-capacity-pvc2.yaml
var nodeCapacityPVC2YAML []byte
//go:embed testdata/e2e/generic-ephemeral-volume.yaml
var e2eGenericEphemeralVolumeYAML []byte
func testE2E() {
testNamespacePrefix := "e2etest-"
var ns string
var cc CleanupContext
BeforeEach(func() {
cc = commonBeforeEach()
ns = testNamespacePrefix + randomString()
createNamespace(ns)
})
AfterEach(func() {
// When a test fails, I want to investigate the cause. So please don't remove the namespace!
if !CurrentSpecReport().State.Is(types.SpecStateFailureStates) {
_, err := kubectl("delete", "namespaces/"+ns)
Expect(err).ShouldNot(HaveOccurred())
}
commonAfterEach(cc)
})
It("should be mounted in specified path", func() {
By("deploying Pod with PVC")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 1024, "topolvm-provisioner"))
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu", "topo-pvc"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device exists in the Pod")
Eventually(func() error {
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "mountpoint", "-d", "/test1")
if err != nil {
return fmt.Errorf("failed to check mount point. err: %w", err)
}
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "grep", "/test1", "/proc/mounts")
if err != nil {
return err
}
fields := strings.Fields(string(stdout))
if fields[2] != "xfs" {
return errors.New("/test1 is not xfs")
}
return nil
}).Should(Succeed())
By("writing file under /test1")
writePath := "/test1/bootstrap.log"
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "cp", "/var/log/bootstrap.log", writePath)
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "sync")
Expect(err).ShouldNot(HaveOccurred())
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "cat", writePath)
Expect(err).ShouldNot(HaveOccurred())
Expect(strings.TrimSpace(string(stdout))).ShouldNot(BeEmpty())
By("deleting the Pod, then recreating it")
_, err = kubectl("delete", "--now=true", "-n", ns, "pod/ubuntu")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the file exists")
Eventually(func() error {
stdout, err = kubectl("exec", "-n", ns, "ubuntu", "--", "cat", writePath)
if err != nil {
return fmt.Errorf("failed to cat. err: %w", err)
}
if len(strings.TrimSpace(string(stdout))) == 0 {
return fmt.Errorf(writePath + " is empty")
}
return nil
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume resource is registered in LVM")
var pvc corev1.PersistentVolumeClaim
err = getObjects(&pvc, "pvc", "-n", ns, "topo-pvc")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
return checkLVIsRegisteredInLVM(pvc.Spec.VolumeName)
}).Should(Succeed())
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYaml, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the PV is deleted")
Eventually(func() error {
var pv corev1.PersistentVolume
err := getObjects(&pv, "pv", volName)
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return fmt.Errorf("failed to get pv/%s. err: %w", volName, err)
default:
return fmt.Errorf("target pv exists %s", volName)
}
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume is deleted")
Eventually(func() error {
return checkLVIsDeletedInLVM(volName)
}).Should(Succeed())
})
It("should be mounted in specified path but changed by the minimum allocation default for XFS", func() {
By("deploying Pod with PVC")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 100, "topolvm-provisioner"))
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu", "topo-pvc"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device exists in the Pod")
Eventually(func() error {
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "mountpoint", "-d", "/test1")
if err != nil {
return fmt.Errorf("failed to check mount point. err: %w", err)
}
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "grep", "/test1", "/proc/mounts")
if err != nil {
return err
}
fields := strings.Fields(string(stdout))
if fields[2] != "xfs" {
return errors.New("/test1 is not xfs")
}
return nil
}).Should(Succeed())
By("writing file under /test1")
writePath := "/test1/bootstrap.log"
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "cp", "/var/log/bootstrap.log", writePath)
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "sync")
Expect(err).ShouldNot(HaveOccurred())
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "cat", writePath)
Expect(err).ShouldNot(HaveOccurred())
Expect(strings.TrimSpace(string(stdout))).ShouldNot(BeEmpty())
By("deleting the Pod, then recreating it")
_, err = kubectl("delete", "--now=true", "-n", ns, "pod/ubuntu")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the file exists")
Eventually(func() error {
stdout, err = kubectl("exec", "-n", ns, "ubuntu", "--", "cat", writePath)
if err != nil {
return fmt.Errorf("failed to cat. err: %w", err)
}
if len(strings.TrimSpace(string(stdout))) == 0 {
return fmt.Errorf(writePath + " is empty")
}
return nil
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume resource is registered in LVM")
var pvc corev1.PersistentVolumeClaim
err = getObjects(&pvc, "pvc", "-n", ns, "topo-pvc")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
return checkLVIsRegisteredInLVM(pvc.Spec.VolumeName)
}).Should(Succeed())
By("confirming that the lv was successfully sized to the minimum size of the StorageClass")
Expect(pvc.Status.Capacity.Storage().Cmp(resource.MustParse(app.DefaultMinimumAllocationSizeXFS))).
Should(Equal(0),
"expected: %s as minimum capacity, actual: %s",
app.DefaultMinimumAllocationSizeXFS,
pvc.Status.Capacity.Storage().String(),
)
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYaml, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the PV is deleted")
Eventually(func() error {
var pv corev1.PersistentVolume
err := getObjects(&pv, "pv", volName)
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return fmt.Errorf("failed to get pv/%s. err: %w", volName, err)
default:
return fmt.Errorf("target pv exists %s", volName)
}
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume is deleted")
Eventually(func() error {
return checkLVIsDeletedInLVM(volName)
}).Should(Succeed())
})
It("should use generic ephemeral volumes", func() {
By("deploying a Pod with a generic ephemeral volume")
_, err := kubectlWithInput(e2eGenericEphemeralVolumeYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming the Pod is deployed")
Eventually(func() error {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, "pause")
if err != nil {
return fmt.Errorf("failed to get Pod. err: %w", err)
}
if pod.Status.Phase != corev1.PodRunning {
return errors.New("Pod is not running")
}
return nil
}).Should(Succeed())
By("confirming the PVC is bound")
Eventually(func() error {
var pvc corev1.PersistentVolumeClaim
err := getObjects(&pvc, "pvc", "-n", ns, "pause-generic-ephemeral-volume1")
if err != nil {
return fmt.Errorf("failed to get PVC. err: %w", err)
}
if pvc.Status.Phase != corev1.ClaimBound {
return errors.New("PVC is not bound")
}
return nil
}).Should(Succeed())
By("deleting the Pod with a generic ephemeral volume")
_, err = kubectlWithInput(e2eGenericEphemeralVolumeYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming the Pod is deleted")
Eventually(func() error {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, "pause")
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return err
default:
return errors.New("the Pod exists")
}
}).Should(Succeed())
By("confirming the PVC is deleted")
Eventually(func() error {
var pvc corev1.PersistentVolumeClaim
err := getObjects(&pvc, "pvc", "-n", ns, "pause-generic-ephemeral-volume1")
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return err
default:
return errors.New("the PVC exists")
}
}).Should(Succeed())
})
It("should create a block device for Pod", func() {
deviceFile := "/dev/e2etest"
By("deploying ubuntu Pod with PVC to mount a block device")
podYAML := []byte(fmt.Sprintf(podVolumeDeviceTemplateYAML, deviceFile))
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Block", 1024, "topolvm-provisioner"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that a block device exists in ubuntu pod")
Eventually(func() error {
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "test", "-b", deviceFile)
if err != nil {
podinfo, _ := kubectl("-n", ns, "describe", "pod", "ubuntu")
return fmt.Errorf("failed to test. ubuntu pod info output: %s; err: %w", podinfo, err)
}
return nil
}).Should(Succeed())
By("writing data to a block device")
// /etc/hostname contains "ubuntu"
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "dd", "if=/etc/hostname", "of="+deviceFile)
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "sync")
Expect(err).ShouldNot(HaveOccurred())
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--",
"dd", "if="+deviceFile, "of=/dev/stdout", "bs=6", "count=1", "status=none")
Expect(err).ShouldNot(HaveOccurred())
Expect(string(stdout)).Should(Equal("ubuntu"))
By("deleting the Pod, then recreating it")
_, err = kubectl("delete", "--now=true", "-n", ns, "pod/ubuntu")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("reading data from a block device")
Eventually(func() error {
stdout, err = kubectl("exec", "-n", ns, "ubuntu", "--",
"dd", "if="+deviceFile, "of=/dev/stdout", "bs=6", "count=1", "status=none")
if err != nil {
return fmt.Errorf("failed to cat. err: %w", err)
}
if string(stdout) != "ubuntu" {
return fmt.Errorf("expected: ubuntu, actual: %s", string(stdout))
}
return nil
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume resource is registered in LVM")
var pvc corev1.PersistentVolumeClaim
err = getObjects(&pvc, "pvc", "-n", ns, "topo-pvc")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
return checkLVIsRegisteredInLVM(pvc.Spec.VolumeName)
}).Should(Succeed())
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the PV is deleted")
Eventually(func() error {
var pv corev1.PersistentVolume
err := getObjects(&pv, "pv", volName)
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return fmt.Errorf("failed to get pv/%s. err: %w", volName, err)
default:
return fmt.Errorf("target PV exists %s", volName)
}
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume is deleted")
Eventually(func() error {
return checkLVIsDeletedInLVM(volName)
}).Should(Succeed())
})
It("should create a block device for Pod but changed by the minimum allocation default for block storage", func() {
deviceFile := "/dev/e2etest"
By("deploying ubuntu Pod with PVC to mount a block device")
podYAML := []byte(fmt.Sprintf(podVolumeDeviceTemplateYAML, deviceFile))
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Block", 1, "topolvm-provisioner"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that a block device exists in ubuntu pod")
Eventually(func() error {
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "test", "-b", deviceFile)
if err != nil {
podinfo, _ := kubectl("-n", ns, "describe", "pod", "ubuntu")
return fmt.Errorf("failed to test. ubuntu pod info output: %s; err: %w", podinfo, err)
}
return nil
}).Should(Succeed())
By("writing data to a block device")
// /etc/hostname contains "ubuntu"
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "dd", "if=/etc/hostname", "of="+deviceFile)
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "sync")
Expect(err).ShouldNot(HaveOccurred())
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--",
"dd", "if="+deviceFile, "of=/dev/stdout", "bs=6", "count=1", "status=none")
Expect(err).ShouldNot(HaveOccurred())
Expect(string(stdout)).Should(Equal("ubuntu"))
By("deleting the Pod, then recreating it")
_, err = kubectl("delete", "--now=true", "-n", ns, "pod/ubuntu")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("reading data from a block device")
Eventually(func() error {
stdout, err = kubectl("exec", "-n", ns, "ubuntu", "--",
"dd", "if="+deviceFile, "of=/dev/stdout", "bs=6", "count=1", "status=none")
if err != nil {
return fmt.Errorf("failed to cat. err: %w", err)
}
if string(stdout) != "ubuntu" {
return fmt.Errorf("expected: ubuntu, actual: %s", string(stdout))
}
return nil
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume resource is registered in LVM")
var pvc corev1.PersistentVolumeClaim
err = getObjects(&pvc, "pvc", "-n", ns, "topo-pvc")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
return checkLVIsRegisteredInLVM(pvc.Spec.VolumeName)
}).Should(Succeed())
By("confirming that the lv was successfully sized to the minimum size of the StorageClass")
Expect(pvc.Status.Capacity.Storage().Cmp(resource.MustParse(app.DefaultMinimumAllocationSizeBlock))).
Should(Equal(0),
"expected: %s as minimum capacity, actual: %s",
app.DefaultMinimumAllocationSizeBlock, pvc.Status.Capacity.Storage().String())
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the PV is deleted")
Eventually(func() error {
var pv corev1.PersistentVolume
err := getObjects(&pv, "pv", volName)
switch {
case err == ErrObjectNotFound:
return nil
case err != nil:
return fmt.Errorf("failed to get pv/%s. err: %w", volName, err)
default:
return fmt.Errorf("target PV exists %s", volName)
}
}).Should(Succeed())
By("confirming that the lv correspond to LogicalVolume is deleted")
Eventually(func() error {
return checkLVIsDeletedInLVM(volName)
}).Should(Succeed())
})
It("should choose a node with the largest capacity when volumeBindingMode == Immediate is specified", func() {
skipIfStorageCapacity(
"Storage Capacity Tracking doesn't check Storage Capacity when volumeBindingMode == Immediate is specified",
)
// Repeat applying a PVC to make sure that the volume is created on the node with the largest capacity in each loop.
for i := 0; i < nonControlPlaneNodeCount; i++ {
By("getting the node with max capacity (loop: " + strconv.Itoa(i) + ")")
var maxCapNodes []string
Eventually(func() error {
var maxCapacity int
maxCapNodes = []string{}
var nodes corev1.NodeList
err := getObjects(&nodes, "nodes")
if err != nil {
return fmt.Errorf("kubectl get nodes error: %w", err)
}
for _, node := range nodes.Items {
if node.Name == "topolvm-e2e-control-plane" {
continue
}
strCap, ok := node.Annotations[topolvm.GetCapacityKeyPrefix()+"dc1"]
if !ok {
return fmt.Errorf("capacity is not annotated: %s", node.Name)
}
capacity, err := strconv.Atoi(strCap)
if err != nil {
return err
}
fmt.Printf("%s: %d bytes\n", node.Name, capacity)
switch {
case capacity > maxCapacity:
maxCapacity = capacity
maxCapNodes = []string{node.GetName()}
case capacity == maxCapacity:
maxCapNodes = append(maxCapNodes, node.GetName())
}
}
if len(maxCapNodes) != nonControlPlaneNodeCount-i {
return fmt.Errorf("unexpected number of maxCapNodes: expected: %d, actual: %d",
nonControlPlaneNodeCount-i, len(maxCapNodes))
}
return nil
}).Should(Succeed())
By("creating pvc")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML,
fmt.Sprintf("topo-pvc-%d", i), "Filesystem", 1024, "topolvm-provisioner-immediate"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
var volumeName string
Eventually(func() error {
var pvc corev1.PersistentVolumeClaim
err := getObjects(&pvc, "pvc", "-n", ns, "topo-pvc-"+strconv.Itoa(i))
if err != nil {
return fmt.Errorf("failed to get PVC. err: %w", err)
}
if pvc.Spec.VolumeName == "" {
return errors.New("pvc.Spec.VolumeName should not be empty")
}
volumeName = pvc.Spec.VolumeName
return nil
}).Should(Succeed())
By("confirming that the logical volume was scheduled onto the node with max capacity")
var lv topolvmv1.LogicalVolume
err = getObjects(&lv, "logicalvolumes", volumeName)
Expect(err).ShouldNot(HaveOccurred())
target := lv.Spec.NodeName
Expect(target).To(BeElementOf(maxCapNodes), "maxCapNodes: %v, target: %s", maxCapNodes, target)
}
})
It("should scheduled onto the correct node where PV exists (volumeBindingMode == Immediate)", func() {
By("creating pvc")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 1024, "topolvm-provisioner-immediate"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
var volumeName string
Eventually(func() error {
var pvc corev1.PersistentVolumeClaim
err := getObjects(&pvc, "pvc", "-n", ns, "topo-pvc")
if err != nil {
return fmt.Errorf("failed to get PVC. err: %w", err)
}
if pvc.Spec.VolumeName == "" {
return errors.New("pvc.Spec.VolumeName should not be empty")
}
volumeName = pvc.Spec.VolumeName
return nil
}).Should(Succeed())
By("getting node name of which volume is created")
var lv topolvmv1.LogicalVolume
err = getObjects(&lv, "logicalvolumes", volumeName)
Expect(err).ShouldNot(HaveOccurred())
nodeName := lv.Spec.NodeName
By("deploying ubuntu Pod with PVC")
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu", "topo-pvc"))
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that ubuntu pod is scheduled onto " + nodeName)
Eventually(func() error {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, "ubuntu")
if err != nil {
return fmt.Errorf("failed to create pod. err: %w", err)
}
if pod.Spec.NodeName != nodeName {
return fmt.Errorf("pod is not yet scheduled")
}
return nil
}).Should(Succeed())
By("deleting the Pod, then recreating it")
_, err = kubectl("delete", "--now=true", "-n", ns, "pod/ubuntu")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that ubuntu pod is rescheduled onto " + nodeName)
Eventually(func() error {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, "ubuntu")
if err != nil {
return fmt.Errorf("failed to create pod. err: %w", err)
}
if pod.Spec.NodeName != nodeName {
return fmt.Errorf("pod is not yet scheduled")
}
return nil
}).Should(Succeed())
})
It("should schedule pods and volumes according to topolvm-scheduler", func() {
skipIfStorageCapacity()
skipIfSingleNode()
/*
Check the operation of topolvm-scheduler in multi-node(3-node) environment.
As preparation, set the capacity of each node as follows.
- node1: 18 / 18 GiB (targetNode)
- node2: 4 / 18 GiB
- node3: 4 / 18 GiB
# 1st case: test for `prioritize`
Try to create 8GiB PVC. Then
- node1: 18 / 18 GiB -> `prioritize` 4 -> selected
- node2: 4 / 18 GiB -> `prioritize` 2
- node3: 4 / 18 GiB -> `prioritize` 2
# 2nd case: test for `predicate` (1)
Try to create 6GiB PVC. Then
- node1: 10 / 18 GiB -> selected
- node2: 4 / 18 GiB -> filtered (insufficient capacity)
- node3: 4 / 18 GiB -> filtered (insufficient capacity)
# 3rd case: test for `predicate` (2)
Try to create 8GiB PVC. Then it cause error.
- node1: 4 / 18 GiB -> filtered (insufficient capacity)
- node2: 4 / 18 GiB -> filtered (insufficient capacity)
- node3: 4 / 18 GiB -> filtered (insufficient capacity)
*/
By("initializing node capacity")
_, err := kubectlWithInput(nodeCapacityPVCYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
var pvcList corev1.PersistentVolumeClaimList
err := getObjects(&pvcList, "pvc", "-n", ns)
if err != nil {
return fmt.Errorf("failed to get PVC. err: %w", err)
}
if len(pvcList.Items) != 2 {
return fmt.Errorf("the length of PVC list should be 2")
}
for _, pvc := range pvcList.Items {
if pvc.Spec.VolumeName == "" {
return errors.New("pvc.Spec.VolumeName should not be empty")
}
}
return nil
}).Should(Succeed())
By("selecting a targetNode")
var nodeList corev1.NodeList
err = getObjects(&nodeList, "node")
Expect(err).ShouldNot(HaveOccurred())
var targetNode string
var maxCapacity int
for _, node := range nodeList.Items {
if node.Name == "topolvm-e2e-control-plane" {
continue
}
strCap, ok := node.Annotations[topolvm.GetCapacityKeyPrefix()+"dc1"]
Expect(ok).To(Equal(true), "capacity is not annotated: "+node.Name)
capacity, err := strconv.Atoi(strCap)
Expect(err).ShouldNot(HaveOccurred())
fmt.Printf("%s: %d\n", node.Name, capacity)
if capacity > maxCapacity {
maxCapacity = capacity
targetNode = node.Name
}
}
By("creating pvc")
_, err = kubectlWithInput(nodeCapacityPVC2YAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
var boundNode string
By("confirming that claiming 8GB pv to the targetNode is successful")
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu1", "topo-pvc1"))
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
boundNode, err = waitCreatingPodWithPVC("ubuntu1", ns)
return err
}).Should(Succeed())
Expect(boundNode).To(Equal(targetNode), "bound: %s, target: %s", boundNode, targetNode)
By("confirming that claiming 6GB pv to the targetNode is successful")
podYaml = []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu2", "topo-pvc2"))
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
Eventually(func() error {
boundNode, err = waitCreatingPodWithPVC("ubuntu2", ns)
return err
}).Should(Succeed())
Expect(boundNode).To(Equal(targetNode), "bound: %s, target: %s", boundNode, targetNode)
By("confirming that claiming 8GB pv to the targetNode is unsuccessful")
podYaml = []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu3", "topo-pvc3"))
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
time.Sleep(15 * time.Second)
var pod corev1.Pod
err = getObjects(&pod, "pod", "-n", ns, "ubuntu3")
Expect(pod.Spec.NodeName).To(Equal(""))
})
It("should resize filesystem", func() {
By("deploying Pod with PVC")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 1024, "topolvm-provisioner"))
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu", "topo-pvc"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device is mounted in the Pod")
Eventually(func() error {
return verifyMountExists(ns, "ubuntu", "/test1")
}).Should(Succeed())
By("resizing PVC online")
claimYAML = []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 2*1024, "topolvm-provisioner"))
_, err = kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device is resized in the Pod")
timeout := time.Minute * 5
Eventually(func() error {
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "df", "--output=size", "/test1")
if err != nil {
return fmt.Errorf("failed to get volume size. err: %w", err)
}
dfFields := strings.Fields(string(stdout))
volSize, err := strconv.Atoi(dfFields[1])
if err != nil {
return fmt.Errorf("failed to convert volume size string. data: %s, err: %w", stdout, err)
}
if volSize != 2086912 {
return fmt.Errorf("failed to match volume size. actual: %d, expected: %d", volSize, 2086912)
}
return nil
}, timeout).Should(Succeed())
By("deleting Pod for offline resizing")
_, err = kubectlWithInput(podYaml, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("resizing PVC offline")
claimYAML = []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 3*1024, "topolvm-provisioner"))
_, err = kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("deploying Pod")
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device is resized in the Pod")
Eventually(func() error {
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "df", "--output=size", "/test1")
if err != nil {
return fmt.Errorf("failed to get volume size. err: %w", err)
}
dfFields := strings.Fields((string(stdout)))
volSize, err := strconv.Atoi(dfFields[1])
if err != nil {
return fmt.Errorf("failed to convert volume size string. data: %s, err: %w", stdout, err)
}
if volSize != 3135488 {
return fmt.Errorf("failed to match volume size. actual: %d, expected: %d", volSize, 3135488)
}
return nil
}, timeout).Should(Succeed())
By("deleting topolvm-node Pods to clear /dev/topolvm/*")
_, err = kubectl("delete", "-n", ns, "pod", "-l=app.kubernetes.io/component=node,app.kubernetes.io/name=topolvm")
Expect(err).ShouldNot(HaveOccurred())
By("resizing PVC")
claimYAML = []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 4*1024, "topolvm-provisioner"))
_, err = kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device is resized in the Pod")
Eventually(func() error {
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "df", "--output=size", "/test1")
if err != nil {
return fmt.Errorf("failed to get volume size. err: %w", err)
}
dfFields := strings.Fields(string(stdout))
volSize, err := strconv.Atoi(dfFields[1])
if err != nil {
return fmt.Errorf("failed to convert volume size string. data: %s, err: %w", stdout, err)
}
if volSize != 4184064 {
return fmt.Errorf("failed to match volume size. actual: %d, expected: %d", volSize, 4184064)
}
return nil
}, timeout).Should(Succeed())
By("confirming that no failure event has occurred")
fieldSelector := "involvedObject.kind=PersistentVolumeClaim," +
"involvedObject.name=topo-pvc," +
"reason=VolumeResizeFailed"
var events corev1.EventList
err = getObjects(&events, "events", "-n", ns, "--field-selector="+fieldSelector)
Expect(err).To(BeEquivalentTo(ErrObjectNotFound))
By("resizing PVC over vg capacity")
claimYAML = []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 100*1024, "topolvm-provisioner"))
_, err = kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that a failure event occurs")
Eventually(func() error {
var events corev1.EventList
err := getObjects(&events, "events", "-n", ns, "--field-selector="+fieldSelector)
switch {
case err == ErrObjectNotFound:
return errors.New("failure event not found")
case err != nil:
return fmt.Errorf("failed to get event. err: %w", err)
default:
return nil
}
}).Should(Succeed())
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYaml, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
})
It("should resize a block device", func() {
By("deploying Pod with PVC")
deviceFile := "/dev/e2etest"
podYAML := []byte(fmt.Sprintf(podVolumeDeviceTemplateYAML, deviceFile))
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Block", 1024, "topolvm-provisioner"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that a block device exists in ubuntu pod")
Eventually(func() error {
_, err = kubectl("exec", "-n", ns, "ubuntu", "--", "test", "-b", deviceFile)
if err != nil {
return fmt.Errorf("failed to test. err: %w", err)
}
return nil
}).Should(Succeed())
By("resizing PVC")
claimYAML = []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Block", 2048, "topolvm-provisioner"))
_, err = kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming that the specified device is resized in the Pod")
timeout := time.Minute * 5
Eventually(func() error {
stdout, err := kubectl("exec", "-n", ns, "ubuntu", "--", "blockdev", "--getsize64", deviceFile)
if err != nil {
return fmt.Errorf("failed to get volume size. err: %w", err)
}
volSize, err := strconv.Atoi(strings.TrimSpace(string(stdout)))
if err != nil {
return fmt.Errorf("failed to convert volume size string. data: %s, err: %w", stdout, err)
}
if volSize != 2147483648 {
return fmt.Errorf("failed to match volume size. actual: %d, expected: %d", volSize, 2147483648)
}
return nil
}, timeout).Should(Succeed())
By("deleting the Pod and PVC")
_, err = kubectlWithInput(podYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
})
It("should delete a pod when the pvc is deleted", func() {
By("deploying a pod and PVC")
claimYAML := []byte(fmt.Sprintf(pvcTemplateYAML, "topo-pvc", "Filesystem", 1024, "topolvm-provisioner"))
podYaml := []byte(fmt.Sprintf(podVolumeMountTemplateYAML, "ubuntu", "topo-pvc"))
_, err := kubectlWithInput(claimYAML, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
_, err = kubectlWithInput(podYaml, "apply", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("deleting the PVC")
_, err = kubectlWithInput(claimYAML, "delete", "-n", ns, "-f", "-")
Expect(err).ShouldNot(HaveOccurred())
By("confirming the pod is deleted")
Eventually(func() error {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, "ubuntu")
if err == ErrObjectNotFound {
return nil
}
return errors.New("the pod exists")
}).Should(Succeed())
})
}
func verifyMountExists(ns string, pod string, mount string) error {
_, err := kubectl("exec", "-n", ns, pod, "--", "mountpoint", "-d", mount)
if err != nil {
return fmt.Errorf("failed to check mount point. err: %w", err)
}
return nil
}
func waitCreatingDefaultSA(ns string) error {
var sa corev1.ServiceAccount
err := getObjects(&sa, "sa", "-n", ns, "default")
if err != nil {
return fmt.Errorf("default sa is not found. err=%w", err)
}
return nil
}
func waitCreatingPodWithPVC(podName, ns string) (string, error) {
var pod corev1.Pod
err := getObjects(&pod, "pod", "-n", ns, podName)
if err != nil {
return "", fmt.Errorf("failed to create pod. err: %w", err)
}
if pod.Spec.NodeName == "" {
return "", fmt.Errorf("pod is not yet scheduled")
}
return pod.Spec.NodeName, nil
}