Go tests, fixture environments, retries, assertions, and deterministic teardown patterns — testing infrastructure with real deployments.
8 min readInfrastructure as CodeProvision
Terratest Setup
Gogo-module-setup.go
9 linesLn 1, Col 1Go
The go.mod file manages Terratest dependencies. The testify package provides assertion functions. Pin specific versions for reproducible test environments.
Test Patterns
Gobasic-test-pattern.go
16 linesLn 1, Col 1Go
The pattern is consistent: configure options, defer destroy, init and apply, then assert. t.Parallel() allows concurrent test execution. WithDefaultRetryableErrors retries on transient failures.
Gomulti-environment-test.go
23 linesLn 1, Col 1Go
The subtest pattern tests multiple environments in parallel. Each subtest has its own terraformOptions and defer destroy. This catches environment-specific issues.
Fixture Management
Godynamic-fixture-configuration.go
22 linesLn 1, Col 1Go
Dynamic fixtures create isolated test environments. Each test gets its own directory with unique resources. Cleanup removes the directory after the test.
Retry Patterns
Goretry-with-backoff.go
11 linesLn 1, Col 1Go
retry.DoWithRetryableErrors retries on specific error messages. The retry count and interval control the backoff. This handles transient API failures gracefully.
Assertion Patterns
Gocommon-assertions.go
21 linesLn 1, Col 1Go
testify provides rich assertion functions. Each assertion has a clear error message on failure. Use the most specific assertion for each check.
Teardown Patterns
Goreliable-teardown.go
21 linesLn 1, Col 1Go
The deferred cleanup function retries destroy up to 3 times. terraform.DestroyE returns an error instead of failing immediately. This handles transient API failures during cleanup.
Use unique resource names
Generate unique resource names for each test run to avoid conflicts. Use random.UniqueId() or random.RandomString() to create unique identifiers. This allows parallel test execution.
Tests cost real money
Terratest provisions real cloud resources. A test suite for an EKS module may create VPCs, subnets, and a cluster. Run tests in a dedicated test account with budget alerts.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.