From 1cea606f12639ef4dd120efa43490e3190f60d92 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 2 Jun 2013 23:27:01 -0700 Subject: [PATCH] communicator/ssh: Adhere to new Communicator interface --- communicator/ssh/communicator.go | 32 ++++++++------------------- communicator/ssh/communicator_test.go | 18 ++++++++++----- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index 0c6691795..f02520f0e 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -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 diff --git a/communicator/ssh/communicator_test.go b/communicator/ssh/communicator_test.go index e83d3d571..189b7ec15 100644 --- a/communicator/ssh/communicator_test.go +++ b/communicator/ssh/communicator_test.go @@ -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()) } }