-
Notifications
You must be signed in to change notification settings - Fork 267
/
cloner_test.go
3130 lines (2681 loc) · 150 KB
/
cloner_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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tests
import (
"context"
"crypto/rsa"
"fmt"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"kubevirt.io/containerized-data-importer/pkg/common"
cont "kubevirt.io/containerized-data-importer/pkg/controller"
controller "kubevirt.io/containerized-data-importer/pkg/controller/common"
dvc "kubevirt.io/containerized-data-importer/pkg/controller/datavolume"
"kubevirt.io/containerized-data-importer/pkg/token"
"kubevirt.io/containerized-data-importer/pkg/util/cert"
"kubevirt.io/containerized-data-importer/tests/framework"
"kubevirt.io/containerized-data-importer/tests/utils"
)
const (
namespacePrefix = "cloner"
sourcePodFillerName = "fill-source"
sourcePVCName = "source-pvc"
sizeDetectionPodPrefix = "size-detection-"
fillData = "123456789012345678901234567890123456789012345678901234567890"
fillDataFSMD5sum = "fabc176de7eb1b6ca90b3aa4c7e035f3"
testBaseDir = utils.DefaultPvcMountPath
testFile = "/disk.img"
fillCommand = "echo \"" + fillData + "\" >> " + testBaseDir
blockFillCommand = "dd if=/dev/urandom bs=4096 of=" + testBaseDir + " || echo this is fine"
assertionPollInterval = 2 * time.Second
cloneCompleteTimeout = 270 * time.Second
verifyPodDeletedTimeout = 270 * time.Second
controllerSkipPVCCompleteTimeout = 270 * time.Second
crossVolumeModeCloneMD5NumBytes = 100000
)
var _ = Describe("all clone tests", func() {
var _ = Describe("[rfe_id:1277][crit:high][vendor:cnv-qe@redhat.com][level:component]Cloner Test Suite", func() {
f := framework.NewFramework(namespacePrefix)
tinyCoreIsoURL := func() string { return fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs) }
var originalProfileSpec *cdiv1.StorageProfileSpec
var cloneStorageClassName string
var sourcePvc *v1.PersistentVolumeClaim
var targetPvc *v1.PersistentVolumeClaim
var origSpec *cdiv1.CDIConfigSpec
BeforeEach(func() {
config, err := f.CdiClient.CdiV1beta1().CDIConfigs().Get(context.TODO(), common.ConfigName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
origSpec = config.Spec.DeepCopy()
})
AfterEach(func() {
// Needs better cleanup, in the tests that actually change the overhead
By("Restoring CDIConfig to original state")
err := utils.UpdateCDIConfig(f.CrClient, func(config *cdiv1.CDIConfigSpec) {
origSpec.DeepCopyInto(config)
})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
config, err := f.CdiClient.CdiV1beta1().CDIConfigs().Get(context.TODO(), common.ConfigName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return reflect.DeepEqual(config.Spec, *origSpec)
}, 30*time.Second, time.Second).Should(BeTrue())
if sourcePvc != nil {
By("[AfterEach] Clean up source PVC")
err := f.DeletePVC(sourcePvc)
Expect(err).ToNot(HaveOccurred())
}
if targetPvc != nil {
By("[AfterEach] Clean up target PVC")
err := f.DeletePVC(targetPvc)
Expect(err).ToNot(HaveOccurred())
}
})
It("[test_id:6693]Should clone imported data and retain transfer pods after completion", func() {
smartApplicable := f.IsSnapshotStorageClassAvailable()
sc, err := f.K8sClient.StorageV1().StorageClasses().Get(context.TODO(), f.SnapshotSCName, metav1.GetOptions{})
if err == nil {
value, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"]
if smartApplicable && ok && strings.Compare(value, "true") == 0 {
Skip("Cannot test host assisted cloning for within namespace when all pvcs are smart clone capable.")
}
}
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
By(fmt.Sprintf("Create new datavolume %s", dataVolume.Name))
dataVolume, err = utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
pvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewCloningDataVolume("target-dv", "1Gi", pvc)
targetDV.Annotations[controller.AnnPodRetainAfterCompletion] = "true"
By(fmt.Sprintf("Create new target datavolume %s", targetDV.Name))
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target datavolume phase Succeeded")
utils.WaitForDataVolumePhaseWithTimeout(f, targetDataVolume.Namespace, cdiv1.Succeeded, targetDV.Name, cloneCompleteTimeout)
By("Find cloner source pod after completion")
cloner, err := utils.FindPodBySuffixOnce(f.K8sClient, targetDataVolume.Namespace, common.ClonerSourcePodNameSuffix, common.CDILabelSelector)
Expect(err).ToNot(HaveOccurred())
Expect(cloner.DeletionTimestamp).To(BeNil())
By("Find upload pod after completion")
uploader, err := utils.FindPodByPrefixOnce(f.K8sClient, targetDataVolume.Namespace, "cdi-upload-", common.CDILabelSelector)
Expect(err).ToNot(HaveOccurred())
Expect(uploader.DeletionTimestamp).To(BeNil())
})
Context("DataVolume Garbage Collection", func() {
var (
ns string
err error
config *cdiv1.CDIConfig
origSpec *cdiv1.CDIConfigSpec
)
BeforeEach(func() {
ns = f.Namespace.Name
config, err = f.CdiClient.CdiV1beta1().CDIConfigs().Get(context.TODO(), common.ConfigName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
origSpec = config.Spec.DeepCopy()
})
AfterEach(func() {
By("Restoring CDIConfig to original state")
err = utils.UpdateCDIConfig(f.CrClient, func(config *cdiv1.CDIConfigSpec) {
origSpec.DeepCopyInto(config)
})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
config, err = f.CdiClient.CdiV1beta1().CDIConfigs().Get(context.TODO(), common.ConfigName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return reflect.DeepEqual(config.Spec, *origSpec)
}, timeout, pollingInterval).Should(BeTrue())
})
verifyGC := func(dvName string) {
VerifyGC(f, dvName, ns, false, nil)
}
verifyDisabledGC := func(dvName string) {
VerifyDisabledGC(f, dvName, ns)
}
enableGcAndAnnotateLegacyDv := func(dvName string) {
EnableGcAndAnnotateLegacyDv(f, dvName, ns)
}
DescribeTable("Should", func(ttl int, verifyGCFunc, additionalTestFunc func(dvName string)) {
SetConfigTTL(f, ttl)
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
By(fmt.Sprintf("Create new datavolume %s", dataVolume.Name))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, ns, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
verifyGCFunc(dataVolume.Name)
pvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewCloningDataVolume("target-dv", "1Gi", pvc)
delete(targetDV.Annotations, controller.AnnDeleteAfterCompletion)
By(fmt.Sprintf("Create new target datavolume %s", targetDV.Name))
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, ns, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target datavolume phase Succeeded")
utils.WaitForDataVolumePhaseWithTimeout(f, targetDataVolume.Namespace, cdiv1.Succeeded, targetDV.Name, cloneCompleteTimeout)
verifyGCFunc(targetDV.Name)
if additionalTestFunc != nil {
additionalTestFunc(dataVolume.Name)
additionalTestFunc(targetDV.Name)
}
},
Entry("[test_id:8565] garbage collect dvs after completion when TTL is 0", 0, verifyGC, nil),
Entry("[test_id:8569] Add DeleteAfterCompletion annotation to a legacy DV", -1, verifyDisabledGC, enableGcAndAnnotateLegacyDv),
)
})
ClonerBehavior := func(storageClass string, cloneType string) {
DescribeTable("[test_id:1354]Should clone data within same namespace", func(targetSize string) {
By(storageClass)
pvcDef := utils.NewPVCDefinition(sourcePVCName, "1Gi", nil, nil)
pvcDef.Namespace = f.Namespace.Name
sourcePvc = f.CreateAndPopulateSourcePVC(pvcDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
doFileBasedCloneTest(f, pvcDef, f.Namespace, "target-dv", targetSize)
},
Entry("with same target size", "1Gi"),
Entry("with larger target", "2Gi"),
)
It("[test_id:4953]Should clone imported data within same namespace and preserve fsGroup", func() {
diskImagePath := filepath.Join(testBaseDir, testFile)
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
pvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
// Create targetPvc in new NS.
targetDV := utils.NewCloningDataVolume("target-dv", "1Gi", pvc)
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
targetPvc, err := utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
fmt.Fprintf(GinkgoWriter, "INFO: wait for target DV phase Succeeded: %s\n", targetPvc.Name)
utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, targetDV.Name, 3*90*time.Second)
sourcePvcDiskGroup, err := f.GetDiskGroup(f.Namespace, pvc, true)
fmt.Fprintf(GinkgoWriter, "INFO: %s\n", sourcePvcDiskGroup)
Expect(err).ToNot(HaveOccurred())
By("verifying pvc content")
sourceMD5, err := f.GetMD5(f.Namespace, pvc, diskImagePath, 0)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
completeClone(f, f.Namespace, targetPvc, diskImagePath, sourceMD5, sourcePvcDiskGroup)
})
It("[test_id:6784]Should clone imported data from SourceRef PVC DataSource", func() {
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
By(fmt.Sprintf("Create new datavolume %s", dataVolume.Name))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
pvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDS := utils.NewDataSource("test-datasource", pvc.Namespace, pvc.Name, pvc.Namespace)
By(fmt.Sprintf("Create new datasource %s", targetDS.Name))
targetDataSource, err := f.CdiClient.CdiV1beta1().DataSources(pvc.Namespace).Create(context.TODO(), targetDS, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewDataVolumeWithSourceRef("target-dv", "1Gi", targetDataSource.Namespace, targetDataSource.Name)
By(fmt.Sprintf("Create new target datavolume %s", targetDV.Name))
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target datavolume phase Succeeded")
utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, targetDV.Name, 3*90*time.Second)
})
DescribeTable("[test_id:1355]Should clone data across different namespaces", func(targetSize string) {
pvcDef := utils.NewPVCDefinition(sourcePVCName, "1Gi", nil, nil)
pvcDef.Namespace = f.Namespace.Name
sourcePvc = f.CreateAndPopulateSourcePVC(pvcDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
targetNs, err := f.CreateNamespace(f.NsPrefix, map[string]string{
framework.NsPrefixLabel: f.NsPrefix,
})
Expect(err).NotTo(HaveOccurred())
f.AddNamespaceToDelete(targetNs)
doFileBasedCloneTest(f, pvcDef, targetNs, "target-dv", targetSize)
},
Entry("with same target size", "1Gi"),
Entry("with bigger target size", "2Gi"),
)
It("[test_id:4954]Should clone data across different namespaces when source initially in use", func() {
pvcDef := utils.NewPVCDefinition(sourcePVCName, "1Gi", nil, nil)
pvcDef.Namespace = f.Namespace.Name
sourcePvc = f.CreateAndPopulateSourcePVC(pvcDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
targetNs, err := f.CreateNamespace(f.NsPrefix, map[string]string{
framework.NsPrefixLabel: f.NsPrefix,
})
Expect(err).NotTo(HaveOccurred())
f.AddNamespaceToDelete(targetNs)
doInUseCloneTest(f, pvcDef, targetNs, "target-dv")
})
It("[posneg:negative][test_id:6612]Clone with CSI as PVC source with target name that already exists", func() {
if cloneType == "network" {
Skip("Cannot simulate target pvc name conflict for host-assisted clone ")
}
pvcDef := utils.NewPVCDefinition(sourcePVCName, "1Gi", nil, nil)
sourcePvc = f.CreateAndPopulateSourcePVC(pvcDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
targetNamespaceName := f.Namespace.Name
// 1. use the srcPvc so the clone cannot be started
pod, err := f.CreateExecutorPodWithPVC("temp-pod", f.Namespace.Name, sourcePvc)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
pod, err = f.K8sClient.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), pod.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase == v1.PodRunning
}, 90*time.Second, 2*time.Second).Should(BeTrue())
// 2. Create a clone DataVolume
targetDV := utils.NewCloningDataVolume("target-pvc", "1Gi", sourcePvc)
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, targetNamespaceName, targetDV)
Expect(err).ToNot(HaveOccurred())
actualCloneType := utils.GetCloneType(f.CdiClient, dataVolume)
if actualCloneType == "snapshot" {
f.ExpectEvent(targetNamespaceName).Should(ContainSubstring(dvc.SmartCloneSourceInUse))
} else if actualCloneType == "csivolumeclone" {
f.ExpectEvent(targetNamespaceName).Should(ContainSubstring(dvc.CSICloneSourceInUse))
} else {
Fail(fmt.Sprintf("Unknown clonetype %s", actualCloneType))
}
// 3. Knowing that clone cannot yet advance, Create targetPvc with a "conflicting name"
By(fmt.Sprintf("Creating target pvc: %s/target-pvc", targetNamespaceName))
annotations := map[string]string{"cdi.kubevirt.io/conflicting-pvc": dataVolumeName}
targetPvc, err := utils.CreatePVCFromDefinition(f.K8sClient, targetNamespaceName,
utils.NewPVCDefinition("target-pvc", "1Gi", annotations, nil))
Expect(err).ToNot(HaveOccurred())
f.ForceBindIfWaitForFirstConsumer(targetPvc)
err = f.K8sClient.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
//verify event
f.ExpectEvent(targetNamespaceName).Should(ContainSubstring(dvc.ErrResourceExists))
})
It("[test_id:1356]Should not clone anything when CloneOf annotation exists", func() {
pvcDef := utils.NewPVCDefinition(sourcePVCName, "1Gi", nil, nil)
sourcePvc = f.CreateAndPopulateSourcePVC(pvcDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
cloneOfAnnoExistenceTest(f, f.Namespace.Name)
})
It("[posneg:negative][test_id:3617]Should clone across nodes when multiple local filesystem volumes exist,", func() {
// Get nodes, need at least 2
nodeList, err := f.K8sClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
if len(nodeList.Items) < 2 {
Skip("Need at least 2 nodes to copy accross nodes")
}
nodeMap := make(map[string]bool)
for _, node := range nodeList.Items {
if ok := nodeMap[node.Name]; !ok {
nodeMap[node.Name] = true
}
}
// Find PVs and identify local storage, the PVs should already exist.
pvList, err := f.K8sClient.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
var sourcePV, targetPV *v1.PersistentVolume
var storageClassName string
// Verify we have PVs to at least 2 nodes.
pvNodeNames := make(map[string]int)
for _, pv := range pvList.Items {
if pv.Spec.NodeAffinity == nil || pv.Spec.NodeAffinity.Required == nil || len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) == 0 || (pv.Spec.VolumeMode != nil && *pv.Spec.VolumeMode == v1.PersistentVolumeBlock) {
// Not a local volume filesystem PV
continue
}
pvNode := pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values[0]
if pv.Spec.ClaimRef == nil {
// PV is available and not claimed.
if val, ok := pvNodeNames[pvNode]; !ok {
pvNodeNames[pvNode] = 1
} else {
pvNodeNames[pvNode] = val + 1
}
}
}
if len(pvNodeNames) < 2 {
Skip("Need filesystem PVs on at least 2 nodes to test")
}
// Find the source and target PVs so we can label them.
for _, pv := range pvList.Items {
if pv.Spec.NodeAffinity == nil || pv.Spec.NodeAffinity.Required == nil ||
len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) == 0 || pv.Status.Phase != v1.VolumeAvailable {
// Not an available local volume PV
continue
}
if sourcePV == nil {
if pv.Spec.StorageClassName != "" {
storageClassName = pv.Spec.StorageClassName
}
pvNode := pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values[0]
if ok, val := nodeMap[pvNode]; ok && val {
nodeMap[pvNode] = false
By("Labeling PV " + pv.Name + " as source")
sourcePV = &pv
if sourcePV.GetLabels() == nil {
sourcePV.SetLabels(make(map[string]string))
}
sourcePV.GetLabels()["source-pv"] = "yes"
sourcePV, err = f.K8sClient.CoreV1().PersistentVolumes().Update(context.TODO(), sourcePV, metav1.UpdateOptions{})
Expect(err).ToNot(HaveOccurred())
}
} else if targetPV == nil {
pvNode := pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values[0]
if ok, val := nodeMap[pvNode]; ok && val {
nodeMap[pvNode] = false
By("Labeling PV " + pv.Name + " as target")
targetPV = &pv
if targetPV.GetLabels() == nil {
targetPV.SetLabels(make(map[string]string))
}
targetPV.GetLabels()["target-pv"] = "yes"
targetPV, err = f.K8sClient.CoreV1().PersistentVolumes().Update(context.TODO(), targetPV, metav1.UpdateOptions{})
Expect(err).ToNot(HaveOccurred())
break
}
}
}
Expect(sourcePV).ToNot(BeNil())
Expect(targetPV).ToNot(BeNil())
// Source and target PVs have been annotated, now create PVCs with label selectors.
sourceSelector := make(map[string]string)
sourceSelector["source-pv"] = "yes"
sourcePVCDef := utils.NewPVCDefinitionWithSelector(sourcePVCName, "1Gi", storageClassName, sourceSelector, nil, nil)
sourcePVCDef.Namespace = f.Namespace.Name
sourcePVC := f.CreateAndPopulateSourcePVC(sourcePVCDef, sourcePodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
sourcePVC, err = f.K8sClient.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Get(context.TODO(), sourcePVC.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(sourcePVC.Spec.VolumeName).To(Equal(sourcePV.Name))
targetDV := utils.NewCloningDataVolume("target-dv", "1Gi", sourcePVCDef)
targetDV.Spec.PVC.StorageClassName = &storageClassName
targetLabelSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
"target-pv": "yes",
},
}
targetDV.Spec.PVC.Selector = &targetLabelSelector
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
Eventually(func() bool {
targetPvc, err = utils.WaitForPVC(f.K8sClient, dataVolume.Namespace, dataVolume.Name)
Expect(err).ToNot(HaveOccurred())
return targetPvc.Spec.VolumeName == targetPV.Name
}, 60, 1).Should(BeTrue())
fmt.Fprintf(GinkgoWriter, "INFO: wait for PVC claim phase: %s\n", targetPvc.Name)
utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
completeClone(f, f.Namespace, targetPvc, filepath.Join(testBaseDir, testFile), fillDataFSMD5sum, "")
})
It("[test_id:cnv-5569]Should clone data from filesystem to block", func() {
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}
if cloneType == "csivolumeclone" || cloneType == "snapshot" {
Skip("csivolumeclone only works for the same volumeMode")
}
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
sourcePvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewDataVolumeCloneToBlockPV("target-dv", "1Gi", sourcePvc.Namespace, sourcePvc.Name, f.BlockSCName)
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
targetPvc, err := utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
By("Wait for target PVC Bound phase")
utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, "target-dv", cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
By("Source file system pvc md5summing")
diskImagePath := filepath.Join(testBaseDir, testFile)
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, diskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
By("Target block pvc md5summing")
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, testBaseDir, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
Expect(sourceMD5 == targetMD5).To(BeTrue())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
})
It("[test_id:cnv-5570]Should clone data from block to filesystem", func() {
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}
if cloneType == "csivolumeclone" {
Skip("csivolumeclone only works for the same volumeMode")
}
dataVolume := utils.NewDataVolumeWithHTTPImportToBlockPV(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs), f.BlockSCName)
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
sourcePvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
volumeMode := v1.PersistentVolumeMode(v1.PersistentVolumeFilesystem)
targetDV := utils.NewDataVolumeForImageCloning("target-dv", "1.1Gi", sourcePvc.Namespace, sourcePvc.Name, nil, &volumeMode)
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
targetPvc, err := utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
By("Wait for target PVC Bound phase")
utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, "target-dv", cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
By("Source block pvc md5summing")
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, testBaseDir, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
By("Target file system pvc md5summing")
diskImagePath := filepath.Join(testBaseDir, testFile)
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, diskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
Expect(sourceMD5 == targetMD5).To(BeTrue())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
})
It("bz:2079781 Should clone data from filesystem to block, when using storage API ", func() {
SetFilesystemOverhead(f, "0.50", "0.50")
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}
if cloneType == "csivolumeclone" || cloneType == "snapshot" {
Skip("csivolumeclone only works for the same volumeMode")
}
dataVolume := utils.NewDataVolumeWithHTTPImportAndStorageSpec(dataVolumeName, "2Gi", fmt.Sprintf(utils.LargeVirtualDiskQcow, f.CdiInstallNs))
filesystem := v1.PersistentVolumeFilesystem
dataVolume.Spec.Storage.VolumeMode = &filesystem
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
sourcePvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewDataVolumeCloneToBlockPVStorageAPI("target-dv", "2Gi", sourcePvc.Namespace, sourcePvc.Name, f.BlockSCName)
targetDV.Spec.Storage.AccessModes = []v1.PersistentVolumeAccessMode{v1.ReadWriteMany}
tagretDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
targetPvc, err := utils.WaitForPVC(f.K8sClient, tagretDataVolume.Namespace, tagretDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
By("Wait for target PVC Bound phase")
utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, "target-dv", cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
By("Source file system pvc md5summing")
diskImagePath := filepath.Join(testBaseDir, testFile)
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, diskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
By("Target block pvc md5summing")
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, testBaseDir, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
Expect(sourceMD5 == targetMD5).To(BeTrue())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
})
It("Should clone data from fs to fs while using calculated storage size", func() {
// should clone from fs to fs using the same size in spec.storage.size
// source pvc might be bigger than the size, but the clone should work
// as the actual data is the same
volumeMode := v1.PersistentVolumeMode(v1.PersistentVolumeFilesystem)
dataVolume := utils.NewDataVolumeWithHTTPImportAndStorageSpec(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
dataVolume.Spec.Storage.VolumeMode = &volumeMode
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
sourcePvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewDataVolumeForImageCloningAndStorageSpec("target-dv", "1Gi", sourcePvc.Namespace, sourcePvc.Name, nil, &volumeMode)
controller.AddAnnotation(targetDV, controller.AnnDeleteAfterCompletion, "false")
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
targetPvc, err := utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target PVC Bound phase")
err = utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
Expect(err).ToNot(HaveOccurred())
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, "target-dv", cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
By("Source file system pvc md5summing")
diskImagePath := filepath.Join(testBaseDir, testFile)
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, diskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
By("Target file system pvc md5summing")
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, diskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
Expect(sourceMD5 == targetMD5).To(BeTrue())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
})
It("[rfe_id:1126][crit:High][vendor:cnv-qe@redhat.com][level:component] Should fail with Event when cloning into a smaller sized data volume", func() {
By("Creating a source from a real image")
sourceDv := utils.NewDataVolumeWithHTTPImport("source-dv", "200Mi", tinyCoreIsoURL())
filesystem := v1.PersistentVolumeFilesystem
sourceDv.Spec.PVC.VolumeMode = &filesystem
sourceDv, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, sourceDv)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(sourceDv)
By("Waiting for import to be completed")
utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, sourceDv.Name, 3*90*time.Second)
By("Cloning from the source DataVolume to under sized target")
targetDv := utils.NewDataVolumeForImageCloningAndStorageSpec("target-dv", "100Mi",
f.Namespace.Name,
sourceDv.Name,
sourceDv.Spec.PVC.StorageClassName,
sourceDv.Spec.PVC.VolumeMode)
targetDv, err = utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDv)
Expect(err).ToNot(HaveOccurred())
// As of now, csi and smart clone check the values before the pvc is created, and the network clone
// checks it in the upload phase, so it needs a PVC, this might be improved
if cloneType == "network" {
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDv)
}
f.ExpectEvent(f.Namespace.Name).Should(ContainSubstring(controller.ErrIncompatiblePVC))
})
It("should handle a pre populated PVC during clone", func() {
By(fmt.Sprintf("initializing target PVC %s", dataVolumeName))
targetPodFillerName := fmt.Sprintf("%s-filler-pod", dataVolumeName)
annotations := map[string]string{controller.AnnPopulatedFor: dataVolumeName}
targetPvcDef := utils.NewPVCDefinition(dataVolumeName, "1G", annotations, nil)
targetPvc = f.CreateAndPopulateSourcePVC(targetPvcDef, targetPodFillerName, fillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
By(fmt.Sprintf("initializing source PVC %s witg different data", dataVolumeName))
alternativeFillData := "987654321"
alternativeFillCommand := "echo \"" + alternativeFillData + "\" >> " + testBaseDir
sourcePodFillerName := fmt.Sprintf("%s-filler-pod", "sourcepvcempty")
srcPvcDef := utils.NewPVCDefinition("sourcepvcempty", "1G", nil, nil)
sourcePvc := f.CreateAndPopulateSourcePVC(srcPvcDef, sourcePodFillerName, alternativeFillCommand+testFile+"; chmod 660 "+testBaseDir+testFile)
dataVolume := utils.NewDataVolumeForImageCloning(dataVolumeName, "1G",
sourcePvc.Namespace, sourcePvc.Name, sourcePvc.Spec.StorageClassName, sourcePvc.Spec.VolumeMode)
Expect(dataVolume).ToNot(BeNil())
By(fmt.Sprintf("creating new populated datavolume %s", dataVolume.Name))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
dv, err := f.CdiClient.CdiV1beta1().DataVolumes(f.Namespace.Name).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
pvcName := dv.Annotations["cdi.kubevirt.io/storage.prePopulated"]
return pvcName == targetPvcDef.Name &&
dv.Status.Phase == cdiv1.Succeeded &&
string(dv.Status.Progress) == "N/A"
}, timeout, pollingInterval).Should(BeTrue(), "DV Should succeed with storage.prePopulated==pvcName")
By("Verify no clone - the contents of prepopulated volume did not change")
md5Match, err := f.VerifyTargetPVCContentMD5(f.Namespace, targetPvc, filepath.Join(testBaseDir, testFile), fillDataFSMD5sum)
Expect(err).To(BeNil())
Expect(md5Match).To(BeTrue())
})
DescribeTable("Should clone with empty volume size without using size-detection pod",
func(sourceVolumeMode, targetVolumeMode v1.PersistentVolumeMode) {
// When cloning without defining the target's storage size, the source's size can be attainable
// by different means depending on the clone type and the volume mode used.
// Either if "block" is used as volume mode or smart/csi cloning is used as clone strategy,
// the value is simply extracted from the original PVC's spec.
var sourceSCName string
var targetSCName string
targetDiskImagePath := filepath.Join(testBaseDir, testFile)
sourceDiskImagePath := filepath.Join(testBaseDir, testFile)
if cloneType == "network" && sourceVolumeMode == v1.PersistentVolumeFilesystem {
Skip("Clone strategy and volume mode combination requires of size-detection pod")
}
if sourceVolumeMode == v1.PersistentVolumeBlock {
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}
sourceSCName = f.BlockSCName
sourceDiskImagePath = testBaseDir
}
if targetVolumeMode == v1.PersistentVolumeBlock {
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}
targetSCName = f.BlockSCName
targetDiskImagePath = testBaseDir
}
// Create the source DV
dataVolume := utils.NewDataVolumeWithHTTPImportAndStorageSpec(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
dataVolume.Spec.Storage.VolumeMode = &sourceVolumeMode
if sourceSCName != "" {
dataVolume.Spec.Storage.StorageClassName = &sourceSCName
}
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(dataVolume)
sourcePvc, err = f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
By("Wait for source DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, dataVolumeName, cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
// We attempt to create the sizeless DV
targetDV := utils.NewDataVolumeForCloningWithEmptySize("target-dv", sourcePvc.Namespace, sourcePvc.Name, nil, &targetVolumeMode)
if targetSCName != "" {
targetDV.Spec.Storage.StorageClassName = &targetSCName
}
controller.AddAnnotation(targetDV, controller.AnnDeleteAfterCompletion, "false")
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
targetPvc, err = utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhaseWithTimeout(f, f.Namespace.Name, cdiv1.Succeeded, "target-dv", cloneCompleteTimeout)
Expect(err).ToNot(HaveOccurred())
By("Source file system pvc md5summing")
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, sourceDiskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
By("Target file system pvc md5summing")
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, targetDiskImagePath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
By("Checksum comparison")
Expect(sourceMD5).To(Equal(targetMD5))
},
Entry("[test_id:8492]Block to block (empty storage size)", v1.PersistentVolumeBlock, v1.PersistentVolumeBlock),
Entry("[test_id:8491]Block to filesystem (empty storage size)", v1.PersistentVolumeBlock, v1.PersistentVolumeFilesystem),
Entry("[test_id:8490]Filesystem to filesystem(empty storage size)", v1.PersistentVolumeFilesystem, v1.PersistentVolumeFilesystem),
)
Context("WaitForFirstConsumer status with advanced cloning methods", func() {
var wffcStorageClass *storagev1.StorageClass
BeforeEach(func() {
if cloneType != "csivolumeclone" && cloneType != "snapshot" {
Skip("relevant for csi/smart clones only")
}
sc, err := f.K8sClient.StorageV1().StorageClasses().Get(context.TODO(), utils.DefaultStorageClass.GetName(), metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
if sc.VolumeBindingMode == nil || *sc.VolumeBindingMode == storagev1.VolumeBindingImmediate {
sc, err = f.CreateWFFCVariationOfStorageClass(sc)
Expect(err).ToNot(HaveOccurred())
wffcStorageClass = sc
Eventually(func() bool {
_, err := f.CdiClient.CdiV1beta1().StorageProfiles().Get(context.TODO(), wffcStorageClass.Name, metav1.GetOptions{})
return err == nil
}, time.Minute, time.Second).Should(BeTrue())
spec, err := utils.GetStorageProfileSpec(f.CdiClient, wffcStorageClass.Name)
Expect(err).ToNot(HaveOccurred())
if cloneType == "csivolumeclone" {
utils.ConfigureCloneStrategy(f.CrClient, f.CdiClient, wffcStorageClass.Name, spec, cdiv1.CloneStrategyCsiClone)
} else if cloneType == "snapshot" {
utils.ConfigureCloneStrategy(f.CrClient, f.CdiClient, wffcStorageClass.Name, spec, cdiv1.CloneStrategySnapshot)
}
}
})
It("should report correct status for smart/CSI clones", func() {
volumeMode := v1.PersistentVolumeMode(v1.PersistentVolumeFilesystem)
dataVolume := utils.NewDataVolumeWithHTTPImportAndStorageSpec(dataVolumeName, "1Gi", fmt.Sprintf(utils.TinyCoreIsoURL, f.CdiInstallNs))
dataVolume.Spec.Storage.VolumeMode = &volumeMode
if wffcStorageClass != nil {
dataVolume.Spec.Storage.StorageClassName = &wffcStorageClass.Name
}
dataVolume.Annotations[cont.AnnImmediateBinding] = "true"
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())
By("Waiting for import to be completed")
err = utils.WaitForDataVolumePhase(f, f.Namespace.Name, cdiv1.Succeeded, dataVolume.Name)
Expect(err).ToNot(HaveOccurred())
sourcePvc, err := f.K8sClient.CoreV1().PersistentVolumeClaims(dataVolume.Namespace).Get(context.TODO(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetDV := utils.NewDataVolumeForImageCloningAndStorageSpec("target-dv", "1Gi", sourcePvc.Namespace, sourcePvc.Name, nil, &volumeMode)
if wffcStorageClass != nil {
targetDV.Spec.Storage.StorageClassName = &wffcStorageClass.Name
}
targetDataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, targetDV)
Expect(err).ToNot(HaveOccurred())
targetPvc, err := utils.WaitForPVC(f.K8sClient, targetDataVolume.Namespace, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
By("Ensure WFFC is reported to reflect the situation correctly")
err = utils.WaitForDataVolumePhase(f, targetDataVolume.Namespace, cdiv1.WaitForFirstConsumer, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
// Force bind to ensure integrity after first consumer
f.ForceBindPvcIfDvIsWaitForFirstConsumer(targetDataVolume)
By("Wait for target PVC Bound phase")
err = utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, v1.ClaimBound, targetPvc.Name)
Expect(err).ToNot(HaveOccurred())
By("Wait for target DV Succeeded phase")
err = utils.WaitForDataVolumePhase(f, f.Namespace.Name, cdiv1.Succeeded, targetDataVolume.Name)
Expect(err).ToNot(HaveOccurred())
By("Verify content")
same, err := f.VerifyTargetPVCContentMD5(f.Namespace, targetPvc, utils.DefaultImagePath, utils.UploadFileMD5, utils.UploadFileSize)
Expect(err).ToNot(HaveOccurred())
Expect(same).To(BeTrue())
By("Deleting verifier pod")
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
})
})
}
Context("HostAssisted Clone", func() {
BeforeEach(func() {
cloneStorageClassName = utils.DefaultStorageClass.GetName()
By(fmt.Sprintf("Get original storage profile: %s", cloneStorageClassName))
spec, err := utils.GetStorageProfileSpec(f.CdiClient, cloneStorageClassName)
Expect(err).ToNot(HaveOccurred())
originalProfileSpec = spec
By(fmt.Sprintf("configure storage profile %s", cloneStorageClassName))
utils.ConfigureCloneStrategy(f.CrClient, f.CdiClient, cloneStorageClassName, originalProfileSpec, cdiv1.CloneStrategyHostAssisted)
})
AfterEach(func() {
By("[AfterEach] Restore the profile")
utils.UpdateStorageProfile(f.CrClient, cloneStorageClassName, *originalProfileSpec)
})
ClonerBehavior(cloneStorageClassName, "network")
})
Context("SmartClone", func() {
BeforeEach(func() {
if !f.IsSnapshotStorageClassAvailable() {
Skip("SmartClone does not work without SnapshotStorageClass")
}
cloneStorageClassName = f.SnapshotSCName
By(fmt.Sprintf("Get original storage profile: %s", cloneStorageClassName))
spec, err := utils.GetStorageProfileSpec(f.CdiClient, cloneStorageClassName)
Expect(err).ToNot(HaveOccurred())
By(fmt.Sprintf("Got original storage profile: %v", spec))
originalProfileSpec = spec
})
AfterEach(func() {
By("[AfterEach] Restore the profile")
utils.UpdateStorageProfile(f.CrClient, cloneStorageClassName, *originalProfileSpec)
})
ClonerBehavior(cloneStorageClassName, "snapshot")
})
Context("[rfe_id:4219]CSI Clone", func() {
BeforeEach(func() {
if !f.IsCSIVolumeCloneStorageClassAvailable() {
Skip("CSI Clone does not work without a capable storage class")
}
cloneStorageClassName = f.CsiCloneSCName
By(fmt.Sprintf("Get original storage profile: %s", cloneStorageClassName))
spec, err := utils.GetStorageProfileSpec(f.CdiClient, cloneStorageClassName)
Expect(err).ToNot(HaveOccurred())
originalProfileSpec = spec
By(fmt.Sprintf("configure storage profile %s", cloneStorageClassName))
utils.ConfigureCloneStrategy(f.CrClient, f.CdiClient, cloneStorageClassName, originalProfileSpec, cdiv1.CloneStrategyCsiClone)
})
AfterEach(func() {
By("[AfterEach] Restore the profile")
utils.UpdateStorageProfile(f.CrClient, cloneStorageClassName, *originalProfileSpec)
})
ClonerBehavior(cloneStorageClassName, "csivolumeclone")
})
// The size-detection pod is only used in cloning when three requirements are met:
// 1. The clone manifest is created without defining a storage size.
// 2. 'Filesystem' is used as volume mode.
// 3. 'HostAssisted' is used as clone strategy.
Context("Clone with empty size using the size-detection pod", func() {
diskImagePath := filepath.Join(testBaseDir, testFile)
volumeMode := v1.PersistentVolumeMode(v1.PersistentVolumeFilesystem)
deleteAndWaitForVerifierPod := func() {
By("Deleting verifier pod")
err := utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, utils.VerifierPodName, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
}
deleteAndWaitForSizeDetectionPod := func() {
By("Deleting size-detection pod")
pod, err := utils.FindPodByPrefix(f.K8sClient, f.Namespace.Name, sizeDetectionPodPrefix, "")
Expect(err).ToNot(HaveOccurred())
err = utils.DeletePod(f.K8sClient, pod, f.Namespace.Name)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitPodDeleted(f.K8sClient, pod.Name, f.Namespace.Name, verifyPodDeletedTimeout)
Expect(err).ToNot(HaveOccurred())
}
compareCloneWithSource := func(sourcePvc, targetPvc *v1.PersistentVolumeClaim, sourceImgPath, targetImgPath string) {
By("Source file system pvc md5summing")
sourceMD5, err := f.GetMD5(f.Namespace, sourcePvc, sourceImgPath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())
deleteAndWaitForVerifierPod()
By("Target file system pvc md5summing")
targetMD5, err := f.GetMD5(f.Namespace, targetPvc, targetImgPath, crossVolumeModeCloneMD5NumBytes)
Expect(err).ToNot(HaveOccurred())