Merge pull request #2251 from mitchellh/b-warn-prov

packer: HookProvision errors if no communicator
This commit is contained in:
Mitchell Hashimoto 2015-06-16 09:34:35 -07:00
commit 95d4b845ce
5 changed files with 39 additions and 7 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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
} }
} }

View File

@ -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()

View File

@ -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) {
} }
} }
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) {
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) {
<-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)
} }
} }