2015-01-16 21:52:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-07-09 14:49:41 -04:00
|
|
|
"io"
|
2015-07-01 12:10:31 -04:00
|
|
|
"net/http"
|
2015-07-09 14:49:41 -04:00
|
|
|
"net/http/httputil"
|
2015-01-16 21:52:27 -05:00
|
|
|
"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-06-16 10:08:18 -04:00
|
|
|
"golang.org/x/net/websocket"
|
2015-01-16 21:52:27 -05:00
|
|
|
)
|
|
|
|
|
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))
|
2015-04-27 07:56:55 -04:00
|
|
|
|
|
|
|
outChan := make(chan error)
|
2015-01-16 21:52:27 -05:00
|
|
|
go func() {
|
2015-04-27 07:56:55 -04:00
|
|
|
_, err := ws.Read(actual)
|
|
|
|
outChan <- err
|
|
|
|
close(outChan)
|
2015-01-16 21:52:27 -05:00
|
|
|
}()
|
|
|
|
|
2015-04-27 07:56:55 -04:00
|
|
|
inChan := make(chan error)
|
2015-01-16 21:52:27 -05:00
|
|
|
go func() {
|
2015-04-27 07:56:55 -04:00
|
|
|
_, err := ws.Write(expected)
|
|
|
|
inChan <- err
|
|
|
|
close(inChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-inChan:
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
2015-04-27 07:56:55 -04:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout writing to ws")
|
|
|
|
}
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2015-04-27 07:56:55 -04:00
|
|
|
select {
|
|
|
|
case err := <-outChan:
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout reading from ws")
|
|
|
|
}
|
2015-01-16 21:52:27 -05:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-07-01 12:10:31 -04:00
|
|
|
|
|
|
|
// regression gh14320
|
|
|
|
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
|
|
|
|
status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusNotFound)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expected := "no such id: doesnotexist\n"
|
|
|
|
if !strings.Contains(string(body), expected) {
|
|
|
|
c.Fatalf("Expected response body to contain %q", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
|
|
|
|
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusNotFound)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expected := "no such id: doesnotexist\n"
|
|
|
|
if !strings.Contains(string(body), expected) {
|
|
|
|
c.Fatalf("Expected response body to contain %q", expected)
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 14:49:41 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-dit", "busybox", "cat")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
defer r.Close()
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
conn, err := sockConn(time.Duration(10 * time.Second))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
containerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
client := httputil.NewClientConn(conn, nil)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
// Do POST attach request
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
c.Assert(resp.StatusCode, check.Equals, http.StatusOK)
|
|
|
|
// If we check the err, we get a ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"}
|
|
|
|
// This means that the remote requested this be the last request serviced, is this okay?
|
|
|
|
|
|
|
|
// Test read and write to the attached container
|
|
|
|
expected := []byte("hello")
|
|
|
|
actual := make([]byte, len(expected))
|
|
|
|
|
|
|
|
outChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := r.Read(actual)
|
|
|
|
outChan <- err
|
|
|
|
close(outChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
inChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := w.Write(expected)
|
|
|
|
inChan <- err
|
|
|
|
close(inChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-inChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout writing to stdout")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-outChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout reading from stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, actual) {
|
|
|
|
c.Fatal("Expected output to match input")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPostContainersAttachStderr(c *check.C) {
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
defer r.Close()
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
conn, err := sockConn(time.Duration(10 * time.Second))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
containerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
client := httputil.NewClientConn(conn, nil)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
// Do POST attach request
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
c.Assert(resp.StatusCode, check.Equals, http.StatusOK)
|
|
|
|
// If we check the err, we get a ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"}
|
|
|
|
// This means that the remote requested this be the last request serviced, is this okay?
|
|
|
|
|
|
|
|
// Test read and write to the attached container
|
|
|
|
expected := []byte("hello")
|
|
|
|
actual := make([]byte, len(expected))
|
|
|
|
|
|
|
|
outChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := r.Read(actual)
|
|
|
|
outChan <- err
|
|
|
|
close(outChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
inChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := w.Write(expected)
|
|
|
|
inChan <- err
|
|
|
|
close(inChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-inChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout writing to stdout")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-outChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout reading from stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, actual) {
|
|
|
|
c.Fatal("Expected output to match input")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|