github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_collate_artifacts_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 TestStepCollateArtifacts_impl(t *testing.T) { 12 var _ multistep.Step = new(StepCollateArtifacts) 13 } 14 15 func TestStepCollateArtifacts_exportedArtifacts(t *testing.T) { 16 state := testState(t) 17 step := new(StepCollateArtifacts) 18 19 step.OutputDir = "foopath" 20 vmName := "foo" 21 22 // Uses export path from the state bag 23 exportPath := filepath.Join(step.OutputDir, vmName) 24 state.Put("export_path", exportPath) 25 26 driver := state.Get("driver").(*DriverMock) 27 28 // Test the run 29 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 30 t.Fatalf("Bad action: %v", action) 31 } 32 if _, ok := state.GetOk("error"); ok { 33 t.Fatal("Should NOT have error") 34 } 35 36 // Test the driver 37 if !driver.PreserveLegacyExportBehaviour_Called { 38 t.Fatal("Should have called PreserveLegacyExportBehaviour") 39 } 40 if driver.PreserveLegacyExportBehaviour_SrcPath != exportPath { 41 t.Fatalf("Should call with correct srcPath. Got: %s Wanted: %s", 42 driver.PreserveLegacyExportBehaviour_SrcPath, exportPath) 43 } 44 if driver.PreserveLegacyExportBehaviour_DstPath != step.OutputDir { 45 t.Fatalf("Should call with correct dstPath. Got: %s Wanted: %s", 46 driver.PreserveLegacyExportBehaviour_DstPath, step.OutputDir) 47 } 48 49 // Should only be called when skip_export is true 50 if driver.MoveCreatedVHDsToOutputDir_Called { 51 t.Fatal("Should NOT have called MoveCreatedVHDsToOutputDir") 52 } 53 } 54 55 func TestStepCollateArtifacts_skipExportArtifacts(t *testing.T) { 56 state := testState(t) 57 step := new(StepCollateArtifacts) 58 59 // Needs the path to the main output directory and build directory 60 step.OutputDir = "foopath" 61 buildDir := "fooBuildPath" 62 state.Put("build_dir", buildDir) 63 // Export has been skipped 64 step.SkipExport = true 65 66 driver := state.Get("driver").(*DriverMock) 67 68 // Test the run 69 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 70 t.Fatalf("Bad action: %v", action) 71 } 72 if _, ok := state.GetOk("error"); ok { 73 t.Fatal("Should NOT have error") 74 } 75 76 // Test the driver 77 if !driver.MoveCreatedVHDsToOutputDir_Called { 78 t.Fatal("Should have called MoveCreatedVHDsToOutputDir") 79 } 80 if driver.MoveCreatedVHDsToOutputDir_SrcPath != buildDir { 81 t.Fatalf("Should call with correct srcPath. Got: %s Wanted: %s", 82 driver.MoveCreatedVHDsToOutputDir_SrcPath, buildDir) 83 } 84 if driver.MoveCreatedVHDsToOutputDir_DstPath != step.OutputDir { 85 t.Fatalf("Should call with correct dstPath. Got: %s Wanted: %s", 86 driver.MoveCreatedVHDsToOutputDir_DstPath, step.OutputDir) 87 } 88 89 if driver.PreserveLegacyExportBehaviour_Called { 90 t.Fatal("Should NOT have called PreserveLegacyExportBehaviour") 91 } 92 }