From 660dc68a862f3b56c433505b545e19f2e4840ec2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Aug 2014 10:39:40 -0700 Subject: [PATCH] helper/schema: more tests --- helper/schema/schema.go | 5 +++ helper/schema/schema_test.go | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 5c0a238ce..46a633dde 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -143,6 +143,11 @@ func (m schemaMap) diffList( return fmt.Errorf("%s: must be a list", k) } + // If this field is required, then it must also be non-empty + if len(vs) == 0 && schema.Required { + return fmt.Errorf("%s: required field is not set", k) + } + // Diff the count no matter what countSchema := &Schema{ Type: TypeInt, diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 3ad537df2..f1893d7d9 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -355,6 +355,87 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + { + Schema: map[string]*Schema{ + "ingress": &Schema{ + Type: TypeList, + Required: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "from": &Schema{ + Type: TypeInt, + Required: true, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{}, + + Diff: nil, + + Err: true, + }, + + { + Schema: map[string]*Schema{ + "ingress": &Schema{ + Type: TypeList, + Required: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "from": &Schema{ + Type: TypeInt, + Required: true, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "ingress": []interface{}{}, + }, + + Diff: nil, + + Err: true, + }, + + { + Schema: map[string]*Schema{ + "ingress": &Schema{ + Type: TypeList, + Required: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "from": &Schema{ + Type: TypeInt, + Required: true, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "ingress": []interface{}{ + map[string]interface{}{}, + }, + }, + + Diff: nil, + + Err: true, + }, } for i, tc := range cases {