From c51223bab7db4c6073967c6f38fe0c04f126f79f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 22 May 2013 14:14:40 -0700 Subject: [PATCH] packer: Template parsing error if name/type is not string --- packer/template.go | 17 +++++++++++++---- packer/template_test.go | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packer/template.go b/packer/template.go index 7b6ad5b4c..4b9d1f71c 100644 --- a/packer/template.go +++ b/packer/template.go @@ -52,7 +52,7 @@ func ParseTemplate(data []byte) (t *Template, err error) { for i, v := range rawTpl.Builders { rawType, ok := v["type"] if !ok { - errors = append(errors, fmt.Errorf("missing 'type' for builder %d", i + 1)) + errors = append(errors, fmt.Errorf("builder %d: missing 'type'", i + 1)) continue } @@ -64,9 +64,18 @@ func ParseTemplate(data []byte) (t *Template, err error) { rawName = v["type"] } - // TODO: Error checking if we can't convert - name := rawName.(string) - typeName := rawType.(string) + // Attempt to convert the name/type to strings, but error if we can't + name, ok := rawName.(string) + if !ok { + errors = append(errors, fmt.Errorf("builder %d: name must be a string", i + 1)) + continue + } + + typeName, ok := rawType.(string) + if !ok { + errors = append(errors, fmt.Errorf("builder %d: type must be a string", i + 1)) + continue + } // Check if we already have a builder with this name and error if so if _, ok := t.Builders[name]; ok { diff --git a/packer/template_test.go b/packer/template_test.go index 1a9914e70..7aaf4eb81 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -54,6 +54,22 @@ func TestParseTemplate_BuilderWithoutType(t *testing.T) { assert.NotNil(err, "should have error") } +func TestParseTemplate_BuilderWithNonStringType(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + { + "name": "my-image", + "builders": [{ + "type": 42 + }] + } + ` + + _, err := ParseTemplate([]byte(data)) + assert.NotNil(err, "should have error") +} + func TestParseTemplate_BuilderWithoutName(t *testing.T) { assert := asserts.NewTestingAsserts(t, true)