Merge pull request #20292 from hashicorp/jbardin/sdk
allow 0 and unset to be equal in count tests
This commit is contained in:
commit
3cecacb660
|
@ -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 {
|
||||
vFirst, okFirst := isFirst.Attributes[keyFirst]
|
||||
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 {
|
||||
return fmt.Errorf("%s: Attribute %q not set, but %q is set in %s as %q", nameFirst, keyFirst, keySecond, nameSecond, vSecond)
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue