Documentation
¶
Overview ¶
Package libtftest wraps Terratest with opinionated, LocalStack-aware defaults for Terraform module integration testing.
libtftest manages the LocalStack container lifecycle, injects provider and backend overrides into the workspace under test, provides pre-configured AWS SDK v2 clients via the awsx sub-package, and offers parallel-safe resource naming. The goal: module authors write ~10 lines of Go instead of ~200.
Typical use ¶
func TestS3ModuleApply(t *testing.T) {
tc := libtftest.New(t, &libtftest.Options{
ModuleDir: "../",
})
tc.SetVar("bucket_name", tc.Prefix()+"-bucket")
tc.Apply()
s3assert.BucketExists(t, tc.AWS(), tc.Prefix()+"-bucket")
}
Package layout ¶
libtftest is organised as a small root package plus several sub-packages, each focused on a single concern:
- assert/<service> — post-apply assertions, one package per AWS service (s3, dynamodb, iam, ssm, lambda, tags, snapshot)
- fixtures/<service> — pre-apply data seeding, one package per AWS service (s3, ssm, secretsmanager, sqs)
- awsx — flat package of typed AWS SDK v2 client constructors
- harness — TestMain helpers for shared-container suites
- localstack — testcontainers-go wrapper for LocalStack
- tf — Terraform workspace management, override rendering
- sneakystack — LocalStack gap-filling HTTP proxy
See DESIGN-0001 for the architectural overview and DESIGN-0003 for the v0.2.0 layout refactor that introduced the per-service sub-packages.
Index ¶
- func RequirePro(tb testing.TB)
- func RequireServices(tb testing.TB, _ ...string)
- type Options
- type PlanChanges
- type PlanResult
- type TestCase
- func (tc *TestCase) AWS() aws.Config
- func (tc *TestCase) Apply() *terraform.Options
- func (tc *TestCase) ApplyContext(ctx context.Context) *terraform.Options
- func (tc *TestCase) ApplyContextE(ctx context.Context) (*terraform.Options, error)
- func (tc *TestCase) ApplyE() (*terraform.Options, error)
- func (tc *TestCase) AssertIdempotent()
- func (tc *TestCase) AssertIdempotentApply()
- func (tc *TestCase) AssertIdempotentApplyContext(ctx context.Context)
- func (tc *TestCase) AssertIdempotentContext(ctx context.Context)
- func (tc *TestCase) Output(name string) string
- func (tc *TestCase) OutputContext(ctx context.Context, name string) string
- func (tc *TestCase) Plan() *PlanResult
- func (tc *TestCase) PlanContext(ctx context.Context) *PlanResult
- func (tc *TestCase) PlanContextE(ctx context.Context) (*PlanResult, error)
- func (tc *TestCase) PlanE() (*PlanResult, error)
- func (tc *TestCase) Prefix() string
- func (tc *TestCase) SetVar(key string, val any)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequirePro ¶
RequirePro skips the test when the running container is Community edition.
func RequireServices ¶
RequireServices skips the test when any of the named services is not available in the running container's edition.
Types ¶
type Options ¶
type Options struct {
Edition localstack.Edition
Services []string
Image string
ModuleDir string
Vars map[string]any
Reuse *localstack.Container
PersistOnFailure bool
InitHooks []localstack.InitHook
AutoPrefixVars bool
EdgeURLOverride string
}
Options configure a TestCase.
type PlanChanges ¶
type PlanChanges struct {
Add int // Resources to create.
Change int // Resources to update in-place.
Destroy int // Resources to destroy.
}
PlanChanges summarizes the resource-level diff from a plan.
type PlanResult ¶
type PlanResult struct {
JSON []byte // Raw `terraform show -json` output.
FilePath string // Path to the binary plan file.
Changes PlanChanges // Parsed summary of resource changes.
}
PlanResult holds the output of a terraform plan.
type TestCase ¶
type TestCase struct {
// contains filtered or unexported fields
}
TestCase is the primary handle returned from New. It owns a LocalStack container (or a reference to a shared one), a scratch workspace, and the AWS SDK config used for seeding and assertions.
func New ¶
New creates a TestCase. It starts LocalStack (or attaches to a shared one), copies the module into a scratch workspace, writes the provider override, and registers cleanup with t.Cleanup. It calls t.Fatal on any setup error.
func (*TestCase) ApplyContext ¶ added in v0.1.0
ApplyContext runs terraform init + terraform apply with the supplied context and returns the terraform.Options so callers can chain additional operations.
func (*TestCase) ApplyContextE ¶ added in v0.1.0
ApplyContextE is the error-returning variant of ApplyContext.
func (*TestCase) AssertIdempotent ¶ added in v0.2.0
func (tc *TestCase) AssertIdempotent()
AssertIdempotent is a shim that calls AssertIdempotentContext with tb.Context().
func (*TestCase) AssertIdempotentApply ¶ added in v0.2.0
func (tc *TestCase) AssertIdempotentApply()
AssertIdempotentApply is a shim that calls AssertIdempotentApplyContext with tb.Context().
func (*TestCase) AssertIdempotentApplyContext ¶ added in v0.2.0
AssertIdempotentApplyContext performs the canonical double-Apply check: runs Plan (fails if non-empty), runs Apply again, then runs Plan a second time (fails if non-empty). Catches a strictly larger class of bugs than AssertIdempotentContext — including computed-vs-known mismatches that only surface on the second Apply — at the cost of one extra terraform apply round-trip.
Use this after the caller's initial Apply has completed.
func (*TestCase) AssertIdempotentContext ¶ added in v0.2.0
AssertIdempotentContext runs Plan and fails the test if the plan reports any resource changes (add, change, or destroy). It does NOT call Apply itself — call it after the caller's initial Apply has completed. It calls tb.Errorf on a non-zero change count; the test continues so additional assertions can surface their own failures.
Catches: bad ignore_changes, provider refresh-time drift, and known-after-apply placeholders that didn't resolve.
func (*TestCase) OutputContext ¶ added in v0.1.0
OutputContext reads a single Terraform output value with the supplied context.
func (*TestCase) Plan ¶
func (tc *TestCase) Plan() *PlanResult
Plan is a shim that calls PlanContext with tb.Context().
func (*TestCase) PlanContext ¶ added in v0.1.0
func (tc *TestCase) PlanContext(ctx context.Context) *PlanResult
PlanContext runs terraform init + terraform plan -out with the supplied context and returns a PlanResult.
func (*TestCase) PlanContextE ¶ added in v0.1.0
func (tc *TestCase) PlanContextE(ctx context.Context) (*PlanResult, error)
PlanContextE is the error-returning variant of PlanContext.
func (*TestCase) PlanE ¶
func (tc *TestCase) PlanE() (*PlanResult, error)
PlanE is a shim that calls PlanContextE with tb.Context().
Directories
¶
| Path | Synopsis |
|---|---|
|
Package assert is deprecated.
|
Package assert is deprecated. |
|
dynamodb
Package dynamodb provides post-apply assertions for AWS DynamoDB resources created by Terraform modules under test.
|
Package dynamodb provides post-apply assertions for AWS DynamoDB resources created by Terraform modules under test. |
|
iam
Package iam provides post-apply assertions for AWS IAM resources created by Terraform modules under test.
|
Package iam provides post-apply assertions for AWS IAM resources created by Terraform modules under test. |
|
lambda
Package lambda provides post-apply assertions for AWS Lambda resources created by Terraform modules under test.
|
Package lambda provides post-apply assertions for AWS Lambda resources created by Terraform modules under test. |
|
s3
Package s3 provides post-apply assertions for AWS S3 resources created by Terraform modules under test.
|
Package s3 provides post-apply assertions for AWS S3 resources created by Terraform modules under test. |
|
snapshot
Package snapshot provides JSON snapshot testing for Terraform plans and other deterministic JSON payloads, plus a small extraction toolkit for the IAM-heavy use cases that motivate it.
|
Package snapshot provides JSON snapshot testing for Terraform plans and other deterministic JSON payloads, plus a small extraction toolkit for the IAM-heavy use cases that motivate it. |
|
ssm
Package ssm provides post-apply assertions for AWS Systems Manager (SSM) Parameter Store resources created by Terraform modules under test.
|
Package ssm provides post-apply assertions for AWS Systems Manager (SSM) Parameter Store resources created by Terraform modules under test. |
|
tags
Package tags provides service-agnostic tag-propagation assertions backed by the AWS Resource Groups Tagging API.
|
Package tags provides service-agnostic tag-propagation assertions backed by the AWS Resource Groups Tagging API. |
|
Package awsx provides AWS SDK v2 client constructors configured for LocalStack-backed Terraform module tests.
|
Package awsx provides AWS SDK v2 client constructors configured for LocalStack-backed Terraform module tests. |
|
cmd
|
|
|
libtftest
command
Package main is the entry point for the libtftest CLI.
|
Package main is the entry point for the libtftest CLI. |
|
sneakystack
command
Package main is the entry point for the sneakystack standalone proxy binary.
|
Package main is the entry point for the sneakystack standalone proxy binary. |
|
Package fixtures is deprecated.
|
Package fixtures is deprecated. |
|
s3
Package s3 provides pre-apply data-seeding fixtures for AWS S3 resources in LocalStack-backed Terraform module tests.
|
Package s3 provides pre-apply data-seeding fixtures for AWS S3 resources in LocalStack-backed Terraform module tests. |
|
secretsmanager
Package secretsmanager provides pre-apply data-seeding fixtures for AWS Secrets Manager resources in LocalStack-backed Terraform module tests.
|
Package secretsmanager provides pre-apply data-seeding fixtures for AWS Secrets Manager resources in LocalStack-backed Terraform module tests. |
|
sqs
Package sqs provides pre-apply data-seeding fixtures for AWS SQS resources in LocalStack-backed Terraform module tests.
|
Package sqs provides pre-apply data-seeding fixtures for AWS SQS resources in LocalStack-backed Terraform module tests. |
|
ssm
Package ssm provides pre-apply data-seeding fixtures for AWS Systems Manager (SSM) Parameter Store resources in LocalStack- backed Terraform module tests.
|
Package ssm provides pre-apply data-seeding fixtures for AWS Systems Manager (SSM) Parameter Store resources in LocalStack- backed Terraform module tests. |
|
Package harness provides shared-container TestMain helpers and the Sidecar interface for plugging auxiliary services (e.g.
|
Package harness provides shared-container TestMain helpers and the Sidecar interface for plugging auxiliary services (e.g. |
|
internal
|
|
|
dockerx
Package dockerx provides Docker daemon detection and error classification helpers used by libtftest's container lifecycle code.
|
Package dockerx provides Docker daemon detection and error classification helpers used by libtftest's container lifecycle code. |
|
logx
Package logx provides structured logging configuration and test artifact dumping used by libtftest's TestCase lifecycle.
|
Package logx provides structured logging configuration and test artifact dumping used by libtftest's TestCase lifecycle. |
|
naming
Package naming provides deterministic, parallel-safe resource name prefixes for libtftest's TestCase fixture.
|
Package naming provides deterministic, parallel-safe resource name prefixes for libtftest's TestCase fixture. |
|
testfake
Package testfake provides a minimal in-memory testing.TB stand-in used by libtftest's per-service test packages to verify that assertions and fixtures route failures through the test handle rather than panicking, returning errors, or silently swallowing problems.
|
Package testfake provides a minimal in-memory testing.TB stand-in used by libtftest's per-service test packages to verify that assertions and fixtures route failures through the test handle rather than panicking, returning errors, or silently swallowing problems. |
|
Package localstack manages LocalStack container lifecycle via testcontainers-go.
|
Package localstack manages LocalStack container lifecycle via testcontainers-go. |
|
Package sneakystack provides a LocalStack gap-filling HTTP proxy with an in-memory store, designed to surface AWS APIs that LocalStack itself does not cover (IAM Identity Center, Organizations, Control Tower, etc.).
|
Package sneakystack provides a LocalStack gap-filling HTTP proxy with an in-memory store, designed to surface AWS APIs that LocalStack itself does not cover (IAM Identity Center, Organizations, Control Tower, etc.). |
|
services
Package services hosts the per-service handlers for sneakystack's LocalStack-gap-filling proxy.
|
Package services hosts the per-service handlers for sneakystack's LocalStack-gap-filling proxy. |
|
Package tf handles Terraform workspace management, override rendering, and terraform.Options construction for libtftest's TestCase fixture.
|
Package tf handles Terraform workspace management, override rendering, and terraform.Options construction for libtftest's TestCase fixture. |
|
Package tools is a parent directory marker for repo-local Go tooling that is built but not redistributed as part of the public libtftest module surface.
|
Package tools is a parent directory marker for repo-local Go tooling that is built but not redistributed as part of the public libtftest module surface. |
|
docgen
command
Command docgen scans the libtftest repo for `// libtftest:requires <tag>[,<tag>...] <reason>` marker comments, emits a JSON intermediate representation, renders the human-readable feature matrix to docs/feature-matrix.md, and gates CI by verifying that every function calling libtftest.RequirePro carries a marker.
|
Command docgen scans the libtftest repo for `// libtftest:requires <tag>[,<tag>...] <reason>` marker comments, emits a JSON intermediate representation, renders the human-readable feature matrix to docs/feature-matrix.md, and gates CI by verifying that every function calling libtftest.RequirePro carries a marker. |