From 6624c7cd015c13f2e9556573ebee209fa0b2f080 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Wed, 4 Oct 2023 23:58:44 +0200 Subject: [PATCH 1/4] feat: add an e2e test for testing scaling phase Signed-off-by: Jorge Turrado --- .../scaling_phase/scaling_phase_test.go | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 tests/checks/scaling_phase/scaling_phase_test.go diff --git a/tests/checks/scaling_phase/scaling_phase_test.go b/tests/checks/scaling_phase/scaling_phase_test.go new file mode 100644 index 00000000..937e3825 --- /dev/null +++ b/tests/checks/scaling_phase/scaling_phase_test.go @@ -0,0 +1,188 @@ +//go:build e2e +// +build e2e + +package scaling_phase_test + +import ( + "fmt" + "testing" + + "github.com/tj/assert" + "k8s.io/client-go/kubernetes" + + . "github.com/kedacore/http-add-on/tests/helper" +) + +const ( + testName = "scaling-phase-test" +) + +var ( + testNamespace = fmt.Sprintf("%s-ns", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + serviceName = fmt.Sprintf("%s-service", testName) + httpScaledObjectName = fmt.Sprintf("%s-http-so", testName) + host = testName + minReplicaCount = 0 + maxReplicaCount = 4 +) + +type templateData struct { + TestNamespace string + DeploymentName string + ServiceName string + HTTPScaledObjectName string + Host string + MinReplicas int + MaxReplicas int +} + +const ( + serviceTemplate = ` +apiVersion: v1 +kind: Service +metadata: + name: {{.ServiceName}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + ports: + - port: 8080 + targetPort: http + protocol: TCP + name: http + selector: + app: {{.DeploymentName}} +` + + deploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + replicas: 0 + selector: + matchLabels: + app: {{.DeploymentName}} + template: + metadata: + labels: + app: {{.DeploymentName}} + spec: + containers: + - name: {{.DeploymentName}} + image: registry.k8s.io/e2e-test-images/agnhost:2.45 + args: + - netexec + ports: + - name: http + containerPort: 8080 + protocol: TCP + readinessProbe: + httpGet: + path: / + port: http +` + + loadJobTemplate = ` +apiVersion: batch/v1 +kind: Job +metadata: + name: load-generator + namespace: {{.TestNamespace}} +spec: + template: + spec: + containers: + - name: apache-ab + image: ghcr.io/kedacore/tests-apache-ab + imagePullPolicy: Always + args: + - "-n" + - "2000000" + - "-c" + - "20" + - "-H" + - "Host: {{.Host}}" + - "http://keda-http-add-on-interceptor-proxy.keda:8080/" + restartPolicy: Never + terminationGracePeriodSeconds: 5 + activeDeadlineSeconds: 600 + backoffLimit: 5 +` + httpScaledObjectTemplate = ` +kind: HTTPScaledObject +apiVersion: http.keda.sh/v1alpha1 +metadata: + name: {{.HTTPScaledObjectName}} + namespace: {{.TestNamespace}} +spec: + hosts: + - {{.Host}} + targetPendingRequests: 2 + scaledownPeriod: 10 + scaleTargetRef: + deployment: {{.DeploymentName}} + service: {{.ServiceName}} + port: 8080 + replicas: + min: {{ .MinReplicas }} + max: {{ .MaxReplicas }} +` +) + +func TestCheck(t *testing.T) { + // setup + t.Log("--- setting up ---") + // Create kubernetes resources + kc := GetKubernetesClient(t) + data, templates := getTemplateData() + CreateKubernetesResources(t, kc, testNamespace, data, templates) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 6, 10), + "replica count should be %d after 1 minutes", minReplicaCount) + + testScaleOut(t, kc, data) + testScaleIn(t, kc, data) + + // cleanup + DeleteKubernetesResources(t, testNamespace, data, templates) +} + +func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale out ---") + + KubectlApplyWithTemplate(t, data, "loadJobTemplate", loadJobTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 18, 10), + "replica count should be %d after 3 minutes", maxReplicaCount) + KubectlDeleteWithTemplate(t, data, "loadJobTemplate", loadJobTemplate) +} + +func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale out ---") + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 12, 10), + "replica count should be %d after 2 minutes", minReplicaCount) +} + +func getTemplateData() (templateData, []Template) { + return templateData{ + TestNamespace: testNamespace, + DeploymentName: deploymentName, + ServiceName: serviceName, + HTTPScaledObjectName: httpScaledObjectName, + Host: host, + MinReplicas: minReplicaCount, + MaxReplicas: maxReplicaCount, + }, []Template{ + {Name: "deploymentTemplate", Config: deploymentTemplate}, + {Name: "serviceNameTemplate", Config: serviceTemplate}, + {Name: "httpScaledObjectTemplate", Config: httpScaledObjectTemplate}, + } +} From eeab1f0cce29ce24865b758d22ba628ea6207597 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Thu, 5 Oct 2023 08:04:22 +0200 Subject: [PATCH 2/4] fix style Signed-off-by: Jorge Turrado --- tests/checks/scaling_phase/scaling_phase_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checks/scaling_phase/scaling_phase_test.go b/tests/checks/scaling_phase/scaling_phase_test.go index 937e3825..e16731a0 100644 --- a/tests/checks/scaling_phase/scaling_phase_test.go +++ b/tests/checks/scaling_phase/scaling_phase_test.go @@ -102,12 +102,12 @@ spec: - name: apache-ab image: ghcr.io/kedacore/tests-apache-ab imagePullPolicy: Always - args: + args: - "-n" - "2000000" - "-c" - "20" - - "-H" + - "-H" - "Host: {{.Host}}" - "http://keda-http-add-on-interceptor-proxy.keda:8080/" restartPolicy: Never From c87a4ba7e1e2b1da1ff07df966f2b6dc48ef0bcf Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Thu, 5 Oct 2023 08:54:51 +0200 Subject: [PATCH 3/4] remove not used var Signed-off-by: Jorge Turrado --- tests/checks/scaling_phase/scaling_phase_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/checks/scaling_phase/scaling_phase_test.go b/tests/checks/scaling_phase/scaling_phase_test.go index e16731a0..9446da74 100644 --- a/tests/checks/scaling_phase/scaling_phase_test.go +++ b/tests/checks/scaling_phase/scaling_phase_test.go @@ -164,7 +164,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { KubectlDeleteWithTemplate(t, data, "loadJobTemplate", loadJobTemplate) } -func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { +func testScaleIn(t *testing.T, kc *kubernetes.Clientset) { t.Log("--- testing scale out ---") assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 12, 10), From 2d1f431037e55bddb6729cc981429a99020166e7 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Thu, 5 Oct 2023 09:13:40 +0200 Subject: [PATCH 4/4] remove not used var Signed-off-by: Jorge Turrado --- tests/checks/scaling_phase/scaling_phase_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/checks/scaling_phase/scaling_phase_test.go b/tests/checks/scaling_phase/scaling_phase_test.go index 9446da74..c9359c6a 100644 --- a/tests/checks/scaling_phase/scaling_phase_test.go +++ b/tests/checks/scaling_phase/scaling_phase_test.go @@ -148,7 +148,7 @@ func TestCheck(t *testing.T) { "replica count should be %d after 1 minutes", minReplicaCount) testScaleOut(t, kc, data) - testScaleIn(t, kc, data) + testScaleIn(t, kc) // cleanup DeleteKubernetesResources(t, testNamespace, data, templates)