Commit 2d13db30 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: HookProvision errors if no communicator

parent 523a3342
...@@ -23,9 +23,11 @@ type StepProvision struct { ...@@ -23,9 +23,11 @@ type StepProvision struct {
func (s *StepProvision) Run(state multistep.StateBag) multistep.StepAction { func (s *StepProvision) Run(state multistep.StateBag) multistep.StepAction {
comm := s.Comm comm := s.Comm
if comm == nil { 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) hook := state.Get("hook").(packer.Hook)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
......
...@@ -202,7 +202,7 @@ func TestBuild_Run(t *testing.T) { ...@@ -202,7 +202,7 @@ func TestBuild_Run(t *testing.T) {
} }
// Verify provisioners run // Verify provisioners run
dispatchHook.Run(HookProvision, nil, nil, 42) dispatchHook.Run(HookProvision, nil, new(MockCommunicator), 42)
prov := build.provisioners[0].provisioner.(*MockProvisioner) prov := build.provisioners[0].provisioner.(*MockProvisioner)
if !prov.ProvCalled { if !prov.ProvCalled {
t.Fatal("should be called") t.Fatal("should be called")
......
...@@ -43,7 +43,7 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) { ...@@ -43,7 +43,7 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
} }
if h != nil { 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 return nil, err
} }
} }
......
...@@ -38,6 +38,18 @@ type ProvisionHook struct { ...@@ -38,6 +38,18 @@ type ProvisionHook struct {
// Runs the provisioners in order. // Runs the provisioners in order.
func (h *ProvisionHook) Run(name string, ui Ui, comm Communicator, data interface{}) error { 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() { defer func() {
h.lock.Lock() h.lock.Lock()
defer h.lock.Unlock() defer h.lock.Unlock()
......
...@@ -19,7 +19,7 @@ func TestProvisionHook(t *testing.T) { ...@@ -19,7 +19,7 @@ func TestProvisionHook(t *testing.T) {
pB := &MockProvisioner{} pB := &MockProvisioner{}
ui := testUi() ui := testUi()
var comm Communicator = nil var comm Communicator = new(MockCommunicator)
var data interface{} = nil var data interface{} = nil
hook := &ProvisionHook{ hook := &ProvisionHook{
...@@ -37,6 +37,24 @@ func TestProvisionHook(t *testing.T) { ...@@ -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) { func TestProvisionHook_cancel(t *testing.T) {
var lock sync.Mutex var lock sync.Mutex
order := make([]string, 0, 2) order := make([]string, 0, 2)
...@@ -59,7 +77,7 @@ func TestProvisionHook_cancel(t *testing.T) { ...@@ -59,7 +77,7 @@ func TestProvisionHook_cancel(t *testing.T) {
finished := make(chan struct{}) finished := make(chan struct{})
go func() { go func() {
hook.Run("foo", nil, nil, nil) hook.Run("foo", nil, new(MockCommunicator), nil)
close(finished) close(finished)
}() }()
...@@ -74,7 +92,7 @@ func TestProvisionHook_cancel(t *testing.T) { ...@@ -74,7 +92,7 @@ func TestProvisionHook_cancel(t *testing.T) {
<-finished <-finished
// Verify order // Verify order
if order[0] != "cancel" || order[1] != "prov" { if len(order) != 2 || order[0] != "cancel" || order[1] != "prov" {
t.Fatalf("bad: %#v", order) t.Fatalf("bad: %#v", order)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment