Hello Learners, I am back with another blog. In today’s blog, we will learn about the terratest. Terratest is an essential tool for testing and provides an approach to infrastructure testing. We will explore what Terratest is and how to write the test cases for the Azure subscription. 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.
Prerequisites:-
You will require a few packages such as:-
- An Azure Credentials.
- Terraform is installed on your local machine.
- Terratest set up in your Go project.
Setting Up Your Test Project:-
You can use the below URL to clone the repo:
https://github.com/muzakkirsaifi123/terratest-code-for-subscription/blob/feature/terratest/test/main_test.go
Or you can follow the below code and run paste in your file and first create a file with postfix _test.
touch main_test.go
Importing package:-
First of all, we will import the required package to write our terratest code.

Access Token:-
After the importing, we will write the functions or methods to get the required details and will run the test cases on it. First, we will generate or fetch the access token with the help of the subscription ID. So this is the function for the same:-

Subscription Details:-
After fetching the access token, we will use this token in the subscription call means we will write a function to fetch the subscription details from the subscription. You can clone the below code:-

In the above function, We are using the URL to fetch the subscription details. We have defined the URL in the test function and then we will use the token which we have created in the getaccesstoken function. This function will print the details in byte format.
print subscription details:-
In this function, We will cover the byte format into the JSON and then print them:

Set up environment variables:-
In this function, we will set up the env variable which will be used in the terratest execution:-
func setTerraformVariables() (map[string]string, error) {
ARM_CLIENT_ID := os.Getenv("ARM_CLIENT")
ARM_CLIENT_SECRET := os.Getenv("ARM_CLIENT_SECRET_ID")
ARM_TENANT_ID := os.Getenv("ARM_TENANT")
ARM_SUBSCRIPTION_ID := os.Getenv("ARM_SUBSCRIPTION")
fmt.Println("##################################Environment Variables:#######################################")
fmt.Println("AZURE_CLIENT_ID:", ARM_CLIENT_ID)
fmt.Println("AZURE_CLIENT_SECRET:", ARM_CLIENT_SECRET)
fmt.Println("AZURE_TENANT_ID:", ARM_TENANT_ID)
fmt.Println("AZURE_SUBSCRIPTION_ID:", ARM_SUBSCRIPTION_ID)
fmt.Println("****************************************Environment Variables END:***********************************")
// Creating globalEnVars for terraform call through Terratest
if ARM_CLIENT_ID != "" {
globalEnvVars["ARM_CLIENT_ID"] = ARM_CLIENT_ID
globalEnvVars["ARM_CLIENT_SECRET"] = ARM_CLIENT_SECRET
globalEnvVars["ARM_SUBSCRIPTION_ID"] = ARM_SUBSCRIPTION_ID
globalEnvVars["ARM_TENANT_ID"] = ARM_TENANT_ID
}
return globalEnvVars, nil
}
Terratest Test function:-
1. Run terraform:-
In this function, We will call all the functions which we have defined in the complete code and also write the terratest test cases in it:-

First, We have called the setTerraformVaribales function to set the env for the terratest. and then we have mentioned the terraform code path where we have written the terraform code to create the subscription module. In the vars section, We have defined a few values for the terraform. Actually, we can define the variable here for the terraform.
2. Call the function:-
In this section, we will call the function to fetch the details and apply the terraform and then destroy the terraform

From the start, as you can see we have defined a defer function, Basically, It is used to execute a specific line in the last means after all execution. By this, we want to run the terraform destroy command in the last after all test cases. We are also fetching the values from the output.tf file from the terraform so that we can match them with the actual values. Then we are calling the two functions subscription details and print details function to get the actual values from the resource.
3. Test Cases:-
Now, We will write the test cases where we will check the expected values with the actual values which have been created on the azure.

4. Run the terratest:-
Now, we can run the below command to run the terratest:-
go mod init <github.com/subscription>
go mod tidy
go test -v
Conclusion:-
In today’s blog, We have learned about the terratest to test the Azure subscription and also write the code with test cases. 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.