mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
fix races in pkg/integration/cmd
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
parent
6fafd07282
commit
e17f77ec51
1 changed files with 22 additions and 4 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -49,6 +50,23 @@ func ProcessExitCode(err error) (exitCode int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type lockedBuffer struct {
|
||||||
|
m sync.RWMutex
|
||||||
|
buf bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (buf *lockedBuffer) Write(b []byte) (int, error) {
|
||||||
|
buf.m.Lock()
|
||||||
|
defer buf.m.Unlock()
|
||||||
|
return buf.buf.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (buf *lockedBuffer) String() string {
|
||||||
|
buf.m.RLock()
|
||||||
|
defer buf.m.RUnlock()
|
||||||
|
return buf.buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
// Result stores the result of running a command
|
// Result stores the result of running a command
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Cmd *exec.Cmd
|
Cmd *exec.Cmd
|
||||||
|
@ -56,8 +74,8 @@ type Result struct {
|
||||||
Error error
|
Error error
|
||||||
// Timeout is true if the command was killed because it ran for too long
|
// Timeout is true if the command was killed because it ran for too long
|
||||||
Timeout bool
|
Timeout bool
|
||||||
outBuffer *bytes.Buffer
|
outBuffer *lockedBuffer
|
||||||
errBuffer *bytes.Buffer
|
errBuffer *lockedBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assert compares the Result against the Expected struct, and fails the test if
|
// Assert compares the Result against the Expected struct, and fails the test if
|
||||||
|
@ -255,8 +273,8 @@ func buildCmd(cmd Cmd) *Result {
|
||||||
default:
|
default:
|
||||||
execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...)
|
execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...)
|
||||||
}
|
}
|
||||||
outBuffer := new(bytes.Buffer)
|
outBuffer := new(lockedBuffer)
|
||||||
errBuffer := new(bytes.Buffer)
|
errBuffer := new(lockedBuffer)
|
||||||
|
|
||||||
execCmd.Stdin = cmd.Stdin
|
execCmd.Stdin = cmd.Stdin
|
||||||
execCmd.Dir = cmd.Dir
|
execCmd.Dir = cmd.Dir
|
||||||
|
|
Loading…
Reference in a new issue