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"
|
|
|
|
"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"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2017-04-28 07:53:00 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2015-04-11 17:49:14 -04:00
|
|
|
)
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
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)
|
2015-10-15 13:33:31 -04:00
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
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))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, 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:
|
2015-10-15 13:33:31 -04:00
|
|
|
c.Assert(l.err, checker.IsNil)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) {
|
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)
|
2015-10-15 13:33:31 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
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{})
|
2015-04-11 17:49:14 -04:00
|
|
|
expected := "Bad parameters: you must choose at least one stream"
|
2017-05-23 23:56:26 -04:00
|
|
|
c.Assert(err.Error(), checker.Contains, expected)
|
2015-04-11 17:49:14 -04:00
|
|
|
}
|
2015-04-23 18:08:41 -04:00
|
|
|
|
|
|
|
// Regression test for #12704
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
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()
|
2015-10-15 13:33:31 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
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
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) {
|
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))
|
2015-10-15 13:33:31 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
2015-09-28 16:36:29 -04:00
|
|
|
}
|
2017-04-28 07:53:00 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
untilSecs := 5
|
|
|
|
untilDur, err := time.ParseDuration(fmt.Sprintf("%ds", untilSecs))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
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)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
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:
|
|
|
|
c.Assert(l.err, checker.IsNil)
|
|
|
|
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())
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
c.Fatal("timeout waiting for logs to exit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestLogsAPIUntil(c *check.C) {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
extractBody := func(c *check.C, cfg types.ContainerLogsOptions) []string {
|
|
|
|
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
|
|
|
actualStderr := ioutil.Discard
|
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
return strings.Split(actualStdout.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get timestamp of second log line
|
|
|
|
allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true})
|
2017-12-15 02:20:44 -05:00
|
|
|
c.Assert(len(allLogs), checker.GreaterOrEqualThan, 3)
|
|
|
|
|
2017-04-28 07:53:00 -04:00
|
|
|
t, err := time.Parse(time.RFC3339Nano, strings.Split(allLogs[1], " ")[0])
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
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")
|
|
|
|
c.Assert(logsString, checker.Not(checker.Contains), "log3", check.Commentf("unexpected log message returned, until=%v", until))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *check.C) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
extractBody := func(c *check.C, cfg types.ContainerLogsOptions) []string {
|
|
|
|
reader, err := client.ContainerLogs(context.Background(), name, cfg)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
|
|
|
actualStderr := ioutil.Discard
|
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
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"})
|
|
|
|
c.Assert(defaultLogs, checker.DeepEquals, allLogs)
|
|
|
|
}
|