From 2d13db300c493589c63cc5732b0eeaa52ede6a9a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Jun 2015 10:26:46 -0700 Subject: [PATCH] packer: HookProvision errors if no communicator --- common/step_provision.go | 6 ++++-- packer/build_test.go | 2 +- packer/builder_mock.go | 2 +- packer/provisioner.go | 12 ++++++++++++ packer/provisioner_test.go | 24 +++++++++++++++++++++--- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/common/step_provision.go b/common/step_provision.go index ae06f1b0c..f40cfd896 100644 --- a/common/step_provision.go +++ b/common/step_provision.go @@ -23,9 +23,11 @@ type StepProvision struct { func (s *StepProvision) Run(state multistep.StateBag) multistep.StepAction { comm := s.Comm if comm == nil { - comm = state.Get("communicator").(packer.Communicator) + raw, ok := state.Get("communicator").(packer.Communicator) + if ok { + comm = raw.(packer.Communicator) + } } - hook := state.Get("hook").(packer.Hook) ui := state.Get("ui").(packer.Ui) diff --git a/packer/build_test.go b/packer/build_test.go index b183fb95a..e29318972 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -202,7 +202,7 @@ func TestBuild_Run(t *testing.T) { } // Verify provisioners run - dispatchHook.Run(HookProvision, nil, nil, 42) + dispatchHook.Run(HookProvision, nil, new(MockCommunicator), 42) prov := build.provisioners[0].provisioner.(*MockProvisioner) if !prov.ProvCalled { t.Fatal("should be called") diff --git a/packer/builder_mock.go b/packer/builder_mock.go index 9cb016963..d8fd98e13 100644 --- a/packer/builder_mock.go +++ b/packer/builder_mock.go @@ -43,7 +43,7 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) { } if h != nil { - if err := h.Run(HookProvision, ui, nil, nil); err != nil { + if err := h.Run(HookProvision, ui, new(MockCommunicator), nil); err != nil { return nil, err } } diff --git a/packer/provisioner.go b/packer/provisioner.go index d28d1371a..f4f3fce11 100644 --- a/packer/provisioner.go +++ b/packer/provisioner.go @@ -38,6 +38,18 @@ type ProvisionHook struct { // Runs the provisioners in order. func (h *ProvisionHook) Run(name string, ui Ui, comm Communicator, data interface{}) error { + // Shortcut + if len(h.Provisioners) == 0 { + return nil + } + + if comm == nil { + return fmt.Errorf( + "No communicator found for provisioners! This is usually because the\n" + + "`communicator` config was set to \"none\". If you have any provisioners\n" + + "then a communicator is required. Please fix this to continue.") + } + defer func() { h.lock.Lock() defer h.lock.Unlock() diff --git a/packer/provisioner_test.go b/packer/provisioner_test.go index 5eeebb4a3..7251d6f05 100644 --- a/packer/provisioner_test.go +++ b/packer/provisioner_test.go @@ -19,7 +19,7 @@ func TestProvisionHook(t *testing.T) { pB := &MockProvisioner{} ui := testUi() - var comm Communicator = nil + var comm Communicator = new(MockCommunicator) var data interface{} = nil hook := &ProvisionHook{ @@ -37,6 +37,24 @@ func TestProvisionHook(t *testing.T) { } } +func TestProvisionHook_nilComm(t *testing.T) { + pA := &MockProvisioner{} + pB := &MockProvisioner{} + + ui := testUi() + var comm Communicator = nil + var data interface{} = nil + + hook := &ProvisionHook{ + Provisioners: []Provisioner{pA, pB}, + } + + err := hook.Run("foo", ui, comm, data) + if err == nil { + t.Fatal("should error") + } +} + func TestProvisionHook_cancel(t *testing.T) { var lock sync.Mutex order := make([]string, 0, 2) @@ -59,7 +77,7 @@ func TestProvisionHook_cancel(t *testing.T) { finished := make(chan struct{}) go func() { - hook.Run("foo", nil, nil, nil) + hook.Run("foo", nil, new(MockCommunicator), nil) close(finished) }() @@ -74,7 +92,7 @@ func TestProvisionHook_cancel(t *testing.T) { <-finished // Verify order - if order[0] != "cancel" || order[1] != "prov" { + if len(order) != 2 || order[0] != "cancel" || order[1] != "prov" { t.Fatalf("bad: %#v", order) } }