2017-03-05 15:15:53 -05:00
|
|
|
package common
|
2015-10-23 19:50:14 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2020-08-25 15:18:38 -04:00
|
|
|
"path/filepath"
|
2015-10-23 19:50:14 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2020-08-25 15:18:38 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-10-23 19:50:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepExport_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepExport)
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:18:38 -04:00
|
|
|
func stringPointer(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func remoteExportTestState(t *testing.T) multistep.StateBag {
|
|
|
|
state := testState(t)
|
|
|
|
driverConfig := &DriverConfig{
|
|
|
|
RemoteHost: "123.45.67.8",
|
|
|
|
RemotePassword: "password",
|
|
|
|
RemoteUser: "user",
|
|
|
|
RemoteType: "esx5",
|
|
|
|
}
|
|
|
|
state.Put("driverConfig", driverConfig)
|
|
|
|
state.Put("display_name", "vm_name")
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepExport_ReturnIfSkip(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
driverConfig := &DriverConfig{}
|
|
|
|
state.Put("driverConfig", driverConfig)
|
|
|
|
step := new(StepExport)
|
|
|
|
|
|
|
|
step.SkipExport = true
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We told step to skip so it should not have reached the driver's Export
|
|
|
|
// func.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if d.ExportCalled {
|
|
|
|
t.Fatal("Should not have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepExport_localArgs(t *testing.T) {
|
|
|
|
// even though we aren't overriding the remote args and they are present,
|
|
|
|
// test shouldn't use them since remoteType is not set to esx.
|
|
|
|
state := testState(t)
|
|
|
|
driverConfig := &DriverConfig{}
|
|
|
|
state.Put("driverConfig", driverConfig)
|
|
|
|
step := new(StepExport)
|
|
|
|
|
|
|
|
step.SkipExport = false
|
|
|
|
step.OutputDir = stringPointer("test-output")
|
|
|
|
step.VMName = "test-name"
|
|
|
|
step.Format = "ova"
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that step ran, and called Export with the expected args.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if !d.ExportCalled {
|
|
|
|
t.Fatal("Should have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, d.ExportArgs,
|
|
|
|
[]string{
|
|
|
|
filepath.Join("test-output", "test-name.vmx"),
|
|
|
|
filepath.Join("test-output", "test-name.ova")})
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepExport_localArgsExportOutputPath(t *testing.T) {
|
|
|
|
// even though we aren't overriding the remote args and they are present,
|
|
|
|
// test shouldn't use them since remoteType is not set to esx.
|
|
|
|
state := testState(t)
|
|
|
|
driverConfig := &DriverConfig{}
|
|
|
|
state.Put("driverConfig", driverConfig)
|
|
|
|
state.Put("export_output_path", "local_output")
|
|
|
|
step := new(StepExport)
|
|
|
|
|
|
|
|
step.SkipExport = false
|
|
|
|
step.OutputDir = stringPointer("test-output")
|
|
|
|
step.VMName = "test-name"
|
|
|
|
step.Format = "ova"
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that step ran, and called Export with the expected args.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if !d.ExportCalled {
|
|
|
|
t.Fatal("Should have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, d.ExportArgs,
|
|
|
|
[]string{
|
|
|
|
filepath.Join("local_output", "test-name.vmx"),
|
|
|
|
filepath.Join("local_output", "test-name.ova")})
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepExport_localArgs_OvftoolOptions(t *testing.T) {
|
|
|
|
// even though we aren't overriding the remote args and they are present,
|
|
|
|
// test shouldn't use them since remoteType is not set to esx.
|
2015-10-23 19:50:14 -04:00
|
|
|
state := testState(t)
|
2020-08-25 15:18:38 -04:00
|
|
|
driverConfig := &DriverConfig{}
|
|
|
|
state.Put("driverConfig", driverConfig)
|
2015-10-23 19:50:14 -04:00
|
|
|
step := new(StepExport)
|
|
|
|
|
2020-08-25 15:18:38 -04:00
|
|
|
step.SkipExport = false
|
|
|
|
step.OutputDir = stringPointer("test-output")
|
|
|
|
step.VMName = "test-name"
|
|
|
|
step.Format = "ova"
|
|
|
|
step.OVFToolOptions = []string{"--option=value", "--second-option=\"quoted value\""}
|
2015-10-23 19:50:14 -04:00
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2015-10-23 19:50:14 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:18:38 -04:00
|
|
|
// Check that step ran, and called Export with the expected args.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if !d.ExportCalled {
|
|
|
|
t.Fatal("Should have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, d.ExportArgs, []string{"--option=value",
|
|
|
|
"--second-option=\"quoted value\"",
|
|
|
|
filepath.Join("test-output", "test-name.vmx"),
|
|
|
|
filepath.Join("test-output", "test-name.ova")})
|
|
|
|
|
2015-10-23 19:50:14 -04:00
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:18:38 -04:00
|
|
|
func TestStepExport_RemoteArgs(t *testing.T) {
|
|
|
|
// Even though we aren't overriding the remote args and they are present,
|
|
|
|
// test shouldn't use them since remoteType is not set to esx.
|
|
|
|
state := remoteExportTestState(t)
|
|
|
|
step := new(StepExport)
|
|
|
|
|
|
|
|
step.SkipExport = false
|
|
|
|
step.OutputDir = stringPointer("test-output")
|
|
|
|
step.VMName = "test-name"
|
|
|
|
step.Format = "ova"
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that step ran, and called Export with the expected args.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if !d.ExportCalled {
|
|
|
|
t.Fatal("Should have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, d.ExportArgs, []string{"--noSSLVerify=true",
|
|
|
|
"--skipManifestCheck",
|
|
|
|
"-tt=ova",
|
|
|
|
"vi://user:password@123.45.67.8/vm_name",
|
|
|
|
filepath.Join("test-output", "test-name.ova")})
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepExport_RemoteArgsWithExportOutputPath(t *testing.T) {
|
|
|
|
// Even though we aren't overriding the remote args and they are present,
|
|
|
|
// test shouldn't use them since remoteType is not set to esx.
|
|
|
|
state := remoteExportTestState(t)
|
|
|
|
state.Put("export_output_path", "local_output")
|
|
|
|
step := new(StepExport)
|
|
|
|
|
|
|
|
step.SkipExport = false
|
|
|
|
step.OutputDir = stringPointer("test-output")
|
|
|
|
step.VMName = "test-name"
|
|
|
|
step.Format = "ova"
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that step ran, and called Export with the expected args.
|
|
|
|
d := state.Get("driver").(*DriverMock)
|
|
|
|
if !d.ExportCalled {
|
|
|
|
t.Fatal("Should have called the driver export func")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, d.ExportArgs, []string{"--noSSLVerify=true",
|
|
|
|
"--skipManifestCheck",
|
|
|
|
"-tt=ova",
|
|
|
|
"vi://user:password@123.45.67.8/vm_name",
|
|
|
|
filepath.Join("local_output", "test-name.ova")})
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
step.Cleanup(state)
|
2015-10-23 19:50:14 -04:00
|
|
|
}
|