Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
810d17c0
Commit
810d17c0
authored
Jun 01, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: RemoteCommand.StdoutChan works + tests
parent
ace53450
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
1 deletion
+69
-1
packer/communicator.go
packer/communicator.go
+45
-1
packer/communicator_test.go
packer/communicator_test.go
+24
-0
No files found.
packer/communicator.go
View file @
810d17c0
package
packer
package
packer
import
(
import
(
"bufio"
"io"
"io"
"log"
"log"
"sync"
"sync"
...
@@ -39,13 +40,56 @@ type RemoteCommand struct {
...
@@ -39,13 +40,56 @@ type RemoteCommand struct {
exitChans
[]
chan
<-
int
exitChans
[]
chan
<-
int
exitChanLock
sync
.
Mutex
exitChanLock
sync
.
Mutex
outChans
[]
chan
<-
string
outChanLock
sync
.
Mutex
}
}
// 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.
func
(
r
*
RemoteCommand
)
StdoutChan
()
<-
chan
string
{
func
(
r
*
RemoteCommand
)
StdoutChan
()
<-
chan
string
{
return
nil
r
.
outChanLock
.
Lock
()
defer
r
.
outChanLock
.
Unlock
()
// If no output channels have been made yet, then make that slice
// and start the goroutine to read and send to them.
if
r
.
outChans
==
nil
{
r
.
outChans
=
make
([]
chan
<-
string
,
0
,
5
)
go
func
()
{
buf
:=
bufio
.
NewReader
(
r
.
Stdout
)
var
err
error
for
err
!=
io
.
EOF
{
var
data
[]
byte
data
,
err
=
buf
.
ReadSlice
(
'\n'
)
if
len
(
data
)
>
0
{
for
_
,
ch
:=
range
r
.
outChans
{
// Note: this blocks if the channel is full (they
// are buffered by default). What to do?
ch
<-
string
(
data
)
}
}
}
// Clean up the channels by closing them and setting the
// list to nil.
r
.
outChanLock
.
Lock
()
defer
r
.
outChanLock
.
Unlock
()
for
_
,
ch
:=
range
r
.
outChans
{
close
(
ch
)
}
r
.
outChans
=
nil
}()
}
// Create the channel, append it to the channels we care about
outChan
:=
make
(
chan
string
,
10
)
r
.
outChans
=
append
(
r
.
outChans
,
outChan
)
return
outChan
}
}
// ExitChan returns a channel that will be sent the exit status once
// ExitChan returns a channel that will be sent the exit status once
...
...
packer/communicator_test.go
View file @
810d17c0
package
packer
package
packer
import
(
import
(
"bytes"
"testing"
"testing"
"time"
"time"
)
)
...
@@ -30,6 +31,29 @@ func TestRemoteCommand_ExitChan(t *testing.T) {
...
@@ -30,6 +31,29 @@ func TestRemoteCommand_ExitChan(t *testing.T) {
}
}
}
}
func
TestRemoteCommand_StdoutChan
(
t
*
testing
.
T
)
{
expected
:=
"DATA!!!"
stdoutBuf
:=
new
(
bytes
.
Buffer
)
stdoutBuf
.
WriteString
(
expected
)
rc
:=
&
RemoteCommand
{}
rc
.
Stdout
=
stdoutBuf
outChan
:=
rc
.
StdoutChan
()
results
:=
new
(
bytes
.
Buffer
)
for
data
:=
range
outChan
{
results
.
WriteString
(
data
)
}
if
results
.
String
()
!=
expected
{
t
.
Fatalf
(
"outputs didn't match:
\n
got:
\n
%s
\n
expected:
\n
%s"
,
results
.
String
(),
stdoutBuf
.
String
())
}
}
func
TestRemoteCommand_WaitBlocks
(
t
*
testing
.
T
)
{
func
TestRemoteCommand_WaitBlocks
(
t
*
testing
.
T
)
{
t
.
Parallel
()
t
.
Parallel
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment