@@ -43,64 +43,81 @@ var _ = Describe("Node Eviction Label Controller", func() {
4343 zone = "zone"
4444 )
4545 var (
46- nodeReconciler * NodeEvictionLabelReconciler
47- req = ctrl.Request {NamespacedName : types.NamespacedName {Name : nodeName }}
48- fakeServer testhelper.FakeServer
49- )
50-
51- Context ("When reconciling a node" , func () {
52- ctx := context .Background () //nolint:govet
53-
54- reconcileNodeLoop := func (steps int ) (res ctrl.Result , err error ) {
46+ reconciler * NodeEvictionLabelReconciler
47+ req = ctrl.Request {NamespacedName : types.NamespacedName {Name : nodeName }}
48+ fakeServer testhelper.FakeServer
49+ reconcileNodeLoop = func (ctx context.Context , steps int ) (res ctrl.Result , err error ) {
5550 for range steps {
56- res , err = nodeReconciler .Reconcile (ctx , req )
51+ res , err = reconciler .Reconcile (ctx , req )
5752 if err != nil {
5853 return
5954 }
6055 }
6156 return
6257 }
58+ )
6359
64- BeforeEach (func () {
65- fakeServer = testhelper .SetupHTTP ()
66- Expect (os .Setenv ("KVM_HA_SERVICE_URL" , fakeServer .Endpoint ())).To (Succeed ())
67- nodeReconciler = & NodeEvictionLabelReconciler {
68- Client : k8sClient ,
69- Scheme : k8sClient .Scheme (),
70- }
60+ BeforeEach (func (ctx context.Context ) {
61+ fakeServer = testhelper .SetupHTTP ()
62+ Expect (os .Setenv ("KVM_HA_SERVICE_URL" , fakeServer .Endpoint ())).To (Succeed ())
7163
72- By ("creating the namespace for the reconciler" )
73- ns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "monsoon3" }}
74- Expect (client .IgnoreAlreadyExists (k8sClient .Create (ctx , ns ))).To (Succeed ())
75-
76- By ("creating the node resource" )
77- resource := & corev1.Node {
78- ObjectMeta : metav1.ObjectMeta {
79- Name : nodeName ,
80- Labels : map [string ]string {
81- corev1 .LabelHostname : hostName ,
82- corev1 .LabelTopologyRegion : region ,
83- corev1 .LabelTopologyZone : zone ,
84- labelEvictionRequired : "true" ,
85- },
86- },
87- }
88- Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
89-
90- By ("creating the hypervisor resource" )
91- hypervisor := & kvmv1.Hypervisor {
92- ObjectMeta : metav1.ObjectMeta {
93- Name : nodeName ,
94- Labels : map [string ]string {
95- corev1 .LabelHostname : nodeName ,
96- },
64+ DeferCleanup (func () {
65+ Expect (os .Unsetenv ("KVM_HA_SERVICE_URL" )).To (Succeed ())
66+ fakeServer .Teardown ()
67+ })
68+
69+ reconciler = & NodeEvictionLabelReconciler {
70+ Client : k8sClient ,
71+ Scheme : k8sClient .Scheme (),
72+ }
73+
74+ By ("creating the namespace for the reconciler" )
75+ ns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "monsoon3" }}
76+ Expect (client .IgnoreAlreadyExists (k8sClient .Create (ctx , ns ))).To (Succeed ())
77+
78+ By ("creating the node resource" )
79+ resource := & corev1.Node {
80+ ObjectMeta : metav1.ObjectMeta {
81+ Name : nodeName ,
82+ Labels : map [string ]string {
83+ corev1 .LabelHostname : hostName ,
84+ corev1 .LabelTopologyRegion : region ,
85+ corev1 .LabelTopologyZone : zone ,
86+ labelEvictionRequired : "true" ,
9787 },
98- Spec : kvmv1.HypervisorSpec {
99- LifecycleEnabled : true ,
88+ },
89+ }
90+ Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
91+
92+ DeferCleanup (func (ctx context.Context ) {
93+ By ("Cleanup the specific node" )
94+ Expect (k8sClient .Delete (ctx , resource )).To (Succeed ())
95+ })
96+
97+ By ("creating the hypervisor resource" )
98+ hypervisor := & kvmv1.Hypervisor {
99+ ObjectMeta : metav1.ObjectMeta {
100+ Name : nodeName ,
101+ Labels : map [string ]string {
102+ corev1 .LabelHostname : nodeName ,
100103 },
101- }
102- Expect (client .IgnoreAlreadyExists (k8sClient .Create (ctx , hypervisor ))).To (Succeed ())
104+ },
105+ Spec : kvmv1.HypervisorSpec {
106+ LifecycleEnabled : true ,
107+ },
108+ }
109+ Expect (k8sClient .Create (ctx , hypervisor )).To (Succeed ())
110+ DeferCleanup (func (ctx context.Context ) {
111+ By ("Cleanup the specific hypervisor" )
112+ Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , hypervisor ))).To (Succeed ())
113+ })
114+
115+ })
103116
117+ Context ("When reconciling a node" , func () {
118+ BeforeEach (func (ctx context.Context ) {
119+ hypervisor := & kvmv1.Hypervisor {}
120+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : nodeName }, hypervisor )).To (Succeed ())
104121 By ("updating the hypervisor status sub-resource" )
105122 meta .SetStatusCondition (& hypervisor .Status .Conditions , metav1.Condition {
106123 Type : ConditionTypeOnboarding ,
@@ -111,22 +128,9 @@ var _ = Describe("Node Eviction Label Controller", func() {
111128 Expect (k8sClient .Status ().Update (ctx , hypervisor )).To (Succeed ())
112129 })
113130
114- AfterEach (func () {
115- // Cleanup the hypervisor created for the test
116- hypervisor := & kvmv1.Hypervisor {ObjectMeta : metav1.ObjectMeta {Name : nodeName }}
117- By ("Cleanup the specific hypervisor" )
118- Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , hypervisor ))).To (Succeed ())
119-
120- // Cleanup the node created for the test
121- node := & corev1.Node {ObjectMeta : metav1.ObjectMeta {Name : nodeName }}
122- By ("Cleanup the specific node" )
123- Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , node ))).To (Succeed ())
124- fakeServer .Teardown ()
125- })
126-
127- It ("should successfully reconcile the resource" , func () {
131+ It ("should successfully reconcile the resource" , func (ctx context.Context ) {
128132 By ("ConditionType the created resource" )
129- _ , err := reconcileNodeLoop (5 )
133+ _ , err := reconcileNodeLoop (ctx , 5 )
130134 Expect (err ).NotTo (HaveOccurred ())
131135
132136 // expect node controller to create an eviction for the node
0 commit comments