github.com/sneal/packer@v0.5.2/builder/vmware/common/step_output_dir_test.go (about) 1 package common 2 3 import ( 4 "github.com/mitchellh/multistep" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func testOutputDir(t *testing.T) *LocalOutputDir { 11 td, err := ioutil.TempDir("", "packer") 12 if err != nil { 13 t.Fatalf("err: %s", err) 14 } 15 os.RemoveAll(td) 16 17 result := new(LocalOutputDir) 18 result.SetOutputDir(td) 19 return result 20 } 21 22 func TestStepOutputDir_impl(t *testing.T) { 23 var _ multistep.Step = new(StepOutputDir) 24 } 25 26 func TestStepOutputDir(t *testing.T) { 27 state := testState(t) 28 step := new(StepOutputDir) 29 30 dir := testOutputDir(t) 31 state.Put("dir", dir) 32 33 // Test the run 34 if action := step.Run(state); action != multistep.ActionContinue { 35 t.Fatalf("bad action: %#v", action) 36 } 37 if _, ok := state.GetOk("error"); ok { 38 t.Fatal("should NOT have error") 39 } 40 if _, err := os.Stat(dir.dir); err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 44 // Test the cleanup 45 step.Cleanup(state) 46 if _, err := os.Stat(dir.dir); err != nil { 47 t.Fatalf("err: %s", err) 48 } 49 } 50 51 func TestStepOutputDir_existsNoForce(t *testing.T) { 52 state := testState(t) 53 step := new(StepOutputDir) 54 55 dir := testOutputDir(t) 56 state.Put("dir", dir) 57 58 // Make sure the dir exists 59 if err := os.MkdirAll(dir.dir, 0755); err != nil { 60 t.Fatalf("err: %s", err) 61 } 62 63 // Test the run 64 if action := step.Run(state); action != multistep.ActionHalt { 65 t.Fatalf("bad action: %#v", action) 66 } 67 if _, ok := state.GetOk("error"); !ok { 68 t.Fatal("should have error") 69 } 70 71 // Test the cleanup 72 step.Cleanup(state) 73 if _, err := os.Stat(dir.dir); err != nil { 74 t.Fatal("should not delete dir") 75 } 76 } 77 78 func TestStepOutputDir_existsForce(t *testing.T) { 79 state := testState(t) 80 step := new(StepOutputDir) 81 step.Force = true 82 83 dir := testOutputDir(t) 84 state.Put("dir", dir) 85 86 // Make sure the dir exists 87 if err := os.MkdirAll(dir.dir, 0755); err != nil { 88 t.Fatalf("err: %s", err) 89 } 90 91 // Test the run 92 if action := step.Run(state); action != multistep.ActionContinue { 93 t.Fatalf("bad action: %#v", action) 94 } 95 if _, ok := state.GetOk("error"); ok { 96 t.Fatal("should NOT have error") 97 } 98 if _, err := os.Stat(dir.dir); err != nil { 99 t.Fatalf("err: %s", err) 100 } 101 } 102 103 func TestStepOutputDir_cancel(t *testing.T) { 104 state := testState(t) 105 step := new(StepOutputDir) 106 107 dir := testOutputDir(t) 108 state.Put("dir", dir) 109 110 // Test the run 111 if action := step.Run(state); action != multistep.ActionContinue { 112 t.Fatalf("bad action: %#v", action) 113 } 114 if _, ok := state.GetOk("error"); ok { 115 t.Fatal("should NOT have error") 116 } 117 if _, err := os.Stat(dir.dir); err != nil { 118 t.Fatalf("err: %s", err) 119 } 120 121 // Test cancel/halt 122 state.Put(multistep.StateCancelled, true) 123 step.Cleanup(state) 124 if _, err := os.Stat(dir.dir); err == nil { 125 t.Fatal("directory should not exist") 126 } 127 } 128 129 func TestStepOutputDir_halt(t *testing.T) { 130 state := testState(t) 131 step := new(StepOutputDir) 132 133 dir := testOutputDir(t) 134 state.Put("dir", dir) 135 136 // Test the run 137 if action := step.Run(state); action != multistep.ActionContinue { 138 t.Fatalf("bad action: %#v", action) 139 } 140 if _, ok := state.GetOk("error"); ok { 141 t.Fatal("should NOT have error") 142 } 143 if _, err := os.Stat(dir.dir); err != nil { 144 t.Fatalf("err: %s", err) 145 } 146 147 // Test cancel/halt 148 state.Put(multistep.StateHalted, true) 149 step.Cleanup(state) 150 if _, err := os.Stat(dir.dir); err == nil { 151 t.Fatal("directory should not exist") 152 } 153 }