From b778a65a83c37ba1e7a90d055a5b9ee0c7f6b5a2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 17 Feb 2015 11:10:45 -0800 Subject: [PATCH 1/3] helper/schema: diff of zero value in state with lack of value should not diff --- helper/schema/schema.go | 3 +++ helper/schema/schema_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 81af80de5..d8b4e8166 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -810,6 +810,9 @@ func (m schemaMap) diffString( originalN = n n = schema.StateFunc(n) } + if n == nil { + n = schema.Type.Zero() + } if err := mapstructure.WeakDecode(o, &os); err != nil { return fmt.Errorf("%s: %s", k, err) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 80cff8bd6..f918e1937 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1943,6 +1943,29 @@ func TestSchemaMap_Diff(t *testing.T) { Diff: nil, Err: false, }, + + // #48 + { + Schema: map[string]*Schema{ + "port": &Schema{ + Type: TypeBool, + Optional: true, + ForceNew: true, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "port": "false", + }, + }, + + Config: map[string]interface{}{}, + + Diff: nil, + + Err: false, + }, } for i, tc := range cases { From cbcfb26ec66749a8b6100cd8fc0db9027e7207f2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 17 Feb 2015 11:12:45 -0800 Subject: [PATCH 2/3] helper/schema: add test for sets --- helper/schema/schema_test.go | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index f918e1937..91a7d4d4b 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1944,7 +1944,7 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, - // #48 + // #48 - Zero value in state shouldn't result in diff { Schema: map[string]*Schema{ "port": &Schema{ @@ -1966,6 +1966,49 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + // #42 Set - Same as #47 but for sets + { + Schema: map[string]*Schema{ + "route": &Schema{ + Type: TypeSet, + Optional: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "index": &Schema{ + Type: TypeInt, + Required: true, + }, + + "gateway": &Schema{ + Type: TypeSet, + Optional: true, + Elem: &Schema{Type: TypeInt}, + Set: func(a interface{}) int { + return a.(int) + }, + }, + }, + }, + Set: func(v interface{}) int { + m := v.(map[string]interface{}) + return m["index"].(int) + }, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "route.#": "0", + }, + }, + + Config: map[string]interface{}{}, + + Diff: nil, + + Err: false, + }, } for i, tc := range cases { From c22ba7d3a8383435d4c70ca02ab0a976fd7a558b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 17 Feb 2015 11:14:04 -0800 Subject: [PATCH 3/3] helper/schema: fix test index --- helper/schema/schema_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 91a7d4d4b..bb32374f3 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1967,7 +1967,7 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, - // #42 Set - Same as #47 but for sets + // #49 Set - Same as #47 but for sets { Schema: map[string]*Schema{ "route": &Schema{