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

Start migrating e2e tests to Go. #3079

Merged
merged 15 commits into from
Jun 13, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md

### New

- **General:** Support for Azure AD Workload Identity as a pod identity provider. ([2487](https://github.com/kedacore/keda/issues/2487))
- **General:** Support for Azure AD Workload Identity as a pod identity provider. ([#2487](https://github.com/kedacore/keda/issues/2487))
- **General:** Basic setup for migrating e2e tests to Go. ([#2737](https://github.com/kedacore/keda/issues/2737))

### Improvements

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ e2e-test-local: ## Run e2e tests against Kubernetes cluster configured in ~/.kub
npm install --prefix tests
./tests/run-all.sh

.PHONY: e2e-test-clean-crds
e2e-test-clean-crds: ## Delete all scaled objects and jobs across all namespaces
./tests/clean-crds.sh

.PHONY: e2e-test-clean
e2e-test-clean: get-cluster-context ## Delete all namespaces labeled with type=e2e
kubectl delete ns -l type=e2e
Expand Down Expand Up @@ -251,7 +255,7 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
rm -rf config/default/kustomize-config/metadataLabelTransformer.yaml.out
$(KUSTOMIZE) build config/default | kubectl apply -f -

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
undeploy: e2e-test-clean-crds ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -


Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/dysnix/predictkube-proto v0.0.0-20211223141524-d309509b6b5f
github.com/elastic/go-elasticsearch/v7 v7.17.1
github.com/go-logr/logr v1.2.3
github.com/go-playground/assert/v2 v2.0.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.6.0
Expand All @@ -49,7 +48,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/robfig/cron/v3 v3.0.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.7.2
github.com/tidwall/gjson v1.14.1
github.com/xdg/scram v1.0.5
github.com/xhit/go-str2duration/v2 v2.0.0
Expand Down Expand Up @@ -183,6 +182,7 @@ require (
github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -264,7 +264,7 @@ require (
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.23.5 // indirect
k8s.io/component-base v0.23.6 // indirect
k8s.io/gengo v0.0.0-20220307231824-4627b89bbf1b // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
Expand Down Expand Up @@ -968,6 +970,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down Expand Up @@ -1537,6 +1541,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
Expand Down
2 changes: 1 addition & 1 deletion pkg/scalers/azure/azure_eventhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"

"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/go-playground/assert/v2"
"github.com/stretchr/testify/assert"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)
Expand Down
30 changes: 15 additions & 15 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
AZURE_SP_APP_ID=
AZURE_SP_OBJECT_ID=
AZURE_SP_KEY=
AZURE_SP_TENANT=
AZURE_SUBSCRIPTION=
AZURE_RESOURCE_GROUP=
AZURE_LOG_ANALYTICS_WORKSPACE_ID=
AZURE_STORAGE_CONNECTION_STRING=
OPENSTACK_AUTH_URL=
OPENSTACK_USER_ID=
OPENSTACK_PASSWORD=
OPENSTACK_PROJECT_ID=
# OPENSTACK_SWIFT_URL=
# OPENSTACK_REGION_NAME=
# OPENSTACK_SWIFT_URL=
AWS_ACCESS_KEY=
AWS_SECRET_KEY=
AZURE_DEVOPS_BUILD_DEFINITON_ID=
AZURE_DEVOPS_ORGANIZATION_URL=
AZURE_DEVOPS_PAT=
AZURE_DEVOPS_PROJECT=
AZURE_DEVOPS_BUILD_DEFINITON_ID=
AZURE_DEVOPS_POOL_NAME=
AZURE_DEVOPS_PROJECT=
AZURE_KEYVAULT_URI=
AZURE_LOG_ANALYTICS_WORKSPACE_ID=
AZURE_RESOURCE_GROUP=
AZURE_SP_APP_ID=
AZURE_SP_KEY=
AZURE_SP_OBJECT_ID=
AZURE_SP_TENANT=
AZURE_STORAGE_CONNECTION_STRING=
AZURE_SUBSCRIPTION=
OPENSTACK_AUTH_URL=
OPENSTACK_PASSWORD=
OPENSTACK_PROJECT_ID=
OPENSTACK_USER_ID=
130 changes: 130 additions & 0 deletions tests/OLD-README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
> ⚠⚠ **Important:** ⚠⚠
> - E2E tests are currently in the process of being migrated to Go.
> - Refer to [this](https://github.com/kedacore/keda/issues/2737) issue for more information.
> - We are no longer accepting new tests written in TypeScript.
> - Please refer [README](README.md) for instructions on writing the E2E tests in Go.

## Prerequisites

- [node](https://nodejs.org/en/)
- `kubectl` logged into a Kubernetes cluster.
- Each scaler test might define additional requirements. For example, `azure-queue.test.ts` requires an env var `AZURE_STORAGE_CONNECTION_STRING`

## Running tests:

```bash
npm install
npm test --verbose
```

### Run one test file:

```
npx ava scalers/prometheus.test.ts
```

## E2E test setup

The test script will run 3 phases:
- **Setup**: this is done in [`setup.test.ts`](setup.test.ts). If you're adding any tests for KEDA install/setup process add it to this file.`setup.test.ts` deploys KEDA to `keda` namespace in the cluster, and updates the image to `kedacore/keda:main`.

After `setup.test.ts` is done, we expect to have a cluster with KEDA setup in namespace `keda`. This is done through a `pretest` hook in npm. See [`"scripts"` in package.json](package.json#L14).

- **Tests**: Currently there are only scaler tests in `tests/scalers`. All files run in parallel, but tests within the file can run either in parallel or in series. More about tests below.

- **Global clean up**: this is done in [`cleanup.test.ts`](cleanup.test.ts). This step cleans resources created in `setup.test.ts`.


## Adding tests:

* Tests are written in TypeScript using [ava](https://github.com/avajs/ava) framework. See [ava docs here](https://github.com/avajs/ava/tree/main/docs)
* Each scaler tests should be in a file. **e.g**: `azure-queue.tests.ts`, `kafka.tests.ts`, etc
* All files in `scalers/**.ts` are run in parallel by default. Make sure your tests don't affect the global state of the cluster in a way that can break other tests.
* Each test file is expected to do it's own setup and clean up for its resources.

```ts
// import test from ava framework
import test from 'ava';

test.before(t => {
// this runs once before all tests.
// do setup here. e.g:
// - Create a namespace for your tests using the function createNamespace(namespace: string) available in helpers file
// - Create deployment (using kubectl or kubernetes node-client)
// - Setup event source (deploy redis, or configure azure storage, etc)
// - etc
});


// `test 1` and `test 2` will run in parallel.
test('test 1', t => { });
test('test 2', t => { });

// `test 3` will run first, then `test 4`.
// Tests will run in the order they are defined in.
// All serial tests will run first before parallel tests above
test.serial('test 3', t => { });
test.serial('test 4', t => { });

// Tests are expected to finish synchronously, or using async/await
// if you need to use callbacks, then add `.cb` and call `t.end()` when done.
test('test 6', t => { });
test('test 7', async t => { });
test.cb('test 8', t => { t.end() });

test.after.always.cb('clean up always after all tests', t => {
// Clean up after your test here. without `always` this will only run if all tests are successful.
t.end();
});
```
>⚠⚠ **Important:** ⚠⚠ Even thought the cleaning of the resources is expected inside each e2e test file, all test namespaces are cleaned up to ensure not having dangling resources after global e2e execution finishes. For not breaking this behavior, it's mandatory the usage of the function `createNamespace(namespace: string)` instead of creating them manually.

* **Example test:** for example if I want to add a test for redis

```ts
import * as sh from 'shelljs';
import test from 'ava';
import { createNamespace } from './helpers';

// you can include template in the file or in another file.
const deployYaml = `apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
labels:
app: test-deployment
spec:
replicas: 0
...
...
`

test.before('install redis and create deployment' t => {
if (!sh.which('helm')) {
t.fail('redis tests require helm');
}

sh.exec('helm install redis ......'); // install redis to the cluster
createNamespace("redis-test-deployment")
sh.exec('kubectl apply -f ....'); // create your deployment
});

test.serial('deployment should scale when adding items in redis list', t => {
// use node redis client to add stuff to redis.
// maybe sleep or poll the replica count
const replicaCount = sh.exec(`kubectl get deployment.apps/test-deployment .. -o jsonpath="{.spec.replicas}"`).stdout;
t.is('10', replicaCount, 'expecting replica count to be 10');
});

test.after.always('remove redis and my deployment', t => {
sh.exec('kubectl delete ....');
});
```

* You can see [`azure-queue.test.ts`](scalers/azure-queue.test.ts) for a full example.
* Ava has more options for asserting and writing tests. The docs are very good. https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md
* **debugging**: when debugging, you can force only 1 test to run by adding `only` to the test definition.

```ts
test.serial.only('this will be the only test to run', t => { });
```
Loading