2021-02-02 12:05:04 -05:00
|
|
|
package packer
|
2013-06-01 22:35:19 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-30 13:38:48 -04:00
|
|
|
"bytes"
|
2013-07-25 22:24:49 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-07-30 13:38:48 -04:00
|
|
|
"strings"
|
2013-06-01 22:35:19 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
process := helperProcess("mock")
|
2021-02-02 12:05:04 -05:00
|
|
|
c := NewClient(&PluginClientConfig{Cmd: process})
|
2013-06-01 22:35:19 -04:00
|
|
|
defer c.Kill()
|
|
|
|
|
|
|
|
// Test that it parses the proper address
|
|
|
|
addr, err := c.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err should be nil, got %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 15:24:45 -05:00
|
|
|
if addr.Network() != "tcp" {
|
|
|
|
t.Fatalf("bad: %#v", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr.String() != ":1234" {
|
|
|
|
t.Fatalf("bad: %#v", addr)
|
2013-06-01 22:35:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that it exits properly if killed
|
|
|
|
c.Kill()
|
|
|
|
|
|
|
|
if process.ProcessState == nil {
|
|
|
|
t.Fatal("should have process state")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that it knows it is exited
|
|
|
|
if !c.Exited() {
|
|
|
|
t.Fatal("should say client has exited")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 12:51:14 -04:00
|
|
|
func TestClientStart_badVersion(t *testing.T) {
|
2021-02-02 12:05:04 -05:00
|
|
|
config := &PluginClientConfig{
|
2013-09-22 12:51:14 -04:00
|
|
|
Cmd: helperProcess("bad-version"),
|
|
|
|
StartTimeout: 50 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := NewClient(config)
|
|
|
|
defer c.Kill()
|
|
|
|
|
|
|
|
_, err := c.Start()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-01 22:35:19 -04:00
|
|
|
func TestClient_Start_Timeout(t *testing.T) {
|
2021-02-02 12:05:04 -05:00
|
|
|
config := &PluginClientConfig{
|
2013-06-11 17:09:31 -04:00
|
|
|
Cmd: helperProcess("start-timeout"),
|
2013-06-11 13:39:06 -04:00
|
|
|
StartTimeout: 50 * time.Millisecond,
|
|
|
|
}
|
2013-06-01 22:35:19 -04:00
|
|
|
|
2013-06-11 13:39:06 -04:00
|
|
|
c := NewClient(config)
|
|
|
|
defer c.Kill()
|
2013-06-01 22:35:19 -04:00
|
|
|
|
|
|
|
_, err := c.Start()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should not be nil")
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 22:24:49 -04:00
|
|
|
|
2013-07-30 13:38:48 -04:00
|
|
|
func TestClient_Stderr(t *testing.T) {
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
process := helperProcess("stderr")
|
2021-02-02 12:05:04 -05:00
|
|
|
c := NewClient(&PluginClientConfig{
|
2013-07-30 13:38:48 -04:00
|
|
|
Cmd: process,
|
|
|
|
Stderr: stderr,
|
|
|
|
})
|
|
|
|
defer c.Kill()
|
|
|
|
|
|
|
|
if _, err := c.Start(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for !c.Exited() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stderr.String(), "HELLO\n") {
|
|
|
|
t.Fatalf("bad log data: '%s'", stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stderr.String(), "WORLD\n") {
|
|
|
|
t.Fatalf("bad log data: '%s'", stderr.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 22:24:49 -04:00
|
|
|
func TestClient_Stdin(t *testing.T) {
|
|
|
|
// Overwrite stdin for this test with a temporary file
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
if _, err = tf.WriteString("hello"); err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tf.Sync(); err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = tf.Seek(0, 0); err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oldStdin := os.Stdin
|
|
|
|
defer func() { os.Stdin = oldStdin }()
|
|
|
|
os.Stdin = tf
|
|
|
|
|
|
|
|
process := helperProcess("stdin")
|
2021-02-02 12:05:04 -05:00
|
|
|
c := NewClient(&PluginClientConfig{Cmd: process})
|
2013-07-25 22:24:49 -04:00
|
|
|
defer c.Kill()
|
|
|
|
|
|
|
|
_, err = c.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if c.Exited() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !process.ProcessState.Success() {
|
|
|
|
t.Fatal("process didn't exit cleanly")
|
|
|
|
}
|
|
|
|
}
|