diff --git a/builder/vsphere/common/StepHTTPIPDiscover.go b/builder/vsphere/common/step_http_ip_discover.go similarity index 100% rename from builder/vsphere/common/StepHTTPIPDiscover.go rename to builder/vsphere/common/step_http_ip_discover.go diff --git a/builder/vsphere/common/step_http_ip_discover_test.go b/builder/vsphere/common/step_http_ip_discover_test.go new file mode 100644 index 000000000..fd4480daa --- /dev/null +++ b/builder/vsphere/common/step_http_ip_discover_test.go @@ -0,0 +1,44 @@ +package common + +import ( + "context" + "testing" + + "github.com/hashicorp/packer/helper/multistep" +) + +func TestStepHTTPIPDiscover_Run(t *testing.T) { + state := new(multistep.BasicStateBag) + step := new(StepHTTPIPDiscover) + + // without setting HTTPIP + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + _, ok := state.GetOk("http_ip") + if !ok { + t.Fatal("should have http_ip") + } + + // setting HTTPIP + ip := "10.0.2.2" + step = &StepHTTPIPDiscover{ + HTTPIP: ip, + } + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + httpIp, ok := state.GetOk("http_ip") + if !ok { + t.Fatal("should have http_ip") + } + if httpIp != ip { + t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, ip) + } +} diff --git a/builder/vsphere/iso/step_boot_command.go b/builder/vsphere/iso/step_boot_command.go index 200fc6d6b..dc3b89786 100644 --- a/builder/vsphere/iso/step_boot_command.go +++ b/builder/vsphere/iso/step_boot_command.go @@ -3,13 +3,14 @@ package iso import ( "context" "fmt" + "time" + "github.com/hashicorp/packer/builder/vsphere/driver" "github.com/hashicorp/packer/common/bootcommand" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" "golang.org/x/mobile/event/key" - "time" ) type BootConfig struct { diff --git a/common/bootcommand/usb_driver.go b/common/bootcommand/usb_driver.go index 6ea6291ff..c3eeeabc4 100644 --- a/common/bootcommand/usb_driver.go +++ b/common/bootcommand/usb_driver.go @@ -8,7 +8,6 @@ import ( "time" "unicode" - "github.com/hashicorp/packer/builder/vsphere/driver" "github.com/hashicorp/packer/common" "golang.org/x/mobile/event/key" ) @@ -17,15 +16,10 @@ import ( type SendUsbScanCodes func(k key.Code, down bool) error type usbDriver struct { - vm *driver.VirtualMachine - sendImpl SendUsbScanCodes interval time.Duration specialMap map[string]key.Code scancodeMap map[rune]key.Code - - // keyEvent can set this error which will prevent it from continuing - err error } func NewUSBDriver(send SendUsbScanCodes, interval time.Duration) *usbDriver { @@ -102,11 +96,7 @@ func NewUSBDriver(send SendUsbScanCodes, interval time.Duration) *usbDriver { } func (d *usbDriver) keyEvent(k key.Code, down bool) error { - if d.err != nil { - return d.err - } if err := d.sendImpl(k, down); err != nil { - d.err = err return err } time.Sleep(d.interval) @@ -124,7 +114,7 @@ func (d *usbDriver) SendKey(k rune, action KeyAction) error { return d.keyEvent(keyCode, keyShift) } -func (d *usbDriver) SendSpecial(special string, action KeyAction) error { +func (d *usbDriver) SendSpecial(special string, action KeyAction) (err error) { keyCode, ok := d.specialMap[special] if !ok { return fmt.Errorf("special %s not found.", special) @@ -133,10 +123,10 @@ func (d *usbDriver) SendSpecial(special string, action KeyAction) error { switch action { case KeyOn: - d.keyEvent(keyCode, true) + err = d.keyEvent(keyCode, true) case KeyOff, KeyPress: - d.keyEvent(keyCode, false) + err = d.keyEvent(keyCode, false) } - return d.err + return err } diff --git a/common/bootcommand/usb_driver_test.go b/common/bootcommand/usb_driver_test.go new file mode 100644 index 000000000..dfa85f7d1 --- /dev/null +++ b/common/bootcommand/usb_driver_test.go @@ -0,0 +1,88 @@ +package bootcommand + +import ( + "context" + "testing" + "time" + + "golang.org/x/mobile/event/key" +) + +func TestUSBDriver(t *testing.T) { + tc := []struct { + command string + code key.Code + shift bool + }{ + { + "", + key.CodeLeftShift, + false, + }, + { + "", + key.CodeLeftShift, + false, + }, + { + "", + key.CodeLeftShift, + true, + }, + { + "a", + key.CodeA, + false, + }, + { + "A", + key.CodeA, + true, + }, + { + "_", + key.CodeHyphenMinus, + true, + }, + } + for _, tt := range tc { + t.Run(tt.command, func(t *testing.T) { + var code key.Code + var shift bool + sendCodes := func(c key.Code, d bool) error { + code = c + shift = d + return nil + } + d := NewUSBDriver(sendCodes, time.Duration(0)) + seq, err := GenerateExpressionSequence(tt.command) + if err != nil { + t.Fatalf("bad: not expected error: %s", err.Error()) + } + err = seq.Do(context.Background(), d) + if err != nil { + t.Fatalf("bad: not expected error: %s", err.Error()) + } + if code != tt.code { + t.Fatalf("bad: wrong scan code: \n expected: %s \n actual: %s", tt.code, code) + } + if shift != tt.shift { + t.Fatalf("bad: wrong shift: \n expected: %t \n actual: %t", tt.shift, shift) + } + }) + } +} + +func TestUSBDriver_KeyIntervalNotGiven(t *testing.T) { + d := NewUSBDriver(nil, time.Duration(0)) + if d.interval != time.Duration(100)*time.Millisecond { + t.Fatal("not expected key interval") + } +} + +func TestUSBDriver_KeyIntervalGiven(t *testing.T) { + d := NewUSBDriver(nil, time.Duration(5000)*time.Millisecond) + if d.interval != time.Duration(5000)*time.Millisecond { + t.Fatal("not expected key interval") + } +}