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

     1  package packer
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestRemoteCmd_StartWithUi(t *testing.T) {
    11  	data := "hello\nworld\nthere"
    12  
    13  	originalOutput := new(bytes.Buffer)
    14  	uiOutput := new(bytes.Buffer)
    15  
    16  	testComm := new(MockCommunicator)
    17  	testComm.StartStdout = data
    18  	testUi := &BasicUi{
    19  		Reader: new(bytes.Buffer),
    20  		Writer: uiOutput,
    21  	}
    22  
    23  	rc := &RemoteCmd{
    24  		Command: "test",
    25  		Stdout:  originalOutput,
    26  	}
    27  
    28  	err := rc.StartWithUi(testComm, testUi)
    29  	if err != nil {
    30  		t.Fatalf("err: %s", err)
    31  	}
    32  
    33  	rc.Wait()
    34  
    35  	expected := strings.TrimSpace(data)
    36  	if uiOutput.String() != expected+"\n" {
    37  		t.Fatalf("bad output: '%s'", uiOutput.String())
    38  	}
    39  
    40  	if originalOutput.String() != expected {
    41  		t.Fatalf("bad: %#v", originalOutput.String())
    42  	}
    43  }
    44  
    45  func TestRemoteCmd_Wait(t *testing.T) {
    46  	var cmd RemoteCmd
    47  
    48  	result := make(chan bool)
    49  	go func() {
    50  		cmd.Wait()
    51  		result <- true
    52  	}()
    53  
    54  	cmd.SetExited(42)
    55  
    56  	select {
    57  	case <-result:
    58  		// Success
    59  	case <-time.After(500 * time.Millisecond):
    60  		t.Fatal("never got exit notification")
    61  	}
    62  }