@@ -84,6 +84,27 @@ func AddTestSweepers(name string, s *Sweeper) {
8484 sweeperFuncs [name ] = s
8585}
8686
87+ // TestMain adds sweeper functionality to the "go test" command, otherwise
88+ // tests are executed as normal. Most provider acceptance tests are written
89+ // using the Test() function of this package, which imposes its own
90+ // requirements and Terraform CLI behavior. Refer to that function's
91+ // documentation for additional details.
92+ //
93+ // Sweepers enable infrastructure cleanup functions to be included with
94+ // resource definitions, typically so developers can remove all resources of
95+ // that resource type from testing infrastructure in case of failures that
96+ // prevented the normal resource destruction behavior of acceptance tests.
97+ // Use the AddTestSweepers() function to configure available sweepers.
98+ //
99+ // Sweeper flags added to the "go test" command:
100+ //
101+ // -sweep: Comma-separated list of locations/regions to run available sweepers.
102+ // -sweep-allow-failues: Enable to allow other sweepers to run after failures.
103+ // -sweep-run: Comma-separated list of resource type sweepers to run. Defaults
104+ // to all sweepers.
105+ //
106+ // Refer to the Env prefixed constants for environment variables that further
107+ // control testing functionality.
87108func TestMain (m interface {
88109 Run () int
89110}) {
@@ -252,7 +273,8 @@ func runSweeperWithRegion(region string, s *Sweeper, sweepers map[string]*Sweepe
252273 return runE
253274}
254275
255- const TestEnvVar = "TF_ACC"
276+ // Deprecated: Use EnvTfAcc instead.
277+ const TestEnvVar = EnvTfAcc
256278
257279// TestCheckFunc is the callback type used with acceptance tests to check
258280// the state of a resource. The state passed in is the latest state known,
@@ -275,6 +297,9 @@ type ErrorCheckFunc func(error) error
275297//
276298// When the destroy plan is executed, the config from the last TestStep
277299// is used to plan it.
300+ //
301+ // Refer to the Env prefixed constants for environment variables that further
302+ // control testing functionality.
278303type TestCase struct {
279304 // IsUnitTest allows a test to run regardless of the TF_ACC
280305 // environment variable. This should be used with care - only for
@@ -376,6 +401,9 @@ type ExternalProvider struct {
376401// Multiple TestSteps can be sequenced in a Test to allow testing
377402// potentially complex update logic. In general, simply create/destroy
378403// tests will only need one step.
404+ //
405+ // Refer to the Env prefixed constants for environment variables that further
406+ // control testing functionality.
379407type TestStep struct {
380408 // ResourceName should be set to the name of the resource
381409 // that is being tested. Example: "aws_instance.foo". Various test
@@ -508,11 +536,13 @@ type TestStep struct {
508536}
509537
510538// ParallelTest performs an acceptance test on a resource, allowing concurrency
511- // with other ParallelTest.
539+ // with other ParallelTest. The number of concurrent tests is controlled by the
540+ // "go test" command -parallel flag.
512541//
513542// Tests will fail if they do not properly handle conditions to allow multiple
514543// tests to occur against the same resource or service (e.g. random naming).
515- // All other requirements of the Test function also apply to this function.
544+ //
545+ // Test() function requirements and documentation also apply to this function.
516546func ParallelTest (t testing.T , c TestCase ) {
517547 t .Helper ()
518548 t .Parallel ()
@@ -529,16 +559,37 @@ func ParallelTest(t testing.T, c TestCase) {
529559// the "-test.v" flag) is set. Because some acceptance tests take quite
530560// long, we require the verbose flag so users are able to see progress
531561// output.
562+ //
563+ // Use the ParallelTest() function to automatically set (*testing.T).Parallel()
564+ // to enable testing concurrency. Use the UnitTest() function to automatically
565+ // set the TestCase type IsUnitTest field.
566+ //
567+ // This function will automatically find or install Terraform CLI into a
568+ // temporary directory, based on the following behavior:
569+ //
570+ // - If the TF_ACC_TERRAFORM_PATH environment variable is set, that Terraform
571+ // CLI binary is used if found and executable. If not found or executable,
572+ // an error will be returned unless the TF_ACC_TERRAFORM_VERSION environment
573+ // variable is also set.
574+ // - If the TF_ACC_TERRAFORM_VERSION environment variable is set, install and
575+ // use that Terraform CLI version.
576+ // - If both the TF_ACC_TERRAFORM_PATH and TF_ACC_TERRAFORM_VERSION environment
577+ // variables are unset, perform a lookup for the Terraform CLI binary based
578+ // on the operating system PATH. If not found, the latest available Terraform
579+ // CLI binary is installed.
580+ //
581+ // Refer to the Env prefixed constants for additional details about these
582+ // environment variables, and others, that control testing functionality.
532583func Test (t testing.T , c TestCase ) {
533584 t .Helper ()
534585
535586 // We only run acceptance tests if an env var is set because they're
536587 // slow and generally require some outside configuration. You can opt out
537588 // of this with OverrideEnvVar on individual TestCases.
538- if os .Getenv (TestEnvVar ) == "" && ! c .IsUnitTest {
589+ if os .Getenv (EnvTfAcc ) == "" && ! c .IsUnitTest {
539590 t .Skip (fmt .Sprintf (
540591 "Acceptance tests skipped unless env '%s' set" ,
541- TestEnvVar ))
592+ EnvTfAcc ))
542593 return
543594 }
544595
@@ -621,6 +672,8 @@ func testProviderConfig(c TestCase) (string, error) {
621672// UnitTest is a helper to force the acceptance testing harness to run in the
622673// normal unit test suite. This should only be used for resource that don't
623674// have any external dependencies.
675+ //
676+ // Test() function requirements and documentation also apply to this function.
624677func UnitTest (t testing.T , c TestCase ) {
625678 t .Helper ()
626679
0 commit comments