diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 8e2bff466..f95b3a28b 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -61,6 +61,18 @@ type TestCase struct { // Steps are the apply sequences done within the context of the // same state. Each step can have its own check to verify correctness. Steps []TestStep + + // The settings below control the "ID-only refresh test." This is + // an enabled-by-default test that tests that a refresh can be + // refreshed with only an ID to result in the same attributes. + // This validates completeness of Refresh. + // + // DisableIDRefresh, if true, willl not do the id-only refresh test. + // + // IDRefreshName is the name of the resource to check. This will + // default to the first non-nil primary resource in the state. + DisableIDRefresh bool + IDRefreshName string } // TestStep is a single apply sequence of a test, done within the @@ -163,7 +175,7 @@ func Test(t TestT, c TestCase) { // If we've never checked an id-only refresh and our state isn't // empty, find the first resource and test it. - if idRefreshCheck == nil && !state.Empty() { + if !c.DisableIDRefresh && idRefreshCheck == nil && !state.Empty() { // Find the first non-nil resource in the state for _, m := range state.Modules { if len(m.Resources) > 0 { @@ -196,8 +208,10 @@ func Test(t TestT, c TestCase) { } // If we never checked an id-only refresh, it is a failure. - if !errored && len(c.Steps) > 0 && idRefreshCheck == nil { - t.Error("ID-only refresh check never ran.") + if !c.DisableIDRefresh { + if !errored && len(c.Steps) > 0 && idRefreshCheck == nil { + t.Error("ID-only refresh check never ran.") + } } // If we have a state, then run the destroy diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index ab1ba17b7..fb1624209 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -145,6 +145,47 @@ func TestTest_idRefresh(t *testing.T) { } } +func TestTest_idRefreshDisable(t *testing.T) { + mp := testProvider() + mp.DiffReturn = nil + + mp.ApplyFn = func( + info *terraform.InstanceInfo, + state *terraform.InstanceState, + diff *terraform.InstanceDiff) (*terraform.InstanceState, error) { + if !diff.Destroy { + return &terraform.InstanceState{ + ID: "foo", + }, nil + } + + return nil, nil + } + + var refreshCount int32 + mp.RefreshFn = func(*terraform.InstanceInfo, *terraform.InstanceState) (*terraform.InstanceState, error) { + atomic.AddInt32(&refreshCount, 1) + return &terraform.InstanceState{ID: "foo"}, nil + } + + mt := new(mockT) + Test(mt, TestCase{ + DisableIDRefresh: true, + Providers: map[string]terraform.ResourceProvider{ + "test": mp, + }, + Steps: []TestStep{ + TestStep{ + Config: testConfigStr, + }, + }, + }) + + if mt.failed() { + t.Fatal("test failed") + } +} + func TestTest_idRefreshFail(t *testing.T) { // Refresh count should be 3: // 1.) initial Ref/Plan/Apply