Skip to content

Commit

Permalink
Avoid panics when testing nested sets of varying depths
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Elmore committed Nov 27, 2020
1 parent 98ba036 commit 8e8cd50
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helper/resource/testing_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func testCheckTypeSetElemNestedAttrsInState(is *terraform.InstanceState, attrPar
// a Set/List item with nested attrs would have a flatmap address of
// at least length 3
// foo.0.name = "bar"
if len(stateKeyParts) < 3 {
if len(stateKeyParts) < 3 || len(attrParts) > len(stateKeyParts) {
continue
}
var pathMatch bool
Expand Down
99 changes: 99 additions & 0 deletions helper/resource/testing_sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,105 @@ func TestTestCheckTypeSetElemNestedAttrs(t *testing.T) {
},
},
},
{
Description: "single root TypeSet attribute single nested value match with varying depths",
ResourceAddress: "example_thing.test",
ResourceAttribute: "groups.*.groups.*.groups.*",
Values: map[string]string{
"gid": "group ID 3",
},
TerraformState: &terraform.State{
Version: 3,
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Outputs: map[string]*terraform.OutputState{},
Resources: map[string]*terraform.ResourceState{
"example_thing.test": {
Type: "example_thing",
Provider: "example",
Primary: &terraform.InstanceState{
ID: "11111",
Meta: map[string]interface{}{
"schema_version": 0,
},
Attributes: map[string]string{
"%": "2",
"id": "resource ID",
"groups.%": "2",
"groups.#": "2",
"groups.0.%": "2",
"groups.0.gid": "group ID 0",
"groups.0.groups.#": "0",
"groups.1.%": "2",
"groups.1.gid": "group ID 1",
"groups.1.groups.#": "1",
"groups.1.groups.0.%": "2",
"groups.1.groups.0.gid": "group ID 2",
"groups.1.groups.0.groups.#": "1",
"groups.1.groups.0.groups.0.%": "2",
"groups.1.groups.0.groups.0.gid": "group ID 3",
"groups.1.groups.0.groups.0.groups.#": "0",
},
},
},
},
Dependencies: []string{},
},
},
},
},
{
Description: "single root TypeSet attribute single nested value mismatch with varying depths",
ResourceAddress: "example_thing.test",
ResourceAttribute: "groups.*.groups.*.groups.*",
Values: map[string]string{
"gid": "group ID 7",
},
TerraformState: &terraform.State{
Version: 3,
Modules: []*terraform.ModuleState{
{
Path: []string{"root"},
Outputs: map[string]*terraform.OutputState{},
Resources: map[string]*terraform.ResourceState{
"example_thing.test": {
Type: "example_thing",
Provider: "example",
Primary: &terraform.InstanceState{
ID: "11111",
Meta: map[string]interface{}{
"schema_version": 0,
},
Attributes: map[string]string{
"%": "2",
"id": "resource ID",
"groups.%": "2",
"groups.#": "2",
"groups.0.%": "2",
"groups.0.gid": "group ID 0",
"groups.0.groups.#": "0",
"groups.1.%": "2",
"groups.1.gid": "group ID 1",
"groups.1.groups.#": "1",
"groups.1.groups.0.%": "2",
"groups.1.groups.0.gid": "group ID 2",
"groups.1.groups.0.groups.#": "1",
"groups.1.groups.0.groups.0.%": "2",
"groups.1.groups.0.groups.0.gid": "group ID 3",
"groups.1.groups.0.groups.0.groups.#": "0",
},
},
},
},
Dependencies: []string{},
},
},
},
ExpectedError: func(err error) bool {
return strings.Contains(err.Error(), "\"example_thing.test\" no TypeSet element \"groups.*.groups.*.groups.*\"")
},
},
{
Description: "single root TypeSet attribute single nested value mismatch",
ResourceAddress: "example_thing.test",
Expand Down

0 comments on commit 8e8cd50

Please sign in to comment.