From 84ab00d92e2e91b3b6a09969d2d9d648ca6a551f Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 31 May 2016 10:13:06 +0100 Subject: [PATCH] helper/resource: Add TestStep.PreventPostDestroyRefresh - This is to allow easier testing of data sources which read data from resources created in the same scope --- helper/resource/testing.go | 16 +++++++++++++--- helper/resource/testing_config.go | 10 ++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 619a6747b..a394d6409 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -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") diff --git a/helper/resource/testing_config.go b/helper/resource/testing_config.go index 03f417af6..b49fdc794 100644 --- a/helper/resource/testing_config.go +++ b/helper/resource/testing_config.go @@ -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)