github.phpd.cn/hashicorp/packer@v1.3.2/builder/vmware/common/step_prepare_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 TestStepPrepareTools_impl(t *testing.T) { 13 var _ multistep.Step = new(StepPrepareTools) 14 } 15 16 func TestStepPrepareTools(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 := &StepPrepareTools{ 26 RemoteType: "", 27 ToolsUploadFlavor: "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("tools_upload_source") 53 if !ok { 54 t.Fatal("should have tools_upload_source") 55 } 56 if path != tf.Name() { 57 t.Fatalf("bad: %#v", path) 58 } 59 } 60 61 func TestStepPrepareTools_esx5(t *testing.T) { 62 state := testState(t) 63 step := &StepPrepareTools{ 64 RemoteType: "esx5", 65 ToolsUploadFlavor: "foo", 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 TestStepPrepareTools_nonExist(t *testing.T) { 85 state := testState(t) 86 step := &StepPrepareTools{ 87 RemoteType: "", 88 ToolsUploadFlavor: "foo", 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("tools_upload_source"); ok { 114 t.Fatal("should NOT have tools_upload_source") 115 } 116 }