|
| 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 internal |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + |
| 26 | + vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm" |
| 27 | + vmopbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vmop" |
| 28 | + "github.com/deckhouse/virtualization-controller/pkg/common/testutil" |
| 29 | + "github.com/deckhouse/virtualization-controller/pkg/controller/reconciler" |
| 30 | + "github.com/deckhouse/virtualization-controller/pkg/eventrecord" |
| 31 | + virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("LifecycleHandler", func() { |
| 35 | + const ( |
| 36 | + name = "test" |
| 37 | + namespace = "default" |
| 38 | + ) |
| 39 | + |
| 40 | + var ( |
| 41 | + ctx context.Context |
| 42 | + fakeClient client.WithWatch |
| 43 | + srv *reconciler.Resource[*virtv2.VirtualMachineOperation, virtv2.VirtualMachineOperationStatus] |
| 44 | + recorderMock *eventrecord.EventRecorderLoggerMock |
| 45 | + ) |
| 46 | + |
| 47 | + BeforeEach(func() { |
| 48 | + ctx = testutil.ContextBackgroundWithNoOpLogger() |
| 49 | + recorderMock = &eventrecord.EventRecorderLoggerMock{ |
| 50 | + EventFunc: func(_ client.Object, _, _, _ string) {}, |
| 51 | + EventfFunc: func(_ client.Object, _, _, _ string, _ ...interface{}) {}, |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + newVMOPEvictPending := func(opts ...vmopbuilder.Option) *virtv2.VirtualMachineOperation { |
| 56 | + vmop := vmopbuilder.NewEmpty(name, namespace) |
| 57 | + vmop.Status.Phase = virtv2.VMOPPhasePending |
| 58 | + vmopbuilder.ApplyOptions(vmop, |
| 59 | + vmopbuilder.WithType(virtv2.VMOPTypeEvict), |
| 60 | + vmopbuilder.WithVirtualMachine(name), |
| 61 | + ) |
| 62 | + vmopbuilder.ApplyOptions(vmop, opts...) |
| 63 | + return vmop |
| 64 | + } |
| 65 | + |
| 66 | + newVM := func(vmPolicy virtv2.LiveMigrationPolicy) *virtv2.VirtualMachine { |
| 67 | + vm := vmbuilder.NewEmpty(name, namespace) |
| 68 | + vm.Spec.LiveMigrationPolicy = vmPolicy |
| 69 | + vm.Spec.RunPolicy = virtv2.AlwaysOnPolicy |
| 70 | + vm.Status.Phase = virtv2.MachineRunning |
| 71 | + |
| 72 | + return vm |
| 73 | + } |
| 74 | + |
| 75 | + DescribeTable("Evict operation for migration policy", func(vmop *virtv2.VirtualMachineOperation, vmPolicy virtv2.LiveMigrationPolicy, expectedPhase virtv2.VMOPPhase) { |
| 76 | + vm := newVM(vmPolicy) |
| 77 | + |
| 78 | + fakeClient, srv = setupEnvironment(vmop, vm) |
| 79 | + |
| 80 | + h := NewLifecycleHandler(fakeClient, NewSvcOpCreator(fakeClient), recorderMock) |
| 81 | + _, err := h.Handle(ctx, srv.Changed()) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + |
| 84 | + Expect(srv.Changed().Status.Phase).To(Equal(expectedPhase), "should updated status phase, conditions: %+v", srv.Changed().Status.Conditions) |
| 85 | + }, |
| 86 | + // AlwaysSafe cases. |
| 87 | + Entry("is ok for AlwaysSafe and force=nil", |
| 88 | + newVMOPEvictPending(), |
| 89 | + virtv2.AlwaysSafeMigrationPolicy, |
| 90 | + virtv2.VMOPPhasePending, |
| 91 | + ), |
| 92 | + Entry("is ok for AlwaysSafe and force=false", |
| 93 | + newVMOPEvictPending(vmopbuilder.WithForceFalse()), |
| 94 | + virtv2.AlwaysSafeMigrationPolicy, |
| 95 | + virtv2.VMOPPhasePending, |
| 96 | + ), |
| 97 | + Entry("should become Failed for AlwaysSafe and force=true", |
| 98 | + newVMOPEvictPending(vmopbuilder.WithForce()), |
| 99 | + virtv2.AlwaysSafeMigrationPolicy, |
| 100 | + virtv2.VMOPPhaseFailed, |
| 101 | + ), |
| 102 | + |
| 103 | + // PreferSafe cases. |
| 104 | + Entry("is ok for PreferSafe and force=nil", |
| 105 | + newVMOPEvictPending(), |
| 106 | + virtv2.PreferSafeMigrationPolicy, |
| 107 | + virtv2.VMOPPhasePending, |
| 108 | + ), |
| 109 | + Entry("is ok for PreferSafe and force=false", |
| 110 | + newVMOPEvictPending(vmopbuilder.WithForceFalse()), |
| 111 | + virtv2.PreferSafeMigrationPolicy, |
| 112 | + virtv2.VMOPPhasePending, |
| 113 | + ), |
| 114 | + Entry("is ok for PreferSafe and force=true", |
| 115 | + newVMOPEvictPending(vmopbuilder.WithForce()), |
| 116 | + virtv2.PreferSafeMigrationPolicy, |
| 117 | + virtv2.VMOPPhasePending, |
| 118 | + ), |
| 119 | + |
| 120 | + // AlwaysForced cases. |
| 121 | + Entry("is ok for AlwaysForced and force=nil", |
| 122 | + newVMOPEvictPending(), |
| 123 | + virtv2.AlwaysForcedMigrationPolicy, |
| 124 | + virtv2.VMOPPhasePending, |
| 125 | + ), |
| 126 | + Entry("should become Failed for AlwaysForced and force=false", |
| 127 | + newVMOPEvictPending(vmopbuilder.WithForceFalse()), |
| 128 | + virtv2.AlwaysForcedMigrationPolicy, |
| 129 | + virtv2.VMOPPhaseFailed, |
| 130 | + ), |
| 131 | + Entry("is ok for AlwaysForced and force=true", |
| 132 | + newVMOPEvictPending(vmopbuilder.WithForce()), |
| 133 | + virtv2.AlwaysForcedMigrationPolicy, |
| 134 | + virtv2.VMOPPhasePending, |
| 135 | + ), |
| 136 | + |
| 137 | + // PreferForced cases. |
| 138 | + Entry("is ok for PreferForced and force=nil", |
| 139 | + newVMOPEvictPending(), |
| 140 | + virtv2.PreferForcedMigrationPolicy, |
| 141 | + virtv2.VMOPPhasePending, |
| 142 | + ), |
| 143 | + Entry("is ok for PreferForced and force=false", |
| 144 | + newVMOPEvictPending(vmopbuilder.WithForceFalse()), |
| 145 | + virtv2.PreferForcedMigrationPolicy, |
| 146 | + virtv2.VMOPPhasePending, |
| 147 | + ), |
| 148 | + Entry("is ok for PreferForced and force=true", |
| 149 | + newVMOPEvictPending(vmopbuilder.WithForce()), |
| 150 | + virtv2.PreferForcedMigrationPolicy, |
| 151 | + virtv2.VMOPPhasePending, |
| 152 | + ), |
| 153 | + ) |
| 154 | +}) |
0 commit comments