helper/resource: can disable ID refresh check
This commit is contained in:
parent
35f4201b9e
commit
1a9fae6b2e
|
@ -61,6 +61,18 @@ type TestCase struct {
|
||||||
// Steps are the apply sequences done within the context of the
|
// Steps are the apply sequences done within the context of the
|
||||||
// same state. Each step can have its own check to verify correctness.
|
// same state. Each step can have its own check to verify correctness.
|
||||||
Steps []TestStep
|
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
|
// 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
|
// If we've never checked an id-only refresh and our state isn't
|
||||||
// empty, find the first resource and test it.
|
// 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
|
// Find the first non-nil resource in the state
|
||||||
for _, m := range state.Modules {
|
for _, m := range state.Modules {
|
||||||
if len(m.Resources) > 0 {
|
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 we never checked an id-only refresh, it is a failure.
|
||||||
if !errored && len(c.Steps) > 0 && idRefreshCheck == nil {
|
if !c.DisableIDRefresh {
|
||||||
t.Error("ID-only refresh check never ran.")
|
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
|
// If we have a state, then run the destroy
|
||||||
|
|
|
@ -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) {
|
func TestTest_idRefreshFail(t *testing.T) {
|
||||||
// Refresh count should be 3:
|
// Refresh count should be 3:
|
||||||
// 1.) initial Ref/Plan/Apply
|
// 1.) initial Ref/Plan/Apply
|
||||||
|
|
Loading…
Reference in New Issue