NashTech Insights

Terratest: How to write the terratest cases for the Private ACR – II.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
person using silver macbook pro

Hello Learners, I am back with another blog. In today’s blog, we will learn about the terratest. We will explore how to write the terratest test cases for the Private ACR to perform the docker push and other operations. 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.


Terratest is an open-source Go library designed to enable automated testing of infrastructure code. It can integrate with the very famous infrastructure-as-code (IAC) tools such as Terraform, giving you the ability to validate your IAC deployments through automated tests.

Basically, We have two options to push the images to the private ACR. Actually, If we want to push the image to the private ACR as we can push the image to the public ACR. This is not possible. So we have two options here:-

* IP Configure.

* All resources in single Vnet.

As in the first part of this blog, we have seen how we can use IP configuring to do the docker login and run the docker push. In this blog, we will see how we can use the second approach to configure the private ACR and access it using terratest. In this approach, we need to create the ACR with a private endpoint in the same vnet and also create a VM (where you paste the code) in the same vnet or create an ADO-Agent if you are running your code in the ADO pipeline. The conclusion is that you must create all the resources in the same vnet then you can push the docker image to the private ACR. You can check out this link for the first part of this blog-terratest.

Writing Terratest Test Cases for Docker Push on private ACR using the IP configuration:-

1. Prerequisites and Setup:

  1. Ensure you have to terraform
  2. Install the Go in your system
  3. Azure CLI.

2. Create a test file:-

First of all, we will create a test file. let’s say acr_test.go but you should keep the postfix _test.go. This is responsible to run the test with the test command.

3. Import the libraries:-

First, we will import the required library for the terratest. These libraries will use in the execution of the terratest for ACR and give more functionality for the terratest:

and also define the global variables for the terratest:-

var (

    expectedPublicNetworkAccess = "Disabled"
	acrURL = ""
	testImage = "hello-world"
	testImagePull = ""
	subscriptionID = "< >"
	expectedPullResponse = true
	expectedPushResponse = true
	acrName = "testacrmk2"
	password = "< >"
	resourceGroupName = "< BLOG >"

4. Define the functions:-

In this section, we will define all the functions that are required for the terratest test cases. First, we will fetch the access token, get details from the resource using rest API, checks whether the ACR is private or public, docker login, and then tag and push the images.

i. Get Access Token:-

In this function, we will fetch the access token using the subscription ID. Basically this access token will use for authentication:-

We are using subscription ID to get the access token using the azure cli.

ii. Get ACR Details:-

In this function, we will fetch the ACR details to fetch the network access from the JSON of ACR.

Here, we are using the REST API to call the JSON of ACR to Validate whether the ACR is private or public with the help of network access variable.

iii. Is ACR Private:-

Here, we are checking whether ACR is private or not with the help of JSOn which we got the above function. There is a variable with the name publicNetworkAccess.

We are just fetching the values of this variable- publicNetworkAccess and returning that values.

iv. Docker login:-

This function is use to authenticate to the ACR with the help of docker login:-

v. Tag and push image:-

With the help of this function, we will tag the image and then push the docker image to the private ACR.

vi. Main test function:-

In this function, we will run the all functions one by one such as first running the access token function to fetch the token and pass it to the get acr details function and then docker login and tag then push:-

till now, we have stored the required values in the variables for the test cases. We have defined the two test cases one is checking whether the ACR is private or not and the other one is pushing the docker images to the private ACR.

In this test case, we are comparing the expected values with the actual values. If they matched then it will pass other wise it will fail.

5. Executing the Tests:

To execute the terratest will will run the below command:-

go mod init <>

go mod tidy

go test -v

After running the commands, you will see this output:-

NOTE:- I have written the terratest with hardcode values. you can replace these values using the terraform means you can fetch the values from the terraform file.


In this blog, we have learned about the terratest test case to Docker push operation to your private Azure Container Registry using single Vnet and also run the test case. 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: