Integrating Testkube With Azure Pipelines For Continuous Testing
Tags: kubernetes, azure, testing
Published: 2024-05-14 16:53:47

In the fast-paced world of software development, agility is key. Teams strive to expedite feature releases, yet the critical process of testing often lags behind. To truly achieve agility and ensure reliability, automated testing must be woven throughout the development lifecycle.

Enter the concept of Continuous Testing, or TestOps, which embodies the integration of testing into DevOps practices. This isn't just about triggering tests upon code commits or automating report generation; it's about embedding a robust testing framework within your CI/CD ecosystem.

Testkube emerges as a solution specifically designed to facilitate testing in Kubernetes environments and to dovetail with CI/CD workflows effortlessly. This blog post will explore the synergy between Testkube and Azure Pipelines, illustrating how this integration enhances continuous testing practices.

## Leveraging Testkube with Azure Pipelines for Enhanced TestOps

[Azure Pipelines](https://azure.microsoft.com/en-in/products/devops/pipelines), a pivotal component of Microsoft Azure's DevOps services, offers seamless integration with leading version control systems like GitHub and GitLab. It utilizes an `azure-pipelines.yaml` file to manage pipeline triggers and job configurations, enabling automated builds and deployments with ease.

[Testkube](http://testkube.io), on the other hand, is a versatile testing framework designed to harmonize with various CI/CD tools, bringing your preferred testing mechanisms into the Kubernetes ecosystem. By treating tests as Kubernetes resources through Custom Resource Definitions (CRDs), Testkube revolutionizes test management by allowing tests to be handled as code.

**Integrating Testkube with Azure Pipelines unlocks significant advantages:**

- **Enhanced TestOps:** This integration erases the traditional boundaries between testing and development, fostering a culture of continuous testing. It ensures product reliability by facilitating testing at every stage of the development process, accommodating rapid scaling and deployment speeds.

- **Seamless Continuous Testing:** With Testkube and Azure Pipelines, testing becomes a continuous process that automatically triggers with every code commit. This ensures comprehensive testing with each change, embedding quality directly into the CI/CD pipeline.

- **In-depth Insights:** Testkube excels in capturing and analyzing data from your executed tests. It leverages this information to generate detailed insights and dashboards, offering a clear view of test performance and trends over time.

This integration not only streamlines workflows but also empowers teams to maintain high-quality standards, ensuring agile, reliable product development with Testkube and Azure Pipelines.

## Integrating Azure Pipelines With TestKube

Integrating Testkube with Azure Pipelines follows a process similar to what we did for GitLab and CircleCI in the previous posts. In this case, we'll configure Azure Pipelines with the GitHub Repo used in an earlier blog post.

- Create a DevOps organization in Azure and then create a new project to get started.
- On the first run, it will ask you to configure your code repo with Azure Pipeline.
- Create an `azure-pipelines.yml` in the root of your GitHub repository.
- The pipeline will create and execute a k6 test on a cluster with Testkube configured.
- Lastly, we'll configure a branch protection rule that will not allow a change to be merged if the test fails.

At a high level, this is what we are going to do. The code for this blog post is available in this [GitHub repo](https://github.com/techmaharaj/testkube-GH). It has a folder named k6, which has the test, and an `azure-pipelines.yaml` file with the Azure Pipeline configuration.

### Pre-requisites

- [Azure DevOps organization](https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/create-organization?view=azure-devops) & repository with [Azure Pipeline](https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml) configured.
- Kubernetes cluster - we're using an AKS cluster.
- [Testkube Agent](http://docs.testkube.io/testkube-cloud/articles/installing-agent) configured on the cluster.
- Testkube API token, Org ID, and Environment ID

Once the prerequisites are in place, you should have a target Kubernetes cluster ready with a Testkube agent configured. You should also have a GitHub repository configured with Azure Pipelines integrated with Testkube.

**Note:** Refer to our [API Token document](https://docs.testkube.io/testkube-cloud/articles/organization-management/#api-tokens) to create the API token. To find the Org ID and environment IDs, log in to your Teskube Cloud dashboard, head to any existing tests page, navigate to CI/CD integration, and choose GitHub. Copy the environment and organization IDs from here.

You'll also need to create [variables in Azure Pipelines](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch) to store these IDs and the token.

### K6 Test

We will create a k6 test that performs a load test on the testkube.io website and checks if the response time is less than 1000 milliseconds. If the response is more than that, the test will fail.

```js
import http from 'k6/http';
import { check, sleep } from 'k6';

export let options = {
  vus: 1,            // Virtual Users
  duration: '10s',   // Duration of the test
};

export default function () {
  // Send an HTTP GET request to the website
  let response = http.get('https://testkube.io');   // Replace with your URL

  // Check if the response time is less than 1000 milliseconds
  check(response, {
    'Response time is less than 1000 milliseconds': (r) => r.timings.duration < 1000,   // Check if response time is less than 1000 milliseconds
  });

  // Add a sleep period (in this case, 1 second) between requests
  sleep(1);
}
```

### Configuring Azure Pipeline

We configure the Azure Pipeline to execute whenever a new pull request is made. The job uses the Testkube API key, ORG_ID, and ENV_ID, saved as Azure Pipeline variables.

```yaml
trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

stages:
  - stage: Test
    jobs:
      - job: RunTestkube
        steps:
          - task: SetupTestkube@1
            inputs:
              organization: '$(TK_ORG_ID)'
              environment: '$(TK_ENV_ID)'
              token: '$(TK_API_TOKEN)'
          - script: |
              git clone https://github.com/techmaharaj/testkube-GH.git
              cd testkube-GH/k6
              testkube create test --name k6-test --type k6/script --update -f testkube.js
              testkube run test k6-test
            displayName: Run Testkube Test
```

**Pro Tip:** You can also use this sample workflow generated by Testkube to create and configure your Azure Pipeline.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gn7std1j03qqon586w0.png)

The above workflow does the following things:

- Configures a job to connect with Testkube cloud using the Testkube token, organization, and environment ID.
- Creates and executes the k6 test using `kubeshop/testkube-cli` on the AKS cluster.
- Based on the result, the status will be updated in the pull request. If the test passes, the merge option will be enabled, and anyone can merge it. However, if the test fails, the merge will not be available - [we'll configure a branch protection rule](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule) to prevent the PR from being merged if the run fails.

### Configuring GitHub Repo

You first need to create an Azure DevOps organization. Once this is made, you'll be prompted to add your first project. In this case, we're using a GitHub project. So, we can choose GitHub and follow the prompts to connect your GitHub repository to Azure DevOps.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kdotwpkrsa8mfe44h3r.png)

After successfully creating the project, you need to make your pipeline. Azure will provide you with a default pipeline code that you can commit to your repo. However, since we already have our configuration, we will use that.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vmhtyund7evyxua31dg.png)

At this point, our Azure Pipeline is configured to be triggered on every new PR on the main branch of the GitHub repository.

### Trigger Testkube Test

To check if everything works correctly, raise a new pull request in the repository with some changes. You'll see that your action is triggered almost immediately.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4koogpixq0he1jiptz6d.png)

You see two jobs here, that is because we already have a GitHub action enabled in this repository from our previous blog post.

At the same time, you can also see the execution on Azure Pipeline. In the background, our test will run on the target Kubernetes cluster specified.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9ajnb82211hwmpy7btq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iq50zp6yo7vjt7f4336.png)

If the test passes, our pipeline will update the status in the PR, and a user can merge the PR.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg1bva0446jxgtkqut8h.png)

However, if the workflow fails, the CI job will update the workflow status as failed in the PR. Merging will be blocked because of the branch protection rule that we have created, which prevents merging if the test fails.

## Conclusion: Revolutionizing Testing with Testkube and Azure Pipelines

In this blogpost, we learned about the pivotal role of continuous testing in modern software development, spotlighting how it significantly uplifts testing practices. We delved into the synergy between Testkube and Azure Pipelines, demonstrating a seamless integration that facilitates an efficient CI/CD workflow. By integrating a k6 test with Testkube and executing it through Azure Pipelines, we showcased a practical workflow that enhances testing efficiency and reliability.

For a deeper dive and more comprehensive guidance on harnessing Testkube within your CI/CD ecosystem, our documentation offers extensive insights. Embrace TestOps by experimenting with [Testkube](http://testkube.io) and witness firsthand the transformation it brings to your testing strategy.

Should you have any queries, need guidance, or wish to share feedback, our [Slack Community](https://join.slack.com/t/testkubeworkspace/shared_invite/zt-2arhz5vmu-U2r3WZ69iPya5Fw0hMhRDg) is a vibrant space for support and knowledge exchange. Embark on your journey towards more proactive, streamlined testing today.

Happy testing!