Skip to content

Commit b136f56

Browse files
committed
fix: tests
1 parent 2126d6a commit b136f56

File tree

2 files changed

+45
-67
lines changed

2 files changed

+45
-67
lines changed

internal/controller/qworker_controller_test.go

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"time"
21+
2022
. "github.com/onsi/ginkgo/v2"
2123
. "github.com/onsi/gomega"
2224
"github.com/quickube/QScaler/api/v1alpha1"
@@ -27,7 +29,6 @@ import (
2729
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2830
"k8s.io/apimachinery/pkg/types"
2931
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
30-
"time"
3132
)
3233

3334
var _ = Describe("QWorker Controller", func() {
@@ -109,8 +110,9 @@ var _ = Describe("QWorker Controller", func() {
109110
})
110111

111112
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)
114116

115117
time.Sleep(5 * time.Second)
116118
By("Checking QWorker status")
@@ -121,68 +123,43 @@ var _ = Describe("QWorker Controller", func() {
121123

122124
})
123125

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+
})
186163

187164
})
188165
})

internal/controller/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ package controller
1919
import (
2020
"context"
2121
"fmt"
22-
quickcubecomv1alpha1 "github.com/quickube/QScaler/api/v1alpha1"
2322
"path/filepath"
2423
"runtime"
24+
"testing"
25+
26+
quickcubecomv1alpha1 "github.com/quickube/QScaler/api/v1alpha1"
2527
ctrl "sigs.k8s.io/controller-runtime"
2628
"sigs.k8s.io/controller-runtime/pkg/log"
2729
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2830
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
29-
"testing"
3031

3132
. "github.com/onsi/ginkgo/v2"
3233
. "github.com/onsi/gomega"

0 commit comments

Comments
 (0)