-
Notifications
You must be signed in to change notification settings - Fork 16
Description
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
- Relates Add Option to use Terraform Configuration in TestStep #153
- Ability to Pre-provision Resources for Acceptance Tests #70 and Setup/Teardown hook on
resource.Test()
andresource.TestStep
level #82 are similar, but to the best of my understanding those requests are for setup/teardown hooks which span the entire lifecycle of acceptance test execution. This request is focused more narrowly on an individual test (potentially across multiple steps, but still a single test).