github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_create_build_dir_test.go (about) 1 package common 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "regexp" 8 "testing" 9 10 "github.com/hashicorp/packer/helper/multistep" 11 ) 12 13 func TestStepCreateBuildDir_imp(t *testing.T) { 14 var _ multistep.Step = new(StepCreateBuildDir) 15 } 16 17 func TestStepCreateBuildDir_Defaults(t *testing.T) { 18 state := testState(t) 19 step := new(StepCreateBuildDir) 20 21 // Default is for the user not to supply value for TempPath. When 22 // nothing is set the step should use the OS temp directory as the root 23 // for the build directory 24 step.TempPath = "" 25 26 // Test the run 27 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 28 t.Fatalf("Bad action: %v", action) 29 } 30 if _, ok := state.GetOk("error"); ok { 31 t.Fatal("Should NOT have error") 32 } 33 34 if v, ok := state.GetOk("build_dir"); !ok { 35 t.Fatal("Should store path to build directory in statebag as 'build_dir'") 36 } else { 37 // On windows convert everything to forward slash separated paths 38 // This prevents the regexp interpreting backslashes as escape sequences 39 stateBuildDir := filepath.ToSlash(v.(string)) 40 expectedBuildDirRe := regexp.MustCompile( 41 filepath.ToSlash(filepath.Join(os.TempDir(), "packerhv") + `[[:digit:]]{9}$`)) 42 match := expectedBuildDirRe.MatchString(stateBuildDir) 43 if !match { 44 t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) 45 } 46 } 47 48 // Test Cleanup 49 step.Cleanup(state) 50 if _, err := os.Stat(step.buildDir); err == nil { 51 t.Fatalf("Build directory should NOT exist after Cleanup: %s", step.buildDir) 52 } 53 } 54 55 func TestStepCreateBuildDir_UserDefinedTempPath(t *testing.T) { 56 state := testState(t) 57 step := new(StepCreateBuildDir) 58 59 // Create a directory we'll use as the user supplied temp_path 60 step.TempPath = genTestDirPath("userTempDir") 61 err := os.Mkdir(step.TempPath, 0755) // The directory must exist 62 if err != nil { 63 t.Fatal("Error creating test directory") 64 } 65 defer os.RemoveAll(step.TempPath) 66 67 // Test the run 68 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 69 t.Fatalf("Bad action: %v", action) 70 } 71 if _, ok := state.GetOk("error"); ok { 72 t.Fatal("Should NOT have error") 73 } 74 75 if v, ok := state.GetOk("build_dir"); !ok { 76 t.Fatal("Should store path to build directory in statebag as 'build_dir'") 77 } else { 78 // On windows convert everything to forward slash separated paths 79 // This prevents the regexp interpreting backslashes as escape sequences 80 stateBuildDir := filepath.ToSlash(v.(string)) 81 expectedBuildDirRe := regexp.MustCompile( 82 filepath.ToSlash(filepath.Join(step.TempPath, "packerhv") + `[[:digit:]]{9}$`)) 83 match := expectedBuildDirRe.MatchString(stateBuildDir) 84 if !match { 85 t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) 86 } 87 } 88 89 // Test Cleanup 90 step.Cleanup(state) 91 if _, err := os.Stat(step.buildDir); err == nil { 92 t.Fatalf("Build directory should NOT exist after Cleanup: %s", step.buildDir) 93 } 94 if _, err := os.Stat(step.TempPath); err != nil { 95 t.Fatal("User supplied root for build directory should NOT be deleted by Cleanup") 96 } 97 } 98 99 func TestStepCreateBuildDir_BadTempPath(t *testing.T) { 100 state := testState(t) 101 step := new(StepCreateBuildDir) 102 103 // Bad 104 step.TempPath = genTestDirPath("iDontExist") 105 106 // Test the run 107 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 108 t.Fatalf("Bad action: %v", action) 109 } 110 if _, ok := state.GetOk("error"); !ok { 111 t.Fatal("Should have error due to bad path") 112 } 113 }