github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_output_dir_test.go (about) 1 package common 2 3 import ( 4 "context" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/hashicorp/packer/helper/multistep" 10 ) 11 12 func testStepOutputDir(t *testing.T) *StepOutputDir { 13 td, err := ioutil.TempDir("", "packer") 14 if err != nil { 15 t.Fatalf("err: %s", err) 16 } 17 if err := os.RemoveAll(td); err != nil { 18 t.Fatalf("err: %s", err) 19 } 20 21 return &StepOutputDir{Force: false, Path: td} 22 } 23 24 func TestStepOutputDir_impl(t *testing.T) { 25 var _ multistep.Step = new(StepOutputDir) 26 } 27 28 func TestStepOutputDir(t *testing.T) { 29 state := testState(t) 30 step := testStepOutputDir(t) 31 // Delete the test output directory when done 32 defer os.RemoveAll(step.Path) 33 34 // Test the run 35 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 36 t.Fatalf("bad action: %#v", action) 37 } 38 if _, ok := state.GetOk("error"); ok { 39 t.Fatal("should NOT have error") 40 } 41 if _, err := os.Stat(step.Path); err != nil { 42 t.Fatalf("err: %s", err) 43 } 44 45 // Test the cleanup 46 step.Cleanup(state) 47 if _, err := os.Stat(step.Path); err != nil { 48 t.Fatalf("err: %s", err) 49 } 50 } 51 52 func TestStepOutputDir_cancelled(t *testing.T) { 53 state := testState(t) 54 step := testStepOutputDir(t) 55 56 // Test the run 57 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 58 t.Fatalf("bad action: %#v", action) 59 } 60 if _, ok := state.GetOk("error"); ok { 61 t.Fatal("should NOT have error") 62 } 63 if _, err := os.Stat(step.Path); err != nil { 64 t.Fatalf("err: %s", err) 65 } 66 67 // Mark 68 state.Put(multistep.StateCancelled, true) 69 70 // Test the cleanup 71 step.Cleanup(state) 72 if _, err := os.Stat(step.Path); err == nil { 73 t.Fatal("should not exist") 74 } 75 } 76 77 func TestStepOutputDir_halted(t *testing.T) { 78 state := testState(t) 79 step := testStepOutputDir(t) 80 81 // Test the run 82 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 83 t.Fatalf("bad action: %#v", action) 84 } 85 if _, ok := state.GetOk("error"); ok { 86 t.Fatal("should NOT have error") 87 } 88 if _, err := os.Stat(step.Path); err != nil { 89 t.Fatalf("err: %s", err) 90 } 91 92 // Mark 93 state.Put(multistep.StateHalted, true) 94 95 // Test the cleanup 96 step.Cleanup(state) 97 if _, err := os.Stat(step.Path); err == nil { 98 t.Fatal("should not exist") 99 } 100 }