helper/resource: Add TestStep.PreventPostDestroyRefresh

- This is to allow easier testing of data sources which read data from resources created in the same scope
This commit is contained in:
Radek Simko 2016-05-31 10:13:06 +01:00
parent 0075bd75fd
commit 84ab00d92e
2 changed files with 19 additions and 7 deletions

View File

@ -56,6 +56,10 @@ type TestCase struct {
Providers map[string]terraform.ResourceProvider
ProviderFactories map[string]terraform.ResourceProviderFactory
// PreventPostDestroyRefresh can be set to true for cases where data sources
// are tested alongside real resources
PreventPostDestroyRefresh bool
// CheckDestroy is called after the resource is finally destroyed
// to allow the tester to test that the resource is truly gone.
CheckDestroy TestCheckFunc
@ -131,6 +135,10 @@ type TestStep struct {
// looking to verify that a diff occurs
ExpectNonEmptyPlan bool
// PreventPostDestroyRefresh can be set to true for cases where data sources
// are tested alongside real resources
PreventPostDestroyRefresh bool
//---------------------------------------------------------------
// ImportState testing
//---------------------------------------------------------------
@ -283,10 +291,12 @@ func Test(t TestT, c TestCase) {
// If we have a state, then run the destroy
if state != nil {
lastStep := c.Steps[len(c.Steps)-1]
destroyStep := TestStep{
Config: c.Steps[len(c.Steps)-1].Config,
Check: c.CheckDestroy,
Destroy: true,
Config: lastStep.Config,
Check: c.CheckDestroy,
Destroy: true,
PreventPostDestroyRefresh: c.PreventPostDestroyRefresh,
}
log.Printf("[WARN] Test: Executing destroy step")

View File

@ -101,10 +101,12 @@ func testStep(
}
// And another after a Refresh.
state, err = ctx.Refresh()
if err != nil {
return state, fmt.Errorf(
"Error on follow-up refresh: %s", err)
if !step.Destroy || (step.Destroy && !step.PreventPostDestroyRefresh) {
state, err = ctx.Refresh()
if err != nil {
return state, fmt.Errorf(
"Error on follow-up refresh: %s", err)
}
}
if p, err = ctx.Plan(); err != nil {
return state, fmt.Errorf("Error on second follow-up plan: %s", err)