Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <[email protected]>
  • Loading branch information
JorTurFer committed May 13, 2024
1 parent 083a931 commit b58f3f4
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,3 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {

})
})

func validateResources(expected, value corev1.ResourceRequirements) {
Expect(expected.Requests.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Cpu().AsApproximateFloat64()))
Expect(expected.Requests.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Memory().AsApproximateFloat64()))
Expect(expected.Limits.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Cpu().AsApproximateFloat64()))
Expect(expected.Limits.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Memory().AsApproximateFloat64()))
}
257 changes: 257 additions & 0 deletions operator/controllers/http/httpscalingset_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package http

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

"github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
)

var _ = Describe("HTTPScalingSetController", func() {
Describe("functional tests", func() {
It("HTTPScalingSet generates the interceptor and scaler using default values", func() {
name := "default-values"
namesapce := "http-ss-defaults"
createNamespace(namesapce)
interceptorName := fmt.Sprintf("%s-interceptor", name)
interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name)

scalerName := fmt.Sprintf("%s-external-scaler", name)
scalerServiceName := fmt.Sprintf("%s-external-scaler", name)

css := &v1alpha1.HTTPScalingSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namesapce,
},
Spec: v1alpha1.HTTPScalingSetSpec{
Interceptor: v1alpha1.HTTPInterceptorSepc{},
Scaler: v1alpha1.HTTPScalerSepc{},
},
}
err := k8sClient.Create(context.Background(), css)
Expect(err).ToNot(HaveOccurred())

// Validate interceptor proxy service
interceptorProxyService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorProxyService).ShouldNot(BeNil())
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy"))
Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(int32(8080)))

// Validate interceptor admin service
interceptorAdminService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorAdminService).ShouldNot(BeNil())
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin"))
Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(int32(9090)))

// Validate interceptor deployment
interceptorDeploy := &v1.Deployment{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorDeploy).ShouldNot(BeNil())
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(int32(1)))
Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil())
Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil())

// Validate scaler service
scalerService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(scalerService).ShouldNot(BeNil())
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc"))
Expect(scalerService.Spec.Ports[0].Port).Should(Equal(int32(9090)))

// Validate scaler deployment
scalerDeploy := &v1.Deployment{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(scalerDeploy).ShouldNot(BeNil())
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(*scalerDeploy.Spec.Replicas).Should(Equal(int32(1)))
Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil())
Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil())
})

It("HTTPScalingSet generates the interceptor and scaler using custom values", func() {
name := "custom-values"
namesapce := "http-ss-custom"
createNamespace(namesapce)
interceptorName := fmt.Sprintf("%s-interceptor", name)
interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name)
var interceptorReplicas int32 = 3
interceptorResouces := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": *resource.NewQuantity(10, resource.DecimalSI),
"memory": *resource.NewQuantity(11, resource.DecimalSI),
},
Limits: corev1.ResourceList{
"cpu": *resource.NewQuantity(20, resource.DecimalSI),
"memory": *resource.NewQuantity(21, resource.DecimalSI),
},
}
var interceptorProxyPort int32 = 6666
var interceptorAdminPort int32 = 7777

scalerName := fmt.Sprintf("%s-external-scaler", name)
scalerServiceName := fmt.Sprintf("%s-external-scaler", name)
var scalerReplicas int32 = 2
scalerResouces := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": *resource.NewQuantity(30, resource.DecimalSI),
"memory": *resource.NewQuantity(31, resource.DecimalSI),
},
Limits: corev1.ResourceList{
"cpu": *resource.NewQuantity(40, resource.DecimalSI),
"memory": *resource.NewQuantity(41, resource.DecimalSI),
},
}
var scalerPort int32 = 8888

css := &v1alpha1.HTTPScalingSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namesapce,
},
Spec: v1alpha1.HTTPScalingSetSpec{
Interceptor: v1alpha1.HTTPInterceptorSepc{
Replicas: ptr.To(interceptorReplicas),
Resources: interceptorResouces,
Config: &v1alpha1.HTTPInterceptorConfigurationSepc{
ProxyPort: ptr.To(interceptorProxyPort),
AdminPort: ptr.To(interceptorAdminPort),
},
Labels: map[string]string{
"interceptor-label-1": "value-1",
},
Annotations: map[string]string{
"interceptor-annotation-1": "value-2",
},
},
Scaler: v1alpha1.HTTPScalerSepc{
Replicas: ptr.To(scalerReplicas),
Resources: scalerResouces,
Config: v1alpha1.HTTPScalerConfigurationSepc{
Port: ptr.To(scalerPort),
},
Labels: map[string]string{
"scaler-label-1": "value-3",
},
Annotations: map[string]string{
"scaler-annotation-1": "value-4",
},
},
},
}
err := k8sClient.Create(context.Background(), css)
Expect(err).ToNot(HaveOccurred())

// Validate interceptor proxy service
interceptorProxyService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorProxyService).ShouldNot(BeNil())
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy"))
Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(interceptorProxyPort))

// Validate interceptor admin service
interceptorAdminService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorAdminService).ShouldNot(BeNil())
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin"))
Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(interceptorAdminPort))

// Validate interceptor deployment
interceptorDeploy := &v1.Deployment{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(interceptorDeploy).ShouldNot(BeNil())
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor"))
Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(interceptorReplicas))
Expect(interceptorDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("interceptor-label-1", "value-1"))
Expect(interceptorDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("interceptor-annotation-1", "value-2"))
validateResources(interceptorResouces, interceptorDeploy.Spec.Template.Spec.Containers[0].Resources)

// Validate scaler service
scalerService := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(scalerService).ShouldNot(BeNil())
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc"))
Expect(scalerService.Spec.Ports[0].Port).Should(Equal(scalerPort))

// Validate scaler deployment
scalerDeploy := &v1.Deployment{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy)
}).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred())

Expect(scalerDeploy).ShouldNot(BeNil())
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name))
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler"))
Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet"))
Expect(*scalerDeploy.Spec.Replicas).Should(Equal(scalerReplicas))
Expect(scalerDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("scaler-label-1", "value-3"))
Expect(scalerDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("scaler-annotation-1", "value-4"))
validateResources(scalerResouces, scalerDeploy.Spec.Template.Spec.Containers[0].Resources)
})

})
})
32 changes: 25 additions & 7 deletions operator/controllers/http/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -101,17 +102,17 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&HTTPScalingSetReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

kedaNs := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "keda",
},
}
err = k8sClient.Create(ctx, kedaNs)
Expect(err).ToNot(HaveOccurred())
createNamespace("keda")

go func() {
err = k8sManager.Start(ctx)
Expand All @@ -129,6 +130,23 @@ var _ = AfterSuite(func() {
Expect(err).ToNot(HaveOccurred())
})

func validateResources(expected, value corev1.ResourceRequirements) {
Expect(expected.Requests.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Cpu().AsApproximateFloat64()))
Expect(expected.Requests.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Requests.Memory().AsApproximateFloat64()))
Expect(expected.Limits.Cpu().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Cpu().AsApproximateFloat64()))
Expect(expected.Limits.Memory().AsApproximateFloat64()).Should(BeEquivalentTo(value.Limits.Memory().AsApproximateFloat64()))
}

func createNamespace(name string) {
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
err := k8sClient.Create(ctx, ns)
Expect(err).ToNot(HaveOccurred())
}

type commonTestInfra struct {
ns string
appName string
Expand Down

0 comments on commit b58f3f4

Please sign in to comment.