f555e7a9f2
* I had to contextualise Communicator.Start and RemoteCmd.StartWithUi NOTE: Communicator.Start starts a RemoteCmd but RemoteCmd.StartWithUi will run the cmd and wait for a return, so I renamed StartWithUi to RunWithUi so that the intent is clearer. Ideally in the future RunWithUi will be named back to StartWithUi and the exit status or wait funcs of the command will allow to wait for a return. If you do so please read carrefully https://golang.org/pkg/os/exec/#Cmd.Stdout to avoid a deadlock * cmd.ExitStatus to cmd.ExitStatus() is now blocking to avoid race conditions * also had to simplify StartWithUi
47 lines
792 B
Go
47 lines
792 B
Go
package shell_local
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
func TestCommunicator_impl(t *testing.T) {
|
|
var _ packer.Communicator = new(Communicator)
|
|
}
|
|
|
|
func TestCommunicator(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("windows not supported for this test")
|
|
return
|
|
}
|
|
|
|
c := &Communicator{
|
|
ExecuteCommand: []string{"/bin/sh", "-c", "echo foo"},
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
cmd := &packer.RemoteCmd{
|
|
Stdout: &buf,
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx, cmd); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
if cmd.ExitStatus() != 0 {
|
|
t.Fatalf("err bad exit status: %d", cmd.ExitStatus())
|
|
}
|
|
|
|
if strings.TrimSpace(buf.String()) != "foo" {
|
|
t.Fatalf("bad: %s", buf.String())
|
|
}
|
|
}
|