packer: Test that hooks are callable from builds
This commit is contained in:
parent
0f57370dec
commit
534f32067a
|
@ -63,6 +63,13 @@ func (b *coreBuild) Run(ui Ui) Artifact {
|
|||
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)
|
||||
}
|
||||
|
|
|
@ -5,32 +5,14 @@ import (
|
|||
"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 {
|
||||
return &coreBuild{
|
||||
name: "test",
|
||||
builder: &TestBuilder{},
|
||||
builderConfig: 42,
|
||||
hooks: map[string][]Hook{
|
||||
"foo": []Hook{&TestHook{}},
|
||||
},
|
||||
provisioners: []coreBuildProvisioner{
|
||||
coreBuildProvisioner{&TestProvisioner{}, 42},
|
||||
},
|
||||
|
@ -52,11 +34,21 @@ func TestBuild_Prepare(t *testing.T) {
|
|||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
build := testBuild()
|
||||
builder := build.(*coreBuild).builder.(*TestBuilder)
|
||||
ui := testUi()
|
||||
|
||||
build.Prepare(nil)
|
||||
coreB := build.(*coreBuild)
|
||||
builder := coreB.builder.(*TestBuilder)
|
||||
|
||||
build.Prepare(ui)
|
||||
assert.True(builder.prepareCalled, "prepare should be called")
|
||||
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) {
|
||||
|
@ -70,17 +62,18 @@ func TestBuild_Run(t *testing.T) {
|
|||
|
||||
coreB := build.(*coreBuild)
|
||||
|
||||
// Verify builder was prepared
|
||||
// Verify builder was run
|
||||
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")
|
||||
// Verify hooks are disapatchable
|
||||
dispatchHook := builder.runHook
|
||||
dispatchHook.Run("foo", nil, nil, 42)
|
||||
|
||||
hook := coreB.hooks["foo"][0].(*TestHook)
|
||||
assert.True(hook.runCalled, "run should be called")
|
||||
assert.Equal(hook.runData, 42, "should have correct data")
|
||||
}
|
||||
|
||||
func TestBuild_RunBeforePrepare(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
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,
|
||||
// allowing custom behavior to run at certain points along a build.
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue