2013-05-22 18:39:30 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2013-05-24 00:13:18 -04:00
|
|
|
|
|
|
|
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) {
|
2013-08-31 02:21:15 -04:00
|
|
|
pA := &MockProvisioner{}
|
|
|
|
pB := &MockProvisioner{}
|
2013-05-24 00:13:18 -04:00
|
|
|
|
|
|
|
ui := testUi()
|
|
|
|
var comm Communicator = nil
|
|
|
|
var data interface{} = nil
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
hook := &ProvisionHook{
|
|
|
|
Provisioners: []Provisioner{pA, pB},
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:13:18 -04:00
|
|
|
hook.Run("foo", ui, comm, data)
|
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
if !pA.ProvCalled {
|
2013-05-24 00:13:18 -04:00
|
|
|
t.Error("provision should be called on pA")
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
if !pB.ProvCalled {
|
2013-05-24 00:13:18 -04:00
|
|
|
t.Error("provision should be called on pB")
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 00:13:40 -04:00
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
func TestProvisionHook_cancel(t *testing.T) {
|
|
|
|
var lock sync.Mutex
|
|
|
|
order := make([]string, 0, 2)
|
|
|
|
|
|
|
|
p := &MockProvisioner{
|
|
|
|
ProvFunc: func() error {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
order = append(order, "prov")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hook := &ProvisionHook{
|
|
|
|
Provisioners: []Provisioner{p},
|
|
|
|
}
|
|
|
|
|
|
|
|
finished := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
hook.Run("foo", nil, nil, nil)
|
|
|
|
close(finished)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Cancel it while it is running
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
hook.Cancel()
|
|
|
|
lock.Lock()
|
|
|
|
order = append(order, "cancel")
|
|
|
|
lock.Unlock()
|
|
|
|
|
|
|
|
// Wait
|
|
|
|
<-finished
|
|
|
|
|
|
|
|
// Verify order
|
|
|
|
if order[0] != "cancel" || order[1] != "prov" {
|
|
|
|
t.Fatalf("bad: %#v", order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:13:40 -04:00
|
|
|
// TODO(mitchellh): Test that they're run in the proper order
|