DevOps help for Cloud Platform Engineers
  • Welcome!
  • Quick Start Guide
  • About Me
  • CV
  • 🧠DevOps & SRE Foundations
    • DevOps Overview
      • Engineering Fundamentals
      • Implementing DevOps Strategy
      • DevOps Readiness Assessment
      • Lifecycle Management
      • The 12 Factor App
      • Design for Self Healing
      • Incident Management Best Practices (2025)
    • SRE Fundamentals
      • Toil Reduction
      • System Simplicity
      • Real-world Scenarios
        • AWS VM Log Monitoring API
    • Agile Development
      • Team Agreements
        • Definition of Done
        • Definition of Ready
        • Team Manifesto
        • Working Agreement
    • Industry Scenarios
      • Finance and Banking
      • Public Sector (UK/EU)
      • Energy Sector Edge Computing
  • DevOps Practices
    • Platform Engineering
    • FinOps
    • Observability
      • Modern Practices
  • 🚀Modern DevOps Practices
    • Infrastructure Testing
    • Modern Development
    • Database DevOps
  • 🛠️Infrastructure as Code (IaC)
    • Terraform
      • Getting Started - Installation and initial setup [BEGINNER]
      • Cloud Integrations - Provider-specific implementations
        • Azure Scenarios
        • AWS Scenarios
        • GCP Scenarios
      • Testing and Validation - Ensuring infrastructure quality
        • Unit Testing
        • Integration Testing
        • End-to-End Testing
        • Terratest Guide
      • Best Practices - Production-ready implementation strategies
        • State Management
        • Security
        • Code Organization
        • Performance
      • Tools & Utilities - Enhancing the Terraform workflow
        • Terraform Docs
        • TFLint
        • Checkov
        • Terrascan
      • CI/CD Integration - Automating infrastructure deployment
        • GitHub Actions - GitHub-based automation workflows
        • Azure Pipelines - Azure DevOps integration
        • GitLab CI - GitLab-based deployment pipelines
    • Bicep
      • Getting Started - First steps with Bicep [BEGINNER]
      • Template Specs
      • Best Practices - Guidelines for effective Bicep implementations
      • Modules - Building reusable components [INTERMEDIATE]
      • Examples - Sample implementations for common scenarios
      • Advanced Features
      • CI/CD Integration - Automating Bicep deployments
        • GitHub Actions
        • Azure Pipelines
  • 💰Cost Management & FinOps
    • Cloud Cost Optimization
  • 🐳Containers & Orchestration
    • Containerization Overview
    • Docker
      • Dockerfile Best Practices
      • Docker Compose
    • Kubernetes
      • CLI Tools - Essential command-line utilities
        • Kubectl
        • Kubens
        • Kubectx
      • Core Concepts
      • Components
      • Best Practices
        • Pod Security
        • Security Monitoring
        • Resource Limits
      • Advanced Features - Beyond the basics [ADVANCED]
        • Service Mesh
        • Ingress Controllers
          • NGINX
          • Traefik
          • Kong
          • Gloo Edge
      • Troubleshooting - Diagnosing and resolving common issues
        • Pod Troubleshooting Commands
      • Enterprise Architecture
      • Health Management
      • Security & Compliance
      • Virtual Clusters
    • OpenShift
  • Service Mesh & Networking
    • Service Mesh Implementation
  • Architecture Patterns
    • Data Mesh
    • Multi-Cloud Networking
    • Disaster Recovery
    • Chaos Engineering
  • Edge Computing
    • Implementation Guide
    • Serverless Edge
    • IoT Edge Patterns
    • Real-Time Processing
    • Edge AI/ML
    • Security Hardening
    • Observability Patterns
    • Network Optimization
    • Storage Patterns
  • 🔄CI/CD & GitOps
    • CI/CD Overview
    • Continuous Integration
    • Continuous Delivery
      • Deployment Strategies
      • Secrets Management
      • Blue-Green Deployments
      • Deployment Metrics
      • Progressive Delivery
      • Release Management for DevOps/SRE (2025)
    • CI/CD Platforms - Tool selection and implementation
      • Azure DevOps
        • Pipelines
          • Stages
          • Jobs
          • Steps
          • Templates - Reusable pipeline components
          • Extends
          • Service Connections - External service authentication
          • Best Practices for 2025
          • Agents and Runners
          • Third-Party Integrations
          • Azure DevOps CLI
        • Boards & Work Items
      • GitHub Actions
      • GitLab
        • GitLab Runner
        • Real-life scenarios
        • Installation guides
        • Pros and Cons
        • Comparison with alternatives
    • GitOps
      • Modern GitOps Practices
      • GitOps Patterns for Multi-Cloud (2025)
      • Flux
        • Overview
        • Progressive Delivery
        • Use GitOps with Flux, GitHub and AKS
  • Source Control
    • Source Control Overview
    • Git Branching Strategies
    • Component Versioning
    • Kubernetes Manifest Versioning
    • GitLab
    • Creating a Fork
    • Naming Branches
    • Pull Requests
    • Integrating LLMs into Source Control Workflows
  • ☁️Cloud Platforms
    • Cloud Strategy
    • Azure
      • Best Practices
      • Landing Zones
      • Services
      • Monitoring
      • Administration Tools - Platform management interfaces
        • Azure PowerShell
        • Azure CLI
      • Tips & Tricks
    • AWS
      • Authentication
      • Best Practices
      • Tips & Tricks
    • Google Cloud
      • Services
    • Private Cloud
  • 🔐Security & Compliance
    • DevSecOps Overview
    • DevSecOps Pipeline Security
    • DevSecOps
      • Real-life Examples
      • Scanning & Protection - Automated security tooling
        • Dependency Scanning
        • Credential Scanning
        • Container Security Scanning
        • Static Code Analysis
          • Best Practices
          • Tool Integration Guide
          • Pipeline Configuration
      • CI/CD Security
      • Secrets Rotation
    • Supply Chain Security
      • SLSA Framework
      • Binary Authorization
      • Artifact Signing
    • Security Best Practices
      • Threat Modeling
      • Kubernetes Security
    • SecOps
    • Zero Trust Model
    • Cloud Compliance
      • ISO/IEC 27001:2022
      • ISO 22301:2019
      • PCI DSS
      • CSA STAR
    • Security Frameworks
    • SIEM and SOAR
  • Security Architecture
    • Zero Trust Implementation
      • Identity Management
      • Network Security
      • Access Control
  • 🔍Observability & Monitoring
    • Observability Fundamentals
    • Logging
    • Metrics
    • Tracing
    • Dashboards
    • SLOs and SLAs
    • Observability as Code
    • Pipeline Observability
  • 🧪Testing Strategies
    • Testing Overview
    • Modern Testing Approaches
    • End-to-End Testing
    • Unit Testing
    • Performance Testing
      • Load Testing
    • Fault Injection Testing
    • Integration Testing
    • Smoke Testing
  • 🤖AI Integration
    • AIops Overview
      • Workflow Automation
      • Predictive Analytics
      • Code Quality
  • 🧠AI & LLM Integration
    • Overview
    • Claude
      • Installation Guide
      • Project Guides
      • MCP Server Setup
      • LLM Comparison
    • Ollama
      • Installation Guide
      • Configuration
      • Models and Fine-tuning
      • DevOps Usage
      • Docker Setup
      • GPU Setup
      • Open WebUI
    • Copilot
      • Installation Guide
      • VS Code Integration
      • CLI Usage
    • Gemini
      • Installation Guides - Platform-specific setup
        • Linux Installation
        • WSL Installation
        • NixOS Installation
      • Gemini 2.5 Features
      • Roles and Agents
      • NotebookML Guide
      • Cloud Infrastructure Deployment
      • Summary
  • 💻Development Environment
    • Tools Overview
    • DevOps Tools
    • Operating Systems - Development platforms
      • NixOS
        • Installation
        • Nix Language Guide
        • DevEnv with Nix
        • Cloud Deployments
      • WSL2
        • Distributions
        • Terminal Setup
    • Editor Environments
    • CLI Tools
      • Azure CLI
      • PowerShell
      • Linux Commands
      • YAML Tools
  • 📚Programming Languages
    • Python
    • Go
    • JavaScript/TypeScript
    • Java
    • Rust
  • 📖Documentation Best Practices
    • Documentation Strategy
    • Project Documentation
    • Release Notes
    • Static Sites
    • Documentation Templates
    • Real-World Examples
  • 📋Reference Materials
    • Glossary
    • Tool Comparison
    • Recommended Reading
    • Troubleshooting Guide
  • Platform Engineering
    • Implementation Guide
  • FinOps
    • Implementation Guide
  • AIOps
    • LLMOps Guide
  • Development Setup
    • Development Setup
Powered by GitBook
On this page
  • Getting Started
  • Installation
  • Basic Test Structure
  • Directory Layout
  • Simple Test Example
  • Advanced Testing Patterns
  • 1. Retry Logic
  • 2. HTTP Testing
  • 3. SSH Testing
  • Testing Cloud-Specific Resources
  • AWS Resources
  • Azure Resources
  • Test Fixtures
  • 1. Example Test Fixture
  • 2. Using Test Fixtures
  • Test Parallelization
  • 1. Parallel Test Execution
  • 2. Resource Naming for Parallel Tests
  • Testing Best Practices
  • 1. Environment Cleanup
  • 2. Test Timeouts
  • 3. Test Logging
  • CI/CD Integration
  • Troubleshooting
  • Common Issues and Solutions
Edit on GitHub
  1. Infrastructure as Code (IaC)
  2. Terraform
  3. Testing and Validation - Ensuring infrastructure quality

Terratest Guide

Terratest is a Go library that provides patterns and helper functions for testing infrastructure code. This guide covers how to effectively use Terratest for testing Terraform configurations as of 2025.

Getting Started

Installation

  1. First, ensure you have Go installed:

go version  # Should be 1.21 or higher
  1. Create a new test directory and initialize a Go module:

mkdir test
cd test
go mod init terraform-tests
  1. Install Terratest:

go get -u github.com/gruntwork-io/terratest@latest

Basic Test Structure

Directory Layout

infrastructure/
├── modules/
│   └── vpc/
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf
└── test/
    ├── go.mod
    ├── go.sum
    └── vpc_test.go

Simple Test Example

package test

import (
    "testing"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestVPCModule(t *testing.T) {
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../modules/vpc",
        
        // Variables to pass to our Terraform code
        Vars: map[string]interface{}{
            "vpc_name":   "test-vpc",
            "cidr_block": "10.0.0.0/16",
        },
    })
    
    // Clean up resources when the test is complete
    defer terraform.Destroy(t, terraformOptions)
    
    // Deploy the infrastructure
    terraform.InitAndApply(t, terraformOptions)
    
    // Get output variables
    vpcId := terraform.Output(t, terraformOptions, "vpc_id")
    subnets := terraform.OutputList(t, terraformOptions, "subnet_ids")
    
    // Verify the infrastructure
    assert.NotEmpty(t, vpcId)
    assert.Equal(t, 3, len(subnets))
}

Advanced Testing Patterns

1. Retry Logic

func TestWithRetry(t *testing.T) {
    maxRetries := 3
    timeBetweenRetries := 5 * time.Second
    
    retry.DoWithRetry(t, "Deploy infrastructure", maxRetries, timeBetweenRetries,
        func() (string, error) {
            terraformOptions := &terraform.Options{
                TerraformDir: "../modules/app",
            }
            
            terraform.InitAndApply(t, terraformOptions)
            return "", nil
        },
    )
}

2. HTTP Testing

func TestHTTPEndpoint(t *testing.T) {
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/web-app",
    }
    
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    
    url := terraform.Output(t, terraformOptions, "app_url")
    
    http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello, World!", 30, 5*time.Second)
}

3. SSH Testing

func TestSSHConnection(t *testing.T) {
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/ec2",
    }
    
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    
    publicIP := terraform.Output(t, terraformOptions, "public_ip")
    keyPair := terraform.Output(t, terraformOptions, "key_pair")
    
    host := ssh.Host{
        Hostname:    publicIP,
        SshKeyPair:  keyPair,
        Username:    "ec2-user",
    }
    
    // Run command via SSH
    output := ssh.CheckSshCommand(t, host, "echo 'Hello, World!'")
    assert.Equal(t, "Hello, World!", output)
}

Testing Cloud-Specific Resources

AWS Resources

func TestAWSResources(t *testing.T) {
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/aws",
    }
    
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    
    // Test S3 bucket
    bucketName := terraform.Output(t, terraformOptions, "bucket_name")
    aws.AssertS3BucketExists(t, "us-west-2", bucketName)
    
    // Test RDS instance
    dbAddress := terraform.Output(t, terraformOptions, "db_address")
    aws.GetRdsInstanceById(t, dbAddress, "us-west-2")
}

Azure Resources

func TestAzureResources(t *testing.T) {
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/azure",
    }
    
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    
    // Test Azure Container Registry
    acrName := terraform.Output(t, terraformOptions, "acr_name")
    azure.ContainerRegistryExists(t, acrName, "my-resource-group", "my-subscription")
}

Test Fixtures

1. Example Test Fixture

type TestFixture struct {
    terraform *terraform.Options
    workDir   string
}

func setupTestFixture(t *testing.T) *TestFixture {
    workDir, err := ioutil.TempDir("", "terraform-test")
    require.NoError(t, err)
    
    terraformOptions := &terraform.Options{
        TerraformDir: workDir,
        Vars: map[string]interface{}{
            "environment": "test",
        },
    }
    
    return &TestFixture{
        terraform: terraformOptions,
        workDir:   workDir,
    }
}

func (f *TestFixture) Cleanup() {
    os.RemoveAll(f.workDir)
}

2. Using Test Fixtures

func TestWithFixture(t *testing.T) {
    fixture := setupTestFixture(t)
    defer fixture.Cleanup()
    
    terraform.InitAndApply(t, fixture.terraform)
    
    // Run tests...
}

Test Parallelization

1. Parallel Test Execution

func TestParallelDeployments(t *testing.T) {
    t.Parallel()
    
    uniqueId := random.UniqueId()
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/app",
        Vars: map[string]interface{}{
            "app_name": fmt.Sprintf("test-app-%s", uniqueId),
        },
    }
    
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
}

2. Resource Naming for Parallel Tests

func getUniqueTestName(t *testing.T) string {
    return fmt.Sprintf("test-%s-%s", t.Name(), random.UniqueId())
}

func TestMultipleInstances(t *testing.T) {
    t.Parallel()
    
    testName := getUniqueTestName(t)
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/instance",
        Vars: map[string]interface{}{
            "instance_name": testName,
        },
    }
    
    // Deploy and test...
}

Testing Best Practices

1. Environment Cleanup

func ensureCleanup(t *testing.T, options *terraform.Options) {
    defer func() {
        if err := recover(); err != nil {
            terraform.Destroy(t, options)
            panic(err)
        }
    }()
    
    terraform.Destroy(t, options)
}

2. Test Timeouts

func TestWithTimeout(t *testing.T) {
    timeout := 30 * time.Minute
    ctx, cancel := context.WithTimeout(context.Background(), timeout)
    defer cancel()
    
    done := make(chan bool)
    go func() {
        // Run your test
        terraform.InitAndApply(t, terraformOptions)
        done <- true
    }()
    
    select {
    case <-done:
        // Test completed successfully
    case <-ctx.Done():
        t.Fatal("Test timed out")
    }
}

3. Test Logging

func TestWithLogging(t *testing.T) {
    logger := terraform.NewLogger(t)
    defer logger.Cleanup()
    
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/app",
        Logger:       logger,
    }
    
    logger.Logf(t, "Starting test deployment...")
    terraform.InitAndApply(t, terraformOptions)
    logger.Logf(t, "Deployment complete")
}

CI/CD Integration

name: Terratest
on: [pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'
          
      - name: Install Terraform
        uses: hashicorp/setup-terraform@v3
        
      - name: Download dependencies
        run: |
          cd test
          go mod download
          
      - name: Run Terratest
        run: |
          cd test
          go test -v -timeout 30m
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Troubleshooting

Common Issues and Solutions

  1. State Lock Issues

func TestWithStateLock(t *testing.T) {
    terraformOptions := &terraform.Options{
        TerraformDir: "../modules/app",
        Lock:         true,
        LockTimeout:  "5m",
    }
    
    terraform.InitAndApply(t, terraformOptions)
}
  1. Provider Authentication

func init() {
    os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
    os.Setenv("AWS_DEFAULT_REGION", "us-west-2")
}
  1. Resource Dependencies

func TestWithDependencies(t *testing.T) {
    // Deploy dependencies first
    depOptions := &terraform.Options{
        TerraformDir: "../modules/dependencies",
    }
    terraform.InitAndApply(t, depOptions)
    
    // Get outputs from dependencies
    dbEndpoint := terraform.Output(t, depOptions, "db_endpoint")
    
    // Use in main deployment
    appOptions := &terraform.Options{
        TerraformDir: "../modules/app",
        Vars: map[string]interface{}{
            "db_endpoint": dbEndpoint,
        },
    }
    terraform.InitAndApply(t, appOptions)
}
PreviousEnd-to-End TestingNextBest Practices - Production-ready implementation strategies

Last updated 2 days ago

🛠️