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:
@@ -8,10 +8,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/integration-cli/cli"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"github.com/go-check/check"
|
||||
"gotest.tools/assert"
|
||||
"gotest.tools/icmd"
|
||||
)
|
||||
|
||||
@@ -35,7 +35,7 @@ func testLogsContainerPagination(c *check.C, testLen int) {
|
||||
id := strings.TrimSpace(out)
|
||||
dockerCmd(c, "wait", id)
|
||||
out, _ = dockerCmd(c, "logs", id)
|
||||
c.Assert(out, checker.HasLen, testLen+1)
|
||||
assert.Equal(c, len(out), testLen+1)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
|
||||
@@ -49,16 +49,16 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
|
||||
|
||||
lines := strings.Split(out, "\n")
|
||||
|
||||
c.Assert(lines, checker.HasLen, testLen+1)
|
||||
assert.Equal(c, len(lines), testLen+1)
|
||||
|
||||
ts := regexp.MustCompile(`^.* `)
|
||||
|
||||
for _, l := range lines {
|
||||
if l != "" {
|
||||
_, err := time.Parse(jsonmessage.RFC3339NanoFixed+" ", ts.FindString(l))
|
||||
c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
|
||||
assert.NilError(c, err, "Failed to parse timestamp from %v", l)
|
||||
// ensure we have padded 0's
|
||||
c.Assert(l[29], checker.Equals, uint8('Z'))
|
||||
assert.Equal(c, l[29], uint8('Z'))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,27 +98,27 @@ func (s *DockerSuite) TestLogsTail(c *check.C) {
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "0", id).Combined()
|
||||
lines := strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, 1)
|
||||
assert.Equal(c, len(lines), 1)
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "5", id).Combined()
|
||||
lines = strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, 6)
|
||||
assert.Equal(c, len(lines), 6)
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "99", id).Combined()
|
||||
lines = strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, 100)
|
||||
assert.Equal(c, len(lines), 100)
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "all", id).Combined()
|
||||
lines = strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, testLen+1)
|
||||
assert.Equal(c, len(lines), testLen+1)
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "-1", id).Combined()
|
||||
lines = strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, testLen+1)
|
||||
assert.Equal(c, len(lines), testLen+1)
|
||||
|
||||
out = cli.DockerCmd(c, "logs", "--tail", "random", id).Combined()
|
||||
lines = strings.Split(out, "\n")
|
||||
c.Assert(lines, checker.HasLen, testLen+1)
|
||||
assert.Equal(c, len(lines), testLen+1)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
|
||||
@@ -126,7 +126,7 @@ func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
|
||||
id := getIDByName(c, "test")
|
||||
|
||||
logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
|
||||
c.Assert(logsCmd.Start(), checker.IsNil)
|
||||
assert.NilError(c, logsCmd.Start())
|
||||
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
@@ -136,7 +136,7 @@ func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
|
||||
|
||||
select {
|
||||
case err := <-errChan:
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
case <-time.After(30 * time.Second):
|
||||
c.Fatal("Following logs is hanged")
|
||||
}
|
||||
@@ -149,19 +149,19 @@ func (s *DockerSuite) TestLogsSince(c *check.C) {
|
||||
|
||||
log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
|
||||
t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
|
||||
out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
|
||||
|
||||
// Skip 2 seconds
|
||||
unexpected := []string{"log1", "log2"}
|
||||
for _, v := range unexpected {
|
||||
c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since))
|
||||
assert.Check(c, !strings.Contains(out, v), "unexpected log message returned, since=%v", since)
|
||||
}
|
||||
|
||||
// Test to make sure a bad since format is caught by the client
|
||||
out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)
|
||||
c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server"))
|
||||
assert.Assert(c, strings.Contains(out, `cannot parse "0Z" as "05"`), "bad since format passed to server")
|
||||
|
||||
// Test with default value specified and parameter omitted
|
||||
expected := []string{"log1", "log2", "log3"}
|
||||
@@ -172,7 +172,7 @@ func (s *DockerSuite) TestLogsSince(c *check.C) {
|
||||
result := icmd.RunCommand(dockerBinary, cmd...)
|
||||
result.Assert(c, icmd.Success)
|
||||
for _, v := range expected {
|
||||
c.Assert(result.Combined(), checker.Contains, v)
|
||||
assert.Check(c, strings.Contains(result.Combined(), v))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,18 +195,18 @@ func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(timestamp, checker.Not(checker.Equals), "")
|
||||
assert.Assert(c, timestamp != "")
|
||||
t, err := time.Parse(time.RFC3339Nano, timestamp)
|
||||
c.Assert(err, check.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
since := t.Unix() + 2
|
||||
out, _ := dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name)
|
||||
c.Assert(out, checker.Not(checker.HasLen), 0, check.Commentf("cannot read from empty log"))
|
||||
assert.Assert(c, len(out) != 0, "cannot read from empty log")
|
||||
lines := strings.Split(strings.TrimSpace(out), "\n")
|
||||
for _, v := range lines {
|
||||
ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0])
|
||||
c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'", v))
|
||||
c.Assert(ts.Unix() >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
|
||||
assert.NilError(c, err, "cannot parse timestamp output from log: '%v'", v)
|
||||
assert.Assert(c, ts.Unix() >= since, "earlier log found. since=%v logdate=%v", since, ts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,22 +228,22 @@ func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
|
||||
|
||||
logCmd := exec.Command(dockerBinary, "logs", "-f", id)
|
||||
stdout, err := logCmd.StdoutPipe()
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(logCmd.Start(), checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
assert.NilError(c, logCmd.Start())
|
||||
defer func() { go logCmd.Wait() }()
|
||||
|
||||
// First read slowly
|
||||
bytes1, err := ConsumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
// After the container has finished we can continue reading fast
|
||||
bytes2, err := ConsumeWithSpeed(stdout, 32*1024, 0, nil)
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
|
||||
c.Assert(logCmd.Wait(), checker.IsNil)
|
||||
assert.NilError(c, logCmd.Wait())
|
||||
|
||||
actual := bytes1 + bytes2
|
||||
c.Assert(actual, checker.Equals, expected)
|
||||
assert.Equal(c, actual, expected)
|
||||
}
|
||||
|
||||
// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
|
||||
@@ -272,14 +272,14 @@ func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, s
|
||||
func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
|
||||
id := strings.TrimSpace(out)
|
||||
c.Assert(waitRun(id), checker.IsNil)
|
||||
assert.NilError(c, waitRun(id))
|
||||
|
||||
nroutines, err := getGoroutineNumber()
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
cmd := exec.Command(dockerBinary, "logs", "-f", id)
|
||||
r, w := io.Pipe()
|
||||
cmd.Stdout = w
|
||||
c.Assert(cmd.Start(), checker.IsNil)
|
||||
assert.NilError(c, cmd.Start())
|
||||
go cmd.Wait()
|
||||
|
||||
// Make sure pipe is written to
|
||||
@@ -289,37 +289,37 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
|
||||
_, err := r.Read(b)
|
||||
chErr <- err
|
||||
}()
|
||||
c.Assert(<-chErr, checker.IsNil)
|
||||
c.Assert(cmd.Process.Kill(), checker.IsNil)
|
||||
assert.NilError(c, <-chErr)
|
||||
assert.NilError(c, cmd.Process.Kill())
|
||||
r.Close()
|
||||
cmd.Wait()
|
||||
// NGoroutines is not updated right away, so we need to wait before failing
|
||||
c.Assert(waitForGoroutines(nroutines), checker.IsNil)
|
||||
assert.NilError(c, waitForGoroutines(nroutines))
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
|
||||
id := strings.TrimSpace(out)
|
||||
c.Assert(waitRun(id), checker.IsNil)
|
||||
assert.NilError(c, waitRun(id))
|
||||
|
||||
nroutines, err := getGoroutineNumber()
|
||||
c.Assert(err, checker.IsNil)
|
||||
assert.NilError(c, err)
|
||||
cmd := exec.Command(dockerBinary, "logs", "-f", id)
|
||||
c.Assert(cmd.Start(), checker.IsNil)
|
||||
assert.NilError(c, cmd.Start())
|
||||
go cmd.Wait()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
c.Assert(cmd.Process.Kill(), checker.IsNil)
|
||||
assert.NilError(c, cmd.Process.Kill())
|
||||
cmd.Wait()
|
||||
|
||||
// NGoroutines is not updated right away, so we need to wait before failing
|
||||
c.Assert(waitForGoroutines(nroutines), checker.IsNil)
|
||||
assert.NilError(c, waitForGoroutines(nroutines))
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
|
||||
name := "testlogsnocontainer"
|
||||
out, _, _ := dockerCmdWithError("logs", name)
|
||||
message := fmt.Sprintf("No such container: %s\n", name)
|
||||
c.Assert(out, checker.Contains, message)
|
||||
assert.Assert(c, strings.Contains(out, message))
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestLogsWithDetails(c *check.C) {
|
||||
@@ -327,10 +327,10 @@ func (s *DockerSuite) TestLogsWithDetails(c *check.C) {
|
||||
out, _ := dockerCmd(c, "logs", "--details", "--timestamps", "test")
|
||||
|
||||
logFields := strings.Fields(strings.TrimSpace(out))
|
||||
c.Assert(len(logFields), checker.Equals, 3, check.Commentf("%s", out))
|
||||
assert.Equal(c, len(logFields), 3, out)
|
||||
|
||||
details := strings.Split(logFields[1], ",")
|
||||
c.Assert(details, checker.HasLen, 2)
|
||||
c.Assert(details[0], checker.Equals, "baz=qux")
|
||||
c.Assert(details[1], checker.Equals, "foo=bar")
|
||||
assert.Equal(c, len(details), 2)
|
||||
assert.Equal(c, details[0], "baz=qux")
|
||||
assert.Equal(c, details[1], "foo=bar")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user