github.com/rothwerx/packer@v0.9.0/builder/docker/step_temp_dir_test.go (about) 1 package docker 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/packer" 10 ) 11 12 func TestStepTempDir_impl(t *testing.T) { 13 var _ multistep.Step = new(StepTempDir) 14 } 15 16 func testStepTempDir_impl(t *testing.T) string { 17 state := testState(t) 18 step := new(StepTempDir) 19 defer step.Cleanup(state) 20 21 // sanity test 22 if _, ok := state.GetOk("temp_dir"); ok { 23 t.Fatalf("temp_dir should not be in state yet") 24 } 25 26 // run the step 27 if action := step.Run(state); action != multistep.ActionContinue { 28 t.Fatalf("bad action: %#v", action) 29 } 30 31 // Verify that we got the temp dir 32 dirRaw, ok := state.GetOk("temp_dir") 33 if !ok { 34 t.Fatalf("should've made temp_dir") 35 } 36 dir := dirRaw.(string) 37 38 if _, err := os.Stat(dir); err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 42 // Cleanup 43 step.Cleanup(state) 44 if _, err := os.Stat(dir); err == nil { 45 t.Fatalf("dir should be gone") 46 } 47 48 return dir 49 } 50 51 func TestStepTempDir(t *testing.T) { 52 testStepTempDir_impl(t) 53 } 54 55 func TestStepTempDir_notmpdir(t *testing.T) { 56 tempenv := "PACKER_TMP_DIR" 57 58 oldenv := os.Getenv(tempenv) 59 defer os.Setenv(tempenv, oldenv) 60 os.Setenv(tempenv, "") 61 62 dir1 := testStepTempDir_impl(t) 63 64 cd, err := packer.ConfigDir() 65 if err != nil { 66 t.Fatalf("bad ConfigDir") 67 } 68 td := filepath.Join(cd, "tmp") 69 os.Setenv(tempenv, td) 70 71 dir2 := testStepTempDir_impl(t) 72 73 if filepath.Dir(dir1) != filepath.Dir(dir2) { 74 t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) 75 } 76 } 77 78 func TestStepTempDir_packertmpdir(t *testing.T) { 79 tempenv := "PACKER_TMP_DIR" 80 81 oldenv := os.Getenv(tempenv) 82 defer os.Setenv(tempenv, oldenv) 83 os.Setenv(tempenv, ".") 84 85 dir1 := testStepTempDir_impl(t) 86 87 abspath, err := filepath.Abs(".") 88 if err != nil { 89 t.Fatalf("bad absolute path") 90 } 91 dir2 := filepath.Join(abspath, "tmp") 92 93 if filepath.Dir(dir1) != filepath.Dir(dir2) { 94 t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) 95 } 96 }