Commit f9c59c71 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Better error handling for template parsing

parent 615f993d
...@@ -48,11 +48,12 @@ func ParseTemplate(data []byte) (t *Template, err error) { ...@@ -48,11 +48,12 @@ func ParseTemplate(data []byte) (t *Template, err error) {
t.Builders = make(map[string]rawBuilderConfig) t.Builders = make(map[string]rawBuilderConfig)
t.Hooks = rawTpl.Hooks t.Hooks = rawTpl.Hooks
for _, v := range rawTpl.Builders { errors := make([]error, 0)
for i, v := range rawTpl.Builders {
rawType, ok := v["type"] rawType, ok := v["type"]
if !ok { if !ok {
// TODO: Missing type error errors = append(errors, fmt.Errorf("missing 'type' for builder %d", i + 1))
return continue
} }
// Attempt to get the name of the builder. If the "name" key // Attempt to get the name of the builder. If the "name" key
...@@ -67,17 +68,20 @@ func ParseTemplate(data []byte) (t *Template, err error) { ...@@ -67,17 +68,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
name := rawName.(string) name := rawName.(string)
typeName := rawType.(string) typeName := rawType.(string)
// Check if we already have a builder with this name and record // Check if we already have a builder with this name and error if so
// an error. if _, ok := t.Builders[name]; ok {
_, ok = t.Builders[name] errors = append(errors, fmt.Errorf("builder with name '%s' already exists", name))
if ok { continue
// TODO: We already have a builder with this name
return
} }
t.Builders[name] = rawBuilderConfig{typeName, v} t.Builders[name] = rawBuilderConfig{typeName, v}
} }
if len(errors) > 0 {
err = &MultiError{errors}
return
}
return return
} }
......
...@@ -40,6 +40,20 @@ func TestParseTemplate_Invalid(t *testing.T) { ...@@ -40,6 +40,20 @@ func TestParseTemplate_Invalid(t *testing.T) {
assert.Nil(result, "should have no result") assert.Nil(result, "should have no result")
} }
func TestParseTemplate_BuilderWithoutType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": [{}]
}
`
_, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error")
}
func TestParseTemplate_BuilderWithoutName(t *testing.T) { func TestParseTemplate_BuilderWithoutName(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
...@@ -89,6 +103,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) { ...@@ -89,6 +103,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
assert.Equal(builder.builderName, "amazon-ebs", "builder should be amazon-ebs") assert.Equal(builder.builderName, "amazon-ebs", "builder should be amazon-ebs")
} }
func TestParseTemplate_BuilderWithConflictingName(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": [
{
"name": "bob",
"type": "amazon-ebs"
},
{
"name": "bob",
"type": "foo",
}
]
}
`
_, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error")
}
func TestParseTemplate_Hooks(t *testing.T) { func TestParseTemplate_Hooks(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment