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

chore: Add external-push scaler e2e tests #4585

Merged
merged 7 commits into from
Jun 1, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ New deprecation(s):

### Other

- **General**: Add e2e test for external push scaler ([#2698](https://github.com/kedacore/keda/pull/2698))
- **General**: Bump Golang to 1.20 ([#4517](https://github.com/kedacore/keda/issues/4517))
- **General**: Drop a transitive dependency on bou.ke/monkey ([#4364](https://github.com/kedacore/keda/issues/4364))
- **General**: Fix odd number of arguments passed as key-value pairs for logging ([#4368](https://github.com/kedacore/keda/issues/4368))
Expand Down
14 changes: 13 additions & 1 deletion tests/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,22 @@ func WaitForNamespaceDeletion(t *testing.T, nsName string) bool {
return false
}

func WaitForScaledJobCount(t *testing.T, kc *kubernetes.Clientset, scaledJobName, namespace string,
target, iterations, intervalSeconds int) bool {
return waitForJobCount(t, kc, fmt.Sprintf("scaledjob.keda.sh/name=%s", scaledJobName), namespace, target, iterations, intervalSeconds)
}

func WaitForJobCount(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
return waitForJobCount(t, kc, "", namespace, target, iterations, intervalSeconds)
}

func waitForJobCount(t *testing.T, kc *kubernetes.Clientset, selector, namespace string,
target, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{})
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: selector,
})
count := len(jobList.Items)

t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d",
Expand Down
70 changes: 48 additions & 22 deletions tests/internals/min_replica_sj/min_replica_sj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ const (
)

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
serviceName = fmt.Sprintf("%s-service", testName)
scalerName = fmt.Sprintf("%s-scaler", testName)
scaledJobName = fmt.Sprintf("%s-sj", testName)
iterationCount = 15
testNamespace = fmt.Sprintf("%s-ns", testName)
serviceName = fmt.Sprintf("%s-service", testName)
scalerName = fmt.Sprintf("%s-scaler", testName)
scaledJobName = fmt.Sprintf("%s-sj", testName)
metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, testNamespace)
iterationCount = 60
)

type templateData struct {
TestNamespace string
ServiceName string
ScalerName string
ScaledJobName string
MetricsServerEndpoint string
MetricThreshold, MetricValue int
MinReplicaCount, MaxReplicaCount int
}
Expand All @@ -48,7 +50,11 @@ metadata:
spec:
ports:
- port: 6000
name: grpc
targetPort: 6000
- port: 8080
name: http
targetPort: 8080
selector:
app: {{.ScalerName}}
`
Expand All @@ -73,10 +79,11 @@ spec:
spec:
containers:
- name: scaler
image: ghcr.io/kedacore/tests-external-scaler-e2e:latest
image: ghcr.io/kedacore/tests-external-scaler:latest
imagePullPolicy: Always
ports:
- containerPort: 6000
- containerPort: 8080
`

scaledJobTemplate = `
Expand Down Expand Up @@ -108,21 +115,34 @@ spec:
metadata:
scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000
metricThreshold: "{{.MetricThreshold}}"
metricValue: "{{.MetricValue}}"
`

updateMetricTemplate = `apiVersion: batch/v1
kind: Job
metadata:
name: update-metric-value
namespace: {{.TestNamespace}}
spec:
template:
spec:
containers:
- name: curl-client
image: curlimages/curl
imagePullPolicy: Always
command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"]
restartPolicy: Never`
)

func TestMinReplicaCount(t *testing.T) {
kc := GetKubernetesClient(t)
minReplicaCount := 2
maxReplicaCount := 10
metricValue := 0

data, templates := getTemplateData(minReplicaCount, maxReplicaCount, metricValue)
data, templates := getTemplateData(minReplicaCount, maxReplicaCount)

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

assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, minReplicaCount, iterationCount, 1),
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, minReplicaCount, iterationCount, 1),
"job count should be %d after %d iterations", minReplicaCount, iterationCount)

testMinReplicaCountWithMetricValue(t, kc, data)
Expand All @@ -140,10 +160,12 @@ func testMinReplicaCountWithMetricValue(t *testing.T, kc *kubernetes.Clientset,
data.MetricValue = 1

KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate)
KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)

expectedTarget := data.MinReplicaCount + data.MetricValue
assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, expectedTarget, 15, 1),
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, expectedTarget, iterationCount, 1),
"job count should be %d after %d iterations", expectedTarget, iterationCount)
KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)
}

func testMinReplicaCountGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *testing.T, kc *kubernetes.Clientset, data templateData) {
Expand All @@ -154,9 +176,11 @@ func testMinReplicaCountGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *tes
data.MetricValue = 0

KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate)
KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)

assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, data.MaxReplicaCount, 15, 1),
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, data.MaxReplicaCount, iterationCount, 1),
"job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount)
KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)
}

func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *testing.T, kc *kubernetes.Clientset, data templateData) {
Expand All @@ -167,21 +191,23 @@ func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxRepl
data.MetricValue = 3

KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate)
KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)

assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, data.MaxReplicaCount, 15, 1),
assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, data.MaxReplicaCount, iterationCount, 1),
"job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount)
KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate)
}

func getTemplateData(minReplicaCount int, maxReplicaCount int, metricValue int) (templateData, []Template) {
func getTemplateData(minReplicaCount int, maxReplicaCount int) (templateData, []Template) {
return templateData{
TestNamespace: testNamespace,
ServiceName: serviceName,
ScalerName: scalerName,
ScaledJobName: scaledJobName,
MetricThreshold: 1,
MetricValue: metricValue,
MinReplicaCount: minReplicaCount,
MaxReplicaCount: maxReplicaCount,
TestNamespace: testNamespace,
ServiceName: serviceName,
ScalerName: scalerName,
ScaledJobName: scaledJobName,
MetricThreshold: 1,
MetricsServerEndpoint: metricsServerEndpoint,
MinReplicaCount: minReplicaCount,
MaxReplicaCount: maxReplicaCount,
}, []Template{
{Name: "scalerTemplate", Config: scalerTemplate},
{Name: "serviceTemplate", Config: serviceTemplate},
Expand Down
Loading