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