NashTech Insights

Terratest: How to write the test cases for the azure ADO-Agent – III.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
blue and red light from computer

Hello Learners, I am back with another blog. In today’s blog, We will explore Terratest and how to write the test cases for the ADO-Agent. As we have seen first part and second parts of this blog, we have seen how we can check the VM status and ssh connection of the VM. In this third part of that blog, we will write the terratest to test the ADO-Agent status that is online or offline. If you like my blogs, you can read more blogs here.

Note:- I am working on the terratest blog series. So please follow my blog and learn about the terratest. You can check out this link for more blogs on the terratest.

In the world of DevOps and continuous integration/continuous deployment (CI/CD), Azure DevOps (ADO) plays a crucial role by providing tools and services. A core component of Azure DevOps is the ADO Agent, which facilitates the execution of tasks on behalf of your pipelines. To ensure the all functionality of these agents, we can test this agent using terratest. In this blog, we’ll explore how to write a Terratest test case for Azure ADO Agents.

Introduction to Terratest

Terratest is an open-source Go library designed to test infrastructure code, configure services, and validate results. It enables automated testing of your infrastructure code against real resources, offering the best way to validate that your code behaves as you expected.

Azure ADO Agents:-

Azure ADO Agents are core components in the CI/CD pipeline, responsible for executing tasks such as building, testing, and deploying applications. Ensuring the correct behavior of these agents is necessary for maintaining the efficiency of your pipelines. Terratest allows you to write tests that validate the behavior of these agents in an automated and repeatable manner.

Setting Up Your Testing Environment:-

Before you start writing Terratest test cases for Azure ADO Agents, you’ll need to set up your testing environment:-

  1. Install Go lang in your system.
  2. Install the Azure cli.
  3. Install the terraform [ If you are creating the ADO-Agent using terraform ]
  4. Configuring your Azure DevOps organization.

Once your environment is ready, you can start creating your test cases.

Writing Terratest Test Cases:-

1. Import the required packages:-

In this section, we will import the required packages for the terratest to run the test casesL-

2. Define the global variables and structure:-

In this section, we will define the global variable which will be used in the function to provide the values and also will define the struct in this.

3. Define the functions:-

In this section, we will write the function which is required to fetch the details for the ADO agent and then run the test case for the terratest:-

In this function, we will fetch the status of the ADO-Agent. First, we are calling the rest API to fetch the details of the ADO-Agent and using the personal access token of the ADO with API to establish the connection.

Now, in this part of the function, I fetch the status of the ADO-Agent [ online or offline ] and return the output.

4. Test case and test function:-

Now, we will call the ssh connection function in this main test function and run the test case:-

We are fetching the Agent name from the output.tf file and you notice that I am using for loop in this because I am assuming that you want to scale up or scale down the VM for ADO-agent and you must have more than one ADO-Agent. In that case, this terratest code can test all the Agents if you create more than one Agents.

5. Execute the terratest:-

Now you can run the below command to test the VM or check the status of this VM:-

go mod init github.com/vm-status/ado-agent

go mod tidy

go test -v

Conclusion:-

We have learned many things about the terratest to test the ADO agent’s status. Here we are using the terraform to create a VM and then register as the ADO agent but in this blog, we have seen the terratest to check the status of the agent. This is the third part of this blog and You can check the first and second part also for the ADO-Agent. If you are facing any issues then please comment below If you like my blog then you can like this or want to read more blogs then follow this link. and also check out the terratest official doc.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: