packer: Better error handling for template parsing
This commit is contained in:
parent
615f993de2
commit
f9c59c714f
|
@ -48,11 +48,12 @@ func ParseTemplate(data []byte) (t *Template, err error) {
|
|||
t.Builders = make(map[string]rawBuilderConfig)
|
||||
t.Hooks = rawTpl.Hooks
|
||||
|
||||
for _, v := range rawTpl.Builders {
|
||||
errors := make([]error, 0)
|
||||
for i, v := range rawTpl.Builders {
|
||||
rawType, ok := v["type"]
|
||||
if !ok {
|
||||
// TODO: Missing type error
|
||||
return
|
||||
errors = append(errors, fmt.Errorf("missing 'type' for builder %d", i + 1))
|
||||
continue
|
||||
}
|
||||
|
||||
// Attempt to get the name of the builder. If the "name" key
|
||||
|
@ -67,17 +68,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
|
|||
name := rawName.(string)
|
||||
typeName := rawType.(string)
|
||||
|
||||
// Check if we already have a builder with this name and record
|
||||
// an error.
|
||||
_, ok = t.Builders[name]
|
||||
if ok {
|
||||
// TODO: We already have a builder with this name
|
||||
return
|
||||
// Check if we already have a builder with this name and error if so
|
||||
if _, ok := t.Builders[name]; ok {
|
||||
errors = append(errors, fmt.Errorf("builder with name '%s' already exists", name))
|
||||
continue
|
||||
}
|
||||
|
||||
t.Builders[name] = rawBuilderConfig{typeName, v}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
err = &MultiError{errors}
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -40,6 +40,20 @@ func TestParseTemplate_Invalid(t *testing.T) {
|
|||
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) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
|
@ -89,6 +103,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
|
|||
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) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
|
|
Loading…
Reference in New Issue