provider/test: Test for nested dynamic blocks

This is a HCL feature rather than a Terraform feature really, but we want
to make sure it keeps working consistently in future versions of Terraform
so this is a Terraform-flavored test for the block expansion behavior.

In particular, it tests that a nested dynamic block can access the parent
iterator, so that we won't regress #19543 in future.
This commit is contained in:
Martin Atkins 2018-12-19 17:44:30 -08:00
parent a3f6e67b7d
commit 364d3ffc4a
2 changed files with 67 additions and 0 deletions

View File

@ -39,6 +39,20 @@ func testResourceNested() *schema.Resource {
Optional: true,
ForceNew: true,
},
"nested_again": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"string": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
},
},
},

View File

@ -5,6 +5,7 @@ import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
@ -102,3 +103,55 @@ resource "test_resource_nested" "foo" {
},
})
}
func TestResourceNested_dynamic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
dynamic "nested" {
for_each = [["a"], []]
content {
string = join(",", nested.value)
optional = false
dynamic "nested_again" {
for_each = nested.value
content {
string = nested_again.value
}
}
}
}
}
`),
Check: func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["test_resource_nested.foo"]
if !ok {
return errors.New("missing resource in state")
}
got := rs.Primary.Attributes
want := map[string]string{
"nested.#": "2",
"nested.0.string": "a",
"nested.0.optional": "false",
"nested.0.nested_again.#": "1",
"nested.0.nested_again.0.string": "a",
"nested.1.string": "",
"nested.1.optional": "false",
}
delete(got, "id") // it's random, so not useful for testing
if !cmp.Equal(got, want) {
return errors.New("wrong result\n" + cmp.Diff(want, got))
}
return nil
},
},
},
})
}