2013-05-22 18:39:30 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2013-08-31 02:39:29 -04:00
|
|
|
"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()
|
2015-06-15 13:26:46 -04:00
|
|
|
var comm Communicator = new(MockCommunicator)
|
2013-05-24 00:13:18 -04:00
|
|
|
var data interface{} = nil
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
hook := &ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*HookedProvisioner{
|
|
|
|
{pA, nil, ""},
|
|
|
|
{pB, nil, ""},
|
2017-11-03 02:31:32 -04:00
|
|
|
},
|
2013-08-31 02:39:29 -04:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
hook.Run(context.Background(), "foo", ui, comm, data)
|
2013-05-24 00:13:18 -04:00
|
|
|
|
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
|
|
|
|
2015-06-15 13:26:46 -04:00
|
|
|
func TestProvisionHook_nilComm(t *testing.T) {
|
|
|
|
pA := &MockProvisioner{}
|
|
|
|
pB := &MockProvisioner{}
|
|
|
|
|
|
|
|
ui := testUi()
|
|
|
|
var comm Communicator = nil
|
|
|
|
var data interface{} = nil
|
|
|
|
|
|
|
|
hook := &ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*HookedProvisioner{
|
|
|
|
{pA, nil, ""},
|
|
|
|
{pB, nil, ""},
|
2017-11-03 02:31:32 -04:00
|
|
|
},
|
2015-06-15 13:26:46 -04:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
err := hook.Run(context.Background(), "foo", ui, comm, data)
|
2015-06-15 13:26:46 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-30 19:30:28 -05:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2013-08-31 02:39:29 -04:00
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
order = append(order, "prov")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hook := &ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*HookedProvisioner{
|
|
|
|
{p, nil, ""},
|
2017-11-03 02:31:32 -04:00
|
|
|
},
|
2013-08-31 02:39:29 -04:00
|
|
|
}
|
2019-03-22 09:50:33 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2013-08-31 02:39:29 -04:00
|
|
|
|
|
|
|
finished := make(chan struct{})
|
|
|
|
go func() {
|
2019-03-22 09:50:33 -04:00
|
|
|
hook.Run(ctx, "foo", nil, new(MockCommunicator), nil)
|
2013-08-31 02:39:29 -04:00
|
|
|
close(finished)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Cancel it while it is running
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2019-03-22 09:50:33 -04:00
|
|
|
cancel()
|
2013-08-31 02:39:29 -04:00
|
|
|
lock.Lock()
|
|
|
|
order = append(order, "cancel")
|
|
|
|
lock.Unlock()
|
|
|
|
|
|
|
|
// Wait
|
|
|
|
<-finished
|
|
|
|
|
|
|
|
// Verify order
|
2015-06-15 13:26:46 -04:00
|
|
|
if len(order) != 2 || order[0] != "cancel" || order[1] != "prov" {
|
2013-08-31 02:39:29 -04:00
|
|
|
t.Fatalf("bad: %#v", order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:13:40 -04:00
|
|
|
// TODO(mitchellh): Test that they're run in the proper order
|
2013-12-21 00:36:41 -05:00
|
|
|
|
|
|
|
func TestPausedProvisioner_impl(t *testing.T) {
|
|
|
|
var _ Provisioner = new(PausedProvisioner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedProvisionerPrepare(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &PausedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
prov.Prepare(42)
|
|
|
|
if !mock.PrepCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
if mock.PrepConfigs[0] != 42 {
|
|
|
|
t.Fatal("should have proper configs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedProvisionerProvision(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &PausedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := testUi()
|
|
|
|
comm := new(MockCommunicator)
|
2019-03-19 13:11:19 -04:00
|
|
|
prov.Provision(context.Background(), ui, comm)
|
2013-12-21 00:36:41 -05:00
|
|
|
if !mock.ProvCalled {
|
|
|
|
t.Fatal("prov should be called")
|
|
|
|
}
|
|
|
|
if mock.ProvUi != ui {
|
|
|
|
t.Fatal("should have proper ui")
|
|
|
|
}
|
|
|
|
if mock.ProvCommunicator != comm {
|
|
|
|
t.Fatal("should have proper comm")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedProvisionerProvision_waits(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &PausedProvisioner{
|
|
|
|
PauseBefore: 50 * time.Millisecond,
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
dataCh := make(chan struct{})
|
|
|
|
mock.ProvFunc = func() error {
|
|
|
|
close(dataCh)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:11:19 -04:00
|
|
|
go prov.Provision(context.Background(), testUi(), new(MockCommunicator))
|
2013-12-21 00:36:41 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
case <-dataCh:
|
|
|
|
t.Fatal("should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatal("never called")
|
|
|
|
case <-dataCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedProvisionerCancel(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &PausedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
provCh := make(chan struct{})
|
|
|
|
mock.ProvFunc = func() error {
|
|
|
|
close(provCh)
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-22 09:50:33 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2013-12-21 00:36:41 -05:00
|
|
|
|
|
|
|
// Start provisioning and wait for it to start
|
2019-03-22 09:50:33 -04:00
|
|
|
go func() {
|
|
|
|
<-provCh
|
|
|
|
cancel()
|
|
|
|
}()
|
2013-12-21 00:36:41 -05:00
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
prov.Provision(ctx, testUi(), new(MockCommunicator))
|
2013-12-21 00:36:41 -05:00
|
|
|
if !mock.CancelCalled {
|
|
|
|
t.Fatal("cancel should be called")
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 18:03:30 -04:00
|
|
|
|
|
|
|
func TestDebuggedProvisioner_impl(t *testing.T) {
|
|
|
|
var _ Provisioner = new(DebuggedProvisioner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebuggedProvisionerPrepare(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &DebuggedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
prov.Prepare(42)
|
|
|
|
if !mock.PrepCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
if mock.PrepConfigs[0] != 42 {
|
|
|
|
t.Fatal("should have proper configs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebuggedProvisionerProvision(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &DebuggedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := testUi()
|
|
|
|
comm := new(MockCommunicator)
|
|
|
|
writeReader(ui, "\n")
|
2019-03-19 13:11:19 -04:00
|
|
|
prov.Provision(context.Background(), ui, comm)
|
2017-03-16 18:03:30 -04:00
|
|
|
if !mock.ProvCalled {
|
|
|
|
t.Fatal("prov should be called")
|
|
|
|
}
|
|
|
|
if mock.ProvUi != ui {
|
|
|
|
t.Fatal("should have proper ui")
|
|
|
|
}
|
|
|
|
if mock.ProvCommunicator != comm {
|
|
|
|
t.Fatal("should have proper comm")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebuggedProvisionerCancel(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &DebuggedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
provCh := make(chan struct{})
|
|
|
|
mock.ProvFunc = func() error {
|
|
|
|
close(provCh)
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-22 09:50:33 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-03-16 18:03:30 -04:00
|
|
|
|
|
|
|
// Start provisioning and wait for it to start
|
2019-03-22 09:50:33 -04:00
|
|
|
go func() {
|
|
|
|
<-provCh
|
|
|
|
cancel()
|
|
|
|
}()
|
2017-03-16 18:03:30 -04:00
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
prov.Provision(ctx, testUi(), new(MockCommunicator))
|
2017-03-16 18:03:30 -04:00
|
|
|
if !mock.CancelCalled {
|
|
|
|
t.Fatal("cancel should be called")
|
|
|
|
}
|
|
|
|
}
|