diff --git a/packer/provisioner.go b/packer/provisioner.go index 375524c71..07a2052f0 100644 --- a/packer/provisioner.go +++ b/packer/provisioner.go @@ -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) + } +} diff --git a/packer/provisioner_test.go b/packer/provisioner_test.go index 75c497508..1188dba51 100644 --- a/packer/provisioner_test.go +++ b/packer/provisioner_test.go @@ -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") + } +}