Hello Learners, I am back with another blog. In today’s blog, we will learn about the terratest. We will explore what Terratest is and how to write the test cases for the ADO-Agent. 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:-
- Install Go lang in your system.
- Install the Azure cli.
- Install the terraform [ If you are creating the ADO-Agent using terraform ]
- 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 define the functions to fetch the values for the terratest test cases:-
i. Get Access Token:-
We will fetch or get the access token with the help of this function so that we can use that token to make any rest API call:-
ii. VM Details:-
Now, we will fetch the VM details as we know that when we create an ado-agent then first we need to create a VM and that VM will be configured as the ADO-Agent so we will need to fetch the VM details:-
iii. Print in JSON Format:-
Now, we will print the VM’s complete details in the JSON format as when we fetch the details with the rest API the details are not in the JSON format.
iv. Main test function:-
Now in this section, we will call all the functions which we created before to fetch the details to run the test case:-
In the above code, We are using the terraform to create a VM and then register that VM as the ADO-Agent to the Azure DevOps. First, we are mentioning the path for the terraform module, and as we are using the terraform.tfvars file so in the next line, we are mentioning the path of terraform.tfvars file. and then apply and destroy with the defer [ this will run in the last after all the functions or steps execute ].
In this code, We are fetching the resource group name from the output.tf file and subscription id and token from the env. In the last, we are fetching the access token using the subscription ID.
This is the main section where we are using all the collected values for the test case. here we are testing whether our VM for the ado-agent is created successfully or not and compares with the expected result with the actual result.
4. 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
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 check the status of the VM for ADO-Agent. This is the first 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.