github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_prepare_parallels_tools_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 TestStepPrepareParallelsTools_impl(t *testing.T) { 13 var _ multistep.Step = new(StepPrepareParallelsTools) 14 } 15 16 func TestStepPrepareParallelsTools(t *testing.T) { 17 tf, err := ioutil.TempFile("", "packer") 18 if err != nil { 19 t.Fatalf("err: %s", err) 20 } 21 tf.Close() 22 defer os.Remove(tf.Name()) 23 24 state := testState(t) 25 step := &StepPrepareParallelsTools{ 26 ParallelsToolsMode: "", 27 ParallelsToolsFlavor: "foo", 28 } 29 30 driver := state.Get("driver").(*DriverMock) 31 32 // Mock results 33 driver.ToolsISOPathResult = tf.Name() 34 35 // Test the run 36 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 37 t.Fatalf("bad action: %#v", action) 38 } 39 if _, ok := state.GetOk("error"); ok { 40 t.Fatal("should NOT have error") 41 } 42 43 // Test the driver 44 if !driver.ToolsISOPathCalled { 45 t.Fatal("tools iso path should be called") 46 } 47 if driver.ToolsISOPathFlavor != "foo" { 48 t.Fatalf("bad: %#v", driver.ToolsISOPathFlavor) 49 } 50 51 // Test the resulting state 52 path, ok := state.GetOk("parallels_tools_path") 53 if !ok { 54 t.Fatal("should have parallels_tools_path") 55 } 56 if path != tf.Name() { 57 t.Fatalf("bad: %#v", path) 58 } 59 } 60 61 func TestStepPrepareParallelsTools_disabled(t *testing.T) { 62 state := testState(t) 63 step := &StepPrepareParallelsTools{ 64 ParallelsToolsFlavor: "foo", 65 ParallelsToolsMode: ParallelsToolsModeDisable, 66 } 67 68 driver := state.Get("driver").(*DriverMock) 69 70 // Test the run 71 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 72 t.Fatalf("bad action: %#v", action) 73 } 74 if _, ok := state.GetOk("error"); ok { 75 t.Fatal("should NOT have error") 76 } 77 78 // Test the driver 79 if driver.ToolsISOPathCalled { 80 t.Fatal("tools ISO path should NOT be called") 81 } 82 } 83 84 func TestStepPrepareParallelsTools_nonExist(t *testing.T) { 85 state := testState(t) 86 step := &StepPrepareParallelsTools{ 87 ParallelsToolsFlavor: "foo", 88 ParallelsToolsMode: "", 89 } 90 91 driver := state.Get("driver").(*DriverMock) 92 93 // Mock results 94 driver.ToolsISOPathResult = "foo" 95 96 // Test the run 97 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 98 t.Fatalf("bad action: %#v", action) 99 } 100 if _, ok := state.GetOk("error"); !ok { 101 t.Fatal("should have error") 102 } 103 104 // Test the driver 105 if !driver.ToolsISOPathCalled { 106 t.Fatal("tools iso path should be called") 107 } 108 if driver.ToolsISOPathFlavor != "foo" { 109 t.Fatalf("bad: %#v", driver.ToolsISOPathFlavor) 110 } 111 112 // Test the resulting state 113 if _, ok := state.GetOk("parallels_tools_path"); ok { 114 t.Fatal("should NOT have parallels_tools_path") 115 } 116 }