From ade3795123bef99e5081a05178964b48a143be47 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 1 Jun 2013 18:49:49 -0700 Subject: [PATCH] packer: RemoteCommand.StderrChan --- packer/communicator.go | 20 ++++++++++++++++++++ packer/communicator_test.go | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/packer/communicator.go b/packer/communicator.go index 183b78158..dd322ff23 100644 --- a/packer/communicator.go +++ b/packer/communicator.go @@ -40,10 +40,30 @@ type RemoteCommand struct { exitChans []chan<- int exitChanLock sync.Mutex + errChans []chan<- string + errChanLock sync.Mutex outChans []chan<- string outChanLock sync.Mutex } +// StderrStream returns a channel that will be sent all the +func (r *RemoteCommand) StderrChan() <-chan string { + r.errChanLock.Lock() + defer r.errChanLock.Unlock() + + // If no channels have been made, make that slice and start + // the goroutine to read and send to them + if r.errChans == nil { + r.errChans = make([]chan<- string, 0, 5) + go r.channelReader(r.Stderr, &r.errChans, &r.errChanLock) + } + + // Create the channel, append it to the channels we care about + errChan := make(chan string, 10) + r.errChans = append(r.errChans, errChan) + return errChan +} + // StdoutStream returns a channel that will be sent all the output // of stdout as it comes. The output isn't guaranteed to be a full line. // When the channel is closed, the process is exited. diff --git a/packer/communicator_test.go b/packer/communicator_test.go index 5d0b65ebe..f5865430d 100644 --- a/packer/communicator_test.go +++ b/packer/communicator_test.go @@ -31,6 +31,29 @@ func TestRemoteCommand_ExitChan(t *testing.T) { } } +func TestRemoteCommand_StderrChan(t *testing.T) { + expected := "DATA!!!" + + stderrBuf := new(bytes.Buffer) + stderrBuf.WriteString(expected) + + rc := &RemoteCommand{} + rc.Stderr = stderrBuf + + errChan := rc.StderrChan() + + results := new(bytes.Buffer) + for data := range errChan { + results.WriteString(data) + } + + if results.String() != expected { + t.Fatalf( + "outputs didn't match:\ngot:\n%s\nexpected:\n%s", + results.String(), stderrBuf.String()) + } +} + func TestRemoteCommand_StdoutChan(t *testing.T) { expected := "DATA!!!"