packer: Prepare provisioners as part of Build prepare

This commit is contained in:
Mitchell Hashimoto 2013-05-22 16:29:07 -07:00
parent cb91ca72ac
commit 0f57370dec
4 changed files with 28 additions and 3 deletions

View File

@ -39,12 +39,21 @@ 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(ui Ui) (err error) {
// TODO: lock
b.prepareCalled = true
// Prepare the builder
err = b.builder.Prepare(b.builderConfig)
if err != nil {
log.Printf("Build '%s' prepare failure: %s\n", b.name, err)
}
// Prepare the provisioners
// TODO: error handling
for _, coreProv := range b.provisioners {
coreProv.provisioner.Prepare(coreProv.config, ui)
}
return
}

View File

@ -31,6 +31,9 @@ func testBuild() Build {
name: "test",
builder: &TestBuilder{},
builderConfig: 42,
provisioners: []coreBuildProvisioner{
coreBuildProvisioner{&TestProvisioner{}, 42},
},
}
}
@ -65,10 +68,19 @@ func TestBuild_Run(t *testing.T) {
build.Prepare(ui)
build.Run(ui)
builder := build.(*coreBuild).builder.(*TestBuilder)
coreB := build.(*coreBuild)
// Verify builder was prepared
builder := coreB.builder.(*TestBuilder)
assert.True(builder.runCalled, "run should be called")
assert.Equal(builder.runUi, ui, "run should be called with ui")
// Verify provisioners were prepared
coreProv := coreB.provisioners[0]
prov := coreProv.provisioner.(*TestProvisioner)
assert.True(prov.prepCalled, "prepare should be called")
assert.Equal(prov.prepConfig, 42, "prepare should be called with proper config")
assert.Equal(prov.prepUi, ui, "prepare should be called with proper ui")
}
func TestBuild_RunBeforePrepare(t *testing.T) {

View File

@ -2,11 +2,15 @@ package packer
type TestProvisioner struct {
prepCalled bool
prepConfig interface{}
prepUi Ui
provCalled bool
}
func (t *TestProvisioner) Prepare(interface{}, Ui) {
func (t *TestProvisioner) Prepare(config interface{}, ui Ui) {
t.prepCalled = true
t.prepConfig = config
t.prepUi = ui
}
func (t *TestProvisioner) Provision(Ui, Communicator) {

View File

@ -12,7 +12,7 @@ var testBuildArtifact = &testArtifact{}
type testBuild struct {
nameCalled bool
prepareCalled bool
prepareUi packer.Ui
prepareUi packer.Ui
runCalled bool
runUi packer.Ui
}