2018-11-01 21:11:19 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-20 02:44:30 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-11-01 21:11:19 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceNested_basic(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" {
|
|
|
|
nested {
|
|
|
|
string = "val"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.#", "1",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.1877647874.string", "val",
|
|
|
|
),
|
2019-01-15 17:55:02 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "list_block.0.sub_list_block.0.bool", "false",
|
|
|
|
),
|
2019-01-10 18:24:05 +01:00
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceNested_addRemove(t *testing.T) {
|
|
|
|
var id string
|
2019-01-10 18:24:05 +01:00
|
|
|
idCheck := func(s *terraform.State) error {
|
2018-11-01 21:11:19 +01:00
|
|
|
root := s.ModuleByPath(addrs.RootModuleInstance)
|
|
|
|
res := root.Resources["test_resource_nested.foo"]
|
|
|
|
if res.Primary.ID == id {
|
|
|
|
return errors.New("expected new resource")
|
|
|
|
}
|
|
|
|
id = res.Primary.ID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckResourceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.#", "0",
|
|
|
|
),
|
|
|
|
// Checking for a count of 0 and a nonexistent count should
|
|
|
|
// now be the same operation.
|
|
|
|
resource.TestCheckNoResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.#",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
nested {
|
|
|
|
string = "val"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.1877647874.string", "val",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
optional = true
|
|
|
|
nested {
|
|
|
|
string = "val"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.1877647874.string", "val",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "optional", "true",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
nested {
|
|
|
|
string = "val"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.1877647874.string", "val",
|
|
|
|
),
|
|
|
|
resource.TestCheckNoResourceAttr(
|
|
|
|
"test_resource_nested.foo", "optional",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
nested {
|
|
|
|
string = "val"
|
|
|
|
optional = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.2994502535.string", "val",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.2994502535.optional", "true",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_nested" "foo" {
|
|
|
|
}
|
|
|
|
`),
|
2019-01-10 18:24:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
idCheck,
|
|
|
|
resource.TestCheckNoResourceAttr(
|
|
|
|
"test_resource_nested.foo", "nested.#",
|
|
|
|
),
|
|
|
|
),
|
2018-11-01 21:11:19 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-12-20 02:44:30 +01:00
|
|
|
|
|
|
|
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{
|
2019-01-10 19:08:54 +01:00
|
|
|
"nested.#": "2",
|
|
|
|
"nested.33842314.string": "a",
|
|
|
|
"nested.33842314.optional": "false",
|
|
|
|
"nested.33842314.nested_again.#": "1",
|
|
|
|
"nested.33842314.nested_again.936590934.string": "a",
|
|
|
|
"nested.140280279.string": "",
|
|
|
|
"nested.140280279.optional": "false",
|
|
|
|
"nested.140280279.nested_again.#": "0",
|
2019-01-15 17:55:02 +01:00
|
|
|
"list_block.#": "1",
|
|
|
|
"list_block.0.sub_list_block.#": "1",
|
|
|
|
"list_block.0.sub_list_block.0.bool": "false",
|
|
|
|
"list_block.0.sub_list_block.0.set.#": "0",
|
2018-12-20 02:44:30 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|