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

Complete e2e test migration. #3505

Merged
merged 1 commit into from
Aug 5, 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
6 changes: 0 additions & 6 deletions tests/.prettierrc

This file was deleted.

130 changes: 0 additions & 130 deletions tests/OLD-README.md

This file was deleted.

12 changes: 6 additions & 6 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Make sure that you are in `keda/tests` directory.

```bash
go test -v -tags e2e ./utils/setup_test.go # Only needs to be run once.
go test -v -tags e2e ./scalers_go/...
go test -v -tags e2e ./scalers/...
go test -v -tags e2e ./utils/cleanup_test.go # Skip if you want to keep testing.
```

### Specific test

```bash
go test -v -tags e2e ./scalers_go/azure_queue/azure_queue_test.go # Assumes that setup has been run before
go test -v -tags e2e ./scalers/azure_queue/azure_queue_test.go # Assumes that setup has been run before
```

> **Note**
Expand All @@ -38,20 +38,20 @@ The test script will run in 3 phases:

After `utils/setup_test.go` is done, we expect to have KEDA setup in the `keda` namespace.

- **Tests:** Currently there are only scaler tests in `tests/scalers_go/`. Each test is kept in its own package. This is to prevent conflicting variable declarations for commoly used variables (**ex -** `testNamespace`). Individual scaler tests are run
- **Tests:** Currently there are only scaler tests in `tests/scalers/`. Each test is kept in its own package. This is to prevent conflicting variable declarations for commoly used variables (**ex -** `testNamespace`). Individual scaler tests are run
in parallel, but tests within a file can be run in parallel or in series. More about tests below.

- **Global cleanup:** This is done in [`utils/cleanup_test.go`](utils/cleanup_test.go). It cleans up all the resources created in `utils/setup_test.go`.

## Adding tests

- Tests are written using `Go`'s default [`testing`](https://pkg.go.dev/testing) framework, and [`testify`](https://pkg.go.dev/github.com/stretchr/testify).
- Each e2e test should be in its own package, **ex -** `scalers_go/azure_queue/azure_queue_test.go`, or `scalers_go/kafka/kafka_test.go`, etc
- Each e2e test should be in its own package, **ex -** `scalers/azure_queue/azure_queue_test.go`, or `scalers/kafka/kafka_test.go`, etc
- Each test file is expected to do its own setup and clean for resources.

Test are split in different folders based on what it's testing:
- `internals`: KEDA internals (ie: HPA related stuff).
- `scalers_go`: Anything related with scalers.
- `scalers`: Anything related with scalers.
- `secret-providers`: Anything related with how KEDA gets the secrets for working (ie: pod-identity, vault, etc).

#### ⚠⚠ Important: ⚠⚠
Expand Down Expand Up @@ -186,7 +186,7 @@ func cleanupTest(t *testing.T) {

#### Notes

- You can see [`azure_queue_test.go`](scalers_go/azure_queue/azure_queue_test.go) for a full example.
- You can see [`azure_queue_test.go`](scalers/azure_queue/azure_queue_test.go) for a full example.
- All tests must have the `// +build e2e` build tag.
- Refer [`helper.go`](helper.go) for various helper methods available to use in your tests.
- Prefer using helper methods or `k8s` libraries in `Go` over manually executing `shell` commands. Only if the task
Expand Down
173 changes: 173 additions & 0 deletions tests/internals/value_metric_type/value_metric_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//go:build e2e
// +build e2e

package value_metric_type_test

import (
"fmt"
"testing"

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

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

const (
testName = "value-metric-type-test"
)

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
deploymentName = fmt.Sprintf("%s-deployment", testName)
monitoredDeploymentName = fmt.Sprintf("%s-monitored", testName)
scaledObjectName = fmt.Sprintf("%s-so", testName)
)

type templateData struct {
TestNamespace string
DeploymentName string
ScaledObjectName string
MonitoredDeploymentName string
MetricType string
}
type templateValues map[string]string

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

deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
replicas: 8
selector:
matchLabels:
app: {{.DeploymentName}}
template:
metadata:
labels:
app: {{.DeploymentName}}
spec:
containers:
- name: {{.DeploymentName}}
image: nginx
`

scaledObjectTemplate = `
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {{.ScaledObjectName}}
namespace: {{.TestNamespace}}
spec:
advanced:
restoreToOriginalReplicaCount: true
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
stabilizationWindowSeconds: 5
scaleTargetRef:
name: {{.DeploymentName}}
pollingInterval: 5
idleReplicaCount: 0
minReplicaCount: 0
maxReplicaCount: 10
cooldownPeriod: 10
triggers:
- type: kubernetes-workload
metricType: {{.MetricType}}
metadata:
podSelector: 'app={{.MonitoredDeploymentName}}'
value: '2'
`
)

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

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

// test scaling
testScaleByAverageValue(t, kc, data)
testScaleByValue(t, kc, data)

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

func getTemplateData() (templateData, templateValues) {
return templateData{
TestNamespace: testNamespace,
DeploymentName: deploymentName,
ScaledObjectName: scaledObjectName,
MonitoredDeploymentName: monitoredDeploymentName,
}, templateValues{
"deploymentTemplate": deploymentTemplate,
"monitoredDeploymentTemplate": monitoredDeploymentTemplate,
}
}

func testScaleByAverageValue(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing scale by AverageValue ---")

// initial replica count
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 8, 60, 1),
"replica count should be 8 after 1 minute")

data.MetricType = "AverageValue"
KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)

// Metric Value = 8, DesiredAverageMetricValue = 2
// should scale down to 8/2 = 4 replicas, irrespective of current replicas
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 4, 60, 1),
"replica count should be 4 after 1 minute")

KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
}

func testScaleByValue(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing scale by Value ---")

// initial replica count
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 8, 60, 1),
"replica count should be 8 after 1 minute")

data.MetricType = "Value"
KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)

// MetricValue = 8, DesiredMetricValue = 2, Current Replicas = 8
// should scale to 8/2 * 8 = 32 (limited by max replicas which in this case is 10)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 10, 60, 1),
"replica count should be 10 after 1 minute")
}
Loading