Skills2026年4月16日·1 分钟阅读

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 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Terratest Overview
直接安装命令
npx -y tokrepo@latest install c79522cf-3997-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Terratest is a Go testing library that validates Terraform, Packer, Kubernetes, and Docker infrastructure with real deployments.
§01

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.

§02

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.

§03

How to use

  1. Create a Go test file in your Terraform module directory:
go mod init test
go get github.com/gruntwork-io/terratest/modules/terraform
  1. 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")
}
  1. Run the test:
go test -v -timeout 30m
§04

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)
}
§05

Related on TokRepo

§06

Common pitfalls

  • Not setting timeouts. Infrastructure provisioning takes minutes, not seconds. Use go test -timeout 30m and 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.

常见问题

Does Terratest deploy real infrastructure?+

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.

How long do Terratest tests take to run?+

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.

Can Terratest test Kubernetes configurations?+

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).

Is Terratest only for Go developers?+

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.

How does Terratest compare to Kitchen-Terraform?+

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)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产