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

Avoid panics when testing nested sets of varying depth #648

Merged
merged 1 commit into from
Jan 4, 2021
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
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