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")
|
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 (
|
||||||
"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) {
|
||||||
assert := asserts.NewTestingAsserts(t, true)
|
assert := asserts.NewTestingAsserts(t, true)
|
||||||
|
|
||||||
build := testBuild()
|
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.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) {
|
||||||
|
|
||||||
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) {
|
||||||
|
|
|
@ -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
|
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.
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue