Skip to content

Commit

Permalink
e2e - cpu
Browse files Browse the repository at this point in the history
Resolve kedacore#3169

Signed-off-by: aviadlevy <[email protected]>
  • Loading branch information
aviadlevy committed Jun 21, 2022
1 parent 40cd0a9 commit 522dc7f
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 147 deletions.
147 changes: 0 additions & 147 deletions tests/scalers/cpu.test.ts

This file was deleted.

158 changes: 158 additions & 0 deletions tests/scalers_go/cpu/cpu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//go:build e2e
// +build e2e

package cpu_test

import (
"fmt"
"testing"

"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"

. "github.com/kedacore/keda/v2/tests/helper"
)

// Load environment variables from .env file
var _ = godotenv.Load("../../.env")

const (
testName = "cpu-test"
)

type templateData struct {
TestNamespace string
DeploymentName string
ScaledObjectName string
}

const (
deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
spec:
selector:
matchLabels:
run: {{.DeploymentName}}
replicas: 1
template:
metadata:
labels:
run: {{.DeploymentName}}
spec:
containers:
- name: {{.DeploymentName}}
image: k8s.gcr.io/hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
imagePullPolicy: IfNotPresent
`

serviceTemplate = `apiVersion: v1
kind: Service
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
labels:
run: {{.DeploymentName}}
spec:
ports:
- port: 80
selector:
run: {{.DeploymentName}}
`

scaledObjectTemplate = `
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {{.ScaledObjectName}}
namespace: {{.TestNamespace}}
labels:
run: {{.DeploymentName}}
spec:
advanced:
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
stabilizationWindowSeconds: 0
maxReplicaCount: 2
minReplicaCount: 1
scaleTargetRef:
name: {{.DeploymentName}}
triggers:
- type: cpu
metadata:
type: Utilization
value: "50"
`

triggerJob = `apiVersion: batch/v1
kind: Job
metadata:
name: trigger-job
namespace: {{.TestNamespace}}
spec:
template:
spec:
containers:
- image: busybox
name: test
command: ["/bin/sh"]
args: ["-c", "for i in $(seq 1 400);do wget -q -O- http://{{.DeploymentName}}.{{.TestNamespace}}.svc/;sleep 0.1;done"]
restartPolicy: Never
activeDeadlineSeconds: 400
backoffLimit: 3`
)

func TestCpuScaler(t *testing.T) {
testNamespace := fmt.Sprintf("%s-ns", testName)
deploymentName := fmt.Sprintf("%s-deployment", testName)
scaledObjectName := fmt.Sprintf("%s-so", testName)

// Create kubernetes resources
kc := GetKubernetesClient(t)
data := getTemplateData(testNamespace, deploymentName, scaledObjectName)
templates := []string{serviceTemplate, deploymentTemplate, scaledObjectTemplate}

CreateKubernetesResources(t, kc, testNamespace, data, templates...)

assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, 1, 60, 1),
"Replica count should start out as 1")

t.Log("--- testing scale up ---")
t.Log("--- applying job ---")

templateTriggerJob := []string{triggerJob}
KubectlApplyMultipleWithTemplate(t, data, templateTriggerJob...)

assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, 2, 180, 1),
"Replica count should scale up in next 3 minutes")

t.Log("--- testing scale down ---")
t.Log("--- deleting job ---")

KubectlDeleteWithTemplate(t, data, triggerJob)

assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, 1, 180, 1),
"Replica count should be 1 in next 3 minutes")

// cleanup
DeleteKubernetesResources(t, kc, testNamespace, data, templates...)
}

func getTemplateData(testNamespace string, deploymentName string, scaledObjectName string) templateData {
return templateData{
TestNamespace: testNamespace,
DeploymentName: deploymentName,
ScaledObjectName: scaledObjectName,
}
}

0 comments on commit 522dc7f

Please sign in to comment.