2013-05-28 21:02:14 -04:00
|
|
|
package packer
|
2013-06-03 13:49:23 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-23 23:35:05 -04:00
|
|
|
"bytes"
|
|
|
|
"strings"
|
2013-06-03 13:49:23 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-07-23 23:35:05 -04:00
|
|
|
func TestRemoteCmd_StartWithUi(t *testing.T) {
|
|
|
|
data := "hello\nworld\nthere"
|
|
|
|
|
2013-07-23 23:38:03 -04:00
|
|
|
originalOutput := new(bytes.Buffer)
|
2013-07-23 23:35:05 -04:00
|
|
|
uiOutput := new(bytes.Buffer)
|
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
testComm := new(MockCommunicator)
|
|
|
|
testComm.StartStdout = data
|
2013-08-11 21:20:27 -04:00
|
|
|
testUi := &BasicUi{
|
2013-07-23 23:35:05 -04:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: uiOutput,
|
|
|
|
}
|
|
|
|
|
|
|
|
rc := &RemoteCmd{
|
|
|
|
Command: "test",
|
2013-07-23 23:38:03 -04:00
|
|
|
Stdout: originalOutput,
|
2013-07-23 23:35:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := rc.StartWithUi(testComm, testUi)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
rc.Wait()
|
|
|
|
|
|
|
|
expected := strings.TrimSpace(data)
|
2015-05-28 18:13:10 -04:00
|
|
|
if strings.TrimSpace(uiOutput.String()) != expected {
|
2013-07-23 23:35:05 -04:00
|
|
|
t.Fatalf("bad output: '%s'", uiOutput.String())
|
|
|
|
}
|
2013-07-23 23:38:03 -04:00
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
if originalOutput.String() != expected {
|
|
|
|
t.Fatalf("bad: %#v", originalOutput.String())
|
2013-07-23 23:38:03 -04:00
|
|
|
}
|
2013-07-23 23:35:05 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 13:49:23 -04:00
|
|
|
func TestRemoteCmd_Wait(t *testing.T) {
|
|
|
|
var cmd RemoteCmd
|
|
|
|
|
|
|
|
result := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
result <- true
|
|
|
|
}()
|
|
|
|
|
2013-07-29 15:04:48 -04:00
|
|
|
cmd.SetExited(42)
|
2013-06-03 13:49:23 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-result:
|
|
|
|
// Success
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Fatal("never got exit notification")
|
|
|
|
}
|
|
|
|
}
|