packer: RemoteCommand.StderrChan

This commit is contained in:
Mitchell Hashimoto 2013-06-01 18:49:49 -07:00
parent 3bdd74f9c5
commit ade3795123
2 changed files with 43 additions and 0 deletions

View File

@ -40,10 +40,30 @@ type RemoteCommand struct {
exitChans []chan<- int exitChans []chan<- int
exitChanLock sync.Mutex exitChanLock sync.Mutex
errChans []chan<- string
errChanLock sync.Mutex
outChans []chan<- string outChans []chan<- string
outChanLock sync.Mutex 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 // 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. // of stdout as it comes. The output isn't guaranteed to be a full line.
// When the channel is closed, the process is exited. // When the channel is closed, the process is exited.

View File

@ -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) { func TestRemoteCommand_StdoutChan(t *testing.T) {
expected := "DATA!!!" expected := "DATA!!!"