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"
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2015-06-19 18:27:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommunicator_impl(t *testing.T) {
|
2020-11-19 18:10:00 -05:00
|
|
|
var _ packersdk.Communicator = new(Communicator)
|
2015-06-19 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.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())
|
|
|
|
}
|
|
|
|
}
|