How to use Terratest using Packer?

terratest
Reading Time: 3 minutes

Hey readers, today we’ll be looking into how we can use Terratest with Packer to test infrastructure code. So Terratest is a library based on the GO language which provides helper functions and patterns for testing the code. Terratest is developed by Gruntwork which is open source. It is used to create and automate infrastructure tests as a code portrayed in Terraform.

Here we will also use Packer for creating images of our code. Packer is an open-source tool that allows users to create identical images for different platforms from a single source template. Here Packer will build the Docker container without using the Dockerfile

Installing Packer

Adding the key GPG key:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

Now we’ll add the official Hashicorp Linux repository:

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

In the last, well just update the system and install Packer:

sudo apt-get update && sudo apt-get install packer

Building Packer Template

Now we will define some requirements of the docker version and the source image location.

packer {
  required_plugins {
    docker = {
      version = ">=v1.0.1"
      source  = "github.com/hashicorp/docker"
    }
  }
}

Now we have to pull the official docker image of Ubuntu from the docker hub. Basically, here we define the image builder plugin. Also, we will give an entry point and will also commit the container to the image so that the image can be easily tagged and pushed. And image name and version will also mention. and the code will look like below:

source "docker" "ubuntu-docker" {
  changes = ["ENTRYPOINT [\"\"]"]
  commit  = true
  image   = "gruntwork/ubuntu-test:16.04"
}

Now under the Build section, we will define provisioners and post processors. So Provisioner is the part of the build block and here you define the configuration of the image. After that, the Post-processor is not a so much much-required block but we can use it to specify what to do after creating an image. The block will look like the below:

build {
  sources = ["source.docker.ubuntu-docker"]

  provisioner "shell" {
    inline = ["echo 'Hello, World!' > /output.txt"]
  }

  post-processor "docker-tag" {
    repository = "your docker hub repository"
    tag        = ["latest"]
  }
}

Keep the above template code into a single file with extension .pkr.hcl.

Building GO script

First, we will be creating a package and will import the packages we will be using like below:

package test

import (
	"testing"

	"github.com/gruntwork-io/terratest/modules/docker"
	"github.com/gruntwork-io/terratest/modules/packer"
	"github.com/stretchr/testify/assert"
)

Then we will create a function that will use the above packer template we’ve created Don’t forget to give the path of your template file:

func TestPackerHelloWorldExample(t *testing.T) {
	packerOptions := &packer.Options{
		Template: "../examples/packer-hello-world-example/build.pkr.hcl",
	}

	packer.BuildArtifact(t, packerOptions)

	opts := &docker.RunOptions{Command: []string{"cat", "/output.txt"}}
	output := docker.Run(t, "hello-world-example", opts)
	assert.Equal(t, "Hello, World!", output)
}

Building the Packer template

Use packer version 1.7.0 or above for a successful build. Also, use Sudo privileges for building the template. For building the template, we have to initialize the template and for that, use the below command:

sudo packer init build.pkr.hcl
How to use Terratest using Packer?

Now building the template, use the below command:

sudo packer build build.pkr.hcl
How to use Terratest using Packer?

It will start pulling the images from the docker hub to run the container and after that, it will look like the below snap:

How to use Terratest using Packer?

Conclusion

After building the template, you have to run the container which contains the output. For that we have to run the container like the below:

docker run -it --rm hello-world-example cat /output.txt

If you can see the Hello, World! in the output, then you have done the process perfectly otherwise, there is some error. For more info, visit here.

knoldus

Written by 

Shubham Saini is a DevOps Engineer who loves to play with DevOps tools, Security methods and is also interested in Ethical Hacking & Cyber Security. He is a gamer also.

Leave a Reply