From c02f1d72569ee72d36b213d5e88d36bfd8bbdc73 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 11 Feb 2019 11:27:32 -0500 Subject: [PATCH] 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. --- helper/resource/testing.go | 15 +++++ helper/resource/testing_test.go | 106 ++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index d7289099c5..aa7454d472 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -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) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index bb8435ad69..1c358f663c 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -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()) + } + }) + } +}