mirror of
https://github.com/moby/moby.git
synced 2025-07-30 18:23:29 +03:00
Replace some checkers and assertions with gotest.tools
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@ -14,12 +14,13 @@ import (
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/internal/test/request"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/go-check/check"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/websocket"
|
||||
"gotest.tools/assert"
|
||||
is "gotest.tools/assert/cmp"
|
||||
)
|
||||
|
||||
func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
||||
@ -27,17 +28,17 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
|
||||
|
||||
rwc, err := request.SockConn(time.Duration(10*time.Second), request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
config, err := websocket.NewConfig(
|
||||
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
|
||||
"http://localhost",
|
||||
)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
ws, err := websocket.NewClient(config, rwc)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
defer ws.Close()
|
||||
|
||||
expected := []byte("hello")
|
||||
@ -59,41 +60,41 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
||||
|
||||
select {
|
||||
case err := <-inChan:
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
case <-time.After(5 * time.Second):
|
||||
c.Fatal("Timeout writing to ws")
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-outChan:
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
case <-time.After(5 * time.Second):
|
||||
c.Fatal("Timeout reading from ws")
|
||||
}
|
||||
|
||||
c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data"))
|
||||
assert.Assert(c, is.DeepEqual(actual, expected), "Websocket didn't return the expected data")
|
||||
}
|
||||
|
||||
// regression gh14320
|
||||
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
|
||||
resp, _, err := request.Post("/containers/doesnotexist/attach")
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
// connection will shutdown, err should be "persistent connection closed"
|
||||
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
||||
assert.Equal(c, resp.StatusCode, http.StatusNotFound)
|
||||
content, err := request.ReadBody(resp.Body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expected := "No such container: doesnotexist\r\n"
|
||||
c.Assert(string(content), checker.Equals, expected)
|
||||
assert.Equal(c, string(content), expected)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
|
||||
res, body, err := request.Get("/containers/doesnotexist/attach/ws")
|
||||
c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.Equal(c, res.StatusCode, http.StatusNotFound)
|
||||
assert.NilError(c, err)
|
||||
b, err := request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expected := "No such container: doesnotexist"
|
||||
c.Assert(getErrorMessage(c, b), checker.Contains, expected)
|
||||
assert.Assert(c, strings.Contains(getErrorMessage(c, b), expected))
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
@ -103,7 +104,7 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
defer conn.Close()
|
||||
expected := []byte("success")
|
||||
_, err := conn.Write(expected)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||
lenHeader := 0
|
||||
@ -112,29 +113,29 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
}
|
||||
actual := make([]byte, len(expected)+lenHeader)
|
||||
_, err = io.ReadFull(br, actual)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
if !tty {
|
||||
fdMap := map[string]byte{
|
||||
"stdin": 0,
|
||||
"stdout": 1,
|
||||
"stderr": 2,
|
||||
}
|
||||
c.Assert(actual[0], checker.Equals, fdMap[stream])
|
||||
assert.Equal(c, actual[0], fdMap[stream])
|
||||
}
|
||||
c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
||||
assert.Assert(c, is.DeepEqual(actual[lenHeader:], expected), "Attach didn't return the expected data from %s", stream)
|
||||
}
|
||||
|
||||
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
||||
defer conn.Close()
|
||||
_, err := conn.Write([]byte{'t'})
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||
actual := make([]byte, 1)
|
||||
_, err = io.ReadFull(br, actual)
|
||||
opErr, ok := err.(*net.OpError)
|
||||
c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
|
||||
c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
|
||||
assert.Assert(c, ok, "Error is expected to be *net.OpError, got %v", err)
|
||||
assert.Assert(c, opErr.Timeout(), "Read from %s is expected to timeout", stream)
|
||||
}
|
||||
|
||||
// Create a container that only emits stdout.
|
||||
@ -142,12 +143,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
cid = strings.TrimSpace(cid)
|
||||
// Attach to the container's stdout stream.
|
||||
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
// Check if the data from stdout can be received.
|
||||
expectSuccess(conn, br, "stdout", false)
|
||||
// Attach to the container's stderr stream.
|
||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
// Since the container only emits stdout, attaching to stderr should return nothing.
|
||||
expectTimeout(conn, br, "stdout")
|
||||
|
||||
@ -155,10 +156,10 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
|
||||
cid = strings.TrimSpace(cid)
|
||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expectSuccess(conn, br, "stderr", false)
|
||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expectTimeout(conn, br, "stderr")
|
||||
|
||||
// Test with tty.
|
||||
@ -166,19 +167,19 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
cid = strings.TrimSpace(cid)
|
||||
// Attach to stdout only.
|
||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expectSuccess(conn, br, "stdout", true)
|
||||
|
||||
// Attach without stdout stream.
|
||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
// Nothing should be received because both the stdout and stderr of the container will be
|
||||
// sent to the client as stdout when tty is enabled.
|
||||
expectTimeout(conn, br, "stdout")
|
||||
|
||||
// Test the client API
|
||||
client, err := client.NewClientWithOpts(client.FromEnv)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
defer client.Close()
|
||||
|
||||
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat")
|
||||
@ -194,19 +195,19 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
}
|
||||
|
||||
resp, err := client.ContainerAttach(context.Background(), cid, attachOpts)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
expectSuccess(resp.Conn, resp.Reader, "stdout", false)
|
||||
|
||||
// Make sure we do see "hello" if Logs is true
|
||||
attachOpts.Logs = true
|
||||
resp, err = client.ContainerAttach(context.Background(), cid, attachOpts)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
defer resp.Conn.Close()
|
||||
resp.Conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||
|
||||
_, err = resp.Conn.Write([]byte("success"))
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
var outBuf, errBuf bytes.Buffer
|
||||
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
|
||||
@ -214,9 +215,9 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||
// ignore the timeout error as it is expected
|
||||
err = nil
|
||||
}
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(errBuf.String(), checker.Equals, "")
|
||||
c.Assert(outBuf.String(), checker.Equals, "hello\nsuccess")
|
||||
assert.NilError(c, err)
|
||||
assert.Equal(c, errBuf.String(), "")
|
||||
assert.Equal(c, outBuf.String(), "hello\nsuccess")
|
||||
}
|
||||
|
||||
// SockRequestHijack creates a connection to specified host (with method, contenttype, …) and returns a hijacked connection
|
||||
|
Reference in New Issue
Block a user