GitOps Testing with Flux
Tutorial
The following is a step-by-step walkthrough to test the automated application deployment and execution of Postman collections in a local Kind cluster.
Let’s start with setting things up for our GitOps-powered testing machine!
1. Fork the example repository and clone it locally.
git clone https://github.com/$GITHUB_USER/testkube-flux.git
2. Start a Kubernetes cluster.
You can use Minikube, Kind or any managed cluster with a cloud provider (EKS, GKE, etc). In this example we're using Kind.
kind create cluster
3. Create a Github Classic Token.
Must be of type Classic (i.e. starts with ghp_).
GITHUB_TOKEN=<ghp_>
GITHUB_USER=<username>
And export the environment variables in your terminal.
4. Install Flux in the cluster and connect it to the repository.
Install the Flux CLI and run:
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=testkube-flux \
--path=cluster \
--personal
5. Create a Flux Source and a Kustomize Controller.
The following command will create a Flux source to tell Flux to apply changes that are created in your repository:
flux create source git testkube-tests \
--url=https://github.com/$GITHUB_USER/testkube-flux \
--branch=main \
--interval=30s \
--export > ./cluster/flux-system/sources/testkube-tests/test-source.yaml
And now create a Flux Kustomize Controller to apply the Testkube Test CRDs in the cluser using Kustomize:
flux create kustomization testkube-test \
--target-namespace=testkube \
--source=testkube-tests \
--path="cluster/testkube" \
--prune=true \
--interval=30s \
--export > ./cluster/flux-system/sources/testkube-tests/testkube-kustomization.yaml
6. Install Testkube in the cluster.
If you don't have the Testkube CLI, follow the instructions here to install it.
Run the following command to install Testkube and its components in the cluster:
testkube install
7. Create a Test CRD with Testkube CLI.
In this example, the test being used is a Postman test, which you can find in /img/server/tests/postman-collection.json.
To create a Kubernetes CRD for the test, run:
testkube generate tests-crds img/server/tests/postman-collection.json > cluster/testkube/server-postman-test.yaml
Note: You can run Testkube from your CI/CD pipeline if you want to automate the creation of the Test CRDs.
8. Add the generated test to the Kustomize file.
The name of the test file created in the previous step is server-postman-test.yaml. Add that to the Kustomize file located in cluster/testkube/kustomization.yaml
:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
+ - server-postman-test.yaml
9. Push all the changes to your repository.
git pull origin main
git add -A && git commit -m "Configure Testkube tests"
git push
10. Your tests should be applied in the cluster.
To see if Flux detected your changes run:
flux get kustomizations --watch
And to ensure that the test has been created run:
testkube get test
| NAME | TYPE | CREATED | LABELS |
| ----------------------- | ------------------ | ----------------------------- | ---------------------------------------------- |
| postman-collection-test | postman/collection | 2023-01-30 18:04:13 +0000 UTC | kustomize.toolkit.fluxcd.io/name=testkube-test |
| | | | kustomize.toolkit.fluxcd.io/name=testkube-test |
11. Run your tests.
Now that you have deployed your tests in a GitOps fashion to the cluster, you can use Testkube to run the tests for you through multiple ways:
- Using the Testkube CLI.
- Using the Testkube Pro Dashboard.
- Running Testkube CLI from a CI/CD pipeline.
We'll use the Testkube CLI for brevity. Run the following command to run the recently created test:
testkube run test postman-collection-test
And see the test result with:
testkube get execution postman-collection-test-1
Test execution completed with success in 13.345s
GitOps Takeaways
Once fully realized - using GitOps for testing of Kubernetes applications as described above provides a powerful alternative to the more traditional approach where orchestration is tied to your current CI/CD tooling and not closely aligned with the lifecycle of Kubernetes applications.