helper/resource: allow tests to opt out of temporary file cleanup

This is rarely needed, but sometimes tests need to create temporary files as part of their operation. This should be used sparingly, since it prevents the pro-active cleanup of the temporary working directory.
This commit is contained in:
Stuart Auld 2018-02-17 06:24:01 +11:00 committed by Martin Atkins
parent 4f833d5682
commit df9446f490
1 changed files with 13 additions and 4 deletions

View File

@ -310,6 +310,11 @@ type TestStep struct {
// no-op plans
PlanOnly bool
// PreventDiskCleanup can be set to true for testing terraform modules which
// require access to disk at runtime. Note that this will leave files in the
// temp folder
PreventDiskCleanup bool
// PreventPostDestroyRefresh can be set to true for cases where data sources
// are tested alongside real resources
PreventPostDestroyRefresh bool
@ -564,6 +569,7 @@ func Test(t TestT, c TestCase) {
Config: lastStep.Config,
Check: c.CheckDestroy,
Destroy: true,
PreventDiskCleanup: lastStep.PreventDiskCleanup,
PreventPostDestroyRefresh: c.PreventPostDestroyRefresh,
}
@ -730,9 +736,7 @@ func testIDOnlyRefresh(c TestCase, opts terraform.ContextOpts, step TestStep, r
return nil
}
func testModule(
opts terraform.ContextOpts,
step TestStep) (*module.Tree, error) {
func testModule(opts terraform.ContextOpts, step TestStep) (*module.Tree, error) {
if step.PreConfig != nil {
step.PreConfig()
}
@ -742,7 +746,12 @@ func testModule(
return nil, fmt.Errorf(
"Error creating temporary directory for config: %s", err)
}
defer os.RemoveAll(cfgPath)
if step.PreventDiskCleanup {
log.Printf("[INFO] Skipping defer os.RemoveAll call")
} else {
defer os.RemoveAll(cfgPath)
}
// Write the configuration
cfgF, err := os.Create(filepath.Join(cfgPath, "main.tf"))