Skip to content

Enhancement: Allow composition of config directories #383

@jar-b

Description

@jar-b

terraform-plugin-testing version

github.com/hashicorp/terraform-plugin-testing v1.10.0

Use cases

In the Terraform AWS provider, we frequently have acceptance tests which share common setup infrastructure across tests. To avoid repeating the setup, we have a helper function (acctest.ConfigCompose) which composes a variable number of configuration strings into a single string which is then passed to the TestStep.Config argument. The setup is roughly:

func TestAccSomeResource_basic(t *testing.T) {
	resource.ParallelTest(t, resource.TestCase{
		// Other setup
		Steps: []resource.TestStep{
			{
				Config: testAccSomeResource_basic(rName),
				// Other checks
			},

		},
	})
}


func testAccSomeResourceConfigBase(rName string) string {
	return fmt.Sprintf(`
data "aws_other_resource" "test" {
  name = %[1]q
}
`, rName)
}

func testAccSomeResourceConfig_basic(rName string) string {
	return acctest.ConfigCompose(
		testAccSomeResourceConfigBase(rName),
		fmt.Sprintf(`
resource "aws_some_resource" "test" {
  name      = %[1]q
  other_arn = aws_other_resource.test.arn
}
`, rName))
}

// Composition is repeated for additional test configurations

As we are investigating adoption of the new ConfigDirectory test step option, we're finding that migrating from the pattern above requires copying the "setup" configuration into the testdata subdirectory for each test. If we need to make a change to the setup, we now have to apply that change several times (each testdata subdirectory), rather than in a single location.

Attempted solutions

  • Copying "setup" configuration into each testdata directory

Proposal

A helper function to compose directories would allow us to replicate this pattern while gaining the benefits of ConfigDirectory/ConfigFile over HCL strings embedded within our Go source code.

func TestAccSomeResource_basic(t *testing.T) {
	resource.ParallelTest(t, resource.TestCase{
		// Other setup
		Steps: []resource.TestStep{
			{
				ConfigDirecotory: config.ComposeDirectories(,
				        config.StaticDirectory("testdata/setup"),
				        config.TestNameDirectory(),
				),
				// Other checks
			},

		},
	})
}

Composing files may also be a viable option which avoids complexities such as file name collisions across directories.

func TestAccSomeResource_basic(t *testing.T) {
	resource.ParallelTest(t, resource.TestCase{
		// Other setup
		Steps: []resource.TestStep{
			{
				ConfigFile: config.ComposeFiles(,
				        config.StaticFile("testdata/setup/base.tf"),
				        config.TestNameFile("main.tf"),
				),
				// Other checks
			},

		},
	})
}

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions