Commit 534f3206 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Test that hooks are callable from builds

parent 0f57370d
...@@ -63,6 +63,13 @@ func (b *coreBuild) Run(ui Ui) Artifact { ...@@ -63,6 +63,13 @@ func (b *coreBuild) Run(ui Ui) Artifact {
panic("Prepare must be called first") panic("Prepare must be called first")
} }
hook := &DispatchHook{b.hooks} // Copy the hooks
hooks := make(map[string][]Hook)
for hookName, hookList := range b.hooks {
hooks[hookName] = make([]Hook, len(hookList))
copy(hooks[hookName], hookList)
}
hook := &DispatchHook{hooks}
return b.builder.Run(ui, hook) return b.builder.Run(ui, hook)
} }
...@@ -5,32 +5,14 @@ import ( ...@@ -5,32 +5,14 @@ import (
"testing" "testing"
) )
type TestBuilder struct {
prepareCalled bool
prepareConfig interface{}
runCalled bool
runHook Hook
runUi Ui
}
func (tb *TestBuilder) Prepare(config interface{}) error {
tb.prepareCalled = true
tb.prepareConfig = config
return nil
}
func (tb *TestBuilder) Run(ui Ui, h Hook) Artifact {
tb.runCalled = true
tb.runHook = h
tb.runUi = ui
return nil
}
func testBuild() Build { func testBuild() Build {
return &coreBuild{ return &coreBuild{
name: "test", name: "test",
builder: &TestBuilder{}, builder: &TestBuilder{},
builderConfig: 42, builderConfig: 42,
hooks: map[string][]Hook{
"foo": []Hook{&TestHook{}},
},
provisioners: []coreBuildProvisioner{ provisioners: []coreBuildProvisioner{
coreBuildProvisioner{&TestProvisioner{}, 42}, coreBuildProvisioner{&TestProvisioner{}, 42},
}, },
...@@ -52,11 +34,21 @@ func TestBuild_Prepare(t *testing.T) { ...@@ -52,11 +34,21 @@ func TestBuild_Prepare(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
build := testBuild() build := testBuild()
builder := build.(*coreBuild).builder.(*TestBuilder) ui := testUi()
coreB := build.(*coreBuild)
builder := coreB.builder.(*TestBuilder)
build.Prepare(nil) build.Prepare(ui)
assert.True(builder.prepareCalled, "prepare should be called") assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, 42, "prepare config should be 42") assert.Equal(builder.prepareConfig, 42, "prepare config should be 42")
// 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_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
...@@ -70,17 +62,18 @@ func TestBuild_Run(t *testing.T) { ...@@ -70,17 +62,18 @@ func TestBuild_Run(t *testing.T) {
coreB := build.(*coreBuild) coreB := build.(*coreBuild)
// Verify builder was prepared // Verify builder was run
builder := coreB.builder.(*TestBuilder) 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 // Verify hooks are disapatchable
coreProv := coreB.provisioners[0] dispatchHook := builder.runHook
prov := coreProv.provisioner.(*TestProvisioner) dispatchHook.Run("foo", nil, nil, 42)
assert.True(prov.prepCalled, "prepare should be called")
assert.Equal(prov.prepConfig, 42, "prepare should be called with proper config") hook := coreB.hooks["foo"][0].(*TestHook)
assert.Equal(prov.prepUi, ui, "prepare should be called with proper ui") assert.True(hook.runCalled, "run should be called")
assert.Equal(hook.runData, 42, "should have correct data")
} }
func TestBuild_RunBeforePrepare(t *testing.T) { func TestBuild_RunBeforePrepare(t *testing.T) {
......
package packer
type TestBuilder struct {
prepareCalled bool
prepareConfig interface{}
runCalled bool
runHook Hook
runUi Ui
}
func (tb *TestBuilder) Prepare(config interface{}) error {
tb.prepareCalled = true
tb.prepareConfig = config
return nil
}
func (tb *TestBuilder) Run(ui Ui, h Hook) Artifact {
tb.runCalled = true
tb.runHook = h
tb.runUi = ui
return nil
}
package packer package packer
// This is the hook that should be fired for provisioners to run.
const HookProvision = "packer_provision"
// A Hook is used to hook into an arbitrarily named location in a build, // A Hook is used to hook into an arbitrarily named location in a build,
// allowing custom behavior to run at certain points along a build. // allowing custom behavior to run at certain points along a build.
// //
......
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