2013-03-30 09:55:47 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-03-27 15:16:03 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2013-03-30 09:55:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
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 {
|
2014-04-17 17:43:01 -04:00
|
|
|
l := globalDaemon.List()
|
2014-08-31 11:20:35 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-03-31 23:52:35 -04:00
|
|
|
func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
|
2013-03-30 09:55:47 -04:00
|
|
|
c := make(chan bool)
|
|
|
|
|
|
|
|
// Make sure we are not too long
|
|
|
|
go func() {
|
|
|
|
time.Sleep(d)
|
|
|
|
c <- true
|
|
|
|
}()
|
2013-03-31 23:52:35 -04:00
|
|
|
go func() {
|
|
|
|
f()
|
|
|
|
c <- false
|
|
|
|
}()
|
2013-07-18 10:54:58 -04:00
|
|
|
if <-c && msg != "" {
|
2013-03-31 23:52:35 -04:00
|
|
|
t.Fatal(msg)
|
2013-03-30 09:55:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:42:36 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-03-30 09:55:47 -04:00
|
|
|
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
|
|
|
|
}
|
2014-01-21 23:42:36 -05:00
|
|
|
if err := expectPipe(output, r); err != nil {
|
2013-03-30 09:55:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|