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"
|
2020-04-16 05:58:54 -04:00
|
|
|
"errors"
|
2019-05-24 11:56:27 -04:00
|
|
|
"fmt"
|
2013-08-31 02:39:29 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-11-20 13:21:29 -05:00
|
|
|
|
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-08-31 02:39:29 -04:00
|
|
|
)
|
2013-05-24 00:13:18 -04:00
|
|
|
|
|
|
|
func TestProvisionHook_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &ProvisionHook{}
|
2020-11-20 13:21:29 -05:00
|
|
|
if _, ok := raw.(packersdk.Hook); !ok {
|
2013-05-24 00:13:18 -04:00
|
|
|
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()
|
2020-11-20 13:21:29 -05:00
|
|
|
var comm packersdk.Communicator = new(packersdk.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()
|
2020-11-20 13:21:29 -05:00
|
|
|
var comm packersdk.Communicator = nil
|
2015-06-15 13:26:46 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
err := hook.Run(topCtx, "foo", nil, new(packersdk.MockCommunicator), nil)
|
2019-03-27 07:29:09 -04:00
|
|
|
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) {
|
2020-12-01 18:02:15 -05:00
|
|
|
var _ packersdk.Provisioner = new(PausedProvisioner)
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := new(packersdk.MockCommunicator)
|
2019-12-12 13:59:44 -05:00
|
|
|
prov.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
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
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
err := prov.Provision(context.Background(), testUi(), new(packersdk.MockCommunicator), make(map[string]interface{}))
|
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
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{}))
|
2019-03-27 07:29:09 -04:00
|
|
|
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) {
|
2020-12-01 18:02:15 -05:00
|
|
|
var _ packersdk.Provisioner = new(DebuggedProvisioner)
|
2017-03-16 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := new(packersdk.MockCommunicator)
|
2017-03-16 18:03:30 -04:00
|
|
|
writeReader(ui, "\n")
|
2019-12-12 13:59:44 -05:00
|
|
|
prov.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
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
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{}))
|
2019-03-27 07:29:09 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
2017-03-16 18:03:30 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 05:58:54 -04:00
|
|
|
|
|
|
|
func TestRetriedProvisioner_impl(t *testing.T) {
|
2020-12-01 18:02:15 -05:00
|
|
|
var _ packersdk.Provisioner = new(RetriedProvisioner)
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetriedProvisionerPrepare(t *testing.T) {
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &RetriedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := prov.Prepare(42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have errored")
|
|
|
|
}
|
|
|
|
if !mock.PrepCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
if mock.PrepConfigs[0] != 42 {
|
|
|
|
t.Fatal("should have proper configs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetriedProvisionerProvision(t *testing.T) {
|
|
|
|
mock := &MockProvisioner{
|
|
|
|
ProvFunc: func(ctx context.Context) error {
|
|
|
|
return errors.New("failed")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
prov := &RetriedProvisioner{
|
|
|
|
MaxRetries: 2,
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := testUi()
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := new(packersdk.MockCommunicator)
|
2020-04-16 05:58:54 -04:00
|
|
|
err := prov.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have errored")
|
|
|
|
}
|
|
|
|
if !mock.ProvCalled {
|
|
|
|
t.Fatal("prov should be called")
|
|
|
|
}
|
|
|
|
if !mock.ProvRetried {
|
|
|
|
t.Fatal("prov should be retried")
|
|
|
|
}
|
|
|
|
if mock.ProvUi != ui {
|
|
|
|
t.Fatal("should have proper ui")
|
|
|
|
}
|
|
|
|
if mock.ProvCommunicator != comm {
|
|
|
|
t.Fatal("should have proper comm")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetriedProvisionerCancelledProvision(t *testing.T) {
|
|
|
|
// Don't retry if context is cancelled
|
|
|
|
ctx, topCtxCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
mock := &MockProvisioner{
|
|
|
|
ProvFunc: func(ctx context.Context) error {
|
|
|
|
topCtxCancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
prov := &RetriedProvisioner{
|
|
|
|
MaxRetries: 2,
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := testUi()
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := new(packersdk.MockCommunicator)
|
2020-04-16 05:58:54 -04:00
|
|
|
err := prov.Provision(ctx, ui, comm, make(map[string]interface{}))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have errored")
|
|
|
|
}
|
|
|
|
if !mock.ProvCalled {
|
|
|
|
t.Fatal("prov should be called")
|
|
|
|
}
|
|
|
|
if mock.ProvRetried {
|
|
|
|
t.Fatal("prov should NOT be retried")
|
|
|
|
}
|
|
|
|
if mock.ProvUi != ui {
|
|
|
|
t.Fatal("should have proper ui")
|
|
|
|
}
|
|
|
|
if mock.ProvCommunicator != comm {
|
|
|
|
t.Fatal("should have proper comm")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetriedProvisionerCancel(t *testing.T) {
|
|
|
|
topCtx, cancelTopCtx := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
mock := new(MockProvisioner)
|
|
|
|
prov := &RetriedProvisioner{
|
|
|
|
Provisioner: mock,
|
|
|
|
}
|
|
|
|
|
|
|
|
mock.ProvFunc = func(ctx context.Context) error {
|
|
|
|
cancelTopCtx()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{}))
|
2020-04-16 05:58:54 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have err")
|
|
|
|
}
|
|
|
|
}
|