NashTech Insights

Terratest: How to test the network access for the Azure ACR.

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

Introduction:-

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 Private ACR. 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 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:-

git clone https://github.com/muzakkirsaifi123/private-ACR-terratest

You can paste the below code into your file.

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:-

ACR JSON:-

Now, we will write a function to fetch the ACR details which return the values in the JSON.

Checking ACR:-

Now it is time to write the main function to identify whether the ACR is private or not. This function will fetch the value of particular key from the JSON which we fetch from the last function. It will check the values of publicNetworkAccess. If it is enabled then it is public otherwise it is private.

Main Test Function:-

As we have written all the functions. Now we will call all functions into the test function where we will perform the terratest to test the ACR endpoint.

This is the main test function. Here, First of all, I am giving the path of the terraform module of ACR because terratest first executes the terraform module then runs the test cases on it then destroys the terraform resource. And in the next lines, we are passing a path to the terraform.tfvars. We are fetching a few details from the terraform output.tf file to use in the test like acrName, resourceGroupName, and subscriptionid from the environment variable.

You can check in the last we are running the test as t.run this is responsible to run the test case.

Running the terratest:-

Now, we have the terratest code to test the private ACR. You can run the code with this command:

go mod init github.com/<>

go mod tidy

go test -v

Conclusion:-

In this blog, we have seen how we can write the terratest to test the private ACR. I have divided the terratest code into parts and explained it. If you are facing any issues then please comment below and 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: