2014-04-17 22:14:00 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-29 01:50:16 -04:00
|
|
|
"io"
|
2014-04-17 22:14:00 -04:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-29 01:50:16 -04:00
|
|
|
const attachWait = 5 * time.Second
|
|
|
|
|
2014-09-19 08:30:02 -04:00
|
|
|
func TestAttachMultipleAndRestart(t *testing.T) {
|
2014-07-29 01:50:16 -04:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
|
|
|
endGroup := &sync.WaitGroup{}
|
|
|
|
startGroup := &sync.WaitGroup{}
|
|
|
|
endGroup.Add(3)
|
|
|
|
startGroup.Add(3)
|
|
|
|
|
|
|
|
if err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
startDone := make(chan struct{})
|
|
|
|
endDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
endGroup.Wait()
|
|
|
|
close(endDone)
|
2014-06-13 13:32:25 -04:00
|
|
|
}()
|
|
|
|
|
2014-04-17 22:14:00 -04:00
|
|
|
go func() {
|
2014-07-29 01:50:16 -04:00
|
|
|
startGroup.Wait()
|
|
|
|
close(startDone)
|
2014-04-17 22:14:00 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
go func() {
|
|
|
|
c := exec.Command(dockerBinary, "attach", "attacher")
|
|
|
|
|
2014-07-29 01:50:16 -04:00
|
|
|
defer func() {
|
|
|
|
c.Wait()
|
|
|
|
endGroup.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, err := c.StdoutPipe()
|
2014-04-17 22:14:00 -04:00
|
|
|
if err != nil {
|
2014-07-29 01:50:16 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-03 20:42:25 -05:00
|
|
|
if err := c.Start(); err != nil {
|
2014-07-29 01:50:16 -04:00
|
|
|
t.Fatal(err)
|
2014-04-17 22:14:00 -04:00
|
|
|
}
|
2014-07-29 01:50:16 -04:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
if _, err := out.Read(buf); err != nil && err != io.EOF {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
startGroup.Done()
|
|
|
|
|
|
|
|
if !strings.Contains(string(buf), "hello") {
|
|
|
|
t.Fatalf("unexpected output %s expected hello\n", string(buf))
|
2014-04-17 22:14:00 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-07-29 01:50:16 -04:00
|
|
|
select {
|
|
|
|
case <-startDone:
|
|
|
|
case <-time.After(attachWait):
|
|
|
|
t.Fatalf("Attaches did not initialize properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "kill", "attacher")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-endDone:
|
|
|
|
case <-time.After(attachWait):
|
|
|
|
t.Fatalf("Attaches did not finish properly")
|
|
|
|
}
|
2014-04-17 22:14:00 -04:00
|
|
|
|
2014-06-13 13:32:25 -04:00
|
|
|
logDone("attach - multiple attach")
|
2014-04-17 22:14:00 -04:00
|
|
|
}
|