Responsive

Leveraging Testkube as a Quality Gate in Multi-Stage Deployments with Keptn

Leveraging Testkube as a Quality Gate in Multi-Stage Deployments with Keptn

Published
February 13, 2025
Atulpriya Sharma
Sr. Developer Advocate
InfraCloud Technologies
Share on X
Share on LinkedIn
Share on Reddit
Share on HackerNews
Copy URL

Table of Contents

Start Using Testkube with a Free Trial Today

Subscribe to our monthly newsletter to stay up to date with all-things Testkube.

Last updated
February 12, 2025
Atulpriya Sharma
Sr. Developer Advocate
InfraCloud Technologies
Share on X
Share on LinkedIn
Share on Reddit
Share on HackerNews
Copy URL

Table of Contents

Applications today are more complex and distributed than ever, and maintaining their quality and reliability is difficult. When traditional testing approaches fail, continuous testing is necessary. 

Continuous testing involves integrating testing throughout the development cycle, helping teams to identify issues early on. By implementing automated tests into the deployment pipeline, teams can ensure that every change is thoroughly tested before releasing it to the users. 

In this blog post, we'll use Testkube as a quality gate for deployments using Keptn to ensure that only tested and validated changes make their way into production or the next stage of your deployment pipeline. 

What is Keptn for Cloud Native CI/CD Lifecycle Orchestration?

Keptn is an open source tool that seamlessly integrates with cloud native deployment tools like ArgoCD and Flux to simplify and streamline the deployment process. It focuses on three major aspects of cloud native deployment: Metrics, Observability, and Release lifecycle management

Our focus for this post will be release lifecycle management, which "wraps" a standard Kubernetes deployment with the capability to handle issues automatically before and after the actual deployment. 

Using constructs like pre-deployment and post-deployment tasks and evaluations, Keptn provides greater control over your deployment cycle. You can define these as a KeptnTaskDefinition, which will execute these tasks as part of your deployment process.

Using Keptn’s Quality Gates and Analysis feature, you can validate a deployment or release before progressing to the next stage. 

What is Testkube for Test Orchestration?

Testkube is a cloud-native test orchestration framework that integrates with your existing CI/CD pipelines to provide continuous testing capabilities. This ensures code quality as they move through different development and deployment stages.

Testkube complements the CI/CD environment by offering the following benefits:

  • Seamless integration: Testkube effortlessly articulates with CI/CD platforms like Jenkins, GitLab CI, and Argo CD, allowing for automated testing as part of the deployment process.
  • GitOps Integration: Testkube follows the GitOps principle, which allows you to manage test configurations, test scripts, and infrastructure as code using a version-controlled Git repository. This makes it simple to track changes and ensures consistency across all settings.
  • Single pane of Glass: All test results, logs, and artifacts are in one place, no matter how tests are triggered, and they provide streamlined test troubleshooting and powerful holistic reporting.
  • Run any test at scale: Testkube leverages Kubernetes for test execution, allowing massive parallelization and sharded test executions for both functional and performance testing.

By integrating Testkube and Keptn, we can maintain the quality of our deployments and prevent untested and unverified applications from proceeding to the next stage. 

In the following section, let us see how it works.

Integrating Testkube with Keptn

To understand this better, we have a simple scenario where we will configure Keptn for a namespace to watch for any changes. We’ll also create a KeptnTaskDefinition that includes our Testkube code and triggers an already created Test Workflow. 

In the same namespace we’ll deploy an nginx application with relevant annotations.This will trigger the KeptnTask which will initiate the Test Workflow. Based on the result of the Test Workflow, Keptn will either allow the deployment to proceed or block it. 

Prerequisites

  • Testkube account
  • Kubernetes cluster - we’re using a local Minikube cluster
  • Testkube Agent configured on the K8s cluster.
  • Testkube API Token, Org ID, and Environment ID
  • Keptn installed and configured on the cluster.

To create the API token, refer to our API Token document. To find the Org ID and environment IDs, log in to your Testkube Dashboard and select Settings from the menu bar.

Creating a Test Workflow in Testkube

Testkube allows you to create Test Workflows, which are YAML-based specifications that define how to execute your test together with associated images, artifacts, and resource parameters. 

The first step is to create a Test Workflow:

  • Log in to your Testkube dashboard and navigate to the Test Workflow section.
  • Click the "Add New Test Workflow" button. 
  • A "Create a Test Workflow" dialog box appears, and you can build the sample workflow by selecting the "Start from an Example" option.

Here, we’ll create a simple Curl Test and modify it to suit our test scenario or testing the end point of the nginx application.

kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata: name: curl-sample
namespace: testkube
spec: 
	config: 
    url:
    	type: string
container: {} 
steps:
- name: Run curl 
	container:
    image: curlimages/curl:8.7.1
    shell: |
    response_code=$(curl -s -o /dev/null -w "%{http_code}" {{ config.url }})
    if [ "$response_code" -ne 200 ]; then
    	echo "Error: HTTP response code is $response_code (expected 200)"
        exit 1
    fi
    echo "Success: HTTP response code is 200"
