2018-02-23 16:26:31 -05:00
|
|
|
package shell_local
|
2015-06-19 18:27:44 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2015-06-19 18:27:44 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-19 18:27:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2018-02-28 17:35:42 -05:00
|
|
|
ExecuteCommand: []string{"/bin/sh", "-c", "echo foo"},
|
2015-06-19 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2018-02-28 17:35:42 -05:00
|
|
|
Stdout: &buf,
|
2015-06-19 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
if err := c.Start(ctx, cmd); err != nil {
|
2015-06-19 18:27:44 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
|
|
|
t.Fatalf("err bad exit status: %d", cmd.ExitStatus())
|
2015-06-19 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(buf.String()) != "foo" {
|
|
|
|
t.Fatalf("bad: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|