Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow composition of config directories #383

Open
jar-b opened this issue Oct 31, 2024 · 0 comments
Open

Enhancement: Allow composition of config directories #383

jar-b opened this issue Oct 31, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jar-b
Copy link
Member

jar-b commented Oct 31, 2024

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

@jar-b jar-b added the enhancement New feature or request label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant