Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate e2e test for /scale subresource to Go. #3502

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions tests/internals/subresource_scale/subresource_scale_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
//go:build e2e
// +build e2e

package subresource_scale_test

import (
"fmt"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"

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

const (
testName = "subresource-scale-test"
)

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
argoNamespace = "argo-rollouts"
monitoredDeploymentName = fmt.Sprintf("%s-monitored", testName)
argoRolloutName = fmt.Sprintf("%s-rollout", testName)
scaledObjectName = fmt.Sprintf("%s-so", testName)
)

type templateData struct {
TestNamespace string
MonitoredDeploymentName string
ArgoRolloutName string
ScaledObjectName string
}

type templateValues map[string]string

const (
monitoredDeploymentTemplate = `apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.MonitoredDeploymentName}}
namespace: {{.TestNamespace}}
labels:
app: {{.MonitoredDeploymentName}}
spec:
replicas: 0
selector:
matchLabels:
app: {{.MonitoredDeploymentName}}
template:
metadata:
labels:
app: {{.MonitoredDeploymentName}}
spec:
containers:
- name: nginx
image: 'nginx'`

argoRolloutTemplate = `apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: {{.ArgoRolloutName}}
namespace: {{.TestNamespace}}
labels:
app: {{.ArgoRolloutName}}
spec:
replicas: 0
strategy:
canary:
steps:
- setWeight: 50
- pause: {duration: 10}
selector:
matchLabels:
app: {{.ArgoRolloutName}}
template:
metadata:
labels:
app: {{.ArgoRolloutName}}
spec:
containers:
- name: nginx
image: nginx
`

scaledObjectTemplate = `apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {{.ScaledObjectName}}
namespace: {{.TestNamespace}}
spec:
scaleTargetRef:
apiVersion: argoproj.io/v1alpha1
kind: Rollout
name: {{.ArgoRolloutName}}
pollingInterval: 5
cooldownPeriod: 5
minReplicaCount: 0
maxReplicaCount: 10
advanced:
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
stabilizationWindowSeconds: 5
triggers:
- type: kubernetes-workload
metadata:
podSelector: 'app={{.MonitoredDeploymentName}}'
value: '1'
`
)

func TestScaler(t *testing.T) {
// setup
t.Log("--- setting up ---")
// Create kubernetes resources
kc := GetKubernetesClient(t)
data, templates := getTemplateData()

setupArgo(t, kc)

CreateKubernetesResources(t, kc, testNamespace, data, templates)
assert.True(t, waitForArgoRolloutReplicaCount(t, argoRolloutName, testNamespace, 0),
"replica count should be 0 after 1 minute")

// test scaling
testScaleUp(t, kc)
testScaleDown(t, kc)

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

func setupArgo(t *testing.T, kc *kubernetes.Clientset) {
CreateNamespace(t, kc, argoNamespace)
cmdWithNamespace := fmt.Sprintf("kubectl apply -n %s -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml",
argoNamespace)
_, err := ExecuteCommand(cmdWithNamespace)

assert.NoErrorf(t, err, "cannot install argo resources - %s", err)
}

func cleanupArgo(t *testing.T, kc *kubernetes.Clientset) {
cmdWithNamespace := fmt.Sprintf("kubectl delete -n %s -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml",
argoNamespace)
_, err := ExecuteCommand(cmdWithNamespace)

assert.NoErrorf(t, err, "cannot delete argo resources - %s", err)
DeleteNamespace(t, kc, argoNamespace)
}

func testScaleUp(t *testing.T, kc *kubernetes.Clientset) {
t.Log("--- testing scale up ---")

// scale monitored deployment to 5 replicas
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 5, testNamespace)
assert.True(t, waitForArgoRolloutReplicaCount(t, argoRolloutName, testNamespace, 5),
"replica count should be 5 after 1 minute")

// scale monitored deployment to 10 replicas
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 10, testNamespace)
assert.True(t, waitForArgoRolloutReplicaCount(t, argoRolloutName, testNamespace, 10),
"replica count should be 10 after 1 minute")
}

func testScaleDown(t *testing.T, kc *kubernetes.Clientset) {
t.Log("--- testing scale down ---")

// scale monitored deployment to 5 replicas
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 5, testNamespace)
assert.True(t, waitForArgoRolloutReplicaCount(t, argoRolloutName, testNamespace, 5),
"replica count should be 5 after 1 minute")

// scale monitored deployment to 0 replicas
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 0, testNamespace)
assert.True(t, waitForArgoRolloutReplicaCount(t, argoRolloutName, testNamespace, 0),
"replica count should be 0 after 1 minute")
}

func getTemplateData() (templateData, templateValues) {
return templateData{
TestNamespace: testNamespace,
MonitoredDeploymentName: monitoredDeploymentName,
ArgoRolloutName: argoRolloutName,
ScaledObjectName: scaledObjectName,
}, templateValues{
"monitoredDeploymentTemplate": monitoredDeploymentTemplate,
"argoRolloutTemplate": argoRolloutTemplate,
"scaledObjectTemplate": scaledObjectTemplate}
}

func waitForArgoRolloutReplicaCount(t *testing.T, name, namespace string, target int) bool {
for i := 0; i < 60; i++ {
kctlGetCmd := fmt.Sprintf(`kubectl get rollouts.argoproj.io/%s -n %s -o jsonpath="{.spec.replicas}"`, argoRolloutName, namespace)
output, err := ExecuteCommand(kctlGetCmd)

assert.NoErrorf(t, err, "cannot get rollout info - %s", err)

unqoutedOutput := strings.ReplaceAll(string(output), "\"", "")
replicas, err := strconv.ParseInt(unqoutedOutput, 10, 64)
assert.NoErrorf(t, err, "cannot convert rollout count to int - %s", err)

t.Logf("Waiting for rollout replicas to hit target. Deployment - %s, Current - %d, Target - %d",
name, replicas, target)

if replicas == int64(target) {
return true
}

time.Sleep(time.Second)
}

return false
}
Loading