All skills
hashicorp avatar

/provider-test-patterns

@4451cec official
by hashicorphashicorp/agent-skills880 stars
130

Terraform provider acceptance test patterns using terraform-plugin-testing with the Plugin Framework. Covers test structure, TestCase/TestStep fields, ConfigStateChecks with custom statecheck.StateCheck implementations, plan checks, CompareValue for cross-step assertions, config helpers, import testing with ImportStateKind, sweepers, and scenario patterns (basic, update, disappears, validation, regression), and ephemeral resource testing with the echoprovider package. Use when writing, reviewing, or debugging provider acceptance tests, including questions about statecheck, plancheck, TestCheckFunc, CheckDestroy, ExpectError, import state verification, ephemeral resources, or how to structure test files.

Use this Skill: https://skilld.dev/gh/hashicorp/agent-skills/provider-test-patterns

This session only. Nothing lands on disk.

referencessweepers.md

≈601 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Test Sweepers Reference

Sweepers clean up infrastructure resources that leak during acceptance tests — when test infrastructure fails to be destroyed due to API errors or test failures.

Source: Sweepers


Setup

TestMain (required)

Add to a dedicated file (e.g., sweep_test.go):

func TestMain(m *testing.M) {
    resource.TestMain(m)
}

This parses the -sweep flag and invokes registered sweepers.

Register a Sweeper

Register in the test file for the resource being swept, using init():

func init() {
    resource.AddTestSweepers("example_widget", &resource.Sweeper{
        Name: "example_widget",
        F:    sweepWidgets,
    })
}

func sweepWidgets(region string) error {
    client, err := sharedClientForRegion(region)
    if err != nil {
        return fmt.Errorf("getting client: %w", err)
    }

    conn := client.(*Client)
    widgets, err := conn.ListWidgets()
    if err != nil {
        return fmt.Errorf("listing widgets: %w", err)
    }

    for _, w := range widgets {
        if !strings.HasPrefix(w.Name, "test-acc") {
            continue
        }
        if err := conn.DeleteWidget(w.ID); err != nil {
            log.Printf("[WARN] Failed to delete widget %s: %s", w.ID, err)
        }
    }

    return nil
}

Use a consistent test name prefix (e.g., "test-acc") to identify test-created resources.

Dependencies

When resources have ordering requirements (e.g., child resources must be deleted before parents), the parent sweeper declares children as dependencies so they run first:

resource.AddTestSweepers("example_widget", &resource.Sweeper{
    Name:         "example_widget",
    Dependencies: []string{"example_widget_child"},
    F:            sweepWidgets,
})

Dependencies run before the sweeper that declares them. In this example, example_widget_child is swept first, then example_widget.

Shared Client

Create a helper to build an API client for the sweep region:

func sharedClientForRegion(region string) (any, error) {
    // Build and return a configured API client
    return NewClient(region)
}

Running Sweepers

# Run all sweepers for a region
TF_ACC=1 go test ./internal/service/example -sweep=us-east-1 -v

# Makefile target (common convention)
make sweep

Source: SKILL.md on GitHub

No alerts16d4 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides templates and design patterns for writing Terraform provider acceptance tests. It contains no security issues and adheres to standard testing practices.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4451cec. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "lifecycle-status": "active",
  "copyright": "Copyright IBM Corp. 2026",
  "version": "0.0.1"
}
  • Go
  • terraform
  • provider
  • acceptance-testing
  • plugin-framework
  • statecheck
  • plancheck
  • testing-patterns

README badge

README badge for hashicorp/agent-skills/provider-test-patterns

Terraform provider acceptance test patterns using terraform-plugin-testing with the Plugin Framework. Covers TestCase and TestStep configuration, ConfigStateChecks with custom statecheck implementations, plan checks, import testing, and scenario patterns (basic, update, disappears, validation, regression). Use when writing or reviewing provider acceptance tests.

Generated from the current SKILL.md.

Does this skill cover ephemeral resource testing?
Yes. The skill includes patterns for ephemeral resource testing with the echoprovider package and multi-step patterns, documented in the references/ephemeral.md reference file.
What assertion methods does this skill cover?
The skill prioritizes ConfigStateChecks with statecheck.StateCheck implementations (type-safe, modern approach) but also covers legacy TestCheckFunc patterns for CheckDestroy and migration scenarios. Custom StateCheck implementations for exists and disappears checks are included.
Does this cover import state verification?
Yes. The skill includes ImportState, ImportStateVerify, ImportStateVerifyIgnore, and ImportStateKind patterns for testing resource import workflows.
What plan-checking capabilities are included?
The skill covers ConfigPlanChecks with PreApply plancheck.PlanCheck composition. Full plancheck types and comparers are documented in the references/checks.md reference file.
Does this skill explain sweeper setup?
Yes. Sweeper patterns, TestMain setup, and dependency management are covered in the references/sweepers.md reference file.

Generated from the current SKILL.md. These answers refresh after source changes.