Commit 1cea606f authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

communicator/ssh: Adhere to new Communicator interface

parent a21fe8c4
......@@ -23,33 +23,19 @@ func New(c net.Conn, config *ssh.ClientConfig) (result *comm, err error) {
return
}
func (c *comm) Start(cmd string) (remote *packer.RemoteCommand, err error) {
func (c *comm) Start(cmd *packer.RemoteCmd) (err error) {
session, err := c.client.NewSession()
if err != nil {
return
}
// Create the buffers to store our stdin/stdout/stderr
stdin := new(bytes.Buffer)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
// Setup our session
session.Stdin = stdin
session.Stdout = stdout
session.Stderr = stderr
// Setup the remote command
remote = &packer.RemoteCommand{
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
Exited: false,
ExitStatus: -1,
}
session.Stdin = cmd.Stdin
session.Stdout = cmd.Stdout
session.Stderr = cmd.Stderr
log.Printf("starting remote command: %s", cmd)
err = session.Start(cmd + "\n")
log.Printf("starting remote command: %s", cmd.Command)
err = session.Start(cmd.Command + "\n")
if err != nil {
return
}
......@@ -60,15 +46,15 @@ func (c *comm) Start(cmd string) (remote *packer.RemoteCommand, err error) {
defer session.Close()
err := session.Wait()
remote.ExitStatus = 0
cmd.ExitStatus = 0
if err != nil {
exitErr, ok := err.(*ssh.ExitError)
if ok {
remote.ExitStatus = exitErr.ExitStatus()
cmd.ExitStatus = exitErr.ExitStatus()
}
}
remote.Exited = true
cmd.Exited = true
}()
return
......
......@@ -8,6 +8,7 @@ import (
"net"
"strings"
"testing"
"time"
)
// private key for mock server
......@@ -178,19 +179,24 @@ func TestStart(t *testing.T) {
t.Fatalf("error connecting to SSH: %s", err)
}
remote, err := client.Start("echo foo")
var cmd packer.RemoteCmd
stdout := new(bytes.Buffer)
cmd.Command = "echo foo"
cmd.Stdout = stdout
err = client.Start(&cmd)
if err != nil {
t.Fatalf("error executing command: %s", err)
}
// Wait for it to complete
t.Log("Waiting for command to complete")
remote.Wait()
for !cmd.Exited {
time.Sleep(50 * time.Millisecond)
}
// Should have the correct output
output := remote.Stdout.(*bytes.Buffer).String()
if output != "ack: echo foo" {
t.Fatalf("unknown output: %#v", output)
if stdout.String() != "ack: echo foo" {
t.Fatalf("unknown output: %#v", stdout.String())
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment