NashTech Insights

Terratest: How to write the terratest using the docker push operation with Public ACR

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents

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 Public ACR to perform the docker push operation. 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.

Introduction:-

In the dynamic world of containerized applications, the Docker push operation plays an important role in seamlessly deploying software across environments. Microsoft Azure’s Public Azure Container Registry (ACR) is a trusted platform for storing, managing, and securing Docker container images. To ensure the reliability of your image distribution process, Terratest comes into play. Terratest, a powerful infrastructure testing framework, allows you to automate and validate Docker push operations to your Public ACR. In this blog post, we’ll learn about the process of executing Terratest test cases to ensure the success of your Docker pushes operation on a Public ACR.

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.

Writing Terratest Test Cases for Docker Push on Public ACR:-

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 push_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 provide you with the tools and functions required to interact with your Public ACR and perform tests.

and define the global variable for this terratest:-

var (
	acrURL = "testacrmk.azurecr.io"
	acrName = "testacrmk"
    testImage = "hello-world:latest"
	expectedPushedResponse = true
)

4. Define the functions:-

We need to identify the scenario to perform the action. So for the docker push operation. First of all, we need to fetch the local image and tag it with our choice and then we will push that image to the ACR. Let’s start writing the function:-

i. Tagging Function:-

We will write a go function to tag the image:-

In this function, we are passing the tag image and test image so that we can tag the image with our tag.

ii. Push Function:-

This is the main function of our terratest. Now we will create a push function which can push the tagged image to the ACR:-

iii. Main Test function:-

In this function, we will call all the functions which we have defined to perform some action to fetch some details:-

Now, we are defining the tag first which we want, then calling the tag function to tag the image and then pushing the Function to push the image. The very important part – is the test cases, here we are comparing the expected value and actual values from the resource, if I will match then the test case will pass otherwise failed.

5. Executing the Tests:

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

go mod init <github.com/publicacr/pushimage>

go mod tidy

go test -v

Conclusion:-

In this blog, we have learned about the terratest test case to Docker push operation to your Public Azure Container Registry 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: