Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/resource: Return error with TestCheckResourceAttrPair() when comparing self #335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,14 @@ func TestCheckModuleResourceAttrPair(mpFirst []string, nameFirst string, keyFirs
}

func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst string, keyFirst string, isSecond *terraform.InstanceState, nameSecond string, keySecond string) error {
if nameFirst == nameSecond && keyFirst == keySecond {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I like the enhancement, I'm inclined to think in V1 maybe this should just be a warning like "you may be comparing an attribute with itself". We can make it an explicit error in V2.... tough one kind of a grey area on if this is a "breaking change" or a bugfix. I could be convinced either way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, would the warning ever get noticed in review/during development vs explicit error, it certainly is valuable in avoiding accidental merging of true == true when a PR is showing all tests passing.

return fmt.Errorf(
"comparing self: resource %s attribute %s",
nameFirst,
keyFirst,
)
}

vFirst, okFirst := isFirst.Attributes[keyFirst]
vSecond, okSecond := isSecond.Attributes[keySecond]

Expand Down
137 changes: 85 additions & 52 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,49 @@ func TestCheckNoResourceAttr_empty(t *testing.T) {

func TestTestCheckResourceAttrPair(t *testing.T) {
tests := map[string]struct {
state *terraform.State
wantErr string
nameFirst string
keyFirst string
nameSecond string
keySecond string
state *terraform.State
wantErr string
}{
"self": {
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.a",
keySecond: "a",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test.a": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"a": "boop",
},
},
},
"test.b": {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
"b": "boop",
},
},
},
},
},
},
},
wantErr: `comparing self: resource test.a attribute a`,
},
"exist match": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.b",
keySecond: "b",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -959,10 +997,14 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
},
},
},
``,
wantErr: ``,
},
"nonexist match": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.b",
keySecond: "b",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -981,10 +1023,14 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
},
},
},
``,
wantErr: ``,
},
"exist nonmatch": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.b",
keySecond: "b",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1007,10 +1053,14 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
},
},
},
`test.a: Attribute 'a' expected "boop", got "beep"`,
wantErr: `test.a: Attribute 'a' expected "boop", got "beep"`,
},
"inconsistent exist a": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.b",
keySecond: "b",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1031,10 +1081,14 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
},
},
},
`test.a: Attribute "a" is "beep", but "b" is not set in test.b`,
wantErr: `test.a: Attribute "a" is "beep", but "b" is not set in test.b`,
},
"inconsistent exist b": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a",
nameSecond: "test.b",
keySecond: "b",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1055,40 +1109,14 @@ func TestTestCheckResourceAttrPair(t *testing.T) {
},
},
},
`test.a: Attribute "a" not set, but "b" is set in test.b as "boop"`,
wantErr: `test.a: Attribute "a" not set, but "b" is set in test.b as "boop"`,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
fn := TestCheckResourceAttrPair("test.a", "a", "test.b", "b")
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())
}
})
}
}

func TestTestCheckResourceAttrPairCount(t *testing.T) {
tests := map[string]struct {
state *terraform.State
attr string
wantErr string
}{
"unset and 0 equal list": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a.#",
nameSecond: "test.b",
keySecond: "a.#",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1109,11 +1137,14 @@ func TestTestCheckResourceAttrPairCount(t *testing.T) {
},
},
},
"a.#",
``,
wantErr: ``,
},
"unset and 0 equal map": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a.%",
nameSecond: "test.b",
keySecond: "a.%",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1134,11 +1165,14 @@ func TestTestCheckResourceAttrPairCount(t *testing.T) {
},
},
},
"a.%",
``,
wantErr: ``,
},
"count equal": {
&terraform.State{
nameFirst: "test.a",
keyFirst: "a.%",
nameSecond: "test.b",
keySecond: "a.%",
state: &terraform.State{
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Expand All @@ -1160,14 +1194,13 @@ func TestTestCheckResourceAttrPairCount(t *testing.T) {
},
},
},
"a.%",
``,
wantErr: ``,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
fn := TestCheckResourceAttrPair("test.a", test.attr, "test.b", test.attr)
fn := TestCheckResourceAttrPair(test.nameFirst, test.keyFirst, test.nameSecond, test.keySecond)
err := fn(test.state)

if test.wantErr != "" {
Expand Down