github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/integration-cli/docker_api_logs_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "context" 7 "fmt" 8 "io" 9 "io/ioutil" 10 "net/http" 11 "strconv" 12 "strings" 13 "testing" 14 "time" 15 16 "github.com/docker/docker/api/types" 17 "github.com/docker/docker/client" 18 "github.com/docker/docker/internal/test/request" 19 "github.com/docker/docker/pkg/stdcopy" 20 "gotest.tools/assert" 21 ) 22 23 func (s *DockerSuite) TestLogsAPIWithStdout(c *testing.T) { 24 out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done") 25 id := strings.TrimSpace(out) 26 assert.NilError(c, waitRun(id)) 27 28 type logOut struct { 29 out string 30 err error 31 } 32 33 chLog := make(chan logOut) 34 res, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id)) 35 assert.NilError(c, err) 36 assert.Equal(c, res.StatusCode, http.StatusOK) 37 38 go func() { 39 defer body.Close() 40 out, err := bufio.NewReader(body).ReadString('\n') 41 if err != nil { 42 chLog <- logOut{"", err} 43 return 44 } 45 chLog <- logOut{strings.TrimSpace(out), err} 46 }() 47 48 select { 49 case l := <-chLog: 50 assert.NilError(c, l.err) 51 if !strings.HasSuffix(l.out, "hello") { 52 c.Fatalf("expected log output to container 'hello', but it does not") 53 } 54 case <-time.After(30 * time.Second): 55 c.Fatal("timeout waiting for logs to exit") 56 } 57 } 58 59 func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *testing.T) { 60 name := "logs_test" 61 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 62 cli, err := client.NewClientWithOpts(client.FromEnv) 63 assert.NilError(c, err) 64 defer cli.Close() 65 66 _, err = cli.ContainerLogs(context.Background(), name, types.ContainerLogsOptions{}) 67 assert.ErrorContains(c, err, "Bad parameters: you must choose at least one stream") 68 } 69 70 // Regression test for #12704 71 func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *testing.T) { 72 name := "logs_test" 73 t0 := time.Now() 74 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10") 75 76 _, body, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name)) 77 t1 := time.Now() 78 assert.NilError(c, err) 79 body.Close() 80 elapsed := t1.Sub(t0).Seconds() 81 if elapsed > 20.0 { 82 c.Fatalf("HTTP response was not immediate (elapsed %.1fs)", elapsed) 83 } 84 } 85 86 func (s *DockerSuite) TestLogsAPIContainerNotFound(c *testing.T) { 87 name := "nonExistentContainer" 88 resp, _, err := request.Get(fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name)) 89 assert.NilError(c, err) 90 assert.Equal(c, resp.StatusCode, http.StatusNotFound) 91 } 92 93 func (s *DockerSuite) TestLogsAPIUntilFutureFollow(c *testing.T) { 94 testRequires(c, DaemonIsLinux) 95 name := "logsuntilfuturefollow" 96 dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", "while true; do date +%s; sleep 1; done") 97 assert.NilError(c, waitRun(name)) 98 99 untilSecs := 5 100 untilDur, err := time.ParseDuration(fmt.Sprintf("%ds", untilSecs)) 101 assert.NilError(c, err) 102 until := daemonTime(c).Add(untilDur) 103 104 client, err := client.NewClientWithOpts(client.FromEnv) 105 if err != nil { 106 c.Fatal(err) 107 } 108 109 cfg := types.ContainerLogsOptions{Until: until.Format(time.RFC3339Nano), Follow: true, ShowStdout: true, Timestamps: true} 110 reader, err := client.ContainerLogs(context.Background(), name, cfg) 111 assert.NilError(c, err) 112 113 type logOut struct { 114 out string 115 err error 116 } 117 118 chLog := make(chan logOut) 119 120 go func() { 121 bufReader := bufio.NewReader(reader) 122 defer reader.Close() 123 for i := 0; i < untilSecs; i++ { 124 out, _, err := bufReader.ReadLine() 125 if err != nil { 126 if err == io.EOF { 127 return 128 } 129 chLog <- logOut{"", err} 130 return 131 } 132 133 chLog <- logOut{strings.TrimSpace(string(out)), err} 134 } 135 }() 136 137 for i := 0; i < untilSecs; i++ { 138 select { 139 case l := <-chLog: 140 assert.NilError(c, l.err) 141 i, err := strconv.ParseInt(strings.Split(l.out, " ")[1], 10, 64) 142 assert.NilError(c, err) 143 assert.Assert(c, time.Unix(i, 0).UnixNano() <= until.UnixNano()) 144 case <-time.After(20 * time.Second): 145 c.Fatal("timeout waiting for logs to exit") 146 } 147 } 148 } 149 150 func (s *DockerSuite) TestLogsAPIUntil(c *testing.T) { 151 testRequires(c, MinimumAPIVersion("1.34")) 152 name := "logsuntil" 153 dockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; sleep 1; done") 154 155 client, err := client.NewClientWithOpts(client.FromEnv) 156 if err != nil { 157 c.Fatal(err) 158 } 159 160 extractBody := func(c *testing.T, cfg types.ContainerLogsOptions) []string { 161 reader, err := client.ContainerLogs(context.Background(), name, cfg) 162 assert.NilError(c, err) 163 164 actualStdout := new(bytes.Buffer) 165 actualStderr := ioutil.Discard 166 _, err = stdcopy.StdCopy(actualStdout, actualStderr, reader) 167 assert.NilError(c, err) 168 169 return strings.Split(actualStdout.String(), "\n") 170 } 171 172 // Get timestamp of second log line 173 allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true}) 174 assert.Assert(c, len(allLogs) >= 3) 175 176 t, err := time.Parse(time.RFC3339Nano, strings.Split(allLogs[1], " ")[0]) 177 assert.NilError(c, err) 178 until := t.Format(time.RFC3339Nano) 179 180 // Get logs until the timestamp of second line, i.e. first two lines 181 logs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true, Until: until}) 182 183 // Ensure log lines after cut-off are excluded 184 logsString := strings.Join(logs, "\n") 185 assert.Assert(c, !strings.Contains(logsString, "log3"), "unexpected log message returned, until=%v", until) 186 } 187 188 func (s *DockerSuite) TestLogsAPIUntilDefaultValue(c *testing.T) { 189 name := "logsuntildefaultval" 190 dockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; done") 191 192 client, err := client.NewClientWithOpts(client.FromEnv) 193 if err != nil { 194 c.Fatal(err) 195 } 196 197 extractBody := func(c *testing.T, cfg types.ContainerLogsOptions) []string { 198 reader, err := client.ContainerLogs(context.Background(), name, cfg) 199 assert.NilError(c, err) 200 201 actualStdout := new(bytes.Buffer) 202 actualStderr := ioutil.Discard 203 _, err = stdcopy.StdCopy(actualStdout, actualStderr, reader) 204 assert.NilError(c, err) 205 206 return strings.Split(actualStdout.String(), "\n") 207 } 208 209 // Get timestamp of second log line 210 allLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true}) 211 212 // Test with default value specified and parameter omitted 213 defaultLogs := extractBody(c, types.ContainerLogsOptions{Timestamps: true, ShowStdout: true, Until: "0"}) 214 assert.DeepEqual(c, defaultLogs, allLogs) 215 }