Skip to content

Commit b58f3f4

Browse files
committed
Add some unit tests
Signed-off-by: Jorge Turrado <[email protected]>
1 parent 083a931 commit b58f3f4

File tree

3 files changed

+282
-14
lines changed

3 files changed

+282
-14
lines changed

operator/controllers/http/clusterhttpscalingset_controller_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,3 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
249249

250250
})
251251
})
252-
253-
func validateResources(expected, value corev1.ResourceRequirements) {
254-
Expect(expected.Requests.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Cpu().AsApproximateFloat64()))
255-
Expect(expected.Requests.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Memory().AsApproximateFloat64()))
256-
Expect(expected.Limits.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Cpu().AsApproximateFloat64()))
257-
Expect(expected.Limits.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Memory().AsApproximateFloat64()))
258-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
package http
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
v1 "k8s.io/api/apps/v1"
11+
corev1 "k8s.io/api/core/v1"
12+
"k8s.io/apimachinery/pkg/api/resource"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/types"
15+
"k8s.io/utils/ptr"
16+
17+
"github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
18+
)
19+
20+
var _ = Describe("HTTPScalingSetController", func() {
21+
Describe("functional tests", func() {
22+
It("HTTPScalingSet generates the interceptor and scaler using default values", func() {
23+
name := "default-values"
24+
namesapce := "http-ss-defaults"
25+
createNamespace(namesapce)
26+
interceptorName := fmt.Sprintf("%s-interceptor", name)
27+
interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name)
28+
29+
scalerName := fmt.Sprintf("%s-external-scaler", name)
30+
scalerServiceName := fmt.Sprintf("%s-external-scaler", name)
31+
32+
css := &v1alpha1.HTTPScalingSet{
33+
ObjectMeta: metav1.ObjectMeta{
34+
Name: name,
35+
Namespace: namesapce,
36+
},
37+
Spec: v1alpha1.HTTPScalingSetSpec{
38+
Interceptor: v1alpha1.HTTPInterceptorSepc{},
39+
Scaler: v1alpha1.HTTPScalerSepc{},
40+
},
41+
}
42+
err := k8sClient.Create(context.Background(), css)
43+
Expect(err).ToNot(HaveOccurred())
44+
45+
// Validate interceptor proxy service
46+
interceptorProxyService := &corev1.Service{}
47+
Eventually(func() error {
48+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService)
49+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
50+
51+
Expect(interceptorProxyService).ShouldNot(BeNil())
52+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
53+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
54+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
55+
Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy"))
56+
Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(int32(8080)))
57+
58+
// Validate interceptor admin service
59+
interceptorAdminService := &corev1.Service{}
60+
Eventually(func() error {
61+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService)
62+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
63+
64+
Expect(interceptorAdminService).ShouldNot(BeNil())
65+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
66+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
67+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
68+
Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin"))
69+
Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(int32(9090)))
70+
71+
// Validate interceptor deployment
72+
interceptorDeploy := &v1.Deployment{}
73+
Eventually(func() error {
74+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy)
75+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
76+
77+
Expect(interceptorDeploy).ShouldNot(BeNil())
78+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
79+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
80+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
81+
Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(int32(1)))
82+
Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil())
83+
Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil())
84+
85+
// Validate scaler service
86+
scalerService := &corev1.Service{}
87+
Eventually(func() error {
88+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService)
89+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
90+
91+
Expect(scalerService).ShouldNot(BeNil())
92+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
93+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
94+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
95+
Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc"))
96+
Expect(scalerService.Spec.Ports[0].Port).Should(Equal(int32(9090)))
97+
98+
// Validate scaler deployment
99+
scalerDeploy := &v1.Deployment{}
100+
Eventually(func() error {
101+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy)
102+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
103+
104+
Expect(scalerDeploy).ShouldNot(BeNil())
105+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
106+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
107+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
108+
Expect(*scalerDeploy.Spec.Replicas).Should(Equal(int32(1)))
109+
Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil())
110+
Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil())
111+
})
112+
113+
It("HTTPScalingSet generates the interceptor and scaler using custom values", func() {
114+
name := "custom-values"
115+
namesapce := "http-ss-custom"
116+
createNamespace(namesapce)
117+
interceptorName := fmt.Sprintf("%s-interceptor", name)
118+
interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name)
119+
var interceptorReplicas int32 = 3
120+
interceptorResouces := corev1.ResourceRequirements{
121+
Requests: corev1.ResourceList{
122+
"cpu": *resource.NewQuantity(10, resource.DecimalSI),
123+
"memory": *resource.NewQuantity(11, resource.DecimalSI),
124+
},
125+
Limits: corev1.ResourceList{
126+
"cpu": *resource.NewQuantity(20, resource.DecimalSI),
127+
"memory": *resource.NewQuantity(21, resource.DecimalSI),
128+
},
129+
}
130+
var interceptorProxyPort int32 = 6666
131+
var interceptorAdminPort int32 = 7777
132+
133+
scalerName := fmt.Sprintf("%s-external-scaler", name)
134+
scalerServiceName := fmt.Sprintf("%s-external-scaler", name)
135+
var scalerReplicas int32 = 2
136+
scalerResouces := corev1.ResourceRequirements{
137+
Requests: corev1.ResourceList{
138+
"cpu": *resource.NewQuantity(30, resource.DecimalSI),
139+
"memory": *resource.NewQuantity(31, resource.DecimalSI),
140+
},
141+
Limits: corev1.ResourceList{
142+
"cpu": *resource.NewQuantity(40, resource.DecimalSI),
143+
"memory": *resource.NewQuantity(41, resource.DecimalSI),
144+
},
145+
}
146+
var scalerPort int32 = 8888
147+
148+
css := &v1alpha1.HTTPScalingSet{
149+
ObjectMeta: metav1.ObjectMeta{
150+
Name: name,
151+
Namespace: namesapce,
152+
},
153+
Spec: v1alpha1.HTTPScalingSetSpec{
154+
Interceptor: v1alpha1.HTTPInterceptorSepc{
155+
Replicas: ptr.To(interceptorReplicas),
156+
Resources: interceptorResouces,
157+
Config: &v1alpha1.HTTPInterceptorConfigurationSepc{
158+
ProxyPort: ptr.To(interceptorProxyPort),
159+
AdminPort: ptr.To(interceptorAdminPort),
160+
},
161+
Labels: map[string]string{
162+
"interceptor-label-1": "value-1",
163+
},
164+
Annotations: map[string]string{
165+
"interceptor-annotation-1": "value-2",
166+
},
167+
},
168+
Scaler: v1alpha1.HTTPScalerSepc{
169+
Replicas: ptr.To(scalerReplicas),
170+
Resources: scalerResouces,
171+
Config: v1alpha1.HTTPScalerConfigurationSepc{
172+
Port: ptr.To(scalerPort),
173+
},
174+
Labels: map[string]string{
175+
"scaler-label-1": "value-3",
176+
},
177+
Annotations: map[string]string{
178+
"scaler-annotation-1": "value-4",
179+
},
180+
},
181+
},
182+
}
183+
err := k8sClient.Create(context.Background(), css)
184+
Expect(err).ToNot(HaveOccurred())
185+
186+
// Validate interceptor proxy service
187+
interceptorProxyService := &corev1.Service{}
188+
Eventually(func() error {
189+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService)
190+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
191+
192+
Expect(interceptorProxyService).ShouldNot(BeNil())
193+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
194+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
195+
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
196+
Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy"))
197+
Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(interceptorProxyPort))
198+
199+
// Validate interceptor admin service
200+
interceptorAdminService := &corev1.Service{}
201+
Eventually(func() error {
202+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService)
203+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
204+
205+
Expect(interceptorAdminService).ShouldNot(BeNil())
206+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
207+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
208+
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
209+
Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin"))
210+
Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(interceptorAdminPort))
211+
212+
// Validate interceptor deployment
213+
interceptorDeploy := &v1.Deployment{}
214+
Eventually(func() error {
215+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy)
216+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
217+
218+
Expect(interceptorDeploy).ShouldNot(BeNil())
219+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
220+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
221+
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
222+
Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(interceptorReplicas))
223+
Expect(interceptorDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("interceptor-label-1", "value-1"))
224+
Expect(interceptorDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("interceptor-annotation-1", "value-2"))
225+
validateResources(interceptorResouces, interceptorDeploy.Spec.Template.Spec.Containers[0].Resources)
226+
227+
// Validate scaler service
228+
scalerService := &corev1.Service{}
229+
Eventually(func() error {
230+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService)
231+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
232+
233+
Expect(scalerService).ShouldNot(BeNil())
234+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
235+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
236+
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
237+
Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc"))
238+
Expect(scalerService.Spec.Ports[0].Port).Should(Equal(scalerPort))
239+
240+
// Validate scaler deployment
241+
scalerDeploy := &v1.Deployment{}
242+
Eventually(func() error {
243+
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy)
244+
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())
245+
246+
Expect(scalerDeploy).ShouldNot(BeNil())
247+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
248+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
249+
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
250+
Expect(*scalerDeploy.Spec.Replicas).Should(Equal(scalerReplicas))
251+
Expect(scalerDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("scaler-label-1", "value-3"))
252+
Expect(scalerDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("scaler-annotation-1", "value-4"))
253+
validateResources(scalerResouces, scalerDeploy.Spec.Template.Spec.Containers[0].Resources)
254+
})
255+
256+
})
257+
})

operator/controllers/http/suite_test.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
2626
. "github.com/onsi/ginkgo/v2"
2727
. "github.com/onsi/gomega"
28+
corev1 "k8s.io/api/core/v1"
2829
v1 "k8s.io/api/core/v1"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/runtime"
@@ -101,17 +102,17 @@ var _ = BeforeSuite(func() {
101102
}).SetupWithManager(k8sManager)
102103
Expect(err).ToNot(HaveOccurred())
103104

105+
err = (&HTTPScalingSetReconciler{
106+
Client: k8sManager.GetClient(),
107+
Scheme: k8sManager.GetScheme(),
108+
}).SetupWithManager(k8sManager)
109+
Expect(err).ToNot(HaveOccurred())
110+
104111
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
105112
Expect(err).ToNot(HaveOccurred())
106113
Expect(k8sClient).ToNot(BeNil())
107114

108-
kedaNs := &v1.Namespace{
109-
ObjectMeta: metav1.ObjectMeta{
110-
Name: "keda",
111-
},
112-
}
113-
err = k8sClient.Create(ctx, kedaNs)
114-
Expect(err).ToNot(HaveOccurred())
115+
createNamespace("keda")
115116

116117
go func() {
117118
err = k8sManager.Start(ctx)
@@ -129,6 +130,23 @@ var _ = AfterSuite(func() {
129130
Expect(err).ToNot(HaveOccurred())
130131
})
131132

133+
func validateResources(expected, value corev1.ResourceRequirements) {
134+
Expect(expected.Requests.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Cpu().AsApproximateFloat64()))
135+
Expect(expected.Requests.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Memory().AsApproximateFloat64()))
136+
Expect(expected.Limits.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Cpu().AsApproximateFloat64()))
137+
Expect(expected.Limits.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Memory().AsApproximateFloat64()))
138+
}
139+
140+
func createNamespace(name string) {
141+
ns := &v1.Namespace{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Name: name,
144+
},
145+
}
146+
err := k8sClient.Create(ctx, ns)
147+
Expect(err).ToNot(HaveOccurred())
148+
}
149+
132150
type commonTestInfra struct {
133151
ns string
134152
appName string

0 commit comments

Comments
 (0)