packer-cn/builder/hyperv/common/step_export_vm_test.go

88 lines
2.3 KiB
Go
Raw Normal View History

2018-07-07 12:35:15 -04:00
package common
import (
"context"
"path/filepath"
"testing"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
2018-07-07 12:35:15 -04:00
)
func TestStepExportVm_impl(t *testing.T) {
var _ multistep.Step = new(StepExportVm)
}
func TestStepExportVm(t *testing.T) {
state := testState(t)
step := new(StepExportVm)
// ExportVirtualMachine needs the VM name and a path to export to
vmName := "foo"
state.Put("vmName", vmName)
outputDir := "foopath"
step.OutputDir = outputDir
driver := state.Get("driver").(*DriverMock)
// Test the run
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")
}
// Test the driver
if !driver.ExportVirtualMachine_Called {
t.Fatal("Should have called ExportVirtualMachine")
}
if driver.ExportVirtualMachine_Path != outputDir {
t.Fatalf("Should call with correct path. Got: %s Wanted: %s",
driver.ExportVirtualMachine_Path, outputDir)
}
if driver.ExportVirtualMachine_VmName != vmName {
t.Fatalf("Should call with correct vm name. Got: %s Wanted: %s",
driver.ExportVirtualMachine_VmName, vmName)
}
// Test we stored the export path in the statebag and it is correct
expectedPath := filepath.Join(outputDir, vmName)
if exportPath, ok := state.GetOk("export_path"); !ok {
t.Fatal("Should set export_path")
} else if exportPath != expectedPath {
t.Fatalf("Bad path stored for export_path. Got: %#v Wanted: %#v", exportPath, expectedPath)
2018-07-07 12:35:15 -04:00
}
}
func TestStepExportVm_skip(t *testing.T) {
state := testState(t)
step := new(StepExportVm)
step.SkipExport = true
// ExportVirtualMachine needs the VM name and a path to export to
vmName := "foo"
state.Put("vmName", vmName)
outputDir := "foopath"
step.OutputDir = outputDir
driver := state.Get("driver").(*DriverMock)
// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("Bad action: %v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatalf("Should NOT have error")
}
// Test the driver
if driver.ExportVirtualMachine_Called {
t.Fatal("Should NOT have called ExportVirtualMachine")
2018-07-07 12:35:15 -04:00
}
// Should not store the export path in the statebag
if _, ok := state.GetOk("export_path"); ok {
t.Fatal("Should NOT have stored export_path in the statebag")
2018-07-07 12:35:15 -04:00
}
}