allow 0 and unset to be equal in count tests

This was changed in the single attribute test cases, but the AttrPair
test is used a lot for data source. As far as tests are concerned, 0 and
unset should be treated equally for flatmapped collections.
This commit is contained in:
James Bardin 2019-02-11 11:27:32 -05:00
parent 6eb7bfbdfb
commit c02f1d7256
2 changed files with 121 additions and 0 deletions

View File

@ -1148,6 +1148,21 @@ func TestCheckModuleResourceAttrPair(mpFirst []string, nameFirst string, keyFirs
func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst string, keyFirst string, isSecond *terraform.InstanceState, nameSecond string, keySecond string) error { func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst string, keyFirst string, isSecond *terraform.InstanceState, nameSecond string, keySecond string) error {
vFirst, okFirst := isFirst.Attributes[keyFirst] vFirst, okFirst := isFirst.Attributes[keyFirst]
vSecond, okSecond := isSecond.Attributes[keySecond] vSecond, okSecond := isSecond.Attributes[keySecond]
// Container count values of 0 should not be relied upon, and not reliably
// maintained by helper/schema. For the purpose of tests, consider unset and
// 0 to be equal.
if len(keyFirst) > 2 && len(keySecond) > 2 && keyFirst[len(keyFirst)-2:] == keySecond[len(keySecond)-2:] &&
(strings.HasSuffix(keyFirst, ".#") || strings.HasSuffix(keyFirst, ".%")) {
// they have the same suffix, and it is a collection count key.
if vFirst == "0" || vFirst == "" {
okFirst = false
}
if vSecond == "0" || vSecond == "" {
okSecond = false
}
}
if okFirst != okSecond { if okFirst != okSecond {
if !okFirst { if !okFirst {
return fmt.Errorf("%s: Attribute %q not set, but %q is set in %s as %q", nameFirst, keyFirst, keySecond, nameSecond, vSecond) return fmt.Errorf("%s: Attribute %q not set, but %q is set in %s as %q", nameFirst, keyFirst, keySecond, nameSecond, vSecond)

View File

@ -1326,3 +1326,109 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
}) })
} }
} }
func TestTestCheckResourceAttrPairCount(t *testing.T) {
tests := map[string]struct {
state *terraform.State
attr string
wantErr string
}{
"unset and 0 equal list": {
&terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test.a": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"a.#": "0",
},
},
},
"test.b": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{},
},
},
},
},
},
},
"a.#",
``,
},
"unset and 0 equal map": {
&terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test.a": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"a.%": "0",
},
},
},
"test.b": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{},
},
},
},
},
},
},
"a.%",
``,
},
"count equal": {
&terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test.a": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"a.%": "1",
},
},
},
"test.b": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"a.%": "1",
}},
},
},
},
},
},
"a.%",
``,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
fn := TestCheckResourceAttrPair("test.a", test.attr, "test.b", test.attr)
err := fn(test.state)
if test.wantErr != "" {
if err == nil {
t.Fatalf("succeeded; want error\nwant: %s", test.wantErr)
}
if got, want := err.Error(), test.wantErr; got != want {
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
}
return
}
if err != nil {
t.Fatalf("failed; want success\ngot: %s", err.Error())
}
})
}
}