Commit 57fef224 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: panic if Prepare called twice on build, lock

parent e851ac5d
package packer package packer
import "log" import (
"log"
"sync"
)
// This is the key in configurations that is set to "true" when Packer // This is the key in configurations that is set to "true" when Packer
// debugging is enabled. // debugging is enabled.
...@@ -48,6 +51,7 @@ type coreBuild struct { ...@@ -48,6 +51,7 @@ type coreBuild struct {
provisioners []coreBuildProvisioner provisioners []coreBuildProvisioner
debug bool debug bool
l sync.Mutex
prepareCalled bool prepareCalled bool
} }
...@@ -66,7 +70,13 @@ func (b *coreBuild) Name() string { ...@@ -66,7 +70,13 @@ func (b *coreBuild) Name() string {
// Prepare prepares the build by doing some initialization for the builder // Prepare prepares the build by doing some initialization for the builder
// and any hooks. This _must_ be called prior to Run. // and any hooks. This _must_ be called prior to Run.
func (b *coreBuild) Prepare() (err error) { 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 b.prepareCalled = true
debugConfig := map[string]interface{}{ debugConfig := map[string]interface{}{
......
...@@ -49,6 +49,26 @@ func TestBuild_Prepare(t *testing.T) { ...@@ -49,6 +49,26 @@ func TestBuild_Prepare(t *testing.T) {
assert.Equal(prov.prepConfigs, []interface{}{42, debugFalseConfig}, "prepare should be called with proper config") 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) { func TestBuild_Prepare_Debug(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment