Commit 0f57370d authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Prepare provisioners as part of Build prepare

parent cb91ca72
...@@ -39,12 +39,21 @@ func (b *coreBuild) Name() string { ...@@ -39,12 +39,21 @@ 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(ui Ui) (err error) { func (b *coreBuild) Prepare(ui Ui) (err error) {
// TODO: lock
b.prepareCalled = true b.prepareCalled = true
// Prepare the builder
err = b.builder.Prepare(b.builderConfig) err = b.builder.Prepare(b.builderConfig)
if err != nil { if err != nil {
log.Printf("Build '%s' prepare failure: %s\n", b.name, err) 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 return
} }
......
...@@ -31,6 +31,9 @@ func testBuild() Build { ...@@ -31,6 +31,9 @@ func testBuild() Build {
name: "test", name: "test",
builder: &TestBuilder{}, builder: &TestBuilder{},
builderConfig: 42, builderConfig: 42,
provisioners: []coreBuildProvisioner{
coreBuildProvisioner{&TestProvisioner{}, 42},
},
} }
} }
...@@ -65,10 +68,19 @@ func TestBuild_Run(t *testing.T) { ...@@ -65,10 +68,19 @@ func TestBuild_Run(t *testing.T) {
build.Prepare(ui) build.Prepare(ui)
build.Run(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.True(builder.runCalled, "run should be called")
assert.Equal(builder.runUi, ui, "run should be called with ui") 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) { func TestBuild_RunBeforePrepare(t *testing.T) {
......
...@@ -2,11 +2,15 @@ package packer ...@@ -2,11 +2,15 @@ package packer
type TestProvisioner struct { type TestProvisioner struct {
prepCalled bool prepCalled bool
prepConfig interface{}
prepUi Ui
provCalled bool provCalled bool
} }
func (t *TestProvisioner) Prepare(interface{}, Ui) { func (t *TestProvisioner) Prepare(config interface{}, ui Ui) {
t.prepCalled = true t.prepCalled = true
t.prepConfig = config
t.prepUi = ui
} }
func (t *TestProvisioner) Provision(Ui, Communicator) { func (t *TestProvisioner) Provision(Ui, Communicator) {
......
...@@ -12,7 +12,7 @@ var testBuildArtifact = &testArtifact{} ...@@ -12,7 +12,7 @@ var testBuildArtifact = &testArtifact{}
type testBuild struct { type testBuild struct {
nameCalled bool nameCalled bool
prepareCalled bool prepareCalled bool
prepareUi packer.Ui prepareUi packer.Ui
runCalled bool runCalled bool
runUi packer.Ui runUi packer.Ui
} }
......
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