Skip to content

Commit

Permalink
add test and revise old test after forbidden the circumstance scaleTa…
Browse files Browse the repository at this point in the history
…rgetRef not found

Signed-off-by: Shane <[email protected]>
  • Loading branch information
ctccxxd committed Nov 20, 2024
1 parent c76c19f commit e20ff99
Showing 1 changed file with 104 additions and 4 deletions.
108 changes: 104 additions & 4 deletions apis/keda/v1alpha1/scaledobject_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ var _ = It("should validate the so creation when there isn't any hpa", func() {

namespaceName := "valid"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
Expand All @@ -50,12 +56,23 @@ var _ = It("should validate the so creation when there are other SO for other wo

namespaceName := "valid-multiple-so"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)
otherWorkload := createDeployment(namespaceName, false, false)
otherWorkload.Spec.Template.Name = "other-workload"

so1 := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so2 := createScaledObject("other-so-name", namespaceName, "other-workload", "apps/v1", "Deployment", false, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), otherWorkload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), so1)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -68,12 +85,18 @@ var _ = It("should validate the so creation when there are other HPA for other w

namespaceName := "valid-other-hpa"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa("other-hpa-name", namespaceName, "other-workload", "apps/v1", "Deployment", nil)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -87,12 +110,18 @@ var _ = It("should validate the so creation when it's own hpa is already generat
hpaName := "test-so-hpa"
namespaceName := "own-hpa"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", so)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -106,12 +135,18 @@ var _ = It("should validate the so update when it's own hpa is already generated
hpaName := "test-so-hpa"
namespaceName := "update-so"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", so)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -129,12 +164,18 @@ var _ = It("shouldn't validate the so creation when there is another unmanaged h
hpaName := "test-unmanaged-hpa"
namespaceName := "unmanaged-hpa"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -146,13 +187,19 @@ var _ = It("shouldn't validate the so creation when there is another unmanaged h
var _ = It("shouldn't validate the so creation when the replica counts are wrong", func() {
namespaceName := "wrong-replica-count"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.MinReplicaCount = ptr.To[int32](10)
so.Spec.MaxReplicaCount = ptr.To[int32](5)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
Expand All @@ -162,6 +209,8 @@ var _ = It("shouldn't validate the so creation when the fallback is wrong", func
namespaceName := "wrong-fallback"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Fallback = &Fallback{
FailureThreshold: -1,
Expand All @@ -171,6 +220,9 @@ var _ = It("shouldn't validate the so creation when the fallback is wrong", func
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
Expand Down Expand Up @@ -201,12 +253,18 @@ var _ = It("shouldn't validate the so creation when there is another unmanaged h
hpaName := "test-unmanaged-hpa-ownership"
namespaceName := "unmanaged-hpa-ownership"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{ScaledObjectTransferHpaOwnershipAnnotation: "true"}, hpaName)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -220,13 +278,19 @@ var _ = It("shouldn't validate the so creation when hpa has shared-ownership una
hpaName := "test-hpa-disabled-validation-by-hpa-ownership"
namespaceName := "hpa-ownership"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
hpa.ObjectMeta.Annotations = map[string]string{ValidationsHpaOwnershipAnnotation: "false"}
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{ScaledObjectTransferHpaOwnershipAnnotation: "false"}, hpaName)

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -240,12 +304,18 @@ var _ = It("shouldn't validate the so creation when there is another so", func()
so2Name := "test-so2"
namespaceName := "managed-hpa"
namespace := createNamespace(namespaceName)

workload := createDeployment(namespaceName, false, false)

so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so2 := createScaledObject(so2Name, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), so2)
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -291,29 +361,51 @@ var _ = It("should validate the so creation with cpu and memory when deployment
}).ShouldNot(HaveOccurred())
})

var _ = It("shouldn't validate the creation with cpu and memory when deployment is missing", func() {
var _ = It("shouldn't validate the so creation when deployment is missing", func() {

namespaceName := "deployment-missing"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so, client.DryRunAll)
}).Should(HaveOccurred())
})

var _ = It("should validate the so creation with cpu and memory", func() {

namespaceName := "deployment-not-missing"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
}).ShouldNot(HaveOccurred())
})

var _ = It("should validate the creation with cpu and memory when deployment is missing and dry-run is true", func() {
var _ = It("should validate the so creation with cpu and memory when dry-run is true", func() {

namespaceName := "deployment-missing-dry-run"
namespaceName := "deployment-dry-run"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so, client.DryRunAll)
}).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -468,11 +560,15 @@ var _ = It("should validate the so creation without cpu and memory when custom r

namespaceName := "crd-not-resources"
namespace := createNamespace(namespaceName)
workload := createStatefulSet(namespaceName, false, true)
so := createScaledObject(soName, namespaceName, workloadName, "custom-api", "StatefulSet", true, map[string]string{}, "")

err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -623,6 +719,7 @@ var _ = It("shouldn't create so when stabilizationWindowSeconds exceeds 3600", f

namespaceName := "fail-so-creation"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, false, false)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Advanced.HorizontalPodAutoscalerConfig = &HorizontalPodAutoscalerConfig{
Behavior: &v2.HorizontalPodAutoscalerBehavior{
Expand All @@ -634,6 +731,9 @@ var _ = It("shouldn't create so when stabilizationWindowSeconds exceeds 3600", f
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())

err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())

Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).
Expand Down

0 comments on commit e20ff99

Please sign in to comment.