packer: Panic if provisioners and no provisioner function

This commit is contained in:
Mitchell Hashimoto 2013-05-22 17:05:36 -07:00
parent 3d0dde03e6
commit e69e74b2dc
2 changed files with 67 additions and 0 deletions

View File

@ -162,6 +162,10 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
panic("no builder function") panic("no builder function")
} }
if len(t.Provisioners) > 0 && components.Provisioner == nil {
panic("no provisioner function")
}
builder, err := components.Builder(builderConfig.builderType) builder, err := components.Builder(builderConfig.builderType)
if err != nil { if err != nil {
return return

View File

@ -327,6 +327,69 @@ func TestTemplate_Build_NilBuilderFunc(t *testing.T) {
template.Build("test1", &ComponentFinder{}) template.Build("test1", &ComponentFinder{})
} }
func TestTemplate_Build_NilProvisionerFunc(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": [
{
"name": "test1",
"type": "test-builder"
}
],
"provisioners": [
{
"type": "test-prov"
}
]
}
`
template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error")
defer func() {
p := recover()
assert.NotNil(p, "should panic")
if p != nil {
assert.Equal(p.(string), "no provisioner function", "right panic")
}
}()
template.Build("test1", &ComponentFinder{
Builder: func(string) (Builder, error) { return nil, nil },
})
}
func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": [
{
"name": "test1",
"type": "test-builder"
}
],
"provisioners": []
}
`
template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error")
template.Build("test1", &ComponentFinder{
Builder: func(string) (Builder, error) { return nil, nil },
})
}
func TestTemplate_Build(t *testing.T) { func TestTemplate_Build(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)