packer: Template parsing error if name/type is not string

This commit is contained in:
Mitchell Hashimoto 2013-05-22 14:14:40 -07:00
parent f9c59c714f
commit c51223bab7
2 changed files with 29 additions and 4 deletions

View File

@ -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 {

View File

@ -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)