|
| 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 | + corev1 "k8s.io/api/core/v1" |
| 25 | + |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + virtv1 "kubevirt.io/api/core/v1" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + |
| 30 | + vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm" |
| 31 | + "github.com/deckhouse/virtualization-controller/pkg/common/testutil" |
| 32 | + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" |
| 33 | + "github.com/deckhouse/virtualization-controller/pkg/controller/reconciler" |
| 34 | + "github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" |
| 35 | + virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 36 | + "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" |
| 37 | +) |
| 38 | + |
| 39 | +var _ = Describe("AgentHandler Tests", func() { |
| 40 | + const ( |
| 41 | + name = "vm-agent" |
| 42 | + namespace = "default" |
| 43 | + ) |
| 44 | + |
| 45 | + var ( |
| 46 | + ctx = testutil.ContextBackgroundWithNoOpLogger() |
| 47 | + fakeClient client.WithWatch |
| 48 | + resource *reconciler.Resource[*virtv2.VirtualMachine, virtv2.VirtualMachineStatus] |
| 49 | + vmState state.VirtualMachineState |
| 50 | + ) |
| 51 | + |
| 52 | + AfterEach(func() { |
| 53 | + fakeClient = nil |
| 54 | + resource = nil |
| 55 | + vmState = nil |
| 56 | + }) |
| 57 | + |
| 58 | + newVM := func(phase virtv2.MachinePhase) *virtv2.VirtualMachine { |
| 59 | + vm := vmbuilder.NewEmpty(name, namespace) |
| 60 | + vm.Status.Phase = phase |
| 61 | + return vm |
| 62 | + } |
| 63 | + |
| 64 | + newKVVMI := func(agentConnected bool, agentUnsupported bool) *virtv1.VirtualMachineInstance { |
| 65 | + kvvmi := newEmptyKVVMI(name, namespace) |
| 66 | + conditions := make([]virtv1.VirtualMachineInstanceCondition, 0) |
| 67 | + if agentConnected { |
| 68 | + conditions = append(conditions, virtv1.VirtualMachineInstanceCondition{ |
| 69 | + Type: virtv1.VirtualMachineInstanceAgentConnected, |
| 70 | + Status: corev1.ConditionTrue, |
| 71 | + }) |
| 72 | + } |
| 73 | + if agentUnsupported { |
| 74 | + conditions = append(conditions, virtv1.VirtualMachineInstanceCondition{ |
| 75 | + Type: virtv1.VirtualMachineInstanceUnsupportedAgent, |
| 76 | + Status: corev1.ConditionTrue, |
| 77 | + }) |
| 78 | + } else { |
| 79 | + conditions = append(conditions, virtv1.VirtualMachineInstanceCondition{ |
| 80 | + Type: virtv1.VirtualMachineInstanceUnsupportedAgent, |
| 81 | + Status: corev1.ConditionFalse, |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + kvvmi.Status.Conditions = conditions |
| 86 | + return kvvmi |
| 87 | + } |
| 88 | + |
| 89 | + reconcile := func() { |
| 90 | + h := NewAgentHandler() |
| 91 | + _, err := h.Handle(ctx, vmState) |
| 92 | + Expect(err).NotTo(HaveOccurred()) |
| 93 | + err = resource.Update(context.Background()) |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + } |
| 96 | + |
| 97 | + DescribeTable("AgentReady Condition Tests", |
| 98 | + func(phase virtv2.MachinePhase, agentConnected bool, expectedStatus metav1.ConditionStatus, expectedExistence bool) { |
| 99 | + vm := newVM(phase) |
| 100 | + kvvmi := newKVVMI(agentConnected, false) |
| 101 | + fakeClient, resource, vmState = setupEnvironment(vm, kvvmi) |
| 102 | + |
| 103 | + reconcile() |
| 104 | + |
| 105 | + newVM := &virtv2.VirtualMachine{} |
| 106 | + err := fakeClient.Get(ctx, client.ObjectKeyFromObject(vm), newVM) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + |
| 109 | + cond, exists := conditions.GetCondition(vmcondition.TypeAgentReady, newVM.Status.Conditions) |
| 110 | + Expect(exists).To(Equal(expectedExistence)) |
| 111 | + if exists { |
| 112 | + Expect(cond.Status).To(Equal(expectedStatus)) |
| 113 | + } |
| 114 | + }, |
| 115 | + Entry("Should add AgentReady as True if agent is connected", virtv2.MachineRunning, true, metav1.ConditionTrue, true), |
| 116 | + Entry("Should add AgentReady as False if agent is not connected", virtv2.MachineRunning, false, metav1.ConditionFalse, true), |
| 117 | + |
| 118 | + Entry("Should add AgentReady as True if agent is connected", virtv2.MachineStopping, true, metav1.ConditionTrue, true), |
| 119 | + Entry("Should add AgentReady as False if agent is not connected", virtv2.MachineStopping, false, metav1.ConditionFalse, true), |
| 120 | + |
| 121 | + Entry("Should add AgentReady as True if agent is connected", virtv2.MachineMigrating, true, metav1.ConditionTrue, true), |
| 122 | + Entry("Should add AgentReady as False if agent is not connected", virtv2.MachineMigrating, false, metav1.ConditionFalse, true), |
| 123 | + |
| 124 | + Entry("Should not add AgentReady if VM is in Pending phase and the agent is connected", virtv2.MachinePending, true, metav1.ConditionUnknown, false), |
| 125 | + Entry("Should not add AgentReady if VM is in Pending phase and the agent is not connected", virtv2.MachinePending, false, metav1.ConditionUnknown, false), |
| 126 | + |
| 127 | + Entry("Should not add AgentReady if VM is in Starting phase and the agent is connected", virtv2.MachineStarting, true, metav1.ConditionUnknown, false), |
| 128 | + Entry("Should not add AgentReady if VM is in Starting phase and the agent is not connected", virtv2.MachineStarting, false, metav1.ConditionUnknown, false), |
| 129 | + |
| 130 | + Entry("Should not add AgentReady if VM is in Stopped phase and the agent is connected", virtv2.MachineStopped, true, metav1.ConditionUnknown, false), |
| 131 | + Entry("Should not add AgentReady if VM is in Stopped phase and the agent is not connected", virtv2.MachineStopped, false, metav1.ConditionUnknown, false), |
| 132 | + ) |
| 133 | + |
| 134 | + DescribeTable("AgentVersionNotSupported Condition Tests", |
| 135 | + func(phase virtv2.MachinePhase, agentUnsupported bool, expectedStatus metav1.ConditionStatus, expectedExistence bool) { |
| 136 | + vm := newVM(phase) |
| 137 | + vmi := newKVVMI(true, agentUnsupported) |
| 138 | + fakeClient, resource, vmState = setupEnvironment(vm, vmi) |
| 139 | + |
| 140 | + reconcile() |
| 141 | + |
| 142 | + newVM := &virtv2.VirtualMachine{} |
| 143 | + err := fakeClient.Get(ctx, client.ObjectKeyFromObject(vm), newVM) |
| 144 | + Expect(err).NotTo(HaveOccurred()) |
| 145 | + |
| 146 | + cond, exists := conditions.GetCondition(vmcondition.TypeAgentVersionNotSupported, newVM.Status.Conditions) |
| 147 | + Expect(exists).To(Equal(expectedExistence)) |
| 148 | + if exists { |
| 149 | + Expect(cond.Status).To(Equal(expectedStatus)) |
| 150 | + } |
| 151 | + }, |
| 152 | + Entry("Should set unsupported version condition as True in Running phase", virtv2.MachineRunning, true, metav1.ConditionTrue, true), |
| 153 | + Entry("Should not set unsupported version condition as False in Running phase", virtv2.MachineRunning, false, metav1.ConditionUnknown, false), |
| 154 | + |
| 155 | + Entry("Should set unsupported version condition as True in Stopping phase", virtv2.MachineStopping, true, metav1.ConditionTrue, true), |
| 156 | + Entry("Should set unsupported version condition as False in Stopping phase", virtv2.MachineStopping, false, metav1.ConditionUnknown, false), |
| 157 | + |
| 158 | + Entry("Should set unsupported version condition as True in Migrating phase", virtv2.MachineMigrating, true, metav1.ConditionTrue, true), |
| 159 | + Entry("Should set unsupported version condition as False in Migrating phase", virtv2.MachineMigrating, false, metav1.ConditionUnknown, false), |
| 160 | + |
| 161 | + Entry("Should not set unsupported version condition as True in Pending phase", virtv2.MachinePending, true, metav1.ConditionUnknown, false), |
| 162 | + Entry("Should not set unsupported version condition as False in Pending phase", virtv2.MachinePending, false, metav1.ConditionUnknown, false), |
| 163 | + |
| 164 | + Entry("Should not set unsupported version condition as True in Starting phase", virtv2.MachineStarting, true, metav1.ConditionUnknown, false), |
| 165 | + Entry("Should not set unsupported version condition as False in Starting phase", virtv2.MachineStarting, false, metav1.ConditionUnknown, false), |
| 166 | + |
| 167 | + Entry("Should not set unsupported version condition as True in Stopped phase", virtv2.MachineStopped, true, metav1.ConditionUnknown, false), |
| 168 | + Entry("Should not set unsupported version condition as False in Stopped phase", virtv2.MachineStopped, false, metav1.ConditionUnknown, false), |
| 169 | + ) |
| 170 | +}) |
0 commit comments