1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration/utils.go

89 lines
1.6 KiB
Go
Raw Normal View History

package docker
import (
"bufio"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/docker/docker/daemon"
)
func closeWrap(args ...io.Closer) error {
e := false
ret := fmt.Errorf("Error closing elements")
for _, c := range args {
if err := c.Close(); err != nil {
e = true
ret = fmt.Errorf("%s\n%s", ret, err)
}
}
if e {
return ret
}
return nil
}
func waitContainerStart(t *testing.T, timeout time.Duration) *daemon.Container {
var container *daemon.Container
2013-11-29 12:57:59 -05:00
setTimeout(t, "Waiting for the container to be started timed out", timeout, func() {
for {
l := globalDaemon.List()
if len(l) == 1 && l[0].IsRunning() {
2013-11-29 12:57:59 -05:00
container = l[0]
break
}
time.Sleep(10 * time.Millisecond)
}
})
if container == nil {
t.Fatal("An error occured while waiting for the container to start")
}
return container
}
func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
c := make(chan bool)
// Make sure we are not too long
go func() {
time.Sleep(d)
c <- true
}()
go func() {
f()
c <- false
}()
2013-07-18 10:54:58 -04:00
if <-c && msg != "" {
t.Fatal(msg)
}
}
func expectPipe(expected string, r io.Reader) error {
o, err := bufio.NewReader(r).ReadString('\n')
if err != nil {
return err
}
if strings.Trim(o, " \r\n") != expected {
return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", expected, o)
}
return nil
}
func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
for i := 0; i < count; i++ {
if _, err := w.Write([]byte(input)); err != nil {
return err
}
if err := expectPipe(output, r); err != nil {
return err
}
}
return nil
}