mirror of https://github.com/docker/docs.git
Merge pull request #14505 from kostickm/12905-port-container-attach-tests
Port POST container attach tests
This commit is contained in:
commit
e64af7a70e
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -98,3 +100,133 @@ func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
|
||||||
c.Fatalf("Expected response body to contain %q", expected)
|
c.Fatalf("Expected response body to contain %q", expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue