use a time.Duration instead of a hardcoded ms

This commit is contained in:
Megan Marsh 2018-08-23 13:16:01 -07:00
parent 918db58604
commit 31d4f8af45
15 changed files with 89 additions and 47 deletions

View File

@ -25,7 +25,7 @@ type StepTypeBootCommand struct {
BootWait time.Duration
SwitchName string
Ctx interpolate.Context
GroupInterval int
GroupInterval time.Duration
}
func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {

View File

@ -26,7 +26,7 @@ type StepTypeBootCommand struct {
HostInterfaces []string
VMName string
Ctx interpolate.Context
GroupInterval int
GroupInterval time.Duration
}
// Run types the boot command by sending key scancodes into the VM.

View File

@ -25,7 +25,7 @@ type StepTypeBootCommand struct {
BootWait time.Duration
VMName string
Ctx interpolate.Context
GroupInterval int
GroupInterval time.Duration
}
func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {

View File

@ -53,7 +53,6 @@ type Config struct {
KeepRegistered bool `mapstructure:"keep_registered"`
SkipExport bool `mapstructure:"skip_export"`
VMName string `mapstructure:"vm_name"`
KeyInterval int `mapstructure:"key_interval"`
ctx interpolate.Context
}
@ -122,10 +121,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.ISOInterface = "ide"
}
if b.config.KeyInterval == 0 {
b.config.KeyInterval = -1
}
if b.config.VMName == "" {
b.config.VMName = fmt.Sprintf(
"packer-%s-%d", b.config.PackerBuildName, interpolate.InitTime.Unix())

View File

@ -30,7 +30,7 @@ type StepTypeBootCommand struct {
BootWait time.Duration
VMName string
Ctx interpolate.Context
KeyInterval int
KeyInterval time.Duration
}
type bootCommandTemplateData struct {
HTTPIP string

View File

@ -9,28 +9,26 @@ import (
)
type BootConfig struct {
RawBootWait string `mapstructure:"boot_wait"`
BootCommand []string `mapstructure:"boot_command"`
// time in ms to wait between each group of 25 key presses
BootGroupInterval int `mapstructure:"boot_keygroup_interval"`
BootWait time.Duration ``
RawBootGroupInterval string `mapstructure:"boot_keygroup_interval"`
RawBootWait string `mapstructure:"boot_wait"`
BootCommand []string `mapstructure:"boot_command"`
BootGroupInterval time.Duration ``
BootWait time.Duration ``
}
type VNCConfig struct {
BootConfig `mapstructure:",squash"`
DisableVNC bool `mapstructure:"disable_vnc"`
// time in ms to wait between each key press
BootKeyInterval int `mapstructure:"boot_key_interval"`
RawBootKeyInterval string `mapstructure:"boot_key_interval"`
BootKeyInterval time.Duration ``
}
func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error) {
if c.RawBootWait == "" {
c.RawBootWait = "10s"
}
if c.BootGroupInterval == 0 {
c.BootGroupInterval = -1
}
if c.RawBootWait != "" {
bw, err := time.ParseDuration(c.RawBootWait)
if err != nil {
@ -41,6 +39,20 @@ func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error) {
}
}
if c.RawBootGroupInterval == "" {
c.RawBootGroupInterval = "0ms"
}
if c.RawBootGroupInterval != "" {
bgi, err := time.ParseDuration(c.RawBootGroupInterval)
if err != nil {
errs = append(
errs, fmt.Errorf("Failed parsing boot_keygroup_interval: %s", err))
} else {
c.BootGroupInterval = bgi
}
}
if c.BootCommand != nil {
expSeq, err := GenerateExpressionSequence(c.FlatBootCommand())
if err != nil {
@ -62,9 +74,21 @@ func (c *VNCConfig) Prepare(ctx *interpolate.Context) (errs []error) {
errs = append(errs,
fmt.Errorf("A boot command cannot be used when vnc is disabled."))
}
if c.BootKeyInterval == 0 {
c.BootKeyInterval = -1
if c.RawBootKeyInterval == "" {
c.RawBootKeyInterval = "0ms"
}
if c.RawBootKeyInterval != "" {
bki, err := time.ParseDuration(c.RawBootKeyInterval)
if err != nil {
errs = append(
errs, fmt.Errorf("Failed parsing boot_key_interval: %s", err))
} else {
c.BootKeyInterval = bki
}
}
errs = append(errs, c.BootConfig.Prepare(ctx)...)
return
}

View File

@ -38,7 +38,7 @@ func (sc *scancode) makeBreak() []string {
// NewPCXTDriver creates a new boot command driver for VMs that expect PC-XT
// keyboard codes. `send` should send its argument to the VM. `chunkSize` should
// be the maximum number of keyboard codes to send to `send` at one time.
func NewPCXTDriver(send SendCodeFunc, chunkSize int, interval int) *pcXTDriver {
func NewPCXTDriver(send SendCodeFunc, chunkSize int, interval time.Duration) *pcXTDriver {
// We delay (default 100ms) between each input event to allow for CPU or
// network latency. See PackerKeyEnv for tuning.
keyInterval := common.PackerKeyDefault
@ -46,8 +46,8 @@ func NewPCXTDriver(send SendCodeFunc, chunkSize int, interval int) *pcXTDriver {
keyInterval = delay
}
// Override interval based on builder-specific override
if interval >= 0 {
keyInterval = time.Duration(interval) * time.Millisecond
if interval > time.Duration(0) {
keyInterval = interval
}
// Scancodes reference: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
// https://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html

View File

@ -3,6 +3,7 @@ package bootcommand
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -79,7 +80,7 @@ func Test_pcxtSpecialOnOff(t *testing.T) {
codes = c
return nil
}
d := NewPCXTDriver(sendCodes, -1, -1)
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
seq, err := GenerateExpressionSequence(in)
assert.NoError(t, err)
err = seq.Do(context.Background(), d)
@ -95,7 +96,7 @@ func Test_pcxtSpecial(t *testing.T) {
codes = c
return nil
}
d := NewPCXTDriver(sendCodes, -1, -1)
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
seq, err := GenerateExpressionSequence(in)
assert.NoError(t, err)
err = seq.Do(context.Background(), d)
@ -114,10 +115,30 @@ func Test_flushes(t *testing.T) {
actual = append(actual, c)
return nil
}
d := NewPCXTDriver(sendCodes, -1, -1)
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
seq, err := GenerateExpressionSequence(in)
assert.NoError(t, err)
err = seq.Do(context.Background(), d)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func Test_KeyIntervalNotGiven(t *testing.T) {
var codes []string
sendCodes := func(c []string) error {
codes = c
return nil
}
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
assert.Equal(t, d.interval, time.Duration(100)*time.Millisecond)
}
func Test_KeyIntervalGiven(t *testing.T) {
var codes []string
sendCodes := func(c []string) error {
codes = c
return nil
}
d := NewPCXTDriver(sendCodes, -1, time.Duration(5000)*time.Millisecond)
assert.Equal(t, d.interval, time.Duration(5000)*time.Millisecond)
}

View File

@ -25,7 +25,7 @@ type vncDriver struct {
err error
}
func NewVNCDriver(c VNCKeyEvent, interval int) *vncDriver {
func NewVNCDriver(c VNCKeyEvent, interval time.Duration) *vncDriver {
// We delay (default 100ms) between each key event to allow for CPU or
// network latency. See PackerKeyEnv for tuning.
keyInterval := common.PackerKeyDefault
@ -33,8 +33,8 @@ func NewVNCDriver(c VNCKeyEvent, interval int) *vncDriver {
keyInterval = delay
}
// override interval based on builder-specific override.
if interval >= 0 {
keyInterval = time.Duration(interval) * time.Millisecond
if interval > time.Duration(0) {
keyInterval = interval
}
// Scancodes reference: https://github.com/qemu/qemu/blob/master/ui/vnc_keysym.h

View File

@ -3,6 +3,7 @@ package bootcommand
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -30,10 +31,22 @@ func Test_vncSpecialLookup(t *testing.T) {
{0xFFE2, true},
}
s := &sender{}
d := NewVNCDriver(s, -1)
d := NewVNCDriver(s, time.Duration(0))
seq, err := GenerateExpressionSequence(in)
assert.NoError(t, err)
err = seq.Do(context.Background(), d)
assert.NoError(t, err)
assert.Equal(t, expected, s.e)
}
func Test_vncIntervalNotGiven(t *testing.T) {
s := &sender{}
d := NewVNCDriver(s, time.Duration(0))
assert.Equal(t, d.interval, time.Duration(100)*time.Millisecond)
}
func Test_vncIntervalGiven(t *testing.T) {
s := &sender{}
d := NewVNCDriver(s, time.Duration(5000)*time.Millisecond)
assert.Equal(t, d.interval, time.Duration(5000)*time.Millisecond)
}

View File

@ -394,15 +394,13 @@ contention. You can tune this delay on a per-builder basis by specifying
"builders": [
{
"type": "qemu",
"boot_key_interval": "10"
"boot_key_interval": "10ms"
...
}
]
}
```
Note that this option will always be measured in milliseconds.
<%= partial "partials/builders/boot-command" %>
Example boot command. This is actually a working boot command used to start an

View File

@ -345,15 +345,13 @@ contention. If you notice missing keys, you can tune this delay by specifying
"builders": [
{
"type": "virtualbox",
"boot_keygroup_interval": "500"
"boot_keygroup_interval": "500ms"
...
}
]
}
```
Note that this option will always be measured in milliseconds.
<%= partial "partials/builders/boot-command" %>
Example boot command. This is actually a working boot command used to start an

View File

@ -308,16 +308,13 @@ contention. If you notice missing keys, you can tune this delay by specifying
"builders": [
{
"type": "virtualbox",
"boot_keygroup_interval": "500"
"boot_keygroup_interval": "500ms",
...
}
]
}
```
Note that this option will always be measured in milliseconds.
<%= partial "partials/builders/boot-command" %>
Example boot command. This is actually a working boot command used to start an

View File

@ -447,15 +447,13 @@ contention. You can tune this delay on a per-builder basis by specifying
"builders": [
{
"type": "vmware-iso",
"boot_key_interval": "10"
"boot_key_interval": "10ms"
...
}
]
}
```
Note that this option will always be measured in milliseconds.
<%= partial "partials/builders/boot-command" %>
-&gt; **Note**: for the `HTTPIP` to be resolved correctly, your VM's network

View File

@ -223,15 +223,13 @@ contention. You can tune this delay on a per-builder basis by specifying
"builders": [
{
"type": "vmware-vmx",
"boot_key_interval": "10"
"boot_key_interval": "10ms"
...
}
]
}
```
Note that this option will always be measured in milliseconds.
<%= partial "partials/builders/boot-command" %>
Example boot command. This is actually a working boot command used to start an