hashicorp / terraform-plugin-testing

Module for testing Terraform providers
Mozilla Public License 2.0
45 stars 12 forks source link

🧹 Enhance Terraform Sweeper documentation #378

Open oscarhermoso opened 1 month ago

oscarhermoso commented 1 month ago

Does this documentation exist?

Where would you expect to find this documentation?

Description

I would like to follow an idiomatic example of Sweepers, so I can implement them for a provider that is based on the terraform-plugin-framework package.

I'm fairly new to Go and creating Terraform providers; I have tried to follow the Terraform docs, but my sweepers do not run. (No need for you to debug my code, just trying to give some context.)

References

N/A

oscarhermoso commented 1 month ago

Update - I got my Sweeper working - but have some more feedback:

  -sweep string
        List of Regions to run available Sweepers
  -sweep-allow-failures
        Enable to allow Sweeper Tests to continue after failures
  -sweep-run string
        Comma separated list of Sweeper Tests to run

And if someone else finds has the same issue - see below my working code below...

Working code

Create a `sweeper_test.tf` ```go // sweeper_test.tf package provider import ( "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" // if your resources are in different packages, import here // _ "terraform-provider-example/internal/example" ) func TestMain(m *testing.M) { resource.TestMain(m) } ``` Extend your existing test files with an `init()` method: ```go // resource_example_test.tf package provider import ( "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestExampleResource(t *testing.T) { // ... } // init is special, no issues with multiple init functions with same name func init() { resource.AddTestSweepers("example", &resource.Sweeper{ Name: "example", F: func(_ string) error { // ... } } ``` Run sweeper with: ```sh go test -v ./... -sweep=all ```