packer: panic if Prepare called twice on build, lock
This commit is contained in:
parent
e851ac5d26
commit
57fef22429
|
@ -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{}{
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue