From 0c8b0bff2c81df3e0134cc54c49ed269cb42e1d1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 20 Apr 2016 11:12:30 -0700 Subject: [PATCH] helper/resource: can specify specific name to id refresh test --- helper/resource/testing.go | 8 +++++ helper/resource/testing_test.go | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index f95b3a28b1..f52b008773 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -179,6 +179,14 @@ func Test(t TestT, c TestCase) { // Find the first non-nil resource in the state for _, m := range state.Modules { if len(m.Resources) > 0 { + if c.IDRefreshName != "" { + if v, ok := m.Resources[c.IDRefreshName]; ok { + idRefreshCheck = v + } + + break + } + for _, v := range m.Resources { if v != nil && v.Primary != nil { idRefreshCheck = v diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index fb1624209e..761d01e198 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -145,6 +145,58 @@ func TestTest_idRefresh(t *testing.T) { } } +func TestTest_idRefreshCustomName(t *testing.T) { + // Refresh count should be 3: + // 1.) initial Ref/Plan/Apply + // 2.) post Ref/Plan/Apply for plan-check + // 3.) id refresh check + var expectedRefresh int32 = 3 + + 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{ + IDRefreshName: "test_instance.foo", + Providers: map[string]terraform.ResourceProvider{ + "test": mp, + }, + Steps: []TestStep{ + TestStep{ + Config: testConfigStr, + }, + }, + }) + + if mt.failed() { + t.Fatalf("test failed: %s", mt.failMessage()) + } + + // See declaration of expectedRefresh for why that number + if refreshCount != expectedRefresh { + t.Fatalf("bad refresh count: %d", refreshCount) + } +} + func TestTest_idRefreshDisable(t *testing.T) { mp := testProvider() mp.DiffReturn = nil