From 393c32624a3ad082da0cd111d38deb8f70333f7b Mon Sep 17 00:00:00 2001 From: Paddy Carver Date: Thu, 20 Sep 2018 10:52:36 -0700 Subject: [PATCH] Add tests to ensure clearing sub-blocks works. --- helper/schema/resource_diff_test.go | 105 ++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/helper/schema/resource_diff_test.go b/helper/schema/resource_diff_test.go index fdb8937e5..52c02a6ab 100644 --- a/helper/schema/resource_diff_test.go +++ b/helper/schema/resource_diff_test.go @@ -1028,6 +1028,111 @@ func TestClear(t *testing.T) { }, }, }, + resourceDiffTestCase{ + Name: "basic sub-block diff", + Schema: map[string]*Schema{ + "foo": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "bar": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + }, + "baz": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, + }, + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "foo.0.bar": "bar1", + "foo.0.baz": "baz1", + }, + }, + Config: testConfig(t, map[string]interface{}{ + "foo": []map[string]interface{}{ + map[string]interface{}{ + "bar": "bar2", + "baz": "baz1", + }, + }, + }), + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo.0.bar": &terraform.ResourceAttrDiff{ + Old: "bar1", + New: "bar2", + }, + }, + }, + Key: "foo.0.bar", + Expected: &terraform.InstanceDiff{Attributes: map[string]*terraform.ResourceAttrDiff{}}, + }, + resourceDiffTestCase{ + Name: "sub-block diff only partial clear", + Schema: map[string]*Schema{ + "foo": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "bar": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + }, + "baz": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, + }, + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "foo.0.bar": "bar1", + "foo.0.baz": "baz1", + }, + }, + Config: testConfig(t, map[string]interface{}{ + "foo": []map[string]interface{}{ + map[string]interface{}{ + "bar": "bar2", + "baz": "baz2", + }, + }, + }), + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo.0.bar": &terraform.ResourceAttrDiff{ + Old: "bar1", + New: "bar2", + }, + "foo.0.baz": &terraform.ResourceAttrDiff{ + Old: "baz1", + New: "baz2", + }, + }, + }, + Key: "foo.0.bar", + Expected: &terraform.InstanceDiff{Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo.0.baz": &terraform.ResourceAttrDiff{ + Old: "baz1", + New: "baz2", + }, + }}, + }, } for _, tc := range cases { t.Run(tc.Name, func(t *testing.T) {