@@ -17,6 +17,8 @@ limitations under the License.
17
17
package controller
18
18
19
19
import (
20
+ "time"
21
+
20
22
. "github.com/onsi/ginkgo/v2"
21
23
. "github.com/onsi/gomega"
22
24
"github.com/quickube/QScaler/api/v1alpha1"
@@ -27,7 +29,6 @@ import (
27
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
30
"k8s.io/apimachinery/pkg/types"
29
31
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
30
- "time"
31
32
)
32
33
33
34
var _ = Describe ("QWorker Controller" , func () {
@@ -109,8 +110,9 @@ var _ = Describe("QWorker Controller", func() {
109
110
})
110
111
111
112
It ("should reconcile successfully and update QWorker status" , func () {
112
- BrokerMock .On ("GetQueueLength" , mock .Anything , mock .Anything ).Return (5 , nil ).Once ()
113
- BrokerMock .On ("IsConnected" , mock .Anything ).Return (true , nil ).Once ()
113
+ By ("Setting broker mocks" )
114
+ BrokerMock .On ("GetQueueLength" , mock .Anything , mock .Anything ).Return (5 , nil )
115
+ BrokerMock .On ("IsConnected" , mock .Anything ).Return (true , nil )
114
116
115
117
time .Sleep (5 * time .Second )
116
118
By ("Checking QWorker status" )
@@ -121,68 +123,43 @@ var _ = Describe("QWorker Controller", func() {
121
123
122
124
})
123
125
124
- //It("should handle missing ScalerConfig gracefully", func() {
125
- // By("Deleting the ScalerConfig resource")
126
- // Expect(k8sClient.Delete(ctx, scalerConfigResource)).To(Succeed())
127
- //
128
- // By("Reconciling the QWorker resource")
129
- // _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: qworkerNamespaced})
130
- // Expect(err).To(HaveOccurred())
131
- //})
132
-
133
- //It("should scale up pods when needed", func() {
134
- // By("Setting up a scenario where scaling up is required")
135
- // qworkerResource.Status.CurrentReplicas = 1
136
- // Expect(k8sClient.Status().Update(ctx, qworkerResource)).To(Succeed())
137
- //
138
- // By("Reconciling the QWorker resource")
139
- // _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: qworkerNamespaced})
140
- // Expect(err).NotTo(HaveOccurred())
141
- //
142
- // By("Retrieving all Pods in the namespace")
143
- // podList := &corev1.PodList{}
144
- // Expect(k8sClient.List(ctx, podList, ctrlclient.InNamespace(namespace))).To(Succeed())
145
- //
146
- // By("Filtering Pods by owner reference")
147
- // ownedPods := []corev1.Pod{}
148
- // for _, pod := range podList.Items {
149
- // for _, ownerRef := range pod.OwnerReferences {
150
- // if ownerRef.Name == resourceName && ownerRef.Kind == "QWorker" {
151
- // ownedPods = append(ownedPods, pod)
152
- // }
153
- // }
154
- // }
155
- //
156
- // By("Verifying the number of Pods matches the desired replicas")
157
- // Expect(len(ownedPods)).To(Equal(qworkerResource.Status.DesiredReplicas))
158
- //})
159
-
160
- //It("should scale down pods when needed", func() {
161
- // By("Setting up a scenario where scaling down is required")
162
- // qworkerResource.Status.CurrentReplicas = 5
163
- // Expect(k8sClient.Status().Update(ctx, qworkerResource)).To(Succeed())
164
- //
165
- // By("Reconciling the QWorker resource")
166
- // _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: qworkerNamespaced})
167
- // Expect(err).NotTo(HaveOccurred())
168
- //
169
- // By("Retrieving all Pods in the namespace")
170
- // podList := &corev1.PodList{}
171
- // Expect(k8sClient.List(ctx, podList, ctrlclient.InNamespace(namespace))).To(Succeed())
172
- //
173
- // By("Filtering Pods by owner reference")
174
- // ownedPods := []corev1.Pod{}
175
- // for _, pod := range podList.Items {
176
- // for _, ownerRef := range pod.OwnerReferences {
177
- // if ownerRef.Name == resourceName && ownerRef.Kind == "QWorker" {
178
- // ownedPods = append(ownedPods, pod)
179
- // }
180
- // }
181
- // }
182
- //
183
- // By("Verifying the number of Pods matches the desired replicas")
184
- // Expect(len(ownedPods)).To(Equal(qworkerResource.Status.DesiredReplicas))
185
- //})
126
+ It ("should scale up pods when needed" , func () {
127
+ By ("Setting broker mocks" )
128
+ BrokerMock .On ("GetQueueLength" , mock .Anything , mock .Anything ).Return (5 , nil )
129
+ BrokerMock .On ("IsConnected" , mock .Anything ).Return (true , nil )
130
+ time .Sleep (5 * time .Second )
131
+
132
+ By ("Retrieving all Pods in the namespace" )
133
+ podList := & corev1.PodList {}
134
+ Expect (k8sClient .List (ctx , podList , ctrlclient .InNamespace (namespace ))).To (Succeed ())
135
+
136
+ By ("Filtering Pods by owner reference" )
137
+ ownedPods := []corev1.Pod {}
138
+ for _ , pod := range podList .Items {
139
+ for _ , ownerRef := range pod .OwnerReferences {
140
+ if ownerRef .Name == resourceName {
141
+ ownedPods = append (ownedPods , pod )
142
+ }
143
+ }
144
+ }
145
+
146
+ By ("Deleting all Pods" )
147
+ for _ , pod := range ownedPods {
148
+ // Create delete options with GracePeriodSeconds set to 0
149
+ deleteOptions := & ctrlclient.DeleteOptions {
150
+ GracePeriodSeconds : new (int64 ), // A pointer to 0
151
+ }
152
+ * deleteOptions .GracePeriodSeconds = 0
153
+
154
+ Expect (k8sClient .Delete (ctx , & pod , deleteOptions )).To (Succeed ())
155
+ }
156
+
157
+ time .Sleep (5 * time .Second )
158
+
159
+ By ("Verifying the number of Pods matches the desired replicas" )
160
+ Expect (k8sClient .Get (ctx , ctrlclient .ObjectKeyFromObject (qworkerResource ), qworkerResource )).To (Succeed ())
161
+ Expect (ownedPods ).To (HaveLen (qworkerResource .Status .DesiredReplicas ))
162
+ })
186
163
187
164
})
188
165
})
0 commit comments