From cb3cbffb1915155ed9b5b0989b43489afb4975ff Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Thu, 30 Apr 2015 15:20:33 -0500 Subject: [PATCH] helper/schema: add test for statefunc with nested sets refs #1759 --- helper/schema/schema_test.go | 69 +++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 7ec4e5dbb..e532a81b9 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "reflect" + "strconv" "testing" "github.com/hashicorp/terraform/config" @@ -2281,6 +2282,72 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + // #59: StateFunc in nested set (#1759) + { + Schema: map[string]*Schema{ + "service_account": &Schema{ + Type: TypeList, + Optional: true, + ForceNew: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "scopes": &Schema{ + Type: TypeSet, + Required: true, + ForceNew: true, + Elem: &Schema{ + Type: TypeString, + StateFunc: func(v interface{}) string { + return v.(string) + "!" + }, + }, + Set: func(v interface{}) int { + i, err := strconv.Atoi(v.(string)) + if err != nil { + t.Fatalf("err: %s", err) + } + return i + }, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "service_account": []map[string]interface{}{ + { + "scopes": []interface{}{"123"}, + }, + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "service_account.#": &terraform.ResourceAttrDiff{ + Old: "0", + New: "1", + RequiresNew: true, + }, + "service_account.0.scopes.#": &terraform.ResourceAttrDiff{ + Old: "0", + New: "1", + RequiresNew: true, + }, + "service_account.0.scopes.123": &terraform.ResourceAttrDiff{ + Old: "", + New: "123!", + NewExtra: "123", + RequiresNew: true, + }, + }, + }, + + Err: false, + }, } for i, tc := range cases { @@ -2307,7 +2374,7 @@ func TestSchemaMap_Diff(t *testing.T) { } if !reflect.DeepEqual(tc.Diff, d) { - t.Fatalf("#%d: bad:\n\n%#v", i, d) + t.Fatalf("#%d:\n\nexpected: %#v\n\ngot:\n\n%#v", i, tc.Diff, d) } } }