* Migration to EndpointSlices Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com> * finish migration from v1.Endpoints to discoveryv1.EndpointSlices Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com> * handle multiple EndpointSlices per service Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com> * remove deletecollection rbac for endpoint slices Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com> --------- Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com> Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com> Co-authored-by: Jan Wozniak <wozniak.jan@gmail.com> |
||
|---|---|---|
| .. | ||
| checks | ||
| helper | ||
| utils | ||
| README.md | ||
| run-all.go | ||
README.md
Prerequisites
- go
kubectllogged into a Kubernetes cluster.
Running tests:
All tests
Make sure that you are in tests directory.
go test -v -tags e2e ./utils/setup_test.go # Only needs to be run once.
go test -v -tags e2e ./checks/...
go test -v -tags e2e ./utils/cleanup_test.go # Skip if you want to keep testing.
Specific test
go test -v -tags e2e ./checks/internal_service/internal_service_test.go # Assumes that setup has been run before
Note On macOS you might need to set following environment variable in order to run the tests:
GOOS="darwin"eg.
GOOS="darwin" go test -v tags e2e ...
Refer to this for more information about testing in Go.
E2E Test Setup
The test script will run in 3 phases:
-
Setup: This is done in
utils/setup_test.go. If you're adding any tests to the install / setup process, you need to add it to this file.utils/setup_test.godeploys KEDA components to thekedanamespace.After
utils/setup_test.gois done, we expect to have KEDA components setup in thekedanamespace. -
Tests: Currently there are only scaler tests in
tests/checks/. Each test is kept in its own package. This is to prevent conflicting variable declarations for commonly 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. It cleans up all the resources created inutils/setup_test.go.
Note Your IDE might give you errors upon trying to import certain packages that use the
e2ebuild tag. To overcome this, you will need to specify in your IDE settings to use thee2ebuild tag.As an example, in VSCode, it can be achieved by creating a
.vscodedirectory within the project directory (if not present) and creating asettings.jsonfile in that directory (or updating it) with the following content:{ "go.buildFlags": [ "-tags=e2e" ], "go.testTags": "e2e", }
Adding tests
- Tests are written using
Go's defaulttestingframework, andtestify. - Each e2e test should be in its own package, ex -
checks/internal_service/internal_service_test.go, etc - Each test file is expected to do its own setup and clean for resources.
⚠⚠ Important: ⚠⚠
- Even though the cleaning of resources is expected inside each e2e test file, all test namespaces (namespaces with label type=e2e) are cleaned up to ensure not having dangling resources after global e2e execution finishes. To not break this behaviour, it's mandatory to use the
CreateNamespace(t *testing.T, kc *kubernetes.Clientset, nsName string)function fromhelper.go, instead of creating them manually.
⚠⚠ Important: ⚠⚠
Gocode can panic when performing forbidden operations such as accessing a nil pointer, or from code that manually callspanic(). A function thatpanicspasses thepanicup the stack until program execution stops or it is recovered usingrecover()(somewhat similar totry-catchin other languages).- If a test panics, and is not recovered,
Gowill stop running the file, and no further tests will be run. This can cause issues with clean up of resources.- Ensure that you are not executing code that can lead to
panics. If you think that there's a chance the test might panic, callrecover(), and cleanup the created resources.- Read this article for understanding more about
panicandrecoverinGo.
Notes
- You can see
internal_service_test.gofor a full example. - All tests must have the
// +build e2ebuild tag. - Refer
helper.gofor various helper methods available to use in your tests. - Prefer using helper methods or
k8slibraries inGoover manually executingshellcommands. Only if the task you're trying to achieve is too complicated or tedious using above, useParseCommandorExecuteCommandfromhelper.gofor executing shell commands. - Ensure, ensure, ensure that you're cleaning up resources.
- You can use
VS Codefor easily debugging your tests.