From d5971ca25e0a28fa0207285c37b6d81384712781 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 20 Nov 2020 10:21:29 -0800 Subject: [PATCH] fix tests by moving mock hooks and communicators, and BasicUi definition, into SDK --- .../common/step_ami_region_copy_test.go | 4 +- .../common/step_run_spot_instance_test.go | 3 +- .../arm/step_certificate_in_keyvault_test.go | 6 +- builder/azure/chroot/packerui_test.go | 3 +- builder/azure/chroot/step_attach_disk_test.go | 4 +- builder/docker/communicator_test.go | 12 +- builder/docker/step_test.go | 6 +- builder/googlecompute/step_test.go | 6 +- builder/hyperv/common/step_test.go | 4 +- builder/hyperv/iso/builder_test.go | 2 +- builder/hyperv/vmcx/builder_test.go | 2 +- builder/openstack/step_key_pair_test.go | 4 +- builder/oracle/oci/step_instance_info_test.go | 6 +- builder/oracle/oci/step_test.go | 6 +- .../parallels/common/step_shutdown_test.go | 8 +- builder/parallels/common/step_test.go | 4 +- .../step_upload_parallels_tools_test.go | 8 +- .../common/step_upload_version_test.go | 6 +- builder/parallels/pvm/step_test.go | 4 +- builder/qemu/step_http_ip_discover_test.go | 4 +- builder/qemu/step_test.go | 4 +- builder/scaleway/step_pre_validate_test.go | 4 +- builder/triton/step_test.go | 6 +- .../virtualbox/common/step_shutdown_test.go | 12 +- builder/virtualbox/common/step_test.go | 4 +- .../common/step_upload_version_test.go | 6 +- builder/virtualbox/ovf/step_test.go | 4 +- builder/vmware/common/step_shutdown_test.go | 10 +- builder/vmware/common/step_test.go | 4 +- builder/vmware/iso/step_create_vmx_test.go | 2 +- builder/vmware/vmx/step_test.go | 4 +- builder/vsphere/common/cleanup_vm_test.go | 4 +- builder/vsphere/common/common_test.go | 4 +- builder/vsphere/iso/common_test.go | 4 +- builder/vsphere/iso/step_create_test.go | 4 +- builder/yandex/step_create_instance_test.go | 6 +- command/build_parallel_test.go | 2 +- command/build_test.go | 3 +- command/build_timeout_test.go | 3 +- command/command_test.go | 7 +- command/exec_test.go | 3 +- command/fix_test.go | 4 +- helper/builder/testing/testing.go | 2 +- helper/communicator/step_connect_test.go | 6 +- helper/tests/core.go | 3 +- main.go | 2 +- .../chroot/step_copy_files_test.go | 3 +- packer-plugin-sdk/guestexec/elevated_test.go | 3 +- .../commonsteps/step_create_cdrom_test.go | 4 +- .../commonsteps/step_create_floppy_test.go | 4 +- .../commonsteps/step_download_test.go | 3 +- .../commonsteps/step_output_dir_test.go | 3 +- packer-plugin-sdk/packer/ui.go | 138 +++++++++++++++++- packer-plugin-sdk/packer/ui_mock.go | 114 +++++++++++++++ packer/build.go | 2 +- packer/build_test.go | 9 +- packer/core_test.go | 16 +- packer/plugin/plugin_test.go | 3 +- packer/provisioner_test.go | 26 ++-- packer/rpc/artifact_test.go | 3 +- packer/rpc/build_test.go | 6 +- packer/rpc/builder_test.go | 11 +- packer/rpc/communicator_test.go | 3 +- packer/rpc/hook_test.go | 3 +- packer/rpc/post_processor_test.go | 6 +- packer/rpc/provisioner_test.go | 3 +- packer/test-fixtures/sensitive-variables.json | 2 +- packer/testing.go | 2 +- packer/tty.go | 6 - packer/ui.go | 119 --------------- packer/ui_test.go | 22 +-- .../docker-push/post-processor_test.go | 10 +- .../docker-tag/post-processor_test.go | 10 +- .../vagrant-cloud/post-processor_test.go | 15 +- post-processor/vagrant/aws_test.go | 4 +- post-processor/vagrant/azure_test.go | 6 +- post-processor/vagrant/digitalocean_test.go | 4 +- post-processor/vagrant/google_test.go | 4 +- post-processor/vagrant/post-processor_test.go | 9 +- provisioner/ansible-local/provisioner_test.go | 4 +- provisioner/ansible/provisioner_test.go | 9 +- provisioner/chef-client/provisioner_test.go | 9 +- provisioner/file/provisioner_test.go | 21 +-- .../powershell/provisioner_acc_test.go | 3 +- provisioner/powershell/provisioner_test.go | 27 ++-- .../puppet-masterless/provisioner_test.go | 3 +- .../shell-local/provisioner_acc_test.go | 3 +- provisioner/shell/provisioner_acc_test.go | 3 +- .../windows-restart/provisioner_test.go | 20 +-- provisioner/windows-shell/provisioner_test.go | 11 +- 90 files changed, 527 insertions(+), 381 deletions(-) create mode 100644 packer-plugin-sdk/packer/ui_mock.go delete mode 100644 packer/tty.go diff --git a/builder/amazon/common/step_ami_region_copy_test.go b/builder/amazon/common/step_ami_region_copy_test.go index a9f511a61..da386cec6 100644 --- a/builder/amazon/common/step_ami_region_copy_test.go +++ b/builder/amazon/common/step_ami_region_copy_test.go @@ -11,8 +11,8 @@ import ( "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2/ec2iface" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/template/config" ) @@ -87,7 +87,7 @@ func getMockConn(config *AccessConfig, target string) (ec2iface.EC2API, error) { // Create statebag for running test func tState() multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/amazon/common/step_run_spot_instance_test.go b/builder/amazon/common/step_run_spot_instance_test.go index 5df8070f1..e0c1f2819 100644 --- a/builder/amazon/common/step_run_spot_instance_test.go +++ b/builder/amazon/common/step_run_spot_instance_test.go @@ -12,12 +12,13 @@ import ( "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) // Create statebag for running test func tStateSpot() multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/azure/arm/step_certificate_in_keyvault_test.go b/builder/azure/arm/step_certificate_in_keyvault_test.go index ce0dcfa79..4549658a7 100644 --- a/builder/azure/arm/step_certificate_in_keyvault_test.go +++ b/builder/azure/arm/step_certificate_in_keyvault_test.go @@ -7,13 +7,13 @@ import ( azcommon "github.com/hashicorp/packer/builder/azure/common" "github.com/hashicorp/packer/builder/azure/common/constants" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestNewStepCertificateInKeyVault(t *testing.T) { cli := azcommon.MockAZVaultClient{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -46,7 +46,7 @@ func TestNewStepCertificateInKeyVault_error(t *testing.T) { cli := azcommon.MockAZVaultClient{} cli.IsError = true - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/builder/azure/chroot/packerui_test.go b/builder/azure/chroot/packerui_test.go index fb2435a18..c14fdf6ec 100644 --- a/builder/azure/chroot/packerui_test.go +++ b/builder/azure/chroot/packerui_test.go @@ -4,14 +4,13 @@ import ( "io/ioutil" "strings" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) // testUI returns a test ui plus a function to retrieve the errors written to the ui func testUI() (packersdk.Ui, func() string) { errorBuffer := &strings.Builder{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: strings.NewReader(""), Writer: ioutil.Discard, ErrorWriter: errorBuffer, diff --git a/builder/azure/chroot/step_attach_disk_test.go b/builder/azure/chroot/step_attach_disk_test.go index 69261b298..8425f5f2e 100644 --- a/builder/azure/chroot/step_attach_disk_test.go +++ b/builder/azure/chroot/step_attach_disk_test.go @@ -12,8 +12,8 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute" "github.com/Azure/go-autorest/autorest" "github.com/hashicorp/packer/builder/azure/common/client" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepAttachDisk_Run(t *testing.T) { @@ -69,7 +69,7 @@ func TestStepAttachDisk_Run(t *testing.T) { }) errorBuffer := &strings.Builder{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: strings.NewReader(""), Writer: ioutil.Discard, ErrorWriter: errorBuffer, diff --git a/builder/docker/communicator_test.go b/builder/docker/communicator_test.go index fefbc7f54..b97f000b5 100644 --- a/builder/docker/communicator_test.go +++ b/builder/docker/communicator_test.go @@ -64,13 +64,13 @@ func TestUploadDownload(t *testing.T) { hooks := map[string][]packersdk.Hook{} hooks[packersdk.HookProvision] = []packersdk.Hook{ &packer.ProvisionHook{ - Provisioners: []*packersdk.HookedProvisioner{ + Provisioners: []*packer.HookedProvisioner{ {Provisioner: upload, Config: nil, TypeName: ""}, {Provisioner: download, Config: nil, TypeName: ""}, }, }, } - hook := &packer.DispatchHook{Mapping: hooks} + hook := &packersdk.DispatchHook{Mapping: hooks} // Run things artifact, err := builder.Run(context.Background(), ui, hook) @@ -152,14 +152,14 @@ func TestLargeDownload(t *testing.T) { hooks := map[string][]packersdk.Hook{} hooks[packersdk.HookProvision] = []packersdk.Hook{ &packer.ProvisionHook{ - Provisioners: []*packersdk.HookedProvisioner{ + Provisioners: []*packer.HookedProvisioner{ {Provisioner: shell, Config: nil, TypeName: ""}, {Provisioner: downloadCupcake, Config: nil, TypeName: ""}, {Provisioner: downloadBigcake, Config: nil, TypeName: ""}, }, }, } - hook := &packer.DispatchHook{Mapping: hooks} + hook := &packersdk.DispatchHook{Mapping: hooks} // Run things artifact, err := builder.Run(context.Background(), ui, hook) @@ -260,7 +260,7 @@ func TestFixUploadOwner(t *testing.T) { hooks := map[string][]packersdk.Hook{} hooks[packersdk.HookProvision] = []packersdk.Hook{ &packer.ProvisionHook{ - Provisioners: []*packersdk.HookedProvisioner{ + Provisioners: []*packer.HookedProvisioner{ {Provisioner: fileProvisioner, Config: nil, TypeName: ""}, {Provisioner: dirProvisioner, Config: nil, TypeName: ""}, {Provisioner: shellProvisioner, Config: nil, TypeName: ""}, @@ -268,7 +268,7 @@ func TestFixUploadOwner(t *testing.T) { }, }, } - hook := &packer.DispatchHook{Mapping: hooks} + hook := &packersdk.DispatchHook{Mapping: hooks} artifact, err := builder.Run(context.Background(), ui, hook) if err != nil { diff --git a/builder/docker/step_test.go b/builder/docker/step_test.go index b048c3f6e..565a3d805 100644 --- a/builder/docker/step_test.go +++ b/builder/docker/step_test.go @@ -4,16 +4,16 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", testConfigStruct(t)) state.Put("driver", &MockDriver{}) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/googlecompute/step_test.go b/builder/googlecompute/step_test.go index db9ff3dab..c43c2730a 100644 --- a/builder/googlecompute/step_test.go +++ b/builder/googlecompute/step_test.go @@ -4,16 +4,16 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", testConfigStruct(t)) state.Put("driver", &DriverMock{}) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/hyperv/common/step_test.go b/builder/hyperv/common/step_test.go index 30691fa20..966b1e2f8 100644 --- a/builder/hyperv/common/step_test.go +++ b/builder/hyperv/common/step_test.go @@ -6,15 +6,15 @@ import ( "path/filepath" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/uuid" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/hyperv/iso/builder_test.go b/builder/hyperv/iso/builder_test.go index 43790ebb2..2cbddd8eb 100644 --- a/builder/hyperv/iso/builder_test.go +++ b/builder/hyperv/iso/builder_test.go @@ -603,7 +603,7 @@ func TestUserVariablesInBootCommand(t *testing.T) { } ui := packer.TestUi(t) - hook := &packer.MockHook{} + hook := &packersdk.MockHook{} driver := &hypervcommon.DriverMock{} // Set up the state. diff --git a/builder/hyperv/vmcx/builder_test.go b/builder/hyperv/vmcx/builder_test.go index e67f9f5a1..58e7bc4dc 100644 --- a/builder/hyperv/vmcx/builder_test.go +++ b/builder/hyperv/vmcx/builder_test.go @@ -495,7 +495,7 @@ func TestUserVariablesInBootCommand(t *testing.T) { } ui := packer.TestUi(t) - hook := &packer.MockHook{} + hook := &packersdk.MockHook{} driver := &hypervcommon.DriverMock{} // Set up the state. diff --git a/builder/openstack/step_key_pair_test.go b/builder/openstack/step_key_pair_test.go index c204eec03..c002c3d16 100644 --- a/builder/openstack/step_key_pair_test.go +++ b/builder/openstack/step_key_pair_test.go @@ -5,7 +5,7 @@ import ( "os/exec" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "golang.org/x/crypto/ssh" ) @@ -76,7 +76,7 @@ func TestBerToDer(t *testing.T) { } msg := new(bytes.Buffer) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: msg, } diff --git a/builder/oracle/oci/step_instance_info_test.go b/builder/oracle/oci/step_instance_info_test.go index 6b13cc1a7..d5b9f0b3d 100644 --- a/builder/oracle/oci/step_instance_info_test.go +++ b/builder/oracle/oci/step_instance_info_test.go @@ -6,8 +6,8 @@ import ( "errors" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestInstanceInfo(t *testing.T) { @@ -37,8 +37,8 @@ func TestInstanceInfoPrivateIP(t *testing.T) { state := new(multistep.BasicStateBag) state.Put("config", baseTestConfig) state.Put("driver", &driverMock{cfg: baseTestConfig}) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/oracle/oci/step_test.go b/builder/oracle/oci/step_test.go index 1c95870fd..a7d0a1ea1 100644 --- a/builder/oracle/oci/step_test.go +++ b/builder/oracle/oci/step_test.go @@ -4,8 +4,8 @@ import ( "bytes" "os" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) // TODO(apryde): It would be good not to have to write a key file to disk to @@ -55,8 +55,8 @@ func testState() multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", baseTestConfig) state.Put("driver", &driverMock{cfg: baseTestConfig}) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/parallels/common/step_shutdown_test.go b/builder/parallels/common/step_shutdown_test.go index 861be6cd7..3012e7ae0 100644 --- a/builder/parallels/common/step_shutdown_test.go +++ b/builder/parallels/common/step_shutdown_test.go @@ -5,8 +5,8 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepShutdown_impl(t *testing.T) { @@ -17,7 +17,7 @@ func TestStepShutdown_noShutdownCommand(t *testing.T) { state := testState(t) step := new(StepShutdown) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -46,7 +46,7 @@ func TestStepShutdown_shutdownCommand(t *testing.T) { step.Command = "poweroff" step.Timeout = 1 * time.Second - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -83,7 +83,7 @@ func TestStepShutdown_shutdownTimeout(t *testing.T) { step.Command = "poweroff" step.Timeout = 1 * time.Second - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") diff --git a/builder/parallels/common/step_test.go b/builder/parallels/common/step_test.go index 4dc7bd18b..922a3b704 100644 --- a/builder/parallels/common/step_test.go +++ b/builder/parallels/common/step_test.go @@ -4,15 +4,15 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("debug", false) state.Put("driver", new(DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/parallels/common/step_upload_parallels_tools_test.go b/builder/parallels/common/step_upload_parallels_tools_test.go index 5e8b3f8d7..de35b2120 100644 --- a/builder/parallels/common/step_upload_parallels_tools_test.go +++ b/builder/parallels/common/step_upload_parallels_tools_test.go @@ -4,8 +4,8 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepUploadParallelsTools_impl(t *testing.T) { @@ -20,7 +20,7 @@ func TestStepUploadParallelsTools(t *testing.T) { step.ParallelsToolsGuestPath = "/tmp/prl-lin.iso" step.ParallelsToolsFlavor = "lin" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) // Test the run @@ -45,7 +45,7 @@ func TestStepUploadParallelsTools_interpolate(t *testing.T) { step.ParallelsToolsGuestPath = "/tmp/prl-{{ .Flavor }}.iso" step.ParallelsToolsFlavor = "win" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) // Test the run @@ -70,7 +70,7 @@ func TestStepUploadParallelsTools_attach(t *testing.T) { step.ParallelsToolsGuestPath = "/tmp/prl-lin.iso" step.ParallelsToolsFlavor = "lin" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) // Test the run diff --git a/builder/parallels/common/step_upload_version_test.go b/builder/parallels/common/step_upload_version_test.go index ddce60e3c..45f6b78a5 100644 --- a/builder/parallels/common/step_upload_version_test.go +++ b/builder/parallels/common/step_upload_version_test.go @@ -4,8 +4,8 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepUploadVersion_impl(t *testing.T) { @@ -17,7 +17,7 @@ func TestStepUploadVersion(t *testing.T) { step := new(StepUploadVersion) step.Path = "foopath" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) driver := state.Get("driver").(*DriverMock) @@ -45,7 +45,7 @@ func TestStepUploadVersion_noPath(t *testing.T) { step := new(StepUploadVersion) step.Path = "" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) // Test the run diff --git a/builder/parallels/pvm/step_test.go b/builder/parallels/pvm/step_test.go index 6c9983a82..40be30b05 100644 --- a/builder/parallels/pvm/step_test.go +++ b/builder/parallels/pvm/step_test.go @@ -5,14 +5,14 @@ import ( "testing" parallelscommon "github.com/hashicorp/packer/builder/parallels/common" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(parallelscommon.DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/qemu/step_http_ip_discover_test.go b/builder/qemu/step_http_ip_discover_test.go index 263bcb752..2e60e0261 100644 --- a/builder/qemu/step_http_ip_discover_test.go +++ b/builder/qemu/step_http_ip_discover_test.go @@ -5,13 +5,13 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepHTTPIPDiscover_Run(t *testing.T) { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/qemu/step_test.go b/builder/qemu/step_test.go index ff015f747..7b4b40a2c 100644 --- a/builder/qemu/step_test.go +++ b/builder/qemu/step_test.go @@ -4,14 +4,14 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/scaleway/step_pre_validate_test.go b/builder/scaleway/step_pre_validate_test.go index fb40998ab..d447281db 100644 --- a/builder/scaleway/step_pre_validate_test.go +++ b/builder/scaleway/step_pre_validate_test.go @@ -10,8 +10,8 @@ import ( "strconv" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/scw" ) @@ -70,7 +70,7 @@ func setup(t *testing.T, fakeImgNames []string, fakeSnapNames []string) (*multis } state := multistep.BasicStateBag{} - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/triton/step_test.go b/builder/triton/step_test.go index 448a33a8f..ef5b5bf24 100644 --- a/builder/triton/step_test.go +++ b/builder/triton/step_test.go @@ -4,16 +4,16 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", testConfig(t)) state.Put("driver", &DriverMock{}) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/virtualbox/common/step_shutdown_test.go b/builder/virtualbox/common/step_shutdown_test.go index 001195bbb..b0225bf5d 100644 --- a/builder/virtualbox/common/step_shutdown_test.go +++ b/builder/virtualbox/common/step_shutdown_test.go @@ -5,8 +5,8 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepShutdown_impl(t *testing.T) { @@ -19,7 +19,7 @@ func TestStepShutdown_noShutdownCommand(t *testing.T) { step.DisableShutdown = false step.ACPIShutdown = false - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -50,7 +50,7 @@ func TestStepShutdown_shutdownCommand(t *testing.T) { step.DisableShutdown = false step.ACPIShutdown = false - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -89,7 +89,7 @@ func TestStepShutdown_shutdownTimeout(t *testing.T) { step.DisableShutdown = false step.ACPIShutdown = false - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -119,7 +119,7 @@ func TestStepShutdown_DisableShutdown(t *testing.T) { step.ACPIShutdown = false step.Timeout = 2 * time.Second - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") @@ -148,7 +148,7 @@ func TestStepShutdown_ACPIShutdown(t *testing.T) { step.ACPIShutdown = true step.Timeout = 2 * time.Second - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) state.Put("vmName", "foo") diff --git a/builder/virtualbox/common/step_test.go b/builder/virtualbox/common/step_test.go index 7cb9dd45e..17116b361 100644 --- a/builder/virtualbox/common/step_test.go +++ b/builder/virtualbox/common/step_test.go @@ -4,14 +4,14 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/virtualbox/common/step_upload_version_test.go b/builder/virtualbox/common/step_upload_version_test.go index ddce60e3c..45f6b78a5 100644 --- a/builder/virtualbox/common/step_upload_version_test.go +++ b/builder/virtualbox/common/step_upload_version_test.go @@ -4,8 +4,8 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepUploadVersion_impl(t *testing.T) { @@ -17,7 +17,7 @@ func TestStepUploadVersion(t *testing.T) { step := new(StepUploadVersion) step.Path = "foopath" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) driver := state.Get("driver").(*DriverMock) @@ -45,7 +45,7 @@ func TestStepUploadVersion_noPath(t *testing.T) { step := new(StepUploadVersion) step.Path = "" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) state.Put("communicator", comm) // Test the run diff --git a/builder/virtualbox/ovf/step_test.go b/builder/virtualbox/ovf/step_test.go index 59b251a99..f54a47b4a 100644 --- a/builder/virtualbox/ovf/step_test.go +++ b/builder/virtualbox/ovf/step_test.go @@ -5,14 +5,14 @@ import ( "testing" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(vboxcommon.DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/vmware/common/step_shutdown_test.go b/builder/vmware/common/step_shutdown_test.go index 707de7012..2d451914e 100644 --- a/builder/vmware/common/step_shutdown_test.go +++ b/builder/vmware/common/step_shutdown_test.go @@ -8,8 +8,8 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testLocalOutputDir(t *testing.T) *LocalOutputDir { @@ -31,7 +31,7 @@ func testStepShutdownState(t *testing.T) multistep.StateBag { } state := testState(t) - state.Put("communicator", new(packer.MockCommunicator)) + state.Put("communicator", new(packersdk.MockCommunicator)) state.Put("dir", dir) state.Put("vmx_path", "foo") return state @@ -48,7 +48,7 @@ func TestStepShutdown_command(t *testing.T) { step.Timeout = 10 * time.Second step.Testing = true - comm := state.Get("communicator").(*packer.MockCommunicator) + comm := state.Get("communicator").(*packersdk.MockCommunicator) driver := state.Get("driver").(*DriverMock) driver.IsRunningResult = true @@ -109,7 +109,7 @@ func TestStepShutdown_noCommand(t *testing.T) { state := testStepShutdownState(t) step := new(StepShutdown) - comm := state.Get("communicator").(*packer.MockCommunicator) + comm := state.Get("communicator").(*packersdk.MockCommunicator) driver := state.Get("driver").(*DriverMock) // Test the run @@ -149,7 +149,7 @@ func TestStepShutdown_locks(t *testing.T) { step.Testing = true dir := state.Get("dir").(*LocalOutputDir) - comm := state.Get("communicator").(*packer.MockCommunicator) + comm := state.Get("communicator").(*packersdk.MockCommunicator) driver := state.Get("driver").(*DriverMock) // Create some lock files diff --git a/builder/vmware/common/step_test.go b/builder/vmware/common/step_test.go index 56ba540c3..cd46de03d 100644 --- a/builder/vmware/common/step_test.go +++ b/builder/vmware/common/step_test.go @@ -4,14 +4,14 @@ import ( "bytes" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index 80a216ad0..dad326b2a 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -142,7 +142,7 @@ func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisioner "vmware-iso": func() (packer.Builder, error) { return &Builder{}, nil }, }, Hook: func(n string) (packersdk.Hook, error) { - return &packer.DispatchHook{}, nil + return &packersdk.DispatchHook{}, nil }, ProvisionerStore: packer.MapOfProvisioner{ "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, diff --git a/builder/vmware/vmx/step_test.go b/builder/vmware/vmx/step_test.go index a30b06418..0d51de2a0 100644 --- a/builder/vmware/vmx/step_test.go +++ b/builder/vmware/vmx/step_test.go @@ -5,14 +5,14 @@ import ( "testing" vmwcommon "github.com/hashicorp/packer/builder/vmware/common" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(vmwcommon.DriverMock)) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/vsphere/common/cleanup_vm_test.go b/builder/vsphere/common/cleanup_vm_test.go index ad6bb3b02..00775bbb2 100644 --- a/builder/vsphere/common/cleanup_vm_test.go +++ b/builder/vsphere/common/cleanup_vm_test.go @@ -5,14 +5,14 @@ import ( "testing" "github.com/hashicorp/packer/builder/vsphere/driver" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func cleanupTestState(mockVM driver.VirtualMachine) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("vm", mockVM) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/vsphere/common/common_test.go b/builder/vsphere/common/common_test.go index e96110ef7..000310299 100644 --- a/builder/vsphere/common/common_test.go +++ b/builder/vsphere/common/common_test.go @@ -4,13 +4,13 @@ import ( "bytes" "strings" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func basicStateBag(errorBuffer *strings.Builder) *multistep.BasicStateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), ErrorWriter: errorBuffer, diff --git a/builder/vsphere/iso/common_test.go b/builder/vsphere/iso/common_test.go index 531e9dd73..da65dfd1a 100644 --- a/builder/vsphere/iso/common_test.go +++ b/builder/vsphere/iso/common_test.go @@ -3,13 +3,13 @@ package iso import ( "bytes" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func basicStateBag() *multistep.BasicStateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/builder/vsphere/iso/step_create_test.go b/builder/vsphere/iso/step_create_test.go index cd0b4fa57..db07af670 100644 --- a/builder/vsphere/iso/step_create_test.go +++ b/builder/vsphere/iso/step_create_test.go @@ -11,8 +11,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/packer/builder/vsphere/common" "github.com/hashicorp/packer/builder/vsphere/driver" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestCreateConfig_Prepare(t *testing.T) { @@ -307,7 +307,7 @@ func TestStepCreateVM_Cleanup(t *testing.T) { // Destroy fail errorBuffer := &strings.Builder{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: strings.NewReader(""), Writer: ioutil.Discard, ErrorWriter: errorBuffer, diff --git a/builder/yandex/step_create_instance_test.go b/builder/yandex/step_create_instance_test.go index b0f9cf6d7..2aaf6a08a 100644 --- a/builder/yandex/step_create_instance_test.go +++ b/builder/yandex/step_create_instance_test.go @@ -7,8 +7,8 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -116,8 +116,8 @@ func TestCreateInstanceMetadata_fromNotExistFile(t *testing.T) { func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", testConfigStruct(t)) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/command/build_parallel_test.go b/command/build_parallel_test.go index 16980b234..815350375 100644 --- a/command/build_parallel_test.go +++ b/command/build_parallel_test.go @@ -78,7 +78,7 @@ func testMetaParallel(t *testing.T, builder *ParallelTestBuilder, locked *Locked }, }, }, - Ui: &packer.BasicUi{ + Ui: &packersdk.BasicUi{ Writer: &out, ErrorWriter: &err, }, diff --git a/command/build_test.go b/command/build_test.go index 60660ff09..c67505ad7 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -15,6 +15,7 @@ import ( "github.com/hashicorp/packer/builder/file" "github.com/hashicorp/packer/builder/null" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/post-processor/manifest" shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local" filep "github.com/hashicorp/packer/provisioner/file" @@ -859,7 +860,7 @@ func testMetaFile(t *testing.T) Meta { var out, err bytes.Buffer return Meta{ CoreConfig: testCoreConfigBuilder(t), - Ui: &packer.BasicUi{ + Ui: &packersdk.BasicUi{ Writer: &out, ErrorWriter: &err, }, diff --git a/command/build_timeout_test.go b/command/build_timeout_test.go index 5657a37b0..4fde04aea 100644 --- a/command/build_timeout_test.go +++ b/command/build_timeout_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/packer/builder/file" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" shell_local "github.com/hashicorp/packer/provisioner/shell-local" "github.com/hashicorp/packer/provisioner/sleep" ) @@ -33,7 +34,7 @@ func testMetaSleepFile(t *testing.T) Meta { var out, err bytes.Buffer return Meta{ CoreConfig: testCoreConfigSleepBuilder(t), - Ui: &packer.BasicUi{ + Ui: &packersdk.BasicUi{ Writer: &out, ErrorWriter: &err, }, diff --git a/command/command_test.go b/command/command_test.go index 6d571b1eb..833fe4074 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -7,12 +7,13 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) const fixturesDir = "./test-fixtures" func fatalCommand(t *testing.T, m Meta) { - ui := m.Ui.(*packer.BasicUi) + ui := m.Ui.(*packersdk.BasicUi) out := ui.Writer.(*bytes.Buffer) err := ui.ErrorWriter.(*bytes.Buffer) t.Fatalf( @@ -22,7 +23,7 @@ func fatalCommand(t *testing.T, m Meta) { } func outputCommand(t *testing.T, m Meta) (string, string) { - ui := m.Ui.(*packer.BasicUi) + ui := m.Ui.(*packersdk.BasicUi) out := ui.Writer.(*bytes.Buffer) err := ui.ErrorWriter.(*bytes.Buffer) return out.String(), err.String() @@ -48,7 +49,7 @@ func testMeta(t *testing.T) Meta { return Meta{ CoreConfig: packer.TestCoreConfig(t), - Ui: &packer.BasicUi{ + Ui: &packersdk.BasicUi{ Writer: &out, ErrorWriter: &err, }, diff --git a/command/exec_test.go b/command/exec_test.go index 7ecb264b3..8a13b5e76 100644 --- a/command/exec_test.go +++ b/command/exec_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer/builder/file" "github.com/hashicorp/packer/builder/null" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/post-processor/manifest" shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local" filep "github.com/hashicorp/packer/provisioner/file" @@ -99,7 +100,7 @@ func TestHelperProcess(*testing.T) { } func commandMeta() Meta { - basicUi := &packer.BasicUi{ + basicUi := &packersdk.BasicUi{ Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, diff --git a/command/fix_test.go b/command/fix_test.go index 966367c71..fcc8ddd88 100644 --- a/command/fix_test.go +++ b/command/fix_test.go @@ -5,13 +5,13 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" ) func TestFix(t *testing.T) { s := &strings.Builder{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: s, } c := &FixCommand{ diff --git a/helper/builder/testing/testing.go b/helper/builder/testing/testing.go index a1a4f256b..37089c158 100644 --- a/helper/builder/testing/testing.go +++ b/helper/builder/testing/testing.go @@ -157,7 +157,7 @@ func Test(t TestT, c TestCase) { // Run it! We use a temporary directory for caching and discard // any UI output. We discard since it shows up in logs anyways. log.Printf("[DEBUG] Running 'test' build") - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: os.Stdin, Writer: ioutil.Discard, ErrorWriter: ioutil.Discard, diff --git a/helper/communicator/step_connect_test.go b/helper/communicator/step_connect_test.go index e31741b86..c8a522a9f 100644 --- a/helper/communicator/step_connect_test.go +++ b/helper/communicator/step_connect_test.go @@ -5,8 +5,8 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepConnect_impl(t *testing.T) { @@ -31,8 +31,8 @@ func TestStepConnect_none(t *testing.T) { func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("hook", &packer.MockHook{}) - state.Put("ui", &packer.BasicUi{ + state.Put("hook", &packersdk.MockHook{}) + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/helper/tests/core.go b/helper/tests/core.go index 29cdd776f..88e6e6b49 100644 --- a/helper/tests/core.go +++ b/helper/tests/core.go @@ -8,6 +8,7 @@ import ( amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" fileprovisioner "github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/provisioner/shell" ) @@ -43,7 +44,7 @@ func TestMetaFile(t *testing.T) command.Meta { var out, err bytes.Buffer return command.Meta{ CoreConfig: testCoreConfigBuilder(t), - Ui: &packer.BasicUi{ + Ui: &packersdk.BasicUi{ Writer: &out, ErrorWriter: &err, }, diff --git a/main.go b/main.go index 91e84db13..f2ced1ed1 100644 --- a/main.go +++ b/main.go @@ -195,7 +195,7 @@ func wrappedMain() int { return 1 } } else { - basicUi := &packer.BasicUi{ + basicUi := &packersdk.BasicUi{ Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, diff --git a/packer-plugin-sdk/chroot/step_copy_files_test.go b/packer-plugin-sdk/chroot/step_copy_files_test.go index fc257188e..362fecde7 100644 --- a/packer-plugin-sdk/chroot/step_copy_files_test.go +++ b/packer-plugin-sdk/chroot/step_copy_files_test.go @@ -10,7 +10,6 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/common" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" @@ -19,7 +18,7 @@ import ( // testUI returns a test ui plus a function to retrieve the errors written to the ui func testUI() (packersdk.Ui, func() string) { errorBuffer := &strings.Builder{} - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: strings.NewReader(""), Writer: ioutil.Discard, ErrorWriter: errorBuffer, diff --git a/packer-plugin-sdk/guestexec/elevated_test.go b/packer-plugin-sdk/guestexec/elevated_test.go index c9d44e817..1be624036 100644 --- a/packer-plugin-sdk/guestexec/elevated_test.go +++ b/packer-plugin-sdk/guestexec/elevated_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testConfig() map[string]interface{} { @@ -19,7 +20,7 @@ func TestProvisioner_GenerateElevatedRunner(t *testing.T) { config := testConfig() p := new(packer.MockProvisioner) p.Prepare(config) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.ProvCommunicator = comm path, err := GenerateElevatedRunner("whoami", p) diff --git a/packer-plugin-sdk/multistep/commonsteps/step_create_cdrom_test.go b/packer-plugin-sdk/multistep/commonsteps/step_create_cdrom_test.go index efec6b9fc..a639cbf69 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_create_cdrom_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_create_cdrom_test.go @@ -8,8 +8,8 @@ import ( "path" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestStepCreateCD_Impl(t *testing.T) { @@ -22,7 +22,7 @@ func TestStepCreateCD_Impl(t *testing.T) { func testStepCreateCDState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/packer-plugin-sdk/multistep/commonsteps/step_create_floppy_test.go b/packer-plugin-sdk/multistep/commonsteps/step_create_floppy_test.go index d4cde0c94..c70ad3979 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_create_floppy_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_create_floppy_test.go @@ -12,8 +12,8 @@ import ( "strconv" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) const TestFixtures = "test-fixtures" @@ -28,7 +28,7 @@ func TestStepCreateFloppy_Impl(t *testing.T) { func testStepCreateFloppyState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) diff --git a/packer-plugin-sdk/multistep/commonsteps/step_download_test.go b/packer-plugin-sdk/multistep/commonsteps/step_download_test.go index 77ae98a68..460100731 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_download_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_download_test.go @@ -19,6 +19,7 @@ import ( urlhelper "github.com/hashicorp/go-getter/v2/helper/url" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/tmp" ) @@ -242,7 +243,7 @@ func TestStepDownload_download(t *testing.T) { ResultKey: "iso_path", Url: nil, } - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), PB: &packer.NoopProgressTracker{}, diff --git a/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go b/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go index f10e2ddd3..66b37f033 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go @@ -9,11 +9,12 @@ import ( "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ + state.Put("ui", &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), PB: &packer.NoopProgressTracker{}, diff --git a/packer-plugin-sdk/packer/ui.go b/packer-plugin-sdk/packer/ui.go index 1bc1b4591..894abfd44 100644 --- a/packer-plugin-sdk/packer/ui.go +++ b/packer-plugin-sdk/packer/ui.go @@ -1,6 +1,23 @@ package packer -import "github.com/hashicorp/go-getter" +import ( + "errors" + "fmt" + "io" + "log" + "os" + "os/signal" + "strings" + "sync" + "syscall" + + "github.com/hashicorp/go-getter" +) + +type TTY interface { + ReadString() (string, error) + Close() error +} // The Ui interface handles all communication for Packer with the outside // world. This sort of control allows us to strictly control how output @@ -14,3 +31,122 @@ type Ui interface { // TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) getter.ProgressTracker } + +var ErrInterrupted = errors.New("interrupted") + +// BasicUI is an implementation of Ui that reads and writes from a standard Go +// reader and writer. It is safe to be called from multiple goroutines. Machine +// readable output is simply logged for this UI. +type BasicUi struct { + Reader io.Reader + Writer io.Writer + ErrorWriter io.Writer + l sync.Mutex + interrupted bool + TTY TTY + PB getter.ProgressTracker +} + +var _ Ui = new(BasicUi) + +func (rw *BasicUi) Ask(query string) (string, error) { + rw.l.Lock() + defer rw.l.Unlock() + + if rw.interrupted { + return "", ErrInterrupted + } + + if rw.TTY == nil { + return "", errors.New("no available tty") + } + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) + defer signal.Stop(sigCh) + + log.Printf("ui: ask: %s", query) + if query != "" { + if _, err := fmt.Fprint(rw.Writer, query+" "); err != nil { + return "", err + } + } + + result := make(chan string, 1) + go func() { + line, err := rw.TTY.ReadString() + if err != nil { + log.Printf("ui: scan err: %s", err) + return + } + result <- strings.TrimSpace(line) + }() + + select { + case line := <-result: + return line, nil + case <-sigCh: + // Print a newline so that any further output starts properly + // on a new line. + fmt.Fprintln(rw.Writer) + + // Mark that we were interrupted so future Ask calls fail. + rw.interrupted = true + + return "", ErrInterrupted + } +} + +func (rw *BasicUi) Say(message string) { + rw.l.Lock() + defer rw.l.Unlock() + + // Use LogSecretFilter to scrub out sensitive variables + message = LogSecretFilter.FilterString(message) + + log.Printf("ui: %s", message) + _, err := fmt.Fprint(rw.Writer, message+"\n") + if err != nil { + log.Printf("[ERR] Failed to write to UI: %s", err) + } +} + +func (rw *BasicUi) Message(message string) { + rw.l.Lock() + defer rw.l.Unlock() + + // Use LogSecretFilter to scrub out sensitive variables + message = LogSecretFilter.FilterString(message) + + log.Printf("ui: %s", message) + _, err := fmt.Fprint(rw.Writer, message+"\n") + if err != nil { + log.Printf("[ERR] Failed to write to UI: %s", err) + } +} + +func (rw *BasicUi) Error(message string) { + rw.l.Lock() + defer rw.l.Unlock() + + writer := rw.ErrorWriter + if writer == nil { + writer = rw.Writer + } + + // Use LogSecretFilter to scrub out sensitive variables + message = LogSecretFilter.FilterString(message) + + log.Printf("ui error: %s", message) + _, err := fmt.Fprint(writer, message+"\n") + if err != nil { + log.Printf("[ERR] Failed to write to UI: %s", err) + } +} + +func (rw *BasicUi) Machine(t string, args ...string) { + log.Printf("machine readable: %s %#v", t, args) +} + +func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return rw.PB.TrackProgress(src, currentSize, totalSize, stream) +} diff --git a/packer-plugin-sdk/packer/ui_mock.go b/packer-plugin-sdk/packer/ui_mock.go new file mode 100644 index 000000000..2e67ffe8b --- /dev/null +++ b/packer-plugin-sdk/packer/ui_mock.go @@ -0,0 +1,114 @@ +package packer + +import ( + "errors" + "io" + "log" + "strings" +) + +// TestUi is a UI that only outputs unformatted output to the given writer. +type TestUi struct { + Writer io.Writer +} + +var _ Ui = new(TestUi) + +func (u *TestUi) Ask(query string) (string, error) { + return "", errors.New("Test UI can't ask") +} + +func (u *TestUi) Say(message string) { + log.Printf(message) +} + +func (u *TestUi) Message(message string) { + log.Printf(message) +} + +func (u *TestUi) Error(message string) { + log.Printf(message) +} + +func (u *TestUi) Machine(message string, args ...string) { + log.Printf("%s, %s", message, strings.Join(args, ", ")) +} + +func (u *TestUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return &readCloser{ + read: func(p []byte) (int, error) { + return stream.Read(p) + }, + close: func() error { + return stream.Close() + }, + } +} + +type MockUi struct { + AskCalled bool + AskQuery string + ErrorCalled bool + ErrorMessage string + MachineCalled bool + MachineType string + MachineArgs []string + MessageCalled bool + MessageMessage string + SayCalled bool + SayMessage string + + TrackProgressCalled bool + ProgressBarAddCalled bool + ProgressBarCloseCalled bool +} + +func (u *MockUi) Ask(query string) (string, error) { + u.AskCalled = true + u.AskQuery = query + return "foo", nil +} + +func (u *MockUi) Error(message string) { + u.ErrorCalled = true + u.ErrorMessage = message +} + +func (u *MockUi) Machine(t string, args ...string) { + u.MachineCalled = true + u.MachineType = t + u.MachineArgs = args +} + +func (u *MockUi) Message(message string) { + u.MessageCalled = true + u.MessageMessage = message +} + +func (u *MockUi) Say(message string) { + u.SayCalled = true + u.SayMessage = message +} + +func (u *MockUi) TrackProgress(_ string, _, _ int64, stream io.ReadCloser) (body io.ReadCloser) { + u.TrackProgressCalled = true + + return &readCloser{ + read: func(p []byte) (int, error) { + u.ProgressBarAddCalled = true + return stream.Read(p) + }, + close: func() error { + u.ProgressBarCloseCalled = true + return stream.Close() + }, + } +} + +type readCloser struct { + read func([]byte) (int, error) + close func() error +} + +func (c *readCloser) Close() error { return c.close() } +func (c *readCloser) Read(p []byte) (int, error) { return c.read(p) } diff --git a/packer/build.go b/packer/build.go index 5eb4213db..551f8af3e 100644 --- a/packer/build.go +++ b/packer/build.go @@ -412,7 +412,7 @@ PostProcessorRunSeqLoop: } if len(errors) > 0 { - err = &packersdk.MultiError{errors} + err = &packersdk.MultiError{Errors: errors} } return artifacts, err diff --git a/packer/build_test.go b/packer/build_test.go index 49ff68f68..12a70f138 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata" "github.com/hashicorp/packer/version" ) @@ -19,8 +20,8 @@ func testBuild() *CoreBuild { Builder: &MockBuilder{ArtifactId: "b"}, BuilderConfig: 42, BuilderType: "foo", - hooks: map[string][]Hook{ - "foo": {&MockHook{}}, + hooks: map[string][]packersdk.Hook{ + "foo": {&packersdk.MockHook{}}, }, Provisioners: []CoreBuildProvisioner{ { @@ -246,7 +247,7 @@ func TestBuild_Run(t *testing.T) { dispatchHook := builder.RunHook dispatchHook.Run(ctx, "foo", nil, nil, 42) - hook := build.hooks["foo"][0].(*MockHook) + hook := build.hooks["foo"][0].(*packersdk.MockHook) if !hook.RunCalled { t.Fatal("should be called") } @@ -255,7 +256,7 @@ func TestBuild_Run(t *testing.T) { } // Verify provisioners run - dispatchHook.Run(ctx, HookProvision, nil, new(MockCommunicator), 42) + dispatchHook.Run(ctx, packersdk.HookProvision, nil, new(packersdk.MockCommunicator), 42) prov := build.Provisioners[0].Provisioner.(*MockProvisioner) if !prov.ProvCalled { t.Fatal("should be called") diff --git a/packer/core_test.go b/packer/core_test.go index 0b27ffb27..1099d6790 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -642,18 +642,18 @@ func TestSensitiveVars(t *testing.T) { // hardcoded { "sensitive-variables.json", - map[string]string{"foo": "bar"}, + map[string]string{"foo": "bar_extra_sensitive_probably_a_password"}, []string{"foo"}, - "bar", + "the foo jumped over the ", false, }, // interpolated { "sensitive-variables.json", - map[string]string{"foo": "bar", + map[string]string{"foo": "bar_extra_sensitive_probably_a_password", "bang": "{{ user `foo`}}"}, []string{"bang"}, - "bar", + "the foo jumped over the ", false, }, } @@ -680,13 +680,11 @@ func TestSensitiveVars(t *testing.T) { if (err != nil) != tc.Err { t.Fatalf("err: %s\n\n%s", tc.File, err) } - filtered := packersdk.LogSecretFilter.get() - if filtered[0] != tc.Expected && len(filtered) != 1 { + // Check that filter correctly manipulates strings: + filtered := packersdk.LogSecretFilter.FilterString("the foo jumped over the bar_extra_sensitive_probably_a_password") + if filtered != tc.Expected { t.Fatalf("not filtering sensitive vars; filtered is %#v", filtered) } - - // clear filter so it doesn't break other tests - packersdk.LogSecretFilter.s = make(map[string]struct{}) } } diff --git a/packer/plugin/plugin_test.go b/packer/plugin/plugin_test.go index d5867148b..bb38a348a 100644 --- a/packer/plugin/plugin_test.go +++ b/packer/plugin/plugin_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func helperProcess(s ...string) *exec.Cmd { @@ -68,7 +69,7 @@ func TestHelperProcess(*testing.T) { log.Printf("[ERR] %s", err) os.Exit(1) } - server.RegisterHook(new(packer.MockHook)) + server.RegisterHook(new(packersdk.MockHook)) server.Serve() case "invalid-rpc-address": fmt.Println("lolinvalid") diff --git a/packer/provisioner_test.go b/packer/provisioner_test.go index 47addda71..b50be279d 100644 --- a/packer/provisioner_test.go +++ b/packer/provisioner_test.go @@ -6,12 +6,14 @@ import ( "fmt" "testing" "time" + + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestProvisionHook_Impl(t *testing.T) { var raw interface{} raw = &ProvisionHook{} - if _, ok := raw.(Hook); !ok { + if _, ok := raw.(packersdk.Hook); !ok { t.Fatalf("must be a Hook") } } @@ -21,7 +23,7 @@ func TestProvisionHook(t *testing.T) { pB := &MockProvisioner{} ui := testUi() - var comm Communicator = new(MockCommunicator) + var comm packersdk.Communicator = new(packersdk.MockCommunicator) var data interface{} = nil hook := &ProvisionHook{ @@ -47,7 +49,7 @@ func TestProvisionHook_nilComm(t *testing.T) { pB := &MockProvisioner{} ui := testUi() - var comm Communicator = nil + var comm packersdk.Communicator = nil var data interface{} = nil hook := &ProvisionHook{ @@ -80,7 +82,7 @@ func TestProvisionHook_cancel(t *testing.T) { }, } - err := hook.Run(topCtx, "foo", nil, new(MockCommunicator), nil) + err := hook.Run(topCtx, "foo", nil, new(packersdk.MockCommunicator), nil) if err == nil { t.Fatal("should have err") } @@ -114,7 +116,7 @@ func TestPausedProvisionerProvision(t *testing.T) { } ui := testUi() - comm := new(MockCommunicator) + comm := new(packersdk.MockCommunicator) prov.Provision(context.Background(), ui, comm, make(map[string]interface{})) if !mock.ProvCalled { t.Fatal("prov should be called") @@ -144,7 +146,7 @@ func TestPausedProvisionerProvision_waits(t *testing.T) { }, } - err := prov.Provision(context.Background(), testUi(), new(MockCommunicator), make(map[string]interface{})) + err := prov.Provision(context.Background(), testUi(), new(packersdk.MockCommunicator), make(map[string]interface{})) if err != nil { t.Fatalf("prov failed: %v", err) @@ -165,7 +167,7 @@ func TestPausedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[string]interface{})) + err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{})) if err == nil { t.Fatal("should have err") } @@ -197,7 +199,7 @@ func TestDebuggedProvisionerProvision(t *testing.T) { } ui := testUi() - comm := new(MockCommunicator) + comm := new(packersdk.MockCommunicator) writeReader(ui, "\n") prov.Provision(context.Background(), ui, comm, make(map[string]interface{})) if !mock.ProvCalled { @@ -225,7 +227,7 @@ func TestDebuggedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[string]interface{})) + err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{})) if err == nil { t.Fatal("should have error") } @@ -266,7 +268,7 @@ func TestRetriedProvisionerProvision(t *testing.T) { } ui := testUi() - comm := new(MockCommunicator) + comm := new(packersdk.MockCommunicator) err := prov.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have errored") @@ -303,7 +305,7 @@ func TestRetriedProvisionerCancelledProvision(t *testing.T) { } ui := testUi() - comm := new(MockCommunicator) + comm := new(packersdk.MockCommunicator) err := prov.Provision(ctx, ui, comm, make(map[string]interface{})) if err == nil { t.Fatal("should have errored") @@ -336,7 +338,7 @@ func TestRetriedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[string]interface{})) + err := prov.Provision(topCtx, testUi(), new(packersdk.MockCommunicator), make(map[string]interface{})) if err == nil { t.Fatal("should have err") } diff --git a/packer/rpc/artifact_test.go b/packer/rpc/artifact_test.go index ebdec01fe..8778030d6 100644 --- a/packer/rpc/artifact_test.go +++ b/packer/rpc/artifact_test.go @@ -4,13 +4,12 @@ import ( "reflect" "testing" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestArtifactRPC(t *testing.T) { // Create the interface to test - a := new(packer.MockArtifact) + a := new(packersdk.MockArtifact) // Start the server client, server := testClientServer(t) diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index a2fd0ac93..1535572b4 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -10,7 +10,7 @@ import ( packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) -var testBuildArtifact = &packer.MockArtifact{} +var testBuildArtifact = &packersdk.MockArtifact{} type testBuild struct { nameCalled bool @@ -86,7 +86,7 @@ func TestBuild(t *testing.T) { } // Test Run - ui := new(testUi) + ui := new(packersdk.MockUi) artifacts, err := bClient.Run(ctx, ui) if !b.runCalled { t.Fatal("run should be called") @@ -150,7 +150,7 @@ func TestBuild_cancel(t *testing.T) { bClient.Prepare() - ui := new(testUi) + ui := new(packersdk.MockUi) bClient.Run(topCtx, ui) // if context cancellation is not propagated, this will timeout diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 75d442865..d57ca4411 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -6,9 +6,10 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) -var testBuilderArtifact = &packer.MockArtifact{} +var testBuilderArtifact = &packersdk.MockArtifact{} func TestBuilderPrepare(t *testing.T) { b := new(packer.MockBuilder) @@ -68,7 +69,7 @@ func TestBuilderRun(t *testing.T) { bClient := client.Builder() // Test Run - hook := &packer.MockHook{} + hook := &packersdk.MockHook{} ui := &testUi{} artifact, err := bClient.Run(context.Background(), ui, hook) if err != nil { @@ -94,7 +95,7 @@ func TestBuilderRun_nilResult(t *testing.T) { server.RegisterBuilder(b) bClient := client.Builder() - hook := &packer.MockHook{} + hook := &packersdk.MockHook{} ui := &testUi{} artifact, err := bClient.Run(context.Background(), ui, hook) if artifact != nil { @@ -115,7 +116,7 @@ func TestBuilderRun_ErrResult(t *testing.T) { b.RunErrResult = true - hook := &packer.MockHook{} + hook := &packersdk.MockHook{} ui := &testUi{} artifact, err := bClient.Run(context.Background(), ui, hook) if artifact != nil { @@ -143,7 +144,7 @@ func TestBuilderCancel(t *testing.T) { server.RegisterBuilder(b) bClient := client.Builder() - bClient.Run(topCtx, new(testUi), new(packer.MockHook)) + bClient.Run(topCtx, new(testUi), new(packersdk.MockHook)) if !cancelled { t.Fatal("context should have been cancelled") diff --git a/packer/rpc/communicator_test.go b/packer/rpc/communicator_test.go index 9e8f298d6..92aeae100 100644 --- a/packer/rpc/communicator_test.go +++ b/packer/rpc/communicator_test.go @@ -7,13 +7,12 @@ import ( "reflect" "testing" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestCommunicatorRPC(t *testing.T) { // Create the interface to test - c := new(packer.MockCommunicator) + c := new(packersdk.MockCommunicator) // Start the server client, server := testClientServer(t) diff --git a/packer/rpc/hook_test.go b/packer/rpc/hook_test.go index ef28eeb4c..dadd1ff50 100644 --- a/packer/rpc/hook_test.go +++ b/packer/rpc/hook_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -15,7 +14,7 @@ func TestHook_Implements(t *testing.T) { func TestHook_cancelWhileRun(t *testing.T) { topCtx, cancelTopCtx := context.WithCancel(context.Background()) - h := &packer.MockHook{ + h := &packersdk.MockHook{ RunFunc: func(ctx context.Context) error { cancelTopCtx() <-ctx.Done() diff --git a/packer/rpc/post_processor_test.go b/packer/rpc/post_processor_test.go index 2cccb017c..25507bfa5 100644 --- a/packer/rpc/post_processor_test.go +++ b/packer/rpc/post_processor_test.go @@ -10,7 +10,7 @@ import ( packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) -var testPostProcessorArtifact = new(packer.MockArtifact) +var testPostProcessorArtifact = new(packersdk.MockArtifact) type TestPostProcessor struct { configCalled bool @@ -71,7 +71,7 @@ func TestPostProcessorRPC(t *testing.T) { } // Test PostProcess - a := &packer.MockArtifact{ + a := &packersdk.MockArtifact{ IdValue: "ppTestId", } ui := new(testUi) @@ -121,7 +121,7 @@ func TestPostProcessorRPC_cancel(t *testing.T) { } // Test PostProcess - a := &packer.MockArtifact{ + a := &packersdk.MockArtifact{ IdValue: "ppTestId", } ui := new(testUi) diff --git a/packer/rpc/provisioner_test.go b/packer/rpc/provisioner_test.go index f70e09dad..5b3d8844a 100644 --- a/packer/rpc/provisioner_test.go +++ b/packer/rpc/provisioner_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestProvisionerRPC(t *testing.T) { @@ -38,7 +39,7 @@ func TestProvisionerRPC(t *testing.T) { // Test Provision ui := &testUi{} - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} if err := pClient.Provision(topCtx, ui, comm, make(map[string]interface{})); err == nil { t.Fatalf("Provison should have err") } diff --git a/packer/test-fixtures/sensitive-variables.json b/packer/test-fixtures/sensitive-variables.json index 1052740bb..7052ec4cd 100644 --- a/packer/test-fixtures/sensitive-variables.json +++ b/packer/test-fixtures/sensitive-variables.json @@ -1,6 +1,6 @@ { "variables": { - "foo": "bar" + "foo": "bar_extra_sensitive_probably_a_password" }, "sensitive-variables": [ "foo" diff --git a/packer/testing.go b/packer/testing.go index bf5e81a82..29875ed16 100644 --- a/packer/testing.go +++ b/packer/testing.go @@ -33,7 +33,7 @@ func TestCore(t *testing.T, c *CoreConfig) *Core { func TestUi(t *testing.T) packersdk.Ui { var buf bytes.Buffer - return &BasicUi{ + return &packersdk.BasicUi{ Reader: &buf, Writer: ioutil.Discard, ErrorWriter: ioutil.Discard, diff --git a/packer/tty.go b/packer/tty.go deleted file mode 100644 index 61b559d46..000000000 --- a/packer/tty.go +++ /dev/null @@ -1,6 +0,0 @@ -package packer - -type TTY interface { - ReadString() (string, error) - Close() error -} diff --git a/packer/ui.go b/packer/ui.go index 52db011fa..dd70b6d02 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -7,10 +7,8 @@ import ( "io" "log" "os" - "os/signal" "runtime" "strings" - "sync" "syscall" "time" "unicode" @@ -171,123 +169,6 @@ func (u *TargetedUI) TrackProgress(src string, currentSize, totalSize int64, str return u.Ui.TrackProgress(u.prefixLines(false, src), currentSize, totalSize, stream) } -// The BasicUI is a UI that reads and writes from a standard Go reader -// and writer. It is safe to be called from multiple goroutines. Machine -// readable output is simply logged for this UI. -type BasicUi struct { - Reader io.Reader - Writer io.Writer - ErrorWriter io.Writer - l sync.Mutex - interrupted bool - TTY TTY - PB getter.ProgressTracker -} - -var _ packersdk.Ui = new(BasicUi) - -func (rw *BasicUi) Ask(query string) (string, error) { - rw.l.Lock() - defer rw.l.Unlock() - - if rw.interrupted { - return "", ErrInterrupted - } - - if rw.TTY == nil { - return "", errors.New("no available tty") - } - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) - defer signal.Stop(sigCh) - - log.Printf("ui: ask: %s", query) - if query != "" { - if _, err := fmt.Fprint(rw.Writer, query+" "); err != nil { - return "", err - } - } - - result := make(chan string, 1) - go func() { - line, err := rw.TTY.ReadString() - if err != nil { - log.Printf("ui: scan err: %s", err) - return - } - result <- strings.TrimSpace(line) - }() - - select { - case line := <-result: - return line, nil - case <-sigCh: - // Print a newline so that any further output starts properly - // on a new line. - fmt.Fprintln(rw.Writer) - - // Mark that we were interrupted so future Ask calls fail. - rw.interrupted = true - - return "", ErrInterrupted - } -} - -func (rw *BasicUi) Say(message string) { - rw.l.Lock() - defer rw.l.Unlock() - - // Use packersdk.LogSecretFilter to scrub out sensitive variables - message = packersdk.LogSecretFilter.FilterString(message) - - log.Printf("ui: %s", message) - _, err := fmt.Fprint(rw.Writer, message+"\n") - if err != nil { - log.Printf("[ERR] Failed to write to UI: %s", err) - } -} - -func (rw *BasicUi) Message(message string) { - rw.l.Lock() - defer rw.l.Unlock() - - // Use packersdk.LogSecretFilter to scrub out sensitive variables - message = packersdk.LogSecretFilter.FilterString(message) - - log.Printf("ui: %s", message) - _, err := fmt.Fprint(rw.Writer, message+"\n") - if err != nil { - log.Printf("[ERR] Failed to write to UI: %s", err) - } -} - -func (rw *BasicUi) Error(message string) { - rw.l.Lock() - defer rw.l.Unlock() - - writer := rw.ErrorWriter - if writer == nil { - writer = rw.Writer - } - - // Use packersdk.LogSecretFilter to scrub out sensitive variables - message = packersdk.LogSecretFilter.FilterString(message) - - log.Printf("ui error: %s", message) - _, err := fmt.Fprint(writer, message+"\n") - if err != nil { - log.Printf("[ERR] Failed to write to UI: %s", err) - } -} - -func (rw *BasicUi) Machine(t string, args ...string) { - log.Printf("machine readable: %s %#v", t, args) -} - -func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { - return rw.PB.TrackProgress(src, currentSize, totalSize, stream) -} - // MachineReadableUi is a UI that only outputs machine-readable output // to the given Writer. type MachineReadableUi struct { diff --git a/packer/ui_test.go b/packer/ui_test.go index 9684aaf34..201a57aca 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -5,11 +5,13 @@ import ( "os" "strings" "testing" + + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) // This reads the output from the bytes.Buffer in our test object // and then resets the buffer. -func readWriter(ui *BasicUi) (result string) { +func readWriter(ui *packersdk.BasicUi) (result string) { buffer := ui.Writer.(*bytes.Buffer) result = buffer.String() buffer.Reset() @@ -17,20 +19,20 @@ func readWriter(ui *BasicUi) (result string) { } // Reset the input Reader than add some input to it. -func writeReader(ui *BasicUi, input string) { +func writeReader(ui *packersdk.BasicUi, input string) { buffer := ui.Reader.(*bytes.Buffer) buffer.WriteString(input) } -func readErrorWriter(ui *BasicUi) (result string) { +func readErrorWriter(ui *packersdk.BasicUi) (result string) { buffer := ui.ErrorWriter.(*bytes.Buffer) result = buffer.String() buffer.Reset() return } -func testUi() *BasicUi { - return &BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), ErrorWriter: new(bytes.Buffer), @@ -147,7 +149,7 @@ func TestTargetedUI(t *testing.T) { func TestColoredUi_ImplUi(t *testing.T) { var raw interface{} raw = &ColoredUi{} - if _, ok := raw.(Ui); !ok { + if _, ok := raw.(packersdk.Ui); !ok { t.Fatalf("ColoredUi must implement Ui") } } @@ -155,15 +157,15 @@ func TestColoredUi_ImplUi(t *testing.T) { func TestTargetedUI_ImplUi(t *testing.T) { var raw interface{} raw = &TargetedUI{} - if _, ok := raw.(Ui); !ok { + if _, ok := raw.(packersdk.Ui); !ok { t.Fatalf("TargetedUI must implement Ui") } } func TestBasicUi_ImplUi(t *testing.T) { var raw interface{} - raw = &BasicUi{} - if _, ok := raw.(Ui); !ok { + raw = &packersdk.BasicUi{} + if _, ok := raw.(packersdk.Ui); !ok { t.Fatalf("BasicUi must implement Ui") } } @@ -250,7 +252,7 @@ func TestBasicUi_Ask(t *testing.T) { func TestMachineReadableUi_ImplUi(t *testing.T) { var raw interface{} raw = &MachineReadableUi{} - if _, ok := raw.(Ui); !ok { + if _, ok := raw.(packersdk.Ui); !ok { t.Fatalf("MachineReadableUi must implement Ui") } } diff --git a/post-processor/docker-push/post-processor_test.go b/post-processor/docker-push/post-processor_test.go index 79710333b..ba9d84d93 100644 --- a/post-processor/docker-push/post-processor_test.go +++ b/post-processor/docker-push/post-processor_test.go @@ -11,8 +11,8 @@ import ( dockerimport "github.com/hashicorp/packer/post-processor/docker-import" ) -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -25,7 +25,7 @@ func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { func TestPostProcessor_PostProcess(t *testing.T) { driver := &docker.MockDriver{} p := &PostProcessor{Driver: driver} - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: dockerimport.BuilderId, IdValue: "foo/bar", } @@ -58,7 +58,7 @@ func TestPostProcessor_PostProcess(t *testing.T) { func TestPostProcessor_PostProcess_portInName(t *testing.T) { driver := &docker.MockDriver{} p := &PostProcessor{Driver: driver} - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: dockerimport.BuilderId, IdValue: "localhost:5000/foo/bar", } @@ -91,7 +91,7 @@ func TestPostProcessor_PostProcess_portInName(t *testing.T) { func TestPostProcessor_PostProcess_tags(t *testing.T) { driver := &docker.MockDriver{} p := &PostProcessor{Driver: driver} - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: dockerimport.BuilderId, IdValue: "hashicorp/ubuntu:precise", } diff --git a/post-processor/docker-tag/post-processor_test.go b/post-processor/docker-tag/post-processor_test.go index e61489bf1..2d6fc0562 100644 --- a/post-processor/docker-tag/post-processor_test.go +++ b/post-processor/docker-tag/post-processor_test.go @@ -28,8 +28,8 @@ func testPP(t *testing.T) *PostProcessor { return &p } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -46,7 +46,7 @@ func TestPostProcessor_PostProcess(t *testing.T) { t.Fatalf("err: %s", err) } - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: dockerimport.BuilderId, IdValue: "1234567890abcdef", } @@ -94,7 +94,7 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) { t.Fatalf("err: %s", err) } - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: dockerimport.BuilderId, IdValue: "1234567890abcdef", } @@ -139,7 +139,7 @@ func TestPostProcessor_PostProcess_NoTag(t *testing.T) { t.Fatalf("err %s", err) } - artifact := &packer.MockArtifact{BuilderIdValue: dockerimport.BuilderId, IdValue: "1234567890abcdef"} + artifact := &packersdk.MockArtifact{BuilderIdValue: dockerimport.BuilderId, IdValue: "1234567890abcdef"} result, keep, forceOverride, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packersdk.Artifact); !ok { diff --git a/post-processor/vagrant-cloud/post-processor_test.go b/post-processor/vagrant-cloud/post-processor_test.go index 52f68c535..56e2b8006 100644 --- a/post-processor/vagrant-cloud/post-processor_test.go +++ b/post-processor/vagrant-cloud/post-processor_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" ) @@ -229,7 +230,7 @@ func TestPostProcessor_Configure_checkAccessTokenIsNotRequiredForOverridenVagran } func TestPostProcessor_PostProcess_checkArtifactType(t *testing.T) { - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "invalid.builder", } @@ -247,7 +248,7 @@ func TestPostProcessor_PostProcess_checkArtifactType(t *testing.T) { } func TestPostProcessor_PostProcess_checkArtifactFileIsBox(t *testing.T) { - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "mitchellh.post-processor.vagrant", // good FilesValue: []string{"invalid.boxfile"}, // should have .box extension } @@ -278,7 +279,7 @@ func TestPostProcessor_PostProcess_uploadsAndReleases(t *testing.T) { } defer os.Remove(boxfile.Name()) - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "mitchellh.post-processor.vagrant", FilesValue: []string{boxfile.Name()}, } @@ -325,7 +326,7 @@ func TestPostProcessor_PostProcess_uploadsAndNoRelease(t *testing.T) { } defer os.Remove(boxfile.Name()) - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "mitchellh.post-processor.vagrant", FilesValue: []string{boxfile.Name()}, } @@ -373,7 +374,7 @@ func TestPostProcessor_PostProcess_uploadsDirectAndReleases(t *testing.T) { } defer os.Remove(boxfile.Name()) - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "mitchellh.post-processor.vagrant", FilesValue: []string{boxfile.Name()}, } @@ -416,8 +417,8 @@ func TestPostProcessor_PostProcess_uploadsDirectAndReleases(t *testing.T) { } } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/post-processor/vagrant/aws_test.go b/post-processor/vagrant/aws_test.go index d242985d1..df8fce2e3 100644 --- a/post-processor/vagrant/aws_test.go +++ b/post-processor/vagrant/aws_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestAWSProvider_impl(t *testing.T) { @@ -22,7 +22,7 @@ func TestAWSProvider_KeepInputArtifact(t *testing.T) { func TestAWSProvider_ArtifactId(t *testing.T) { p := new(AWSProvider) ui := testUi() - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ IdValue: "us-east-1:ami-1234", } diff --git a/post-processor/vagrant/azure_test.go b/post-processor/vagrant/azure_test.go index 7521f4405..25c1acd88 100644 --- a/post-processor/vagrant/azure_test.go +++ b/post-processor/vagrant/azure_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestAzureProvider_impl(t *testing.T) { @@ -22,7 +22,7 @@ func TestAzureProvider_KeepInputArtifact(t *testing.T) { func TestAzureProvider_ManagedImage(t *testing.T) { p := new(AzureProvider) ui := testUi() - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ StringValue: `Azure.ResourceManagement.VMImage: OSType: Linux @@ -58,7 +58,7 @@ ManagedImageLocation: westus`, func TestAzureProvider_VHD(t *testing.T) { p := new(AzureProvider) ui := testUi() - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ IdValue: "https://packerbuildswest.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.96ed2120-591d-4900-95b0-ee8e985f2213.vhd", StringValue: `Azure.ResourceManagement.VMImage: diff --git a/post-processor/vagrant/digitalocean_test.go b/post-processor/vagrant/digitalocean_test.go index c666b06a3..2dab71859 100644 --- a/post-processor/vagrant/digitalocean_test.go +++ b/post-processor/vagrant/digitalocean_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestDigitalOceanProvider_impl(t *testing.T) { @@ -22,7 +22,7 @@ func TestDigitalOceanProvider_KeepInputArtifact(t *testing.T) { func TestDigitalOceanProvider_ArtifactId(t *testing.T) { p := new(DigitalOceanProvider) ui := testUi() - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ IdValue: "San Francisco:42", } diff --git a/post-processor/vagrant/google_test.go b/post-processor/vagrant/google_test.go index 22eab0c77..ffb1e64e6 100644 --- a/post-processor/vagrant/google_test.go +++ b/post-processor/vagrant/google_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestGoogleProvider_impl(t *testing.T) { @@ -22,7 +22,7 @@ func TestGoogleProvider_KeepInputArtifact(t *testing.T) { func TestGoogleProvider_ArtifactId(t *testing.T) { p := new(GoogleProvider) ui := testUi() - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ IdValue: "packer-1234", } diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 2a86821c4..3be292f99 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testConfig() map[string]interface{} { @@ -25,8 +26,8 @@ func testPP(t *testing.T) *PostProcessor { return &p } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -183,7 +184,7 @@ func TestPostProcessorPrepare_ProviderOverrideExists(t *testing.T) { } func TestPostProcessorPostProcess_badId(t *testing.T) { - artifact := &packer.MockArtifact{ + artifact := &packersdk.MockArtifact{ BuilderIdValue: "invalid.packer", } @@ -214,7 +215,7 @@ func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) { t.Fatalf("err: %s", err) } - a := &packer.MockArtifact{ + a := &packersdk.MockArtifact{ BuilderIdValue: "packer.parallels", } a2, _, _, err := p.PostProcess(context.Background(), testUi(), a) diff --git a/provisioner/ansible-local/provisioner_test.go b/provisioner/ansible-local/provisioner_test.go index a1785df73..225854f87 100644 --- a/provisioner/ansible-local/provisioner_test.go +++ b/provisioner/ansible-local/provisioner_test.go @@ -368,13 +368,13 @@ func testProvisionerProvisionDockerWithPlaybookFiles(t *testing.T, templateStrin hooks := map[string][]packersdk.Hook{} hooks[packersdk.HookProvision] = []packersdk.Hook{ &packer.ProvisionHook{ - Provisioners: []*packersdk.HookedProvisioner{ + Provisioners: []*packer.HookedProvisioner{ {Provisioner: ansible, Config: nil, TypeName: ""}, {Provisioner: download, Config: nil, TypeName: ""}, }, }, } - hook := &packer.DispatchHook{Mapping: hooks} + hook := &packersdk.DispatchHook{Mapping: hooks} artifact, err := builder.Run(context.Background(), ui, hook) if err != nil { diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index b73e5c543..83f4d6960 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" confighelper "github.com/hashicorp/packer/packer-plugin-sdk/template/config" "github.com/stretchr/testify/assert" ) @@ -346,8 +347,8 @@ func TestAnsibleLongMessages(t *testing.T) { t.Fatalf("err: %s", err) } - comm := &packer.MockCommunicator{} - ui := &packer.BasicUi{ + comm := &packersdk.MockCommunicator{} + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -766,8 +767,8 @@ func TestUseProxy(t *testing.T) { p.setupAdapterFunc = l.setupAdapter p.executeAnsibleFunc = l.executeAnsible ctx := context.TODO() - comm := new(packer.MockCommunicator) - ui := &packer.BasicUi{ + comm := new(packersdk.MockCommunicator) + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/provisioner/chef-client/provisioner_test.go b/provisioner/chef-client/provisioner_test.go index 271726f3e..40aca6988 100644 --- a/provisioner/chef-client/provisioner_test.go +++ b/provisioner/chef-client/provisioner_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testConfig() map[string]interface{} { @@ -241,8 +242,8 @@ func TestProvisioner_createDir(t *testing.T) { config["prevent_sudo"] = !sudo p := &Provisioner{} - comm := &packer.MockCommunicator{} - ui := &packer.BasicUi{ + comm := &packersdk.MockCommunicator{} + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -272,8 +273,8 @@ func TestProvisioner_removeDir(t *testing.T) { config["prevent_sudo"] = !sudo p := &Provisioner{} - comm := &packer.MockCommunicator{} - ui := &packer.BasicUi{ + comm := &packersdk.MockCommunicator{} + ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index dd1d0325c..e0c5166ad 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testConfig() map[string]interface{} { @@ -124,11 +125,11 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { } b := bytes.NewBuffer(nil) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: b, PB: &packer.NoopProgressTracker{}, } - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) @@ -183,11 +184,11 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) { } b := bytes.NewBuffer(nil) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: b, PB: &packer.NoopProgressTracker{}, } - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) @@ -253,11 +254,11 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { } b := bytes.NewBuffer(nil) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: b, PB: &packer.NoopProgressTracker{}, } - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) @@ -305,11 +306,11 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { } b := bytes.NewBuffer(nil) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: b, PB: &packer.NoopProgressTracker{}, } - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) @@ -363,11 +364,11 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { t.Fatalf("err: %s", err) } b := bytes.NewBuffer(nil) - ui := &packer.BasicUi{ + ui := &packersdk.BasicUi{ Writer: b, PB: &packer.NoopProgressTracker{}, } - comm := &packer.MockCommunicator{} + comm := &packersdk.MockCommunicator{} err = p.ProvisionDownload(ui, comm) if err != nil { t.Fatalf("should successfully provision: %s", err) diff --git a/provisioner/powershell/provisioner_acc_test.go b/provisioner/powershell/provisioner_acc_test.go index 846ab0c3d..3e485a055 100644 --- a/provisioner/powershell/provisioner_acc_test.go +++ b/provisioner/powershell/provisioner_acc_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/helper/tests/acc" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/provisioner/powershell" windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell" ) @@ -81,7 +82,7 @@ func (s *PowershellProvisionerAccTest) RunTest(c *command.BuildCommand, args []s } if code := c.Run(args); code != 0 { - ui := c.Meta.Ui.(*packer.BasicUi) + ui := c.Meta.Ui.(*packersdk.BasicUi) out := ui.Writer.(*bytes.Buffer) err := ui.ErrorWriter.(*bytes.Buffer) return fmt.Errorf( diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index e5611385b..7fb8f355e 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" ) @@ -349,8 +350,8 @@ func TestProvisionerQuote_EnvironmentVars(t *testing.T) { } } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), ErrorWriter: new(bytes.Buffer), @@ -371,7 +372,7 @@ func TestProvisionerProvision_ValidExitCodes(t *testing.T) { p.config.PackerBuildName = "vmware" p.config.PackerBuilderType = "iso" p.config.ValidExitCodes = []int{0, 200} - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) comm.StartExitStatus = 200 p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) @@ -394,7 +395,7 @@ func TestProvisionerProvision_InvalidExitCodes(t *testing.T) { p.config.PackerBuildName = "vmware" p.config.PackerBuilderType = "iso" p.config.ValidExitCodes = []int{0, 200} - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) comm.StartExitStatus = 201 // Invalid! p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) @@ -417,7 +418,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { // Defaults provided by Packer - env vars should not appear in cmd p.config.PackerBuildName = "vmware" p.config.PackerBuilderType = "iso" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) _ = p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) @@ -468,7 +469,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { ui := testUi() p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) if err != nil { @@ -505,7 +506,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { config["remote_path"] = "c:/Windows/Temp/script.ps1" p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) if err != nil { @@ -550,7 +551,7 @@ func TestProvisionerProvision_SkipClean(t *testing.T) { tc := tc p := new(Provisioner) ui := testUi() - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) config["skip_clean"] = tc.SkipClean if err := p.Prepare(config); err != nil { @@ -577,14 +578,14 @@ func TestProvisionerProvision_UploadFails(t *testing.T) { ui := testUi() p := new(Provisioner) - comm := new(packer.ScriptUploadErrorMockCommunicator) + comm := new(packersdk.ScriptUploadErrorMockCommunicator) p.Prepare(config) p.config.StartRetryTimeout = 1 * time.Second err := p.Provision(context.Background(), ui, comm, generatedData()) - if !strings.Contains(err.Error(), packer.ScriptUploadErrorMockCommunicatorError.Error()) { + if !strings.Contains(err.Error(), packersdk.ScriptUploadErrorMockCommunicatorError.Error()) { t.Fatalf("expected Provision() error %q to contain %q", err.Error(), - packer.ScriptUploadErrorMockCommunicatorError.Error()) + packersdk.ScriptUploadErrorMockCommunicatorError.Error()) } } @@ -790,7 +791,7 @@ func TestProvision_createCommandText(t *testing.T) { config := testConfig() config["remote_path"] = "c:/Windows/Temp/script.ps1" p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.communicator = comm _ = p.Prepare(config) @@ -821,7 +822,7 @@ func TestProvision_createCommandText(t *testing.T) { func TestProvision_uploadEnvVars(t *testing.T) { p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.communicator = comm flattenedEnvVars := `$env:PACKER_BUILDER_TYPE="footype"; $env:PACKER_BUILD_NAME="foobuild";` diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index 2a9b21e6b..ebaee2dd3 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate" "github.com/stretchr/testify/assert" ) @@ -479,7 +480,7 @@ func TestProvisionerProvision_extraArguments(t *testing.T) { ui := &packer.MachineReadableUi{ Writer: ioutil.Discard, } - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) extraArguments := []string{ "--some-arg=yup", diff --git a/provisioner/shell-local/provisioner_acc_test.go b/provisioner/shell-local/provisioner_acc_test.go index e9e7d4f31..a23ceb3c5 100644 --- a/provisioner/shell-local/provisioner_acc_test.go +++ b/provisioner/shell-local/provisioner_acc_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/command" ) @@ -51,7 +52,7 @@ func (s *ShellLocalProvisionerAccTest) IsCompatible(builder string, vmOS string) func (s *ShellLocalProvisionerAccTest) RunTest(c *command.BuildCommand, args []string) error { if code := c.Run(args); code != 0 { - ui := c.Meta.Ui.(*packer.BasicUi) + ui := c.Meta.Ui.(*packersdk.BasicUi) out := ui.Writer.(*bytes.Buffer) err := ui.ErrorWriter.(*bytes.Buffer) return fmt.Errorf( diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 4529c82a7..faf785a0a 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" @@ -64,7 +65,7 @@ func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string defer testshelper.CleanupFiles(file) if code := c.Run(args); code != 0 { - ui := c.Meta.Ui.(*packer.BasicUi) + ui := c.Meta.Ui.(*packersdk.BasicUi) out := ui.Writer.(*bytes.Buffer) err := ui.ErrorWriter.(*bytes.Buffer) return fmt.Errorf( diff --git a/provisioner/windows-restart/provisioner_test.go b/provisioner/windows-restart/provisioner_test.go index ac44db001..6d87216c2 100644 --- a/provisioner/windows-restart/provisioner_test.go +++ b/provisioner/windows-restart/provisioner_test.go @@ -79,8 +79,8 @@ func TestProvisionerPrepare_InvalidKey(t *testing.T) { } } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), ErrorWriter: new(bytes.Buffer), @@ -95,7 +95,7 @@ func TestProvisionerProvision_Success(t *testing.T) { p := new(Provisioner) // Defaults provided by Packer - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) waitForCommunicatorOld := waitForCommunicator waitForCommunicator = func(context.Context, *Provisioner) error { @@ -131,7 +131,7 @@ func TestProvisionerProvision_CustomCommand(t *testing.T) { config["restart_command"] = expectedCommand // Defaults provided by Packer - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) waitForCommunicatorOld := waitForCommunicator waitForCommunicator = func(context.Context, *Provisioner) error { @@ -159,7 +159,7 @@ func TestProvisionerProvision_RestartCommandFail(t *testing.T) { config := testConfig() ui := testUi() p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) comm.StartStderr = "WinRM terminated" comm.StartExitStatus = 1 @@ -177,7 +177,7 @@ func TestProvisionerProvision_WaitForRestartFail(t *testing.T) { p := new(Provisioner) // Defaults provided by Packer - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) waitForCommunicatorOld := waitForCommunicator waitForCommunicator = func(context.Context, *Provisioner) error { @@ -198,7 +198,7 @@ func TestProvision_waitForRestartTimeout(t *testing.T) { config["restart_timeout"] = "1ms" ui := testUi() p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) var err error p.Prepare(config) @@ -239,7 +239,7 @@ func TestProvision_waitForCommunicator(t *testing.T) { p := new(Provisioner) // Defaults provided by Packer - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.comm = comm p.ui = ui comm.StartStderr = "WinRM terminated" @@ -268,7 +268,7 @@ func TestProvision_waitForCommunicatorWithCancel(t *testing.T) { p := new(Provisioner) // Defaults provided by Packer - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.comm = comm p.ui = ui retryableSleep = 5 * time.Second @@ -312,7 +312,7 @@ func TestProvision_Cancel(t *testing.T) { ui := testUi() p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) done := make(chan error) diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 1e33eb639..b523721a2 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func testConfig() map[string]interface{} { @@ -265,8 +266,8 @@ func TestProvisionerQuote_EnvironmentVars(t *testing.T) { } -func testUi() *packer.BasicUi { - return &packer.BasicUi{ +func testUi() *packersdk.BasicUi { + return &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), ErrorWriter: new(bytes.Buffer), @@ -286,7 +287,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { // Defaults provided by Packer p.config.PackerBuildName = "vmware" p.config.PackerBuilderType = "iso" - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) err := p.Provision(context.Background(), ui, comm, generatedData()) @@ -337,7 +338,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { ui := testUi() p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) err = p.Provision(context.Background(), ui, comm, generatedData()) if err != nil { @@ -376,7 +377,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { config["environment_vars"] = envVars p := new(Provisioner) - comm := new(packer.MockCommunicator) + comm := new(packersdk.MockCommunicator) p.Prepare(config) err = p.Provision(context.Background(), ui, comm, generatedData()) if err != nil {