status: {}

We’ll create this Test Workflow and save it on Testkube. 

Configuring Keptn

The first step after installing Keptn is to enable it to watch a namespace for changes. To do that, you can use the following namespace.yaml file

apiVersion: v1
kind: Namespac
metadata:
	name: keptndemo
    annotations:
    	keptn.sh/lifecycle-toolkit: enabled

We’re creating a new namespace called keptndemo and enabling the keptn lifyecycle-toolkit on it. 

Next, we’ll create and deploy our Nginx deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
	name: nginx-deployment
    namespace: keptndemo
    labels:
    	app.kubernetes.io/name: nginx
spec:
	replicas: 1
    selector:
    	matchLabels:
        	app.kubernetes.io/name: nginx
    template:
    metadata:
    	labels:
        	app.kubernetes.io/part-of: keptndemoapp
            app.kubernetes.io/name: nginx
            app.kubernetes.io/version: 0.0.2
            keptn.sh/pre-deployment-tasks: "url-test-runner"    
    spec:
    	containers:
        - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Service
apiVersion: v1
kind: Service
metadata:
	name: nginx
    namespace: keptndemo
spec:
	selector:
    app.kubernetes.io/name: nginx
    ports:
    - protocol: TCP

In the above deployment, we’re configuring a pre-deployment task which will refer to the KeptnTaskDefinition that we’ll create next. Rest of the details are understood.

After this, we’ll create a KeptnTaskDefinition with our Testkube code to execute the Test Workflow we just created.

apiVersion: lifecycle.keptn.sh/v1
kind: KeptnTaskDefinition
metadata:
	name: url-test-runner
    namespace: keptndemo
spec:  
	container:
    name: testkube-runner
    image: kubeshop/testkube-cli:latest
    command:
    - 'sh'
    - '-c'
    - |
    	testkube set context --org-id tkcorg_123456789 --env-id tkcenv_123456789-c cloud  -k tkcapi_123456789
        testkube run tw curl-sample --config url=google.com -f

Here we configure the KeptnTaskDefinition with a container running the kubeshop/testkube-cli image which will be used to trigger the curl Workflow we defined above. We also define the Testkube context using the org ID, env ID, and the API key. Make sure to replace these with your own credentials as discussed in prerequisites above. 

The last line uses the testkube run tw command to execute  the curl Test Workflow that we created and passes the url of google as a config variable. We’re using this to showcase a failure scenario, as this will return HTTP 301 instead of HTTP 200. In regular scenarios, this will be your application’s end point or anything else that you want to test.

Validating Testkube as a Quality Gate

The first step is to check if all the keptn core components are running correctly. 

You can simply do that by running 

kubectl get pods -n keptn-system

The next step is to deploy all the three yaml files in the following order:

  1. namespace.yaml
  2. deployment.yaml
  3. keptn-task.yaml

Once all these are deployed, let us go ahead and update the deployment and change the app.kubernetes.io/version: 0.0.2 to app.kubernetes.io/version: 0.0.3.  and deploy it again. Since there’s a change in the deployment, you’ll see that our pre-deplyoment task kick-in and the status of our pod is SchedulingGated


Let us now go ahead and inspect the events in the cluster to check our pre-deployment task.


We see here that the task has failed. Let's also navigate to our Testkube dashboard and see if the Test Workflow actually executed.

Our Test workflow was executed, and it failed - the response wasn’t HTTP 200. 

Further, if you go back to your cluster and check the status of your pod, you’ll see it as pending. This is because when a pre-deployment task fails, Keptn keeps the pod’s status as pending

You can change the URL in the KeptnTaskDefinition and then deploy the nginx application with a newer version. This time the test will pass and you’ll see that the newer version has propagated.

And that was how you can configure Testkube as a quality gate in Keptn to streamline your deployments and ensure only verified and tested changes progress to the next stages. 

Conclusion

In this blog post, we discussed the importance of continuous testing and its relevance to cloud native applications. Testkube is a tool that enables continuous testing in the development and deployment lifecycle by integrating with existing CI/CD or GitOps tools. 

We also considered Keptn a tool for managing the application release lifecycle and understood the quality gate feature. Using Keptn, we integrated Testkube as a quality gate to ensure that only valid and verified changes proceed to the next stage. 

Take advantage of the power of integration between Keptn and Testkube to improve your continuous validation process before promoting your code to the next stage.

Sign up for Testkube and try this example, or contact our team to learn how Testkube can improve your testing strategy. You can also join our Slack community to start a conversation or read Testkube documentation to see how Testkube can enhance your testing workflow, ensuring seamless, efficient, and automated testing within the Kubernetes environments.

About Testkube

Testkube is a test execution and orchestration framework for Kubernetes that works with any CI/CD system and testing tool you need, empowering teams to deliver on the promise of agile, efficient, and comprehensive testing programs by leveraging all the capabilities of K8s to eliminate CI/CD bottlenecks, perfecting your testing workflow. Get started with Testkube's free trial today.