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"
|
2019-05-24 11:56:27 -04:00
|
|
|
"fmt"
|
2013-08-31 02:39:29 -04:00
|
|
|
"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) {
|
2019-03-27 07:29:09 -04:00
|
|
|
topCtx, topCtxCancel := context.WithCancel(context.Background())
|
2013-08-31 02:39:29 -04:00
|
|
|
|
|
|
|
p := &MockProvisioner{
|
2019-03-27 07:29:09 -04:00
|
|
|
ProvFunc: func(ctx context.Context) error {
|
|
|
|
topCtxCancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2013-08-31 02:39:29 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-27 07:29:09 -04:00
|
|
|
err := hook.Run(topCtx, "foo", nil, new(MockCommunicator), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have err")
|
2013-08-31 02:39:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-05-24 11:56:27 -04:00
|
|
|
startTime := time.Now()
|
|
|
|
waitTime := 50 * time.Millisecond
|
2013-12-21 00:36:41 -05:00
|
|
|
|
2019-05-24 11:56:27 -04:00
|
|
|
prov := &PausedProvisioner{
|
|
|
|
PauseBefore: waitTime,
|
|
|
|
Provisioner: &MockProvisioner{
|
|
|
|
ProvFunc: func(context.Context) error {
|
|
|
|
timeSinceStartTime := time.Since(startTime)
|
|
|
|
if timeSinceStartTime < waitTime {
|
|
|
|
return fmt.Errorf("Spent not enough time waiting: %s", timeSinceStartTime)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:56:27 -04:00
|
|
|
err := prov.Provision(context.Background(), testUi(), new(MockCommunicator))
|
2013-12-21 00:36:41 -05:00
|
|
|
|
2019-05-24 11:56:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("prov failed: %v", err)
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedProvisionerCancel(t *testing.T) {
|
2019-03-27 07:29:09 -04:00
|
|
|
topCtx, cancelTopCtx := context.WithCancel(context.Background())
|
|
|
|
|
2013-12-21 00:36:41 -05:00
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &PausedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
mock.ProvFunc = func(ctx context.Context) error {
|
|
|
|
cancelTopCtx()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
err := prov.Provision(topCtx, testUi(), new(MockCommunicator))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have err")
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2019-03-27 07:29:09 -04:00
|
|
|
topCtx, topCtxCancel := context.WithCancel(context.Background())
|
|
|
|
|
2017-03-16 18:03:30 -04:00
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &DebuggedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
mock.ProvFunc = func(ctx context.Context) error {
|
|
|
|
topCtxCancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2017-03-16 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
err := prov.Provision(topCtx, testUi(), new(MockCommunicator))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
2017-03-16 18:03:30 -04:00
|
|
|
}
|
|
|
|
}
|