mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-13 00:46:16 -06:00
helper/resource: can specify specific name to id refresh test
This commit is contained in:
parent
1a9fae6b2e
commit
0c8b0bff2c
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user