forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone_test.go
815 lines (649 loc) · 31.6 KB
/
clone_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
package tests
import (
"context"
"fmt"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/testsuite"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
virtsnapshot "kubevirt.io/api/snapshot"
"kubevirt.io/api/snapshot/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
clonev1alpha1 "kubevirt.io/api/clone/v1alpha1"
virtv1 "kubevirt.io/api/core/v1"
instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1"
"kubevirt.io/client-go/kubecli"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/tests/console"
cd "kubevirt.io/kubevirt/tests/containerdisk"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libinstancetype"
"kubevirt.io/kubevirt/tests/libstorage"
"kubevirt.io/kubevirt/tests/libvmi"
)
const (
vmAPIGroup = "kubevirt.io"
)
type loginFunction func(*virtv1.VirtualMachineInstance) error
var _ = Describe("[Serial]VirtualMachineClone Tests", Serial, func() {
var err error
var virtClient kubecli.KubevirtClient
BeforeEach(func() {
virtClient = kubevirt.Client()
EnableFeatureGate(virtconfig.SnapshotGate)
format.MaxLength = 0
})
createVM := func(options ...libvmi.Option) (vm *virtv1.VirtualMachine) {
vmi := libvmi.NewCirros(options...)
vmi.Namespace = testsuite.GetTestNamespace(nil)
vm = libvmi.NewVirtualMachine(vmi)
vm.Annotations = vmi.Annotations
vm.Labels = vmi.Labels
vm.Spec.Template.ObjectMeta.Annotations = vmi.Annotations
vm.Spec.Template.ObjectMeta.Labels = vmi.Labels
By(fmt.Sprintf("Creating VM %s", vm.Name))
vm, err := virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm)
Expect(err).ShouldNot(HaveOccurred())
return
}
createSnapshot := func(vm *virtv1.VirtualMachine) *v1alpha1.VirtualMachineSnapshot {
var err error
snapshot := &v1alpha1.VirtualMachineSnapshot{
ObjectMeta: v1.ObjectMeta{
Name: "snapshot-" + vm.Name,
Namespace: vm.Namespace,
},
Spec: v1alpha1.VirtualMachineSnapshotSpec{
Source: k8sv1.TypedLocalObjectReference{
APIGroup: pointer.String(vmAPIGroup),
Kind: "VirtualMachine",
Name: vm.Name,
},
},
}
snapshot, err = virtClient.VirtualMachineSnapshot(snapshot.Namespace).Create(context.Background(), snapshot, v1.CreateOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return snapshot
}
waitSnapshotReady := func(snapshot *v1alpha1.VirtualMachineSnapshot) *v1alpha1.VirtualMachineSnapshot {
var err error
EventuallyWithOffset(1, func() bool {
snapshot, err = virtClient.VirtualMachineSnapshot(snapshot.Namespace).Get(context.Background(), snapshot.Name, v1.GetOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return snapshot.Status != nil && snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse
}, 180*time.Second, time.Second).Should(BeTrue(), "snapshot should be ready")
return snapshot
}
waitSnapshotContentsExist := func(snapshot *v1alpha1.VirtualMachineSnapshot) *v1alpha1.VirtualMachineSnapshot {
var contentsName string
EventuallyWithOffset(1, func() error {
snapshot, err = virtClient.VirtualMachineSnapshot(snapshot.Namespace).Get(context.Background(), snapshot.Name, v1.GetOptions{})
ExpectWithOffset(2, err).ToNot(HaveOccurred())
if snapshot.Status == nil {
return fmt.Errorf("snapshot's status is nil")
}
if snapshot.Status.VirtualMachineSnapshotContentName != nil {
contentsName = *snapshot.Status.VirtualMachineSnapshotContentName
} else {
return fmt.Errorf("vm snapshot contents name is nil")
}
return nil
}, 30*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
EventuallyWithOffset(1, func() error {
_, err := virtClient.VirtualMachineSnapshotContent(snapshot.Namespace).Get(context.Background(), contentsName, v1.GetOptions{})
return err
}).ShouldNot(HaveOccurred())
return snapshot
}
generateCloneFromVMWithParams := func(sourceVM *virtv1.VirtualMachine, targetVMName string) *clonev1alpha1.VirtualMachineClone {
vmClone := kubecli.NewMinimalCloneWithNS("testclone", sourceVM.Namespace)
cloneSourceRef := &k8sv1.TypedLocalObjectReference{
APIGroup: pointer.String(vmAPIGroup),
Kind: "VirtualMachine",
Name: sourceVM.Name,
}
cloneTargetRef := cloneSourceRef.DeepCopy()
cloneTargetRef.Name = targetVMName
vmClone.Spec.Source = cloneSourceRef
vmClone.Spec.Target = cloneTargetRef
return vmClone
}
generateCloneFromSnapshot := func(snapshot *v1alpha1.VirtualMachineSnapshot, targetVMName string) *clonev1alpha1.VirtualMachineClone {
vmClone := kubecli.NewMinimalCloneWithNS("testclone", snapshot.Namespace)
cloneSourceRef := &k8sv1.TypedLocalObjectReference{
APIGroup: pointer.String(virtsnapshot.GroupName),
Kind: "VirtualMachineSnapshot",
Name: snapshot.Name,
}
cloneTargetRef := &k8sv1.TypedLocalObjectReference{
APIGroup: pointer.String(vmAPIGroup),
Kind: "VirtualMachine",
Name: targetVMName,
}
vmClone.Spec.Source = cloneSourceRef
vmClone.Spec.Target = cloneTargetRef
return vmClone
}
createCloneAndWaitForFinish := func(vmClone *clonev1alpha1.VirtualMachineClone) {
By(fmt.Sprintf("Creating clone object %s", vmClone.Name))
vmClone, err = virtClient.VirtualMachineClone(vmClone.Namespace).Create(context.Background(), vmClone, v1.CreateOptions{})
Expect(err).ShouldNot(HaveOccurred())
By(fmt.Sprintf("Waiting for the clone %s to finish", vmClone.Name))
Eventually(func() clonev1alpha1.VirtualMachineClonePhase {
vmClone, err = virtClient.VirtualMachineClone(vmClone.Namespace).Get(context.Background(), vmClone.Name, v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
return vmClone.Status.Phase
}, 3*time.Minute, 3*time.Second).Should(Equal(clonev1alpha1.Succeeded), "clone should finish successfully")
}
expectVMRunnable := func(vm *virtv1.VirtualMachine, login loginFunction) *virtv1.VirtualMachine {
By(fmt.Sprintf("Starting VM %s", vm.Name))
vm = StartVirtualMachine(vm)
targetVMI, err := virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
err = login(targetVMI)
Expect(err).ShouldNot(HaveOccurred())
vm = StopVirtualMachine(vm)
return vm
}
filterOutIrrelevantKeys := func(in map[string]string) map[string]string {
out := make(map[string]string)
for key, val := range in {
if !strings.Contains(key, "kubevirt.io") && !strings.Contains(key, "kubemacpool.io") {
out[key] = val
}
}
return out
}
Context("VM clone", func() {
const (
targetVMName = "vm-clone-target"
cloneShouldEqualSourceMsgPattern = "cloned VM's %s should be equal to source"
key1 = "key1"
key2 = "key2"
value1 = "value1"
value2 = "value2"
)
var (
sourceVM, targetVM *virtv1.VirtualMachine
vmClone *clonev1alpha1.VirtualMachineClone
)
expectEqualStrMap := func(actual, expected map[string]string, expectationMsg string, keysToExclude ...string) {
expected = filterOutIrrelevantKeys(expected)
actual = filterOutIrrelevantKeys(actual)
for _, key := range keysToExclude {
delete(expected, key)
}
Expect(actual).To(Equal(expected), expectationMsg)
}
expectEqualLabels := func(targetVM, sourceVM *virtv1.VirtualMachine, keysToExclude ...string) {
expectEqualStrMap(targetVM.Labels, sourceVM.Labels, fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "labels"), keysToExclude...)
}
expectEqualTemplateLabels := func(targetVM, sourceVM *virtv1.VirtualMachine, keysToExclude ...string) {
expectEqualStrMap(targetVM.Spec.Template.ObjectMeta.Labels, sourceVM.Spec.Template.ObjectMeta.Labels, fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "template.labels"), keysToExclude...)
}
expectEqualAnnotations := func(targetVM, sourceVM *virtv1.VirtualMachine, keysToExclude ...string) {
expectEqualStrMap(targetVM.Annotations, sourceVM.Annotations, fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "annotations"), keysToExclude...)
}
expectEqualTemplateAnnotations := func(targetVM, sourceVM *virtv1.VirtualMachine, keysToExclude ...string) {
expectEqualStrMap(targetVM.Spec.Template.ObjectMeta.Annotations, sourceVM.Spec.Template.ObjectMeta.Annotations, fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "template.annotations"), keysToExclude...)
}
expectSpecsToEqualExceptForMacAddress := func(vm1, vm2 *virtv1.VirtualMachine) {
vm1Spec := vm1.Spec.DeepCopy()
vm2Spec := vm2.Spec.DeepCopy()
for _, spec := range []*virtv1.VirtualMachineSpec{vm1Spec, vm2Spec} {
for i := range spec.Template.Spec.Domain.Devices.Interfaces {
spec.Template.Spec.Domain.Devices.Interfaces[i].MacAddress = ""
}
}
Expect(vm1Spec).To(Equal(vm2Spec), fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "spec not including mac adresses"))
}
createVM := func(options ...libvmi.Option) (vm *virtv1.VirtualMachine) {
defaultOptions := []libvmi.Option{
libvmi.WithLabel(key1, value1),
libvmi.WithLabel(key2, value2),
libvmi.WithAnnotation(key1, value1),
libvmi.WithAnnotation(key2, value2),
}
options = append(options, defaultOptions...)
return createVM(options...)
}
generateCloneFromVM := func() *clonev1alpha1.VirtualMachineClone {
return generateCloneFromVMWithParams(sourceVM, targetVMName)
}
Context("[sig-compute]simple VM and cloning operations", decorators.SigCompute, func() {
expectVMRunnable := func(vm *virtv1.VirtualMachine) *virtv1.VirtualMachine {
return expectVMRunnable(vm, console.LoginToCirros)
}
It("simple default clone", func() {
sourceVM = createVM()
vmClone = generateCloneFromVM()
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
Expect(targetVM.Spec).To(Equal(sourceVM.Spec), fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "spec"))
expectEqualLabels(targetVM, sourceVM)
expectEqualAnnotations(targetVM, sourceVM)
expectEqualTemplateLabels(targetVM, sourceVM)
expectEqualTemplateAnnotations(targetVM, sourceVM)
By("Making sure snapshot and restore objects are cleaned up")
Expect(vmClone.Status.SnapshotName).To(BeNil())
Expect(vmClone.Status.RestoreName).To(BeNil())
})
It("simple clone with snapshot source", func() {
By("Creating a VM")
sourceVM = createVM()
Eventually(func() virtv1.VirtualMachinePrintableStatus {
sourceVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), sourceVM.Name, &v1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return sourceVM.Status.PrintableStatus
}, 30*time.Second, 1*time.Second).Should(Equal(virtv1.VirtualMachineStatusStopped))
By("Creating a snapshot from VM")
snapshot := createSnapshot(sourceVM)
snapshot = waitSnapshotContentsExist(snapshot)
// "waitSnapshotReady" is not used here intentionally since it's okay for a snapshot source
// to not be ready when creating a clone. Therefore, it's not deterministic if snapshot would actually
// be ready for this test or not.
// TODO: use snapshot's createDenyVolumeSnapshotCreateWebhook() once it's refactored to work outside
// of snapshot tests scope.
By("Deleting VM")
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(context.Background(), sourceVM.Name, &v1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
By("Creating a clone with a snapshot source")
vmClone = generateCloneFromSnapshot(snapshot, targetVMName)
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
By("Making sure snapshot source is not being deleted")
_, err = virtClient.VirtualMachineSnapshot(snapshot.Namespace).Get(context.Background(), snapshot.Name, v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
})
It("clone with only some of labels/annotations", func() {
sourceVM = createVM()
vmClone = generateCloneFromVM()
vmClone.Spec.LabelFilters = []string{
"*",
"!" + key2,
}
vmClone.Spec.AnnotationFilters = []string{
key1,
}
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
Expect(targetVM.Spec).To(Equal(sourceVM.Spec), fmt.Sprintf(cloneShouldEqualSourceMsgPattern, "spec"))
expectEqualLabels(targetVM, sourceVM, key2)
expectEqualAnnotations(targetVM, sourceVM, key2)
})
It("clone with only some of template.labels/template.annotations", func() {
sourceVM = createVM()
vmClone = generateCloneFromVM()
vmClone.Spec.Template.LabelFilters = []string{
"*",
"!" + key2,
}
vmClone.Spec.Template.AnnotationFilters = []string{
key1,
}
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
expectEqualTemplateLabels(targetVM, sourceVM, key2)
expectEqualTemplateAnnotations(targetVM, sourceVM, key2)
})
It("clone with changed MAC address", func() {
const newMacAddress = "BE-AD-00-00-BE-04"
sourceVM = createVM(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
libvmi.WithNetwork(virtv1.DefaultPodNetwork()),
)
srcInterfaces := sourceVM.Spec.Template.Spec.Domain.Devices.Interfaces
Expect(srcInterfaces).ToNot(BeEmpty())
srcInterface := srcInterfaces[0]
vmClone = generateCloneFromVM()
vmClone.Spec.NewMacAddresses = map[string]string{
srcInterface.Name: newMacAddress,
}
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
By("Finding target interface with same name as original")
var targetInterface *virtv1.Interface
targetInterfaces := targetVM.Spec.Template.Spec.Domain.Devices.Interfaces
for _, iface := range targetInterfaces {
if iface.Name == srcInterface.Name {
targetInterface = iface.DeepCopy()
break
}
}
Expect(targetInterface).ToNot(BeNil(), fmt.Sprintf("clone target does not have interface with name %s", srcInterface.Name))
By("Making sure new mac address is applied to target VM")
Expect(targetInterface.MacAddress).ToNot(Equal(srcInterface.MacAddress))
expectSpecsToEqualExceptForMacAddress(targetVM, sourceVM)
expectEqualLabels(targetVM, sourceVM)
expectEqualAnnotations(targetVM, sourceVM)
expectEqualTemplateLabels(targetVM, sourceVM)
expectEqualTemplateAnnotations(targetVM, sourceVM)
})
Context("regarding domain Firmware", func() {
It("clone with changed SMBios serial", func() {
const sourceSerial = "source-serial"
const targetSerial = "target-serial"
sourceVM = createVM(
func(vmi *virtv1.VirtualMachineInstance) {
vmi.Spec.Domain.Firmware = &virtv1.Firmware{Serial: sourceSerial}
},
)
vmClone = generateCloneFromVM()
vmClone.Spec.NewSMBiosSerial = pointer.String(targetSerial)
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
By("Making sure new smBios serial is applied to target VM")
Expect(targetVM.Spec.Template.Spec.Domain.Firmware).ToNot(BeNil())
Expect(sourceVM.Spec.Template.Spec.Domain.Firmware).ToNot(BeNil())
Expect(targetVM.Spec.Template.Spec.Domain.Firmware.Serial).ToNot(Equal(sourceVM.Spec.Template.Spec.Domain.Firmware.Serial))
expectEqualLabels(targetVM, sourceVM)
expectEqualAnnotations(targetVM, sourceVM)
expectEqualTemplateLabels(targetVM, sourceVM)
expectEqualTemplateAnnotations(targetVM, sourceVM)
})
It("should strip firmware UUID", func() {
const fakeFirmwareUUID = "fake-uuid"
sourceVM = createVM(
func(vmi *virtv1.VirtualMachineInstance) {
vmi.Spec.Domain.Firmware = &virtv1.Firmware{UUID: fakeFirmwareUUID}
},
)
vmClone = generateCloneFromVM()
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
By("Making sure new smBios serial is applied to target VM")
Expect(targetVM.Spec.Template.Spec.Domain.Firmware).ToNot(BeNil())
Expect(sourceVM.Spec.Template.Spec.Domain.Firmware).ToNot(BeNil())
Expect(targetVM.Spec.Template.Spec.Domain.Firmware.UUID).ToNot(Equal(sourceVM.Spec.Template.Spec.Domain.Firmware.UUID))
})
})
})
Context("[sig-storage]with more complicated VM", decorators.SigStorage, func() {
expectVMRunnable := func(vm *virtv1.VirtualMachine) *virtv1.VirtualMachine {
return expectVMRunnable(vm, console.LoginToAlpine)
}
createVMWithStorageClass := func(storageClass string, running bool) *virtv1.VirtualMachine {
vm := NewRandomVMWithDataVolumeWithRegistryImport(
cd.DataVolumeImportUrlForContainerDisk(cd.ContainerDiskAlpine),
testsuite.GetTestNamespace(nil),
storageClass,
k8sv1.ReadWriteOnce,
)
vm.Spec.Running = pointer.Bool(running)
vm, err := virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm)
Expect(err).ToNot(HaveOccurred())
if !running && libstorage.IsStorageClassBindingModeWaitForFirstConsumer(storageClass) {
return vm
}
for _, dvt := range vm.Spec.DataVolumeTemplates {
libstorage.EventuallyDVWith(vm.Namespace, dvt.Name, 180, HaveSucceeded())
}
return vm
}
Context("and no snapshot storage class", decorators.RequiresNoSnapshotStorageClass, func() {
var (
noSnapshotStorageClass string
)
Context("should reject source with non snapshotable volume", func() {
BeforeEach(func() {
noSnapshotStorageClass = libstorage.GetNoVolumeSnapshotStorageClass("local")
Expect(noSnapshotStorageClass).ToNot(BeEmpty(), "no storage class without snapshot support")
// create running in case storage is WFFC (local storage)
By("Creating source VM")
sourceVM = createVMWithStorageClass(noSnapshotStorageClass, true)
sourceVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), sourceVM.Name, &v1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
sourceVM = StopVirtualMachine(sourceVM)
})
It("with VM source", func() {
vmClone = generateCloneFromVM()
vmClone, err = virtClient.VirtualMachineClone(vmClone.Namespace).Create(context.Background(), vmClone, v1.CreateOptions{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("does not support snapshots"))
})
It("with snapshot source", func() {
By("Snapshotting VM")
snapshot := createSnapshot(sourceVM)
snapshot = waitSnapshotReady(snapshot)
By("Deleting VM")
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(context.Background(), sourceVM.Name, &v1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
By("Creating a clone and expecting error")
vmClone = generateCloneFromSnapshot(snapshot, targetVMName)
vmClone, err = virtClient.VirtualMachineClone(vmClone.Namespace).Create(context.Background(), vmClone, v1.CreateOptions{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not backed up in snapshot"))
})
})
})
Context("and snapshot storage class", decorators.RequiresSnapshotStorageClass, func() {
var (
snapshotStorageClass string
)
BeforeEach(func() {
snapshotStorageClass, err = libstorage.GetSnapshotStorageClass(virtClient)
Expect(err).ToNot(HaveOccurred())
Expect(snapshotStorageClass).ToNot(BeEmpty(), "no storage class with snapshot support")
})
It("with a simple clone", func() {
sourceVM = createVMWithStorageClass(snapshotStorageClass, false)
vmClone = generateCloneFromVM()
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Making sure target is runnable")
targetVM = expectVMRunnable(targetVM)
expectEqualLabels(targetVM, sourceVM)
expectEqualAnnotations(targetVM, sourceVM)
expectEqualTemplateLabels(targetVM, sourceVM)
expectEqualTemplateAnnotations(targetVM, sourceVM)
})
Context("[QUARANTINE] with instancetype and preferences", decorators.Quarantine, func() {
var (
instancetype *instancetypev1beta1.VirtualMachineInstancetype
preference *instancetypev1beta1.VirtualMachinePreference
)
BeforeEach(func() {
ns := testsuite.GetTestNamespace(nil)
instancetype = &instancetypev1beta1.VirtualMachineInstancetype{
ObjectMeta: v1.ObjectMeta{
GenerateName: "vm-instancetype-",
Namespace: ns,
},
Spec: instancetypev1beta1.VirtualMachineInstancetypeSpec{
CPU: instancetypev1beta1.CPUInstancetype{
Guest: 1,
},
Memory: instancetypev1beta1.MemoryInstancetype{
Guest: resource.MustParse("128Mi"),
},
},
}
instancetype, err := virtClient.VirtualMachineInstancetype(ns).Create(context.Background(), instancetype, v1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
preferredCPUTopology := instancetypev1beta1.PreferSockets
preference = &instancetypev1beta1.VirtualMachinePreference{
ObjectMeta: v1.ObjectMeta{
GenerateName: "vm-preference-",
Namespace: ns,
},
Spec: instancetypev1beta1.VirtualMachinePreferenceSpec{
CPU: &instancetypev1beta1.CPUPreferences{
PreferredCPUTopology: &preferredCPUTopology,
},
},
}
preference, err := virtClient.VirtualMachinePreference(ns).Create(context.Background(), preference, v1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
sourceVM = NewRandomVMWithDataVolumeWithRegistryImport(
cd.DataVolumeImportUrlForContainerDisk(cd.ContainerDiskAlpine),
ns,
snapshotStorageClass,
k8sv1.ReadWriteOnce,
)
sourceVM.Spec.Template.Spec.Domain.Resources = virtv1.ResourceRequirements{}
sourceVM.Spec.Instancetype = &virtv1.InstancetypeMatcher{
Name: instancetype.Name,
Kind: "VirtualMachineInstanceType",
}
sourceVM.Spec.Preference = &virtv1.PreferenceMatcher{
Name: preference.Name,
Kind: "VirtualMachinePreference",
}
sourceVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Create(context.Background(), sourceVM)
Expect(err).ToNot(HaveOccurred())
for _, dvt := range sourceVM.Spec.DataVolumeTemplates {
libstorage.EventuallyDVWith(sourceVM.Namespace, dvt.Name, 180, HaveSucceeded())
}
})
DescribeTable("should create new ControllerRevisions for cloned VM", Label("instancetype", "clone"), func(toRunSourceVM bool) {
By("Waiting until the source VM has instancetype and preference RevisionNames")
libinstancetype.WaitForVMInstanceTypeRevisionNames(sourceVM.Name, virtClient)
if toRunSourceVM {
By("Starting the VM and expecting it to run")
sourceVM = StartVMAndExpectRunning(virtClient, sourceVM)
}
vmClone = generateCloneFromVM()
createCloneAndWaitForFinish(vmClone)
By("Waiting until the targetVM has instancetype and preference RevisionNames")
libinstancetype.WaitForVMInstanceTypeRevisionNames(targetVMName, virtClient)
By("Asserting that the targetVM has new instancetype and preference controllerRevisions")
sourceVM, err := virtClient.VirtualMachine(testsuite.GetTestNamespace(sourceVM)).Get(context.Background(), sourceVM.Name, &v1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
targetVM, err := virtClient.VirtualMachine(testsuite.GetTestNamespace(sourceVM)).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(targetVM.Spec.Instancetype.RevisionName).ToNot(Equal(sourceVM.Spec.Instancetype.RevisionName), "source and target instancetype revision names should not be equal")
Expect(targetVM.Spec.Preference.RevisionName).ToNot(Equal(sourceVM.Spec.Preference.RevisionName), "source and target preference revision names should not be equal")
By("Asserting that the source and target ControllerRevisions contain the same Object")
Expect(libinstancetype.EnsureControllerRevisionObjectsEqual(sourceVM.Spec.Instancetype.RevisionName, targetVM.Spec.Instancetype.RevisionName, virtClient)).To(BeTrue(), "source and target instance type controller revisions are expected to be equal")
Expect(libinstancetype.EnsureControllerRevisionObjectsEqual(sourceVM.Spec.Preference.RevisionName, targetVM.Spec.Preference.RevisionName, virtClient)).To(BeTrue(), "source and target preference controller revisions are expected to be equal")
},
Entry("with a running VM", true),
Entry("with a stopped VM", false),
)
})
It("double cloning: clone target as a clone source", func() {
addCloneAnnotationAndLabelFilters := func(vmClone *clonev1alpha1.VirtualMachineClone) {
filters := []string{"somekey/*"}
vmClone.Spec.LabelFilters = filters
vmClone.Spec.AnnotationFilters = filters
vmClone.Spec.Template.LabelFilters = filters
vmClone.Spec.Template.AnnotationFilters = filters
}
generateCloneWithFilters := func(sourceVM *virtv1.VirtualMachine, targetVMName string) *clonev1alpha1.VirtualMachineClone {
vmclone := generateCloneFromVMWithParams(sourceVM, targetVMName)
addCloneAnnotationAndLabelFilters(vmclone)
return vmclone
}
sourceVM = createVMWithStorageClass(snapshotStorageClass, false)
vmClone = generateCloneWithFilters(sourceVM, targetVMName)
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
By("Creating another clone from the target VM")
const cloneFromCloneName = "vm-clone-from-clone"
vmCloneFromClone := generateCloneWithFilters(targetVM, cloneFromCloneName)
vmCloneFromClone.Name = "test-clone-from-clone"
createCloneAndWaitForFinish(vmCloneFromClone)
By(fmt.Sprintf("Getting the target VM %s from clone", cloneFromCloneName))
targetVMCloneFromClone, err := virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), cloneFromCloneName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
expectVMRunnable(targetVMCloneFromClone)
expectEqualLabels(targetVMCloneFromClone, sourceVM)
expectEqualAnnotations(targetVMCloneFromClone, sourceVM)
expectEqualTemplateLabels(targetVMCloneFromClone, sourceVM, "name")
expectEqualTemplateAnnotations(targetVMCloneFromClone, sourceVM)
})
Context("with WaitForFirstConsumer binding mode", func() {
BeforeEach(func() {
snapshotStorageClass, err = libstorage.GetWFFCStorageSnapshotClass(virtClient)
Expect(err).ToNot(HaveOccurred())
Expect(snapshotStorageClass).ToNot(BeEmpty(), "no storage class with snapshot support and wffc binding mode")
})
It("should not delete the vmsnapshot and vmrestore until all the pvc(s) are bound", func() {
addCloneAnnotationAndLabelFilters := func(vmClone *clonev1alpha1.VirtualMachineClone) {
filters := []string{"somekey/*"}
vmClone.Spec.LabelFilters = filters
vmClone.Spec.AnnotationFilters = filters
vmClone.Spec.Template.LabelFilters = filters
vmClone.Spec.Template.AnnotationFilters = filters
}
generateCloneWithFilters := func(sourceVM *virtv1.VirtualMachine, targetVMName string) *clonev1alpha1.VirtualMachineClone {
vmclone := generateCloneFromVMWithParams(sourceVM, targetVMName)
addCloneAnnotationAndLabelFilters(vmclone)
return vmclone
}
sourceVM = createVMWithStorageClass(snapshotStorageClass, true)
vmClone = generateCloneWithFilters(sourceVM, targetVMName)
StopVirtualMachine(sourceVM)
createCloneAndWaitForFinish(vmClone)
By(fmt.Sprintf("Getting the target VM %s", targetVMName))
targetVM, err = virtClient.VirtualMachine(sourceVM.Namespace).Get(context.Background(), targetVMName, &v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
vmClone, err = virtClient.VirtualMachineClone(vmClone.Namespace).Get(context.Background(), vmClone.Name, v1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
Expect(vmClone.Status.SnapshotName).ShouldNot(BeNil())
vmSnapshotName := vmClone.Status.SnapshotName
Expect(vmClone.Status.RestoreName).ShouldNot(BeNil())
vmRestoreName := vmClone.Status.RestoreName
Consistently(func(g Gomega) {
vmSnapshot, err := virtClient.VirtualMachineSnapshot(vmClone.Namespace).Get(context.Background(), *vmSnapshotName, v1.GetOptions{})
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(vmSnapshot).ShouldNot(BeNil())
vmRestore, err := virtClient.VirtualMachineRestore(vmClone.Namespace).Get(context.Background(), *vmRestoreName, v1.GetOptions{})
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(vmRestore).ShouldNot(BeNil())
}, 30*time.Second).Should(Succeed(), "vmsnapshot and vmrestore should not be deleted until the pvc is bound")
By(fmt.Sprintf("Starting the target VM %s", targetVMName))
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(targetVM)).Start(context.Background(), targetVMName, &virtv1.StartOptions{Paused: false})
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega) {
_, err := virtClient.VirtualMachineSnapshot(vmClone.Namespace).Get(context.Background(), *vmSnapshotName, v1.GetOptions{})
g.Expect(err).To(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
_, err = virtClient.VirtualMachineRestore(vmClone.Namespace).Get(context.Background(), *vmRestoreName, v1.GetOptions{})
g.Expect(err).To(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}, 1*time.Minute).Should(Succeed(), "vmsnapshot and vmrestore should be deleted once the pvc is bound")
})
})
})
})
})
})