2014-03-31 18:40:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2014-03-31 18:40:39 +00:00
|
|
|
"fmt"
|
2015-05-07 01:09:27 +00:00
|
|
|
"io"
|
2014-03-31 18:40:39 +00:00
|
|
|
"os/exec"
|
2014-04-02 19:26:06 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2014-04-02 19:26:06 +00:00
|
|
|
"time"
|
2014-09-10 19:36:01 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2017-03-27 15:12:48 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
|
|
|
"github.com/docker/docker/testutil"
|
|
|
|
testdaemon "github.com/docker/docker/testutil/daemon"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-03-31 18:40:39 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLILogsSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLILogsSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLILogsSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:40:39 +00:00
|
|
|
// This used to work, it test a log of PageSize-1 (gh#4851)
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsContainerSmallerThanPage(c *testing.T) {
|
2017-01-02 22:42:45 +00:00
|
|
|
testLogsContainerPagination(c, 32767)
|
2014-03-31 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test: When going over the PageSize, it used to panic (gh#4851)
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsContainerBiggerThanPage(c *testing.T) {
|
2017-01-02 22:42:45 +00:00
|
|
|
testLogsContainerPagination(c, 32768)
|
2014-03-31 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test: When going much over the PageSize, it used to block (gh#4851)
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsContainerMuchBiggerThanPage(c *testing.T) {
|
2017-01-02 22:42:45 +00:00
|
|
|
testLogsContainerPagination(c, 33000)
|
|
|
|
}
|
2014-03-31 18:40:39 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func testLogsContainerPagination(c *testing.T, testLen int) {
|
2023-07-27 16:34:10 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)).Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
|
|
|
cli.DockerCmd(c, "wait", id)
|
|
|
|
out := cli.DockerCmd(c, "logs", id).Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(out), testLen+1)
|
2014-03-31 18:40:39 +00:00
|
|
|
}
|
2014-04-02 19:26:06 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsTimestamps(c *testing.T) {
|
2014-04-02 19:26:06 +00:00
|
|
|
testLen := 100
|
2023-07-27 16:34:10 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen)).Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
|
|
|
cli.DockerCmd(c, "wait", id)
|
2014-04-02 19:26:06 +00:00
|
|
|
|
2023-07-27 16:34:10 +00:00
|
|
|
out := cli.DockerCmd(c, "logs", "-t", id).Combined()
|
2014-04-02 19:26:06 +00:00
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), testLen+1)
|
2014-04-02 19:26:06 +00:00
|
|
|
|
2014-05-10 16:51:16 +00:00
|
|
|
ts := regexp.MustCompile(`^.* `)
|
2014-04-02 19:26:06 +00:00
|
|
|
|
|
|
|
for _, l := range lines {
|
|
|
|
if l != "" {
|
2023-07-30 15:18:56 +00:00
|
|
|
_, err := time.Parse(log.RFC3339NanoFixed+" ", ts.FindString(l))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, "Failed to parse timestamp from %v", l)
|
2015-10-13 01:50:17 +00:00
|
|
|
// ensure we have padded 0's
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, l[29], uint8('Z'))
|
2014-04-02 19:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsSeparateStderr(c *testing.T) {
|
2014-04-02 19:26:06 +00:00
|
|
|
msg := "stderr_log"
|
2017-03-27 15:12:48 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)).Combined()
|
2015-10-13 01:50:17 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "wait", id)
|
|
|
|
cli.DockerCmd(c, "logs", id).Assert(c, icmd.Expected{
|
|
|
|
Out: "",
|
|
|
|
Err: msg,
|
|
|
|
})
|
2014-04-02 19:26:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsStderrInStdout(c *testing.T) {
|
2016-02-24 22:26:08 +00:00
|
|
|
// TODO Windows: Needs investigation why this fails. Obtained string includes
|
|
|
|
// a bunch of ANSI escape sequences before the "stderr_log" message.
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-04-02 19:26:06 +00:00
|
|
|
msg := "stderr_log"
|
2017-03-27 15:12:48 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)).Combined()
|
2015-10-13 01:50:17 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "wait", id)
|
2014-04-02 19:26:06 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "logs", id).Assert(c, icmd.Expected{
|
|
|
|
Out: msg,
|
|
|
|
Err: "",
|
|
|
|
})
|
2014-04-02 19:26:06 +00:00
|
|
|
}
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsTail(c *testing.T) {
|
2014-06-03 11:09:33 +00:00
|
|
|
testLen := 100
|
2017-03-27 15:12:48 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen)).Combined()
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2015-10-13 01:50:17 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "wait", id)
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "0", id).Combined()
|
2014-06-03 11:09:33 +00:00
|
|
|
lines := strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), 1)
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "5", id).Combined()
|
2016-11-10 03:16:47 +00:00
|
|
|
lines = strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), 6)
|
2015-10-13 01:50:17 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "99", id).Combined()
|
2016-11-10 03:16:47 +00:00
|
|
|
lines = strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), 100)
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "all", id).Combined()
|
2014-06-03 11:09:33 +00:00
|
|
|
lines = strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), testLen+1)
|
2014-06-03 11:09:33 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "-1", id).Combined()
|
2016-11-10 03:16:47 +00:00
|
|
|
lines = strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), testLen+1)
|
2015-10-13 01:50:17 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "--tail", "random", id).Combined()
|
2014-06-03 11:09:33 +00:00
|
|
|
lines = strings.Split(out, "\n")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(lines), testLen+1)
|
2014-06-03 11:09:33 +00:00
|
|
|
}
|
2014-08-20 16:02:56 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsFollowStopped(c *testing.T) {
|
2023-07-27 16:34:10 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "hello")
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, "test")
|
2014-08-20 16:02:56 +00:00
|
|
|
|
2015-10-13 01:50:17 +00:00
|
|
|
logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, logsCmd.Start())
|
2014-08-20 16:02:56 +00:00
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
errChan := make(chan error, 1)
|
2014-08-20 16:02:56 +00:00
|
|
|
go func() {
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan <- logsCmd.Wait()
|
|
|
|
close(errChan)
|
2014-08-20 16:02:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-errChan:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-02-24 22:26:08 +00:00
|
|
|
case <-time.After(30 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("Following logs is hanged")
|
2014-08-20 16:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-30 19:33:26 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsSince(c *testing.T) {
|
2015-04-14 04:36:12 +00:00
|
|
|
name := "testlogssince"
|
2023-07-27 16:34:10 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")
|
|
|
|
out := cli.DockerCmd(c, "logs", "-t", name).Combined()
|
2015-04-14 04:36:12 +00:00
|
|
|
|
2015-05-13 02:49:47 +00:00
|
|
|
log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
|
2015-12-07 21:26:30 +00:00
|
|
|
t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-12-07 21:26:30 +00:00
|
|
|
since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
|
2023-07-27 16:34:10 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name).Combined()
|
2015-04-14 04:36:12 +00:00
|
|
|
|
|
|
|
// Skip 2 seconds
|
|
|
|
unexpected := []string{"log1", "log2"}
|
|
|
|
for _, v := range unexpected {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Check(c, !strings.Contains(out, v), "unexpected log message returned, since=%v", since)
|
2015-04-14 04:36:12 +00:00
|
|
|
}
|
2015-10-29 17:51:36 +00:00
|
|
|
|
|
|
|
// Test to make sure a bad since format is caught by the client
|
|
|
|
out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, `cannot parse "0Z" as "05"`), "bad since format passed to server")
|
2015-10-29 17:51:36 +00:00
|
|
|
|
2015-04-14 04:36:12 +00:00
|
|
|
// Test with default value specified and parameter omitted
|
2015-05-13 02:49:47 +00:00
|
|
|
expected := []string{"log1", "log2", "log3"}
|
2017-01-05 11:38:34 +00:00
|
|
|
for _, cmd := range [][]string{
|
2017-01-05 18:08:24 +00:00
|
|
|
{"logs", "-t", name},
|
|
|
|
{"logs", "-t", "--since=0", name},
|
2015-04-14 04:36:12 +00:00
|
|
|
} {
|
2017-01-05 18:08:24 +00:00
|
|
|
result := icmd.RunCommand(dockerBinary, cmd...)
|
2017-01-05 11:38:34 +00:00
|
|
|
result.Assert(c, icmd.Success)
|
2015-04-14 04:36:12 +00:00
|
|
|
for _, v := range expected {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Check(c, strings.Contains(result.Combined(), v))
|
2015-04-14 04:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsSinceFutureFollow(c *testing.T) {
|
2016-04-12 16:39:46 +00:00
|
|
|
// TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now.
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-02-16 21:49:44 +00:00
|
|
|
name := "testlogssincefuturefollow"
|
2023-07-27 16:34:10 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`)
|
2016-02-16 21:49:44 +00:00
|
|
|
|
|
|
|
// Extract one timestamp from the log file to give us a starting point for
|
|
|
|
// our `--since` argument. Because the log producer runs in the background,
|
|
|
|
// we need to check repeatedly for some output to be produced.
|
|
|
|
var timestamp string
|
2016-02-24 22:26:08 +00:00
|
|
|
for i := 0; i != 100 && timestamp == ""; i++ {
|
2023-07-27 16:34:10 +00:00
|
|
|
if out := cli.DockerCmd(c, "logs", "-t", name).Combined(); out == "" {
|
2016-02-16 21:49:44 +00:00
|
|
|
time.Sleep(time.Millisecond * 100) // Retry
|
|
|
|
} else {
|
|
|
|
timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0]
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 04:36:12 +00:00
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, timestamp != "")
|
2016-02-16 21:49:44 +00:00
|
|
|
t, err := time.Parse(time.RFC3339Nano, timestamp)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-02-16 21:49:44 +00:00
|
|
|
|
|
|
|
since := t.Unix() + 2
|
2023-07-27 16:34:10 +00:00
|
|
|
out := cli.DockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name).Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, len(out) != 0, "cannot read from empty log")
|
2015-04-14 04:36:12 +00:00
|
|
|
lines := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
for _, v := range lines {
|
2016-02-16 21:49:44 +00:00
|
|
|
ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0])
|
2019-04-04 13:23:19 +00:00
|
|
|
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)
|
2015-04-14 04:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 19:33:26 +00:00
|
|
|
// Regression test for #8832
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsFollowSlowStdoutConsumer(c *testing.T) {
|
2016-04-06 21:01:12 +00:00
|
|
|
// TODO Windows: Fix this test for TP5.
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-09-18 15:47:55 +00:00
|
|
|
expected := 150000
|
2023-07-27 16:34:10 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", fmt.Sprintf("usleep 600000; yes X | head -c %d", expected)).Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2014-10-30 19:33:26 +00:00
|
|
|
|
|
|
|
stopSlowRead := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
2023-07-27 16:34:10 +00:00
|
|
|
cli.DockerCmd(c, "wait", id)
|
2014-10-30 19:33:26 +00:00
|
|
|
stopSlowRead <- true
|
|
|
|
}()
|
|
|
|
|
2015-10-13 01:50:17 +00:00
|
|
|
logCmd := exec.Command(dockerBinary, "logs", "-f", id)
|
2014-10-30 19:33:26 +00:00
|
|
|
stdout, err := logCmd.StdoutPipe()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.NilError(c, logCmd.Start())
|
2017-09-22 13:52:41 +00:00
|
|
|
defer func() { go logCmd.Wait() }()
|
2014-10-30 19:33:26 +00:00
|
|
|
|
|
|
|
// First read slowly
|
2017-08-21 22:58:25 +00:00
|
|
|
bytes1, err := ConsumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-10-30 19:33:26 +00:00
|
|
|
|
|
|
|
// After the container has finished we can continue reading fast
|
2017-08-21 22:58:25 +00:00
|
|
|
bytes2, err := ConsumeWithSpeed(stdout, 32*1024, 0, nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-10-30 19:33:26 +00:00
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, logCmd.Wait())
|
2017-08-28 12:22:09 +00:00
|
|
|
|
2014-10-30 19:33:26 +00:00
|
|
|
actual := bytes1 + bytes2
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, actual, expected)
|
2014-10-30 19:33:26 +00:00
|
|
|
}
|
2015-05-07 01:09:27 +00:00
|
|
|
|
2017-08-21 22:58:25 +00:00
|
|
|
// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
|
|
|
|
// for interval duration. Returns total read bytes. Send true to the
|
|
|
|
// stop channel to return before reading to EOF on the reader.
|
|
|
|
func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
|
|
|
|
buffer := make([]byte, chunkSize)
|
|
|
|
for {
|
|
|
|
var readBytes int
|
|
|
|
readBytes, err = reader.Read(buffer)
|
|
|
|
n += readBytes
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case <-time.After(interval):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsFollowGoroutinesWithStdout(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
|
|
|
c.Parallel()
|
|
|
|
|
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvVars("OTEL_SDK_DISABLED=1"))
|
|
|
|
defer func() {
|
|
|
|
d.Stop(c)
|
|
|
|
d.Cleanup(c)
|
|
|
|
}()
|
|
|
|
d.StartWithBusybox(ctx, c, "--iptables=false")
|
2015-05-07 01:09:27 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
out, err := d.Cmd("run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-07-14 18:02:38 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
assert.NilError(c, d.WaitRun(id))
|
|
|
|
|
|
|
|
client := d.NewClientT(c)
|
|
|
|
nroutines := waitForStableGourtineCount(ctx, c, client)
|
|
|
|
|
|
|
|
cmd := d.Command("logs", "-f", id)
|
2015-05-07 01:09:27 +00:00
|
|
|
r, w := io.Pipe()
|
2023-01-05 12:01:27 +00:00
|
|
|
defer r.Close()
|
|
|
|
defer w.Close()
|
|
|
|
|
2015-05-07 01:09:27 +00:00
|
|
|
cmd.Stdout = w
|
2023-07-14 18:02:38 +00:00
|
|
|
res := icmd.StartCmd(cmd)
|
|
|
|
assert.NilError(c, res.Error)
|
|
|
|
defer res.Cmd.Process.Kill()
|
2023-01-05 12:01:27 +00:00
|
|
|
|
|
|
|
finished := make(chan error)
|
|
|
|
go func() {
|
2023-07-14 18:02:38 +00:00
|
|
|
finished <- res.Cmd.Wait()
|
2023-01-05 12:01:27 +00:00
|
|
|
}()
|
2015-05-07 01:09:27 +00:00
|
|
|
|
|
|
|
// Make sure pipe is written to
|
|
|
|
chErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
b := make([]byte, 1)
|
|
|
|
_, err := r.Read(b)
|
|
|
|
chErr <- err
|
2023-01-05 12:01:27 +00:00
|
|
|
r.Close()
|
2015-05-07 01:09:27 +00:00
|
|
|
}()
|
2023-01-05 12:01:27 +00:00
|
|
|
|
|
|
|
// Check read from pipe succeeded
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, <-chErr)
|
2023-01-05 12:01:27 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.NilError(c, res.Cmd.Process.Kill())
|
2023-01-05 12:01:27 +00:00
|
|
|
<-finished
|
|
|
|
|
2015-05-07 01:09:27 +00:00
|
|
|
// NGoroutines is not updated right away, so we need to wait before failing
|
2023-07-14 18:02:38 +00:00
|
|
|
waitForGoroutines(ctx, c, client, nroutines)
|
2015-05-07 01:09:27 +00:00
|
|
|
}
|
2015-06-04 19:15:33 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsFollowGoroutinesNoOutput(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
|
|
|
c.Parallel()
|
|
|
|
|
|
|
|
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvVars("OTEL_SDK_DISABLED=1"))
|
|
|
|
defer func() {
|
|
|
|
d.Stop(c)
|
|
|
|
d.Cleanup(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
|
|
|
|
d.StartWithBusybox(ctx, c, "--iptables=false")
|
|
|
|
|
|
|
|
out, err := d.Cmd("run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
|
|
|
|
assert.NilError(c, err)
|
2015-06-04 19:15:33 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.NilError(c, d.WaitRun(id))
|
2015-06-04 19:15:33 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
client := d.NewClientT(c)
|
|
|
|
nroutines := waitForStableGourtineCount(ctx, c, client)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-07-14 18:02:38 +00:00
|
|
|
|
|
|
|
cmd := d.Command("logs", "-f", id)
|
|
|
|
res := icmd.StartCmd(cmd)
|
|
|
|
assert.NilError(c, res.Error)
|
2023-01-05 12:01:27 +00:00
|
|
|
|
|
|
|
finished := make(chan error)
|
|
|
|
go func() {
|
2023-07-14 18:02:38 +00:00
|
|
|
finished <- res.Cmd.Wait()
|
2023-01-05 12:01:27 +00:00
|
|
|
}()
|
|
|
|
|
2015-06-04 19:15:33 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.NilError(c, res.Cmd.Process.Kill())
|
2023-01-05 12:01:27 +00:00
|
|
|
|
|
|
|
<-finished
|
2015-06-04 19:15:33 +00:00
|
|
|
|
|
|
|
// NGoroutines is not updated right away, so we need to wait before failing
|
2023-07-14 18:02:38 +00:00
|
|
|
waitForGoroutines(ctx, c, client, nroutines)
|
2015-06-04 19:15:33 +00:00
|
|
|
}
|
2015-09-28 20:36:29 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsCLIContainerNotFound(c *testing.T) {
|
2015-09-28 20:36:29 +00:00
|
|
|
name := "testlogsnocontainer"
|
|
|
|
out, _, _ := dockerCmdWithError("logs", name)
|
2016-11-22 13:51:22 +00:00
|
|
|
message := fmt.Sprintf("No such container: %s\n", name)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, message))
|
2015-09-28 20:36:29 +00:00
|
|
|
}
|
2016-04-08 16:15:08 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILogsSuite) TestLogsWithDetails(c *testing.T) {
|
2023-07-27 16:34:10 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello")
|
|
|
|
out := cli.DockerCmd(c, "logs", "--details", "--timestamps", "test").Combined()
|
2016-04-08 16:15:08 +00:00
|
|
|
|
|
|
|
logFields := strings.Fields(strings.TrimSpace(out))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(logFields), 3, out)
|
2016-04-08 16:15:08 +00:00
|
|
|
|
|
|
|
details := strings.Split(logFields[1], ",")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(details), 2)
|
|
|
|
assert.Equal(c, details[0], "baz=qux")
|
|
|
|
assert.Equal(c, details[1], "foo=bar")
|
2016-04-08 16:15:08 +00:00
|
|
|
}
|