2015-01-16 21:52:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os/exec"
|
2015-04-06 09:21:18 -04:00
|
|
|
"strings"
|
2015-02-12 14:51:28 -05:00
|
|
|
"time"
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
|
2015-01-16 21:52:27 -05:00
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
2015-01-16 21:52:27 -05:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-dit", "busybox", "cat")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:51:28 -05:00
|
|
|
rwc, err := sockConn(time.Duration(10 * time.Second))
|
2015-01-16 21:52:27 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-01-16 21:52:27 -05:00
|
|
|
config, err := websocket.NewConfig(
|
|
|
|
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
|
|
|
|
"http://localhost",
|
|
|
|
)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := websocket.NewClient(config, rwc)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
expected := []byte("hello")
|
|
|
|
actual := make([]byte, len(expected))
|
|
|
|
outChan := make(chan string)
|
|
|
|
go func() {
|
|
|
|
if _, err := ws.Read(actual); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
outChan <- "done"
|
|
|
|
}()
|
|
|
|
|
|
|
|
inChan := make(chan string)
|
|
|
|
go func() {
|
|
|
|
if _, err := ws.Write(expected); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
inChan <- "done"
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-inChan
|
|
|
|
<-outChan
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, actual) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected output on websocket to match input")
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
|
|
|
}
|