have TestCheckResourceAttr accept missing counts
Missing containers were often erroneously kept in the state, but since the addition of the new provider shims, they can often be correctly eliminated. There are however many tests that check for a "0" count in the flatmap state when there shouldn't be a key at all. This addition looks for a container count key and "0" pair, and allows for the key to be missing. There may be some tests negatively effected by this which were legitimately checking for empty containers, but those were also not reliably detected, and there should be much fewer tests involved.
This commit is contained in:
parent
f7913bb168
commit
c63040c737
|
@ -971,7 +971,19 @@ func TestCheckModuleResourceAttr(mp []string, name string, key string, value str
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCheckResourceAttr(is *terraform.InstanceState, name string, key string, value string) error {
|
func testCheckResourceAttr(is *terraform.InstanceState, name string, key string, value string) error {
|
||||||
|
// Empty containers may be elided from the state.
|
||||||
|
// If the intent here is to check for an empty container, allow the key to
|
||||||
|
// also be non-existent.
|
||||||
|
emptyCheck := false
|
||||||
|
if value == "0" && (strings.HasSuffix(key, ".#") || strings.HasSuffix(key, ".%")) {
|
||||||
|
emptyCheck = true
|
||||||
|
}
|
||||||
|
|
||||||
if v, ok := is.Attributes[key]; !ok || v != value {
|
if v, ok := is.Attributes[key]; !ok || v != value {
|
||||||
|
if emptyCheck && !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
|
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1113,3 +1113,34 @@ resource "test_instance" "foo" {}
|
||||||
const testConfigStrProvider = `
|
const testConfigStrProvider = `
|
||||||
provider "test" {}
|
provider "test" {}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
func TestCheckResourceAttr_empty(t *testing.T) {
|
||||||
|
s := terraform.NewState()
|
||||||
|
s.AddModuleState(&terraform.ModuleState{
|
||||||
|
Path: []string{"root"},
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_resource": &terraform.ResourceState{
|
||||||
|
Primary: &terraform.InstanceState{
|
||||||
|
Attributes: map[string]string{
|
||||||
|
"empty_list.#": "0",
|
||||||
|
"empty_map.%": "0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, key := range []string{
|
||||||
|
"empty_list.#",
|
||||||
|
"empty_map.%",
|
||||||
|
"missing_list.#",
|
||||||
|
"missing_map.%",
|
||||||
|
} {
|
||||||
|
t.Run(key, func(t *testing.T) {
|
||||||
|
check := TestCheckResourceAttr("test_resource", key, "0")
|
||||||
|
if err := check(s); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue