github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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  	service, err := c.Service()
    32  	if err != nil {
    33  		t.Fatalf("err: %s", err)
    34  	}
    35  	if service != "foo" {
    36  		t.Fatalf("bad: %#v", service)
    37  	}
    38  
    39  	// Test that it exits properly if killed
    40  	c.Kill()
    41  
    42  	if process.ProcessState == nil {
    43  		t.Fatal("should have process state")
    44  	}
    45  
    46  	// Test that it knows it is exited
    47  	if !c.Exited() {
    48  		t.Fatal("should say client has exited")
    49  	}
    50  }
    51  
    52  func TestClientStart_badVersion(t *testing.T) {
    53  	config := &ClientConfig{
    54  		Cmd:          helperProcess("bad-version"),
    55  		StartTimeout: 50 * time.Millisecond,
    56  	}
    57  
    58  	c := NewClient(config)
    59  	defer c.Kill()
    60  
    61  	_, err := c.Start()
    62  	if err == nil {
    63  		t.Fatal("err should not be nil")
    64  	}
    65  }
    66  
    67  func TestClient_Start_Timeout(t *testing.T) {
    68  	config := &ClientConfig{
    69  		Cmd:          helperProcess("start-timeout"),
    70  		StartTimeout: 50 * time.Millisecond,
    71  	}
    72  
    73  	c := NewClient(config)
    74  	defer c.Kill()
    75  
    76  	_, err := c.Start()
    77  	if err == nil {
    78  		t.Fatal("err should not be nil")
    79  	}
    80  }
    81  
    82  func TestClient_Stderr(t *testing.T) {
    83  	stderr := new(bytes.Buffer)
    84  	process := helperProcess("stderr")
    85  	c := NewClient(&ClientConfig{
    86  		Cmd:    process,
    87  		Stderr: stderr,
    88  	})
    89  	defer c.Kill()
    90  
    91  	if _, err := c.Start(); err != nil {
    92  		t.Fatalf("err: %s", err)
    93  	}
    94  
    95  	for !c.Exited() {
    96  		time.Sleep(10 * time.Millisecond)
    97  	}
    98  
    99  	if !strings.Contains(stderr.String(), "HELLO\n") {
   100  		t.Fatalf("bad log data: '%s'", stderr.String())
   101  	}
   102  
   103  	if !strings.Contains(stderr.String(), "WORLD\n") {
   104  		t.Fatalf("bad log data: '%s'", stderr.String())
   105  	}
   106  }
   107  
   108  func TestClient_Stdin(t *testing.T) {
   109  	// Overwrite stdin for this test with a temporary file
   110  	tf, err := ioutil.TempFile("", "packer")
   111  	if err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  	defer os.Remove(tf.Name())
   115  	defer tf.Close()
   116  
   117  	if _, err = tf.WriteString("hello"); err != nil {
   118  		t.Fatalf("error: %s", err)
   119  	}
   120  
   121  	if err = tf.Sync(); err != nil {
   122  		t.Fatalf("error: %s", err)
   123  	}
   124  
   125  	if _, err = tf.Seek(0, 0); err != nil {
   126  		t.Fatalf("error: %s", err)
   127  	}
   128  
   129  	oldStdin := os.Stdin
   130  	defer func() { os.Stdin = oldStdin }()
   131  	os.Stdin = tf
   132  
   133  	process := helperProcess("stdin")
   134  	c := NewClient(&ClientConfig{Cmd: process})
   135  	defer c.Kill()
   136  
   137  	_, err = c.Start()
   138  	if err != nil {
   139  		t.Fatalf("error: %s", err)
   140  	}
   141  
   142  	for {
   143  		if c.Exited() {
   144  			break
   145  		}
   146  
   147  		time.Sleep(50 * time.Millisecond)
   148  	}
   149  
   150  	if !process.ProcessState.Success() {
   151  		t.Fatal("process didn't exit cleanly")
   152  	}
   153  }