Voiced by Amazon Polly |
Overview
In today’s fast-paced software development world, robust testing frameworks are paramount. Infrastructure as Code (IaC) tools like Terraform have revolutionized how we manage infrastructure, but ensuring that the infrastructure is correctly deployed and configured is equally important. This is where Terratest comes in—a Go-based testing framework designed to work with Terraform, Packer, and other infrastructure tools. In this blog, we will delve into Terratest, explore its features, and use a hands-on example to demonstrate how to use it effectively.
Terratest
Terratest is a Go library that provides tools and patterns to write automated tests for your infrastructure code. It helps you validate that your infrastructure works as expected, which is crucial for maintaining stability and reliability. With Terratest, you can:
- Write tests in Go, a language known for its performance and simplicity.
- Deploy real infrastructure in a real environment (e.g., AWS, GCP, Azure).
- Verify the accuracy of this infrastructure by running tests against it.
- Destroy the infrastructure after tests run to avoid incurring unnecessary costs.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Why Use Terratest?
- Integration with Go: Leverage the power of Go to write concise and efficient tests.
- Real-world Testing: Test your actual infrastructure rather than relying solely on mocks.
- Reusability: Use existing Go libraries and modules to enhance your tests.
- Community Support: Benefit from a robust community and comprehensive documentation.
Setting Up Terratest
Before we dive into a hands-on example, let’s set up our environment.
- Install Go: Ensure that Go is installed on your machine.
On Linux (Ubuntu/Debian-based systems):
1 2 3 4 5 6 7 8 |
sudo apt update sudo apt install -y wget tar wget https://golang.org/dl/go1.17.7.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.17.7.linux-amd64.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile source ~/.profile |
On macOS:
1 |
brew install go |
On Windows:
- Download the MSI installer from the official website.
- Launch the installation and adhere to the guidelines.
After installation, verify by running:
1 |
go version |
2. Install Terraform: Download and install Terraform from the official Terraform website.
On Linux (Ubuntu/Debian-based systems):
1 2 3 4 5 6 |
sudo apt update sudo apt install -y wget unzip wget https://releases.hashicorp.com/terraform/1.0.11/terraform_1.0.11_linux_amd64.zip unzip terraform_1.0.11_linux_amd64.zip sudo mv terraform /usr/local/bin/ |
On macOS:
1 2 |
brew tap hashicorp/tap brew install hashicorp/tap/terraform |
On Windows:
- Download the binary from the official Terraform website.
- Extract the downloaded zip file and place the exe in a directory included in your system’s PATH.
After installation, verify by running:
1 |
terraform –version |
3. Set Up Go Workspace: Create a Go workspace for your Terratest project.
1 2 |
mkdir -p ~/go/src/github.com/your-username/terratest-eg cd ~/go/src/github.com/your-username/terratest-eg |
4. Initialize Go Module: Initialize a Go module for your project.
1 |
go mod init github.com/your-username/terratest-eg |
5. Install Terratest: Add Terratest to your project dependencies.
1 |
go get github.com/gruntwork-io/terratest/modules/terraform |
Hands-On Example: Testing an Amazon S3 Bucket
Let’s walk through an example where we will test the creation of an Amazon S3 bucket using Terraform and Terratest.
Step 1: Write Terraform Code
Create a main.tf file with the following Terraform code to create an Amazon S3 bucket.
1 2 3 4 5 6 7 8 |
provider "aws" { region = "us-west-2" } resource "aws_s3_bucket" "example" { bucket = "terratest-eg-bucket" acl = "private" } |
Initialize the Terraform configuration.
1 |
terraform init |
Step 2: Write Terratest Code
Create main_test.go file to write code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package test import ( "testing" "github.com/gruntwork-io/terratest/modules/terraform" "github.com/stretchr/testify/assert" "github.com/gruntwork-io/terratest/modules/aws" ) func TestTerraformS3Bucket(t *testing.T) { t.Parallel() // Define the Terraform options terraformOptions := &terraform.Options{ TerraformDir: "../terraform", } // Clean the resources with "terraform destroy" defer terraform.Destroy(t, terraformOptions) // Execute "terraform init" & "terraform apply" terraform.InitAndApply(t, terraformOptions) // Get the S3 bucket ID from the outputs bucketID := terraform.Output(t, terraformOptions, "bucket_id") // Verify that the bucket exists aws.AssertS3BucketExists(t, "us-west-2", bucketID) } |
In this test:
- We set the Terraform directory to the location of our main.tf.
- We ensure that resources are cleaned up after the test using defer terraform.Destroy.
- We run terraform init and terraform apply to deploy the infrastructure.
- We get the bucket ID from the Terraform outputs.
- We verify that the bucket exists using Terratest’s AWS module.
Step 3: Run the Tests
Use the go test command to begin the tests.
1 |
go test -v |
You should see output indicating that Terraform is initializing and applying the configuration, followed by the test results.
Advanced Terratest Features
While our example is simple, Terratest provides advanced features for more complex scenarios.
- Parallel Testing: To expedite execution, run tests concurrently.
- Retries and Timeouts: Implement retries and timeouts to handle transient errors.
- SSH and HTTP Helpers: Use SSH and HTTP helpers to interact with your infrastructure.
- Kubernetes Support: Test Kubernetes resources using the k8s module.
Best Practices
- Modularize Tests: To increase maintainability, divide tests into reusable modules.
- Use Tags: Tag tests to categorize and selectively run them.
- Clean Up: Ensure resources are cleaned after tests to avoid unnecessary costs.
- Version Control: Keep your tests version-controlled along with your infrastructure code.
Conclusion
As you continue to explore Terratest, you’ll discover more features and capabilities that can help you ensure the reliability and correctness of your infrastructure.
Drop a query if you have any questions regarding Terratest and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. What programming language does Terratest support?
ANS: – Primarily supports Go (Golang) for efficient infrastructure testing.
2. Can Terratest be used on cloud platforms other than AWS?
ANS: – Yes, it supports AWS, GCP, Azure, and other cloud providers.
3. How does Terratest handle dependencies and external integrations?
ANS: – Manages dependencies with Go modules for consistency and reproducibility.
![](https://content.cloudthat.com/resources/wp-content/uploads/2023/05/Noopur-150x150.jpg)
WRITTEN BY Noopur Shrivastava
Noopur Shrivastava works as a Research Associate at CloudThat. She is focused on gaining knowledge of the Cloud environment. Noopur loves learning about new technology and trying out different approaches to problem-solving.
Comments