mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Replace some checkers and assertions with gotest.tools
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -14,16 +14,16 @@ 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"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
|
||||
id := strings.TrimSpace(out)
|
||||
c.Assert(waitRun(id), checker.IsNil)
|
||||
assert.NilError(c, waitRun(id))
|
||||
|
||||
type logOut struct {
|
||||
out string
|
||||
@@ -32,8 +32,8 @@ func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
||||
|
||||
chLog := make(chan logOut)
|
||||
res, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id))
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
||||
assert.NilError(c, err)
|
||||
assert.Equal(c, res.StatusCode, http.StatusOK)
|
||||
|
||||
go func() {
|
||||
defer body.Close()
|
||||
@@ -47,7 +47,7 @@ func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
||||
|
||||
select {
|
||||
case l := <-chLog:
|
||||
c.Assert(l.err, checker.IsNil)
|
||||
assert.NilError(c, l.err)
|
||||
if !strings.HasSuffix(l.out, "hello") {
|
||||
c.Fatalf("expected log output to container 'hello', but it does not")
|
||||
}
|
||||
@@ -60,12 +60,11 @@ func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) {
|
||||
name := "logs_test"
|
||||
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
defer cli.Close()
|
||||
|
||||
_, err = cli.ContainerLogs(context.Background(), name, types.ContainerLogsOptions{})
|
||||
expected := "Bad parameters: you must choose at least one stream"
|
||||
c.Assert(err.Error(), checker.Contains, expected)
|
||||
assert.ErrorContains(c, err, "Bad parameters: you must choose at least one stream")
|
||||
}
|
||||
|
||||
// Regression test for #12704
|
||||
@@ -76,7 +75,7 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
||||
|
||||
_, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
||||
t1 := time.Now()
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
body.Close()
|
||||
elapsed := t1.Sub(t0).Seconds()
|
||||
if elapsed > 20.0 {
|
||||
@@ -87,19 +86,19 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
||||
func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) {
|
||||
name := "nonExistentContainer"
|
||||
resp, _, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
||||
assert.NilError(c, err)
|
||||
assert.Equal(c, resp.StatusCode, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsAPIUntilFutureFollow(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
name := "logsuntilfuturefollow"
|
||||
dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", "while true; do date +%s; sleep 1; done")
|
||||
c.Assert(waitRun(name), checker.IsNil)
|
||||
assert.NilError(c, waitRun(name))
|
||||
|
||||
untilSecs := 5
|
||||
untilDur, err := time.ParseDuration(fmt.Sprintf("%ds", untilSecs))
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
until := daemonTime(c).Add(untilDur)
|
||||
|
||||
client, err := client.NewClientWithOpts(client.FromEnv)
|
||||
@@ -109,7 +108,7 @@ func (s *DockerSuite) TestLogsAPIUntilFutureFollow(c *check.C) {
|
||||
|
||||
cfg := types.ContainerLogsOptions{Until: until.Format(time.RFC3339Nano), Follow: true, ShowStdout: true, Timestamps: true}
|
||||
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
type logOut struct {
|
||||
out string
|
||||
@@ -138,10 +137,10 @@ func (s *DockerSuite) TestLogsAPIUntilFutureFollow(c *check.C) {
|
||||
for i := 0; i < untilSecs; i++ {
|
||||
select {
|
||||
case l := <-chLog:
|
||||
c.Assert(l.err, checker.IsNil)
|
||||
assert.NilError(c, l.err)
|
||||
i, err := strconv.ParseInt(strings.Split(l.out, " ")[1], 10, 64)
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(time.Unix(i, 0).UnixNano(), checker.LessOrEqualThan, until.UnixNano())
|
||||
assert.NilError(c, err)
|
||||
assert.Assert(c, time.Unix(i, 0).UnixNano() <= until.UnixNano())
|
||||
case <-time.After(20 * time.Second):
|
||||
c.Fatal("timeout waiting for logs to exit")
|
||||
}
|
||||
@@ -160,22 +159,22 @@ func (s *DockerSuite) TestLogsAPIUntil(c *check.C) {
|
||||
|
||||
extractBody := func(c *check.C, cfg types.ContainerLogsOptions) []string {
|
||||
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
actualStdout := new(bytes.Buffer)
|
||||
actualStderr := ioutil.Discard
|
||||
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
return strings.Split(actualStdout.String(), "\n")
|
||||
}
|
||||
|
||||
// Get timestamp of second log line
|
||||
allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true})
|
||||
c.Assert(len(allLogs), checker.GreaterOrEqualThan, 3)
|
||||
assert.Assert(c, len(allLogs) >= 3)
|
||||
|
||||
t, err := time.Parse(time.RFC3339Nano, strings.Split(allLogs[1], " ")[0])
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
until := t.Format(time.RFC3339Nano)
|
||||
|
||||
// Get logs until the timestamp of second line, i.e. first two lines
|
||||
@@ -183,7 +182,7 @@ func (s *DockerSuite) TestLogsAPIUntil(c *check.C) {
|
||||
|
||||
// Ensure log lines after cut-off are excluded
|
||||
logsString := strings.Join(logs, "\n")
|
||||
c.Assert(logsString, checker.Not(checker.Contains), "log3", check.Commentf("unexpected log message returned, until=%v", until))
|
||||
assert.Assert(c, !strings.Contains(logsString, "log3"), "unexpected log message returned, until=%v", until)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *check.C) {
|
||||
@@ -197,12 +196,12 @@ func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *check.C) {
|
||||
|
||||
extractBody := func(c *check.C, cfg types.ContainerLogsOptions) []string {
|
||||
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
actualStdout := new(bytes.Buffer)
|
||||
actualStderr := ioutil.Discard
|
||||
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
return strings.Split(actualStdout.String(), "\n")
|
||||
}
|
||||
@@ -212,5 +211,5 @@ func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *check.C) {
|
||||
|
||||
// Test with default value specified and parameter omitted
|
||||
defaultLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true, Until: "0"})
|
||||
c.Assert(defaultLogs, checker.DeepEquals, allLogs)
|
||||
assert.DeepEqual(c, defaultLogs, allLogs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user