|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package snapshot |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + "k8s.io/apimachinery/pkg/api/resource" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/utils/ptr" |
| 28 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + |
| 30 | + vdbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vd" |
| 31 | + vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm" |
| 32 | + vmsbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vmsnapshot" |
| 33 | + vmsopbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vmsop" |
| 34 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 35 | + "github.com/deckhouse/virtualization/test/e2e/internal/framework" |
| 36 | + "github.com/deckhouse/virtualization/test/e2e/internal/object" |
| 37 | + "github.com/deckhouse/virtualization/test/e2e/internal/util" |
| 38 | +) |
| 39 | + |
| 40 | +var _ = Describe("VMSOPCreateVirtualMachine", func() { |
| 41 | + var ( |
| 42 | + vd *v1alpha2.VirtualDisk |
| 43 | + vm *v1alpha2.VirtualMachine |
| 44 | + vmsnapshot *v1alpha2.VirtualMachineSnapshot |
| 45 | + vmsop *v1alpha2.VirtualMachineSnapshotOperation |
| 46 | + |
| 47 | + f = framework.NewFramework("vmsop") |
| 48 | + ) |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + DeferCleanup(f.After) |
| 52 | + |
| 53 | + f.Before() |
| 54 | + }) |
| 55 | + |
| 56 | + It("verifies that vmsop are successful", func() { |
| 57 | + By("Environment preparation", func() { |
| 58 | + vd = vdbuilder.New( |
| 59 | + vdbuilder.WithName("vd-root"), |
| 60 | + vdbuilder.WithNamespace(f.Namespace().Name), |
| 61 | + vdbuilder.WithSize(ptr.To(resource.MustParse("10Gi"))), |
| 62 | + vdbuilder.WithDataSourceHTTP(&v1alpha2.DataSourceHTTP{ |
| 63 | + URL: object.ImageURLAlpineBIOS, |
| 64 | + }), |
| 65 | + ) |
| 66 | + vm = object.NewMinimalVM("vmsop-origin-", f.Namespace().Name, |
| 67 | + vmbuilder.WithBlockDeviceRefs( |
| 68 | + v1alpha2.BlockDeviceSpecRef{ |
| 69 | + Kind: v1alpha2.VirtualDiskKind, |
| 70 | + Name: vd.Name, |
| 71 | + }, |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + err := f.CreateWithDeferredDeletion(context.Background(), vd, vm) |
| 76 | + Expect(err).NotTo(HaveOccurred()) |
| 77 | + |
| 78 | + util.UntilVMAgentReady(crclient.ObjectKeyFromObject(vm), framework.LongTimeout) |
| 79 | + }) |
| 80 | + |
| 81 | + By("Create VM Snapshot", func() { |
| 82 | + vmsnapshot = vmsbuilder.New( |
| 83 | + vmsbuilder.WithName("vmsnapshot"), |
| 84 | + vmsbuilder.WithNamespace(f.Namespace().Name), |
| 85 | + vmsbuilder.WithVirtualMachineName(vm.Name), |
| 86 | + vmsbuilder.WithKeepIPAddress(v1alpha2.KeepIPAddressNever), |
| 87 | + vmsbuilder.WithRequiredConsistency(false), |
| 88 | + ) |
| 89 | + |
| 90 | + err := f.CreateWithDeferredDeletion(context.Background(), vmsnapshot) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + |
| 93 | + util.UntilObjectPhase(string(v1alpha2.VirtualMachineSnapshotPhaseReady), framework.LongTimeout, vmsnapshot) |
| 94 | + }) |
| 95 | + |
| 96 | + By("Create and wait for VMSOP", func() { |
| 97 | + vmsop = vmsopbuilder.New( |
| 98 | + vmsopbuilder.WithName("vmsop"), |
| 99 | + vmsopbuilder.WithNamespace(f.Namespace().Name), |
| 100 | + vmsopbuilder.WithVirtualMachineSnapshotName(vmsnapshot.Name), |
| 101 | + vmsopbuilder.WithCreateVirtualMachine(&v1alpha2.VMSOPCreateVirtualMachineSpec{ |
| 102 | + Mode: v1alpha2.SnapshotOperationModeBestEffort, |
| 103 | + Customization: &v1alpha2.VMSOPCreateVirtualMachineCustomization{ |
| 104 | + NamePrefix: "created-from-vmsop-", |
| 105 | + }, |
| 106 | + }), |
| 107 | + ) |
| 108 | + |
| 109 | + err := f.CreateWithDeferredDeletion(context.Background(), vmsop) |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + |
| 112 | + util.UntilObjectPhase(string(v1alpha2.VMSOPPhaseCompleted), framework.LongTimeout, vmsop) |
| 113 | + }) |
| 114 | + |
| 115 | + By("Verify that the created VM is running", func() { |
| 116 | + newName := fmt.Sprintf("created-from-vmsop-%s", vm.Name) |
| 117 | + createdVM, err := f.Clients.VirtClient().VirtualMachines(f.Namespace().Name).Get(context.Background(), newName, metav1.GetOptions{}) |
| 118 | + Expect(err).NotTo(HaveOccurred()) |
| 119 | + |
| 120 | + util.UntilVMAgentReady(crclient.ObjectKeyFromObject(createdVM), framework.LongTimeout) |
| 121 | + }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments