hashicorp / hashicorp/terraform-plugin-testing
Enhancement: Allow composition of config directories
- Dominant language
- Go
- Stars
- 68
- Forks
- 22
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 1
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`](https://github.com/hashicorp/terraform-provider-aws/blob/720fda7f1bc49358a160633cd5f4b3b9d6ffa53a/internal/acctest/acctest.go#L1902-L1911)) 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:
```go
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.
```go
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.
```go
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 #153
- #70 and #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).
Contributor guide
Assessment
This issue has not been assessed yet.