Terratest — Automated Testing for Infrastructure Code
Terratest is a Go library that makes it easy to write automated tests for your Terraform, Packer, Kubernetes, and Docker infrastructure.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install c79522cf-3997-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
Terratest is a Go library for writing automated tests for infrastructure code. It deploys real infrastructure (Terraform modules, Kubernetes manifests, Docker images, Packer templates), validates that it works correctly, and then tears it down. Unlike linting or static analysis, Terratest tests actual behavior.
Terratest targets infrastructure engineers and DevOps teams who want confidence that their Terraform modules, Helm charts, and Kubernetes configurations actually work before merging to main.
How it saves time or tokens
Terratest catches infrastructure bugs before they reach production. A broken Terraform module discovered in CI costs minutes. The same bug discovered in production costs hours of debugging and potential downtime. By testing real infrastructure in an isolated environment, you verify networking, IAM permissions, and resource configuration end-to-end.
Terratest also provides helper functions for common assertions (HTTP requests, SSH commands, Kubernetes queries) that would take significant boilerplate to write from scratch.
How to use
- Create a Go test file in your Terraform module directory:
go mod init test
go get github.com/gruntwork-io/terratest/modules/terraform
- Write a test that deploys and validates infrastructure:
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
func TestWebServer(t *testing.T) {
opts := &terraform.Options{
TerraformDir: "../modules/web-server",
Vars: map[string]interface{}{
"instance_type": "t3.micro",
},
}
defer terraform.Destroy(t, opts)
terraform.InitAndApply(t, opts)
url := terraform.Output(t, opts, "url")
assert.Contains(t, url, "http")
}
- Run the test:
go test -v -timeout 30m
Example
Testing a Kubernetes deployment with Terratest:
func TestK8sDeployment(t *testing.T) {
kubeOpts := k8s.NewKubectlOptions("", "", "test-ns")
k8s.CreateNamespace(t, kubeOpts, "test-ns")
defer k8s.DeleteNamespace(t, kubeOpts, "test-ns")
k8s.KubectlApply(t, kubeOpts, "../manifests/deployment.yaml")
k8s.WaitUntilDeploymentAvailable(t, kubeOpts, "my-app", 30, 10*time.Second)
service := k8s.GetService(t, kubeOpts, "my-app")
assert.Equal(t, int32(80), service.Spec.Ports[0].Port)
}
Related on TokRepo
- AI tools for testing -- Automated testing tools and frameworks
- AI tools for DevOps -- Infrastructure automation and testing
Common pitfalls
- Not setting timeouts. Infrastructure provisioning takes minutes, not seconds. Use
go test -timeout 30mand set appropriate retry intervals in Terratest helper functions. - Testing in shared environments. Always use isolated accounts, namespaces, or resource prefixes to prevent test infrastructure from colliding with production or other test runs.
- Forgetting the
defer terraform.Destroy()call. Without it, failed tests leave orphaned cloud resources that accumulate costs.
常见问题
Yes. Terratest runs actual Terraform apply, creates real cloud resources, validates them, and then destroys them. This is the key difference from tools like terraform validate or tflint, which only check syntax and configuration without deploying.
Terratest tests are slow compared to unit tests because they provision real infrastructure. A simple EC2 instance test takes 3-5 minutes. Complex tests with databases or Kubernetes clusters can take 15-30 minutes. Run them in CI pipelines, not on every commit.
Yes. Terratest has a dedicated k8s module for deploying manifests, waiting for pods, querying services, and making HTTP requests to Kubernetes workloads. It works with any Kubernetes cluster (EKS, GKE, kind, minikube).
Terratest is written in Go and tests are written as Go test functions. However, you do not need to be a Go expert. The API is straightforward and most tests follow a simple pattern: deploy, validate, destroy. Basic Go knowledge is sufficient.
Kitchen-Terraform uses Ruby and the Test Kitchen framework. Terratest uses Go and the standard Go testing library. Terratest is generally preferred in Go-heavy organizations and has a larger community and more helper modules.
引用来源 (3)
- Terratest GitHub— Terratest is a Go library for automated infrastructure testing
- Terratest Documentation— Testing real infrastructure with deploy-validate-destroy pattern
- HashiCorp Testing Guide— Terraform infrastructure as code testing best practices
讨论
相关资产
PHPUnit — The Standard Testing Framework for PHP
PHPUnit is the de facto unit testing framework for PHP, providing assertions, mocks, and code coverage analysis.
GoConvey — Go Testing with Browser UI
A BDD-style testing framework for Go that includes a live-reloading browser interface. Write expressive specs with nested Convey blocks and watch results update in real time.
Ddosify — High-Performance Load Testing and Observability Platform
Ddosify is a high-performance load testing tool written in Go that supports HTTP, HTTPS, HTTP/2, gRPC, and GraphQL protocols, with scenario-based testing and distributed execution.
TestCafe — Cross-Browser E2E Testing Without WebDriver
TestCafe is a Node.js end-to-end testing framework that runs tests in real browsers using a URL-rewriting proxy, requiring no WebDriver or browser plugins.