From 6a549f0548357596b439db7ade2718be9df55a25 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 18 Jun 2013 09:37:14 -0700 Subject: [PATCH] packer: panic if Prepare called twice on build, lock --- packer/build.go | 14 ++++++++++++-- packer/build_test.go | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packer/build.go b/packer/build.go index 4c78c5910..077f8759a 100644 --- a/packer/build.go +++ b/packer/build.go @@ -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{}{ diff --git a/packer/build_test.go b/packer/build_test.go index e365b6958..f77cf8031 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -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)