packer: panic if Prepare called twice on build, lock

This commit is contained in:
Mitchell Hashimoto 2013-06-18 09:37:14 -07:00
parent e851ac5d26
commit 57fef22429
2 changed files with 32 additions and 2 deletions

View File

@ -1,6 +1,9 @@
package packer
import "log"
import (
"log"
"sync"
)
// This is the key in configurations that is set to "true" when Packer
// debugging is enabled.
@ -48,6 +51,7 @@ type coreBuild struct {
provisioners []coreBuildProvisioner
debug bool
l sync.Mutex
prepareCalled bool
}
@ -66,7 +70,13 @@ func (b *coreBuild) Name() string {
// Prepare prepares the build by doing some initialization for the builder
// and any hooks. This _must_ be called prior to Run.
func (b *coreBuild) Prepare() (err error) {
// TODO: lock
b.l.Lock()
defer b.l.Unlock()
if b.prepareCalled {
panic("prepare already called")
}
b.prepareCalled = true
debugConfig := map[string]interface{}{

View File

@ -49,6 +49,26 @@ func TestBuild_Prepare(t *testing.T) {
assert.Equal(prov.prepConfigs, []interface{}{42, debugFalseConfig}, "prepare should be called with proper config")
}
func TestBuild_Prepare_Twice(t *testing.T) {
build := testBuild()
if err := build.Prepare(); err != nil {
t.Fatalf("bad error: %s", err)
}
defer func() {
p := recover()
if p == nil {
t.Fatalf("should've paniced")
}
if p.(string) != "prepare already called" {
t.Fatalf("Invalid panic: %s", p)
}
}()
build.Prepare()
}
func TestBuild_Prepare_Debug(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)