2015-04-11 17:49:14 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-21 21:51:41 -04:00
|
|
|
"bufio"
|
2017-04-28 07:53:00 -04:00
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-04-11 17:49:14 -04:00
|
|
|
"fmt"
|
2017-04-28 07:53:00 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-04-11 17:49:14 -04:00
|
|
|
"net/http"
|
2017-04-28 07:53:00 -04:00
|
|
|
"strconv"
|
2015-04-21 21:51:41 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-04-21 21:51:41 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
2017-04-28 07:53:00 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2015-04-11 17:49:14 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIWithStdout(c *testing.T) {
|
2015-04-21 21:51:41 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
|
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(id))
|
2015-04-11 17:49:14 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
type logOut struct {
|
2015-04-27 12:33:08 -04:00
|
|
|
out string
|
|
|
|
err error
|
2015-04-21 21:51:41 -04:00
|
|
|
}
|
2017-05-19 10:17:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
chLog := make(chan logOut)
|
2017-05-19 10:17:26 -04:00
|
|
|
res, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-04-21 21:51:41 -04:00
|
|
|
|
|
|
|
go func() {
|
2015-07-23 07:24:14 -04:00
|
|
|
defer body.Close()
|
|
|
|
out, err := bufio.NewReader(body).ReadString('\n')
|
|
|
|
if err != nil {
|
2017-05-19 10:17:26 -04:00
|
|
|
chLog <- logOut{"", err}
|
2015-07-23 07:24:14 -04:00
|
|
|
return
|
|
|
|
}
|
2017-05-19 10:17:26 -04:00
|
|
|
chLog <- logOut{strings.TrimSpace(out), err}
|
2015-04-21 21:51:41 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case l := <-chLog:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, l.err)
|
2015-04-21 21:51:41 -04:00
|
|
|
if !strings.HasSuffix(l.out, "hello") {
|
|
|
|
c.Fatalf("expected log output to container 'hello', but it does not")
|
|
|
|
}
|
2017-05-19 10:17:26 -04:00
|
|
|
case <-time.After(30 * time.Second):
|
2015-04-21 21:51:41 -04:00
|
|
|
c.Fatal("timeout waiting for logs to exit")
|
2015-04-11 17:49:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *testing.T) {
|
2015-04-11 17:49:14 -04:00
|
|
|
name := "logs_test"
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
2015-04-11 17:49:14 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
_, err = cli.ContainerLogs(context.Background(), name, types.ContainerLogsOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "Bad parameters: you must choose at least one stream")
|
2015-04-11 17:49:14 -04:00
|
|
|
}
|
2015-04-23 18:08:41 -04:00
|
|
|
|
|
|
|
// Regression test for #12704
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *testing.T) {
|
2015-04-23 18:08:41 -04:00
|
|
|
name := "logs_test"
|
|
|
|
t0 := time.Now()
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10")
|
2015-04-23 18:08:41 -04:00
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
_, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
2015-04-23 18:08:41 -04:00
|
|
|
t1 := time.Now()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-23 18:08:41 -04:00
|
|
|
body.Close()
|
|
|
|
elapsed := t1.Sub(t0).Seconds()
|
2016-02-24 16:43:52 -05:00
|
|
|
if elapsed > 20.0 {
|
2015-04-23 18:08:41 -04:00
|
|
|
c.Fatalf("HTTP response was not immediate (elapsed %.1fs)", elapsed)
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 16:36:29 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIContainerNotFound(c *testing.T) {
|
2015-09-28 16:36:29 -04:00
|
|
|
name := "nonExistentContainer"
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, _, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusNotFound)
|
2015-09-28 16:36:29 -04:00
|
|
|
}
|
2017-04-28 07:53:00 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIUntilFutureFollow(c *testing.T) {
|
2017-04-28 07:53:00 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
name := "logsuntilfuturefollow"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", "while true; do date +%s; sleep 1; done")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(name))
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
untilSecs := 5
|
|
|
|
untilDur, err := time.ParseDuration(fmt.Sprintf("%ds", untilSecs))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
until := daemonTime(c).Add(untilDur)
|
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := client.NewClientWithOpts(client.FromEnv)
|
2017-04-28 07:53:00 -04:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := types.ContainerLogsOptions{Until: until.Format(time.RFC3339Nano), Follow: true, ShowStdout: true, Timestamps: true}
|
|
|
|
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
type logOut struct {
|
|
|
|
out string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
chLog := make(chan logOut)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
bufReader := bufio.NewReader(reader)
|
|
|
|
defer reader.Close()
|
|
|
|
for i := 0; i < untilSecs; i++ {
|
|
|
|
out, _, err := bufReader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
chLog <- logOut{"", err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chLog <- logOut{strings.TrimSpace(string(out)), err}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < untilSecs; i++ {
|
|
|
|
select {
|
|
|
|
case l := <-chLog:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, l.err)
|
2017-04-28 07:53:00 -04:00
|
|
|
i, err := strconv.ParseInt(strings.Split(l.out, " ")[1], 10, 64)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, time.Unix(i, 0).UnixNano() <= until.UnixNano())
|
2017-04-28 07:53:00 -04:00
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
c.Fatal("timeout waiting for logs to exit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIUntil(c *testing.T) {
|
2018-05-04 17:15:00 -04:00
|
|
|
testRequires(c, MinimumAPIVersion("1.34"))
|
2017-04-28 07:53:00 -04:00
|
|
|
name := "logsuntil"
|
2017-12-11 21:28:29 -05:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; sleep 1; done")
|
2017-04-28 07:53:00 -04:00
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := client.NewClientWithOpts(client.FromEnv)
|
2017-04-28 07:53:00 -04:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
extractBody := func(c *testing.T, cfg types.ContainerLogsOptions) []string {
|
2017-04-28 07:53:00 -04:00
|
|
|
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
|
|
|
actualStderr := ioutil.Discard
|
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
return strings.Split(actualStdout.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get timestamp of second log line
|
|
|
|
allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(allLogs) >= 3)
|
2017-12-15 02:20:44 -05:00
|
|
|
|
2017-04-28 07:53:00 -04:00
|
|
|
t, err := time.Parse(time.RFC3339Nano, strings.Split(allLogs[1], " ")[0])
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
until := t.Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
// Get logs until the timestamp of second line, i.e. first two lines
|
|
|
|
logs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true, Until: until})
|
|
|
|
|
|
|
|
// Ensure log lines after cut-off are excluded
|
|
|
|
logsString := strings.Join(logs, "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, !strings.Contains(logsString, "log3"), "unexpected log message returned, until=%v", until)
|
2017-04-28 07:53:00 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *testing.T) {
|
2017-04-28 07:53:00 -04:00
|
|
|
name := "logsuntildefaultval"
|
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; done")
|
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := client.NewClientWithOpts(client.FromEnv)
|
2017-04-28 07:53:00 -04:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
extractBody := func(c *testing.T, cfg types.ContainerLogsOptions) []string {
|
2017-04-28 07:53:00 -04:00
|
|
|
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
|
|
|
actualStderr := ioutil.Discard
|
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
return strings.Split(actualStdout.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get timestamp of second log line
|
|
|
|
allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true})
|
|
|
|
|
|
|
|
// Test with default value specified and parameter omitted
|
|
|
|
defaultLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true, Until: "0"})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.DeepEqual(c, defaultLogs, allLogs)
|
2017-04-28 07:53:00 -04:00
|
|
|
}
|