github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/plugin/client_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestClient(t *testing.T) {
    13  	process := helperProcess("mock")
    14  	c := NewClient(&ClientConfig{Cmd: process})
    15  	defer c.Kill()
    16  
    17  	// Test that it parses the proper address
    18  	addr, err := c.Start()
    19  	if err != nil {
    20  		t.Fatalf("err should be nil, got %s", err)
    21  	}
    22  
    23  	if addr.Network() != "tcp" {
    24  		t.Fatalf("bad: %#v", addr)
    25  	}
    26  
    27  	if addr.String() != ":1234" {
    28  		t.Fatalf("bad: %#v", addr)
    29  	}
    30  
    31  	// Test that it exits properly if killed
    32  	c.Kill()
    33  
    34  	if process.ProcessState == nil {
    35  		t.Fatal("should have process state")
    36  	}
    37  
    38  	// Test that it knows it is exited
    39  	if !c.Exited() {
    40  		t.Fatal("should say client has exited")
    41  	}
    42  }
    43  
    44  func TestClientStart_badVersion(t *testing.T) {
    45  	config := &ClientConfig{
    46  		Cmd:          helperProcess("bad-version"),
    47  		StartTimeout: 50 * time.Millisecond,
    48  	}
    49  
    50  	c := NewClient(config)
    51  	defer c.Kill()
    52  
    53  	_, err := c.Start()
    54  	if err == nil {
    55  		t.Fatal("err should not be nil")
    56  	}
    57  }
    58  
    59  func TestClient_Start_Timeout(t *testing.T) {
    60  	config := &ClientConfig{
    61  		Cmd:          helperProcess("start-timeout"),
    62  		StartTimeout: 50 * time.Millisecond,
    63  	}
    64  
    65  	c := NewClient(config)
    66  	defer c.Kill()
    67  
    68  	_, err := c.Start()
    69  	if err == nil {
    70  		t.Fatal("err should not be nil")
    71  	}
    72  }
    73  
    74  func TestClient_Stderr(t *testing.T) {
    75  	stderr := new(bytes.Buffer)
    76  	process := helperProcess("stderr")
    77  	c := NewClient(&ClientConfig{
    78  		Cmd:    process,
    79  		Stderr: stderr,
    80  	})
    81  	defer c.Kill()
    82  
    83  	if _, err := c.Start(); err != nil {
    84  		t.Fatalf("err: %s", err)
    85  	}
    86  
    87  	for !c.Exited() {
    88  		time.Sleep(10 * time.Millisecond)
    89  	}
    90  
    91  	if !strings.Contains(stderr.String(), "HELLO\n") {
    92  		t.Fatalf("bad log data: '%s'", stderr.String())
    93  	}
    94  
    95  	if !strings.Contains(stderr.String(), "WORLD\n") {
    96  		t.Fatalf("bad log data: '%s'", stderr.String())
    97  	}
    98  }
    99  
   100  func TestClient_Stdin(t *testing.T) {
   101  	// Overwrite stdin for this test with a temporary file
   102  	tf, err := ioutil.TempFile("", "packer")
   103  	if err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  	defer os.Remove(tf.Name())
   107  	defer tf.Close()
   108  
   109  	if _, err = tf.WriteString("hello"); err != nil {
   110  		t.Fatalf("error: %s", err)
   111  	}
   112  
   113  	if err = tf.Sync(); err != nil {
   114  		t.Fatalf("error: %s", err)
   115  	}
   116  
   117  	if _, err = tf.Seek(0, 0); err != nil {
   118  		t.Fatalf("error: %s", err)
   119  	}
   120  
   121  	oldStdin := os.Stdin
   122  	defer func() { os.Stdin = oldStdin }()
   123  	os.Stdin = tf
   124  
   125  	process := helperProcess("stdin")
   126  	c := NewClient(&ClientConfig{Cmd: process})
   127  	defer c.Kill()
   128  
   129  	_, err = c.Start()
   130  	if err != nil {
   131  		t.Fatalf("error: %s", err)
   132  	}
   133  
   134  	for {
   135  		if c.Exited() {
   136  			break
   137  		}
   138  
   139  		time.Sleep(50 * time.Millisecond)
   140  	}
   141  
   142  	if !process.ProcessState.Success() {
   143  		t.Fatal("process didn't exit cleanly")
   144  	}
   145  }