NashTech Insights

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

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents

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 learn in the first part of this blog, we have seen how we can check the VM status for the ADO-Agent. In this second part of that blog, we will write the terratest to test the ssh connection of the VM ssh connection is enabled or not for that VM. 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-Agent is 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 cases-

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 and then run the test case for the terratest:-

For this function, we need the private IP of the VM and the ssh port 22 to check whether the ssh connection is enabled or not.

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 private IP of the VM from the file and you noticed that ia m using it for loop in this because I am assuming that you want to scale up or scale down the VM for ADO-agent. In that case, this terratest code can test all the VM if you create more than one VM.

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

go mod tidy

go test -v


We have learned many things about the terratest to test the VM machine for the ADO agent. 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 test the ssh connection of the VM. This is the second part of this blog and I will publish another blog with more test cases 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: