mirror of https://github.com/hashicorp/packer
parent
65cfb880fd
commit
653eb95bdb
@ -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)
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}{
|
||||
{
|
||||
"<leftShift>",
|
||||
key.CodeLeftShift,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"<leftShiftOff>",
|
||||
key.CodeLeftShift,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"<leftShiftOn>",
|
||||
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")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue