packer: ProvisionHook

This commit is contained in:
Mitchell Hashimoto 2013-05-23 21:13:18 -07:00
parent 73b7d94933
commit 812722c20d
2 changed files with 44 additions and 0 deletions

View File

@ -14,3 +14,17 @@ type Provisioner interface {
// can be done.
Provision(Ui, Communicator)
}
// A Hook implementation that runs the given provisioners.
type ProvisionHook struct {
// The provisioners to run as part of the hook. These should already
// be prepared (by calling Prepare) at some earlier stage.
Provisioners []Provisioner
}
// Runs the provisioners in order.
func (h *ProvisionHook) Run(name string, ui Ui, comm Communicator, data interface{}) {
for _, p := range h.Provisioners {
p.Provision(ui, comm)
}
}

View File

@ -1,5 +1,7 @@
package packer
import "testing"
type TestProvisioner struct {
prepCalled bool
prepConfig interface{}
@ -16,3 +18,31 @@ func (t *TestProvisioner) Prepare(config interface{}, ui Ui) {
func (t *TestProvisioner) Provision(Ui, Communicator) {
t.provCalled = true
}
func TestProvisionHook_Impl(t *testing.T) {
var raw interface{}
raw = &ProvisionHook{}
if _, ok := raw.(Hook); !ok {
t.Fatalf("must be a Hook")
}
}
func TestProvisionHook(t *testing.T) {
pA := &TestProvisioner{}
pB := &TestProvisioner{}
ui := testUi()
var comm Communicator = nil
var data interface{} = nil
hook := &ProvisionHook{[]Provisioner{pA, pB}}
hook.Run("foo", ui, comm, data)
if !pA.provCalled {
t.Error("provision should be called on pA")
}
if !pB.provCalled {
t.Error("provision should be called on pB")
}
}