2015-04-12 00:49:14 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-22 04:51:41 +03:00
|
|
|
"bufio"
|
2017-04-28 14:53:00 +03:00
|
|
|
"bytes"
|
2015-04-12 00:49:14 +03:00
|
|
|
"fmt"
|
2017-04-28 14:53:00 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-04-12 00:49:14 +03:00
|
|
|
"net/http"
|
2017-04-28 14:53:00 +03:00
|
|
|
"strconv"
|
2015-04-22 04:51:41 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
2017-05-24 06:56:26 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
2016-12-30 20:23:00 +03:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-12-30 12:49:36 +03:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2017-04-28 14:53:00 +03:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2015-04-18 19:46:47 +03:00
|
|
|
"github.com/go-check/check"
|
2017-05-24 06:56:26 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-04-12 00:49:14 +03: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-28 04:50:12 +03:00
|
|
|
func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
2015-04-22 04:51:41 +03: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 20:33:31 +03:00
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
2015-04-12 00:49:14 +03:00
|
|
|
|
2015-04-22 04:51:41 +03:00
|
|
|
type logOut struct {
|
2015-04-27 19:33:08 +03:00
|
|
|
out string
|
|
|
|
err error
|
2015-04-22 04:51:41 +03:00
|
|
|
}
|
2017-05-19 17:17:26 +03:00
|
|
|
|
2015-04-22 04:51:41 +03:00
|
|
|
chLog := make(chan logOut)
|
2017-05-19 17:17:26 +03: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-22 04:51:41 +03:00
|
|
|
|
|
|
|
go func() {
|
2015-07-23 14:24:14 +03:00
|
|
|
defer body.Close()
|
|
|
|
out, err := bufio.NewReader(body).ReadString('\n')
|
|
|
|
if err != nil {
|
2017-05-19 17:17:26 +03:00
|
|
|
chLog <- logOut{"", err}
|
2015-07-23 14:24:14 +03:00
|
|
|
return
|
|
|
|
}
|
2017-05-19 17:17:26 +03:00
|
|
|
chLog <- logOut{strings.TrimSpace(out), err}
|
2015-04-22 04:51:41 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case l := <-chLog:
|
2015-10-15 20:33:31 +03:00
|
|
|
c.Assert(l.err, checker.IsNil)
|
2015-04-22 04:51:41 +03:00
|
|
|
if !strings.HasSuffix(l.out, "hello") {
|
|
|
|
c.Fatalf("expected log output to container 'hello', but it does not")
|
|
|
|
}
|
2017-05-19 17:17:26 +03:00
|
|
|
case <-time.After(30 * time.Second):
|
2015-04-22 04:51:41 +03:00
|
|
|
c.Fatal("timeout waiting for logs to exit")
|
2015-04-12 00:49:14 +03: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-28 04:50:12 +03:00
|
|
|
func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) {
|
2015-04-12 00:49:14 +03:00
|
|
|
name := "logs_test"
|
2015-07-14 09:35:36 +03:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
2017-05-24 06:56:26 +03:00
|
|
|
cli, err := client.NewEnvClient()
|
2015-10-15 20:33:31 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-24 06:56:26 +03:00
|
|
|
defer cli.Close()
|
2015-04-12 00:49:14 +03:00
|
|
|
|
2017-05-24 06:56:26 +03:00
|
|
|
_, err = cli.ContainerLogs(context.Background(), name, types.ContainerLogsOptions{})
|
2015-04-12 00:49:14 +03:00
|
|
|
expected := "Bad parameters: you must choose at least one stream"
|
2017-05-24 06:56:26 +03:00
|
|
|
c.Assert(err.Error(), checker.Contains, expected)
|
2015-04-12 00:49:14 +03:00
|
|
|
}
|
2015-04-24 01:08:41 +03: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-28 04:50:12 +03:00
|
|
|
func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
2015-04-24 01:08:41 +03:00
|
|
|
name := "logs_test"
|
|
|
|
t0 := time.Now()
|
2015-07-14 09:35:36 +03:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10")
|
2015-04-24 01:08:41 +03:00
|
|
|
|
2017-03-06 18:35:27 +03:00
|
|
|
_, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
2015-04-24 01:08:41 +03:00
|
|
|
t1 := time.Now()
|
2015-10-15 20:33:31 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-24 01:08:41 +03:00
|
|
|
body.Close()
|
|
|
|
elapsed := t1.Sub(t0).Seconds()
|
2016-02-25 00:43:52 +03:00
|
|
|
if elapsed > 20.0 {
|
2015-04-24 01:08:41 +03:00
|
|
|
c.Fatalf("HTTP response was not immediate (elapsed %.1fs)", elapsed)
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 23:36:29 +03: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-28 04:50:12 +03:00
|
|
|
func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) {
|
2015-09-28 23:36:29 +03:00
|
|
|
name := "nonExistentContainer"
|
2017-03-06 18:35:27 +03:00
|
|
|
resp, _, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
2015-10-15 20:33:31 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
2015-09-28 23:36:29 +03:00
|
|
|
}
|
2017-04-28 14:53:00 +03: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)
|
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
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) {
|
|
|
|
name := "logsuntil"
|
2017-12-08 17:20:56 +03:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; done")
|
2017-04-28 14:53:00 +03:00
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
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})
|
|
|
|
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")
|
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
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)
|
|
|
|
}
|