github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_export_vm_test.go (about) 1 package common 2 3 import ( 4 "context" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 func TestStepExportVm_impl(t *testing.T) { 12 var _ multistep.Step = new(StepExportVm) 13 } 14 15 func TestStepExportVm(t *testing.T) { 16 state := testState(t) 17 step := new(StepExportVm) 18 19 // ExportVirtualMachine needs the VM name and a path to export to 20 vmName := "foo" 21 state.Put("vmName", vmName) 22 outputDir := "foopath" 23 step.OutputDir = outputDir 24 25 driver := state.Get("driver").(*DriverMock) 26 27 // Test the run 28 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 29 t.Fatalf("Bad action: %v", action) 30 } 31 if _, ok := state.GetOk("error"); ok { 32 t.Fatal("Should NOT have error") 33 } 34 35 // Test the driver 36 if !driver.ExportVirtualMachine_Called { 37 t.Fatal("Should have called ExportVirtualMachine") 38 } 39 if driver.ExportVirtualMachine_Path != outputDir { 40 t.Fatalf("Should call with correct path. Got: %s Wanted: %s", 41 driver.ExportVirtualMachine_Path, outputDir) 42 } 43 if driver.ExportVirtualMachine_VmName != vmName { 44 t.Fatalf("Should call with correct vm name. Got: %s Wanted: %s", 45 driver.ExportVirtualMachine_VmName, vmName) 46 } 47 48 // Test we stored the export path in the statebag and it is correct 49 expectedPath := filepath.Join(outputDir, vmName) 50 if exportPath, ok := state.GetOk("export_path"); !ok { 51 t.Fatal("Should set export_path") 52 } else if exportPath != expectedPath { 53 t.Fatalf("Bad path stored for export_path. Got: %#v Wanted: %#v", exportPath, expectedPath) 54 } 55 } 56 57 func TestStepExportVm_skip(t *testing.T) { 58 state := testState(t) 59 step := new(StepExportVm) 60 step.SkipExport = true 61 62 // ExportVirtualMachine needs the VM name and a path to export to 63 vmName := "foo" 64 state.Put("vmName", vmName) 65 outputDir := "foopath" 66 step.OutputDir = outputDir 67 68 driver := state.Get("driver").(*DriverMock) 69 70 // Test the run 71 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 72 t.Fatalf("Bad action: %v", action) 73 } 74 if _, ok := state.GetOk("error"); ok { 75 t.Fatalf("Should NOT have error") 76 } 77 78 // Test the driver 79 if driver.ExportVirtualMachine_Called { 80 t.Fatal("Should NOT have called ExportVirtualMachine") 81 } 82 83 // Should not store the export path in the statebag 84 if _, ok := state.GetOk("export_path"); ok { 85 t.Fatal("Should NOT have stored export_path in the statebag") 86 } 87 }