From e91f381cc4c527283279c458528e74d087fb0fb0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 1 Nov 2018 16:11:19 -0400 Subject: [PATCH] test case for optional bools in schema Booleans in the legacy form were stored as strings, and can appear as the incorrect type in the new type system. Unset fields in sets also might show up erroneously in diffs, with equal old and new values. --- builtin/providers/test/provider.go | 1 + builtin/providers/test/resource_nested.go | 61 ++++++++++ .../providers/test/resource_nested_test.go | 104 ++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 builtin/providers/test/resource_nested.go create mode 100644 builtin/providers/test/resource_nested_test.go diff --git a/builtin/providers/test/provider.go b/builtin/providers/test/provider.go index e8b6cf228..1c0fc574d 100644 --- a/builtin/providers/test/provider.go +++ b/builtin/providers/test/provider.go @@ -23,6 +23,7 @@ func Provider() terraform.ResourceProvider { "test_resource_timeout": testResourceTimeout(), "test_resource_diff_suppress": testResourceDiffSuppress(), "test_resource_force_new": testResourceForceNew(), + "test_resource_nested": testResourceNested(), }, DataSourcesMap: map[string]*schema.Resource{ "test_data_source": testDataSource(), diff --git a/builtin/providers/test/resource_nested.go b/builtin/providers/test/resource_nested.go new file mode 100644 index 000000000..54e78d7fd --- /dev/null +++ b/builtin/providers/test/resource_nested.go @@ -0,0 +1,61 @@ +package test + +import ( + "fmt" + "math/rand" + + "github.com/hashicorp/terraform/helper/schema" +) + +func testResourceNested() *schema.Resource { + return &schema.Resource{ + Create: testResourceNestedCreate, + Read: testResourceNestedRead, + Delete: testResourceNestedDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "optional": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "nested": { + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "string": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "optional": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + }, + }, + }, + }, + } +} + +func testResourceNestedCreate(d *schema.ResourceData, meta interface{}) error { + d.SetId(fmt.Sprintf("%x", rand.Int63())) + return testResourceNestedRead(d, meta) +} + +func testResourceNestedRead(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func testResourceNestedDelete(d *schema.ResourceData, meta interface{}) error { + d.SetId("") + return nil +} diff --git a/builtin/providers/test/resource_nested_test.go b/builtin/providers/test/resource_nested_test.go new file mode 100644 index 000000000..541dfa02f --- /dev/null +++ b/builtin/providers/test/resource_nested_test.go @@ -0,0 +1,104 @@ +package test + +import ( + "errors" + "strings" + "testing" + + "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" + } +} + `), + }, + }, + }) +} + +func TestResourceNested_addRemove(t *testing.T) { + var id string + checkFunc := func(s *terraform.State) error { + 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" { +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested" "foo" { + nested { + string = "val" + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested" "foo" { + optional = true + nested { + string = "val" + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested" "foo" { + nested { + string = "val" + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested" "foo" { + nested { + string = "val" + optional = true + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_nested" "foo" { +} + `), + Check: checkFunc, + }, + }, + }) +}