@@ -31,33 +31,42 @@ import (
3131)
3232
3333var _ = Describe ("Hypervisor Controller" , func () {
34- var hypervisorController * HypervisorController
35- var resource * corev1.Node
34+ const (
35+ resourceName = "other-node"
36+ )
37+ var (
38+ hypervisorController * HypervisorController
39+ resource * corev1.Node
40+ hypervisorName = types.NamespacedName {Name : resourceName }
41+ )
3642
3743 BeforeEach (func (ctx SpecContext ) {
3844 hypervisorController = & HypervisorController {
3945 Client : k8sClient ,
4046 Scheme : k8sClient .Scheme (),
4147 }
4248
49+ DeferCleanup (func () {
50+ hypervisorController = nil
51+ })
52+
4353 By ("creating the namespace for the reconciler" )
4454 ns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "monsoon3" }}
4555 Expect (client .IgnoreAlreadyExists (k8sClient .Create (ctx , ns ))).To (Succeed ())
4656
4757 // pregenerate the resource
4858 resource = & corev1.Node {
4959 ObjectMeta : metav1.ObjectMeta {
50- Name : "other-node" ,
60+ Name : resourceName ,
5161 Labels : map [string ]string {corev1 .LabelTopologyZone : "test-zone" },
5262 Annotations : map [string ]string {annotationCustomTraits : "test-trait" },
5363 },
5464 }
5565 })
5666
5767 AfterEach (func (ctx SpecContext ) {
58- node := & corev1.Node {ObjectMeta : metav1.ObjectMeta {Name : resource .Name }}
5968 By ("Cleanup the specific node" )
60- Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , node ))).To (Succeed ())
69+ Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , resource ))).To (Succeed ())
6170
6271 hypervisor := & kvmv1.Hypervisor {ObjectMeta : metav1.ObjectMeta {Name : resource .Name }}
6372 By ("Cleanup the specific hypervisor" )
@@ -78,11 +87,11 @@ var _ = Describe("Hypervisor Controller", func() {
7887 By ("should have created the Hypervisor resource" )
7988 // Get the Hypervisor resource
8089 hypervisor := & kvmv1.Hypervisor {}
81- hypervisorName := types.NamespacedName {Name : resource .Name }
82- Expect (hypervisorController .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
90+ Expect (k8sClient .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
8391 Expect (hypervisor .Name ).To (Equal (resource .Name ))
8492 Expect (hypervisor .Labels ).ToNot (BeNil ())
8593 Expect (hypervisor .Labels [corev1 .LabelTopologyZone ]).To (Equal ("test-zone" ))
94+ Expect (hypervisor .Spec .Maintenance ).To (BeEmpty ())
8695
8796 By ("Adding a label annotation to the node and reconciling again" )
8897 // Add an aggregate annotation to the node
@@ -98,11 +107,12 @@ var _ = Describe("Hypervisor Controller", func() {
98107 By ("should have updated the Hypervisor resource with the aggregate" )
99108 // Get the Hypervisor resource again
100109 updatedHypervisor := & kvmv1.Hypervisor {}
101- Expect (hypervisorController .Get (ctx , hypervisorName , updatedHypervisor )).To (Succeed ())
110+ Expect (k8sClient .Get (ctx , hypervisorName , updatedHypervisor )).To (Succeed ())
102111 Expect (updatedHypervisor .Name ).To (Equal (resource .Name ))
103112 Expect (updatedHypervisor .Spec .CustomTraits ).ToNot (BeNil ())
104113 Expect (updatedHypervisor .Spec .CustomTraits ).To (ContainElement ("test-trait" ))
105114 Expect (updatedHypervisor .Labels ).To (HaveKeyWithValue ("worker.garden.sapcloud.io/group" , "new-group" ))
115+ Expect (updatedHypervisor .Spec .Maintenance ).To (BeEmpty ())
106116 })
107117 })
108118
@@ -121,8 +131,16 @@ var _ = Describe("Hypervisor Controller", func() {
121131 })
122132 Expect (k8sClient .Status ().Update (ctx , resource )).To (Succeed ())
123133
134+ _ , err := hypervisorController .Reconcile (ctx , ctrl.Request {
135+ NamespacedName : types.NamespacedName {Name : resource .Name },
136+ })
137+ Expect (err ).NotTo (HaveOccurred ())
138+ hypervisor := & kvmv1.Hypervisor {}
139+ Expect (k8sClient .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
140+ Expect (hypervisor .Spec .Maintenance ).To (Equal (kvmv1 .MaintenanceTermination ))
141+
124142 By ("Reconciling the created resource" )
125- for range 3 {
143+ for range 2 {
126144 _ , err := hypervisorController .Reconcile (ctx , ctrl.Request {
127145 NamespacedName : types.NamespacedName {Name : resource .Name },
128146 })
@@ -131,17 +149,16 @@ var _ = Describe("Hypervisor Controller", func() {
131149
132150 By ("should have set the terminating condition on the Hypervisor resource" )
133151 // Get the Hypervisor resource
134- hypervisor := & kvmv1.Hypervisor {}
135- hypervisorName := types.NamespacedName {Name : resource .Name }
136- Expect (hypervisorController .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
137- Expect (hypervisor .Name ).To (Equal (resource .Name ))
138- Expect (hypervisor .Status .Conditions ).ToNot (BeNil ())
139- condition := meta .FindStatusCondition (hypervisor .Status .Conditions , kvmv1 .ConditionTypeReady )
152+ updatedHypervisor := & kvmv1.Hypervisor {}
153+ Expect (hypervisorController .Get (ctx , hypervisorName , updatedHypervisor )).To (Succeed ())
154+ Expect (updatedHypervisor .Name ).To (Equal (resource .Name ))
155+ Expect (updatedHypervisor .Status .Conditions ).ToNot (BeNil ())
156+ condition := meta .FindStatusCondition (updatedHypervisor .Status .Conditions , kvmv1 .ConditionTypeReady )
140157 Expect (condition ).ToNot (BeNil ())
141158 Expect (condition .Reason ).To (Equal ("Terminating" ))
142159 Expect (condition .Status ).To (Equal (metav1 .ConditionFalse ))
143160
144- condition = meta .FindStatusCondition (hypervisor .Status .Conditions , kvmv1 .ConditionTypeTerminating )
161+ condition = meta .FindStatusCondition (updatedHypervisor .Status .Conditions , kvmv1 .ConditionTypeTerminating )
145162 Expect (condition ).ToNot (BeNil ())
146163 Expect (condition .Reason ).To (Equal ("Terminating" ))
147164 Expect (condition .Status ).To (Equal (metav1 .ConditionTrue ))
0 commit comments