Change MonitorCmd/sendkeys to Sendkey

pull/8083/head
Calle Pettersson 7 years ago
parent 8c2c1a82cb
commit df41b56d9a

@ -102,13 +102,10 @@ func (p *proxmoxDriver) SendSpecial(special string, action bootcommand.KeyAction
} }
func (p *proxmoxDriver) send(keys string) error { func (p *proxmoxDriver) send(keys string) error {
res, err := p.client.MonitorCmd(p.vmRef, "sendkey "+keys) err := p.client.Sendkey(p.vmRef, keys)
if err != nil { if err != nil {
return err return err
} }
if data, ok := res["data"].(string); ok && len(data) > 0 {
return fmt.Errorf("failed to send keys: %s", data)
}
time.Sleep(p.interval) time.Sleep(p.interval)
return nil return nil

@ -30,7 +30,7 @@ type bootCommandTemplateData struct {
} }
type commandTyper interface { type commandTyper interface {
MonitorCmd(*proxmox.VmRef, string) (map[string]interface{}, error) Sendkey(*proxmox.VmRef, string) error
} }
var _ commandTyper = &proxmox.Client{} var _ commandTyper = &proxmox.Client{}

@ -13,75 +13,74 @@ import (
) )
type commandTyperMock struct { type commandTyperMock struct {
monitorCmd func(*proxmox.VmRef, string) (map[string]interface{}, error) sendkey func(*proxmox.VmRef, string) error
} }
func (m commandTyperMock) MonitorCmd(ref *proxmox.VmRef, cmd string) (map[string]interface{}, error) { func (m commandTyperMock) Sendkey(ref *proxmox.VmRef, cmd string) error {
return m.monitorCmd(ref, cmd) return m.sendkey(ref, cmd)
} }
var _ commandTyper = commandTyperMock{} var _ commandTyper = commandTyperMock{}
func TestTypeBootCommand(t *testing.T) { func TestTypeBootCommand(t *testing.T) {
cs := []struct { cs := []struct {
name string name string
builderConfig *Config builderConfig *Config
expectCallMonitorCmd bool expectCallSendkey bool
monitorCmdErr error sendkeyErr error
monitorCmdRet map[string]interface{} expectedKeysSent string
expectedKeysSent string expectedAction multistep.StepAction
expectedAction multistep.StepAction
}{ }{
{ {
name: "simple boot command is typed", name: "simple boot command is typed",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello"}}},
expectCallMonitorCmd: true, expectCallSendkey: true,
expectedKeysSent: "hello", expectedKeysSent: "hello",
expectedAction: multistep.ActionContinue, expectedAction: multistep.ActionContinue,
}, },
{ {
name: "interpolated boot command", name: "interpolated boot command",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello<enter>world"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello<enter>world"}}},
expectCallMonitorCmd: true, expectCallSendkey: true,
expectedKeysSent: "helloretworld", expectedKeysSent: "helloretworld",
expectedAction: multistep.ActionContinue, expectedAction: multistep.ActionContinue,
}, },
{ {
name: "merge multiple interpolated boot command", name: "merge multiple interpolated boot command",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"Hello World 2.0", "foo!bar@baz"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"Hello World 2.0", "foo!bar@baz"}}},
expectCallMonitorCmd: true, expectCallSendkey: true,
expectedKeysSent: "shift-hellospcshift-worldspc2dot0fooshift-1barshift-2baz", expectedKeysSent: "shift-hellospcshift-worldspc2dot0fooshift-1barshift-2baz",
expectedAction: multistep.ActionContinue, expectedAction: multistep.ActionContinue,
}, },
{ {
name: "without boot command monitorcmd should not be called", name: "without boot command sendkey should not be called",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{}}},
expectCallMonitorCmd: false, expectCallSendkey: false,
expectedAction: multistep.ActionContinue, expectedAction: multistep.ActionContinue,
}, },
{ {
name: "invalid boot command template function", name: "invalid boot command template function",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"{{ foo }}"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"{{ foo }}"}}},
expectCallMonitorCmd: false, expectCallSendkey: false,
expectedAction: multistep.ActionHalt, expectedAction: multistep.ActionHalt,
}, },
{ {
// When proxmox (or Qemu, really) doesn't recognize the keycode we send, we get no error back, but // When proxmox (or Qemu, really) doesn't recognize the keycode we send, we get no error back, but
// a map {"data": "invalid parameter: X"}, where X is the keycode. // a map {"data": "invalid parameter: X"}, where X is the keycode.
name: "invalid keys sent to proxmox", name: "invalid keys sent to proxmox",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"x"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"x"}}},
expectCallMonitorCmd: true, expectCallSendkey: true,
monitorCmdRet: map[string]interface{}{"data": "invalid parameter: x"}, sendkeyErr: fmt.Errorf("invalid parameter: x"),
expectedKeysSent: "x", expectedKeysSent: "x",
expectedAction: multistep.ActionHalt, expectedAction: multistep.ActionHalt,
}, },
{ {
name: "error in typing should return halt", name: "error in typing should return halt",
builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello"}}}, builderConfig: &Config{BootConfig: bootcommand.BootConfig{BootCommand: []string{"hello"}}},
expectCallMonitorCmd: true, expectCallSendkey: true,
monitorCmdErr: fmt.Errorf("some error"), sendkeyErr: fmt.Errorf("some error"),
expectedKeysSent: "h", expectedKeysSent: "h",
expectedAction: multistep.ActionHalt, expectedAction: multistep.ActionHalt,
}, },
} }
@ -89,17 +88,14 @@ func TestTypeBootCommand(t *testing.T) {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
accumulator := strings.Builder{} accumulator := strings.Builder{}
typer := commandTyperMock{ typer := commandTyperMock{
monitorCmd: func(ref *proxmox.VmRef, cmd string) (map[string]interface{}, error) { sendkey: func(ref *proxmox.VmRef, cmd string) error {
if !c.expectCallMonitorCmd { if !c.expectCallSendkey {
t.Error("Did not expect MonitorCmd to be called") t.Error("Did not expect sendkey to be called")
}
if !strings.HasPrefix(cmd, "sendkey ") {
t.Errorf("Expected all commands to be sendkey, got %s", cmd)
} }
accumulator.WriteString(strings.TrimPrefix(cmd, "sendkey ")) accumulator.WriteString(cmd)
return c.monitorCmdRet, c.monitorCmdErr return c.sendkeyErr
}, },
} }

Loading…
Cancel
Save