github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_logs_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "os/exec" 7 "regexp" 8 "strings" 9 "time" 10 11 "github.com/docker/docker/pkg/integration/checker" 12 "github.com/docker/docker/pkg/jsonlog" 13 "github.com/go-check/check" 14 ) 15 16 // This used to work, it test a log of PageSize-1 (gh#4851) 17 func (s *DockerSuite) TestLogsContainerSmallerThanPage(c *check.C) { 18 testLen := 32767 19 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)) 20 21 id := strings.TrimSpace(out) 22 dockerCmd(c, "wait", id) 23 24 out, _ = dockerCmd(c, "logs", id) 25 26 c.Assert(out, checker.HasLen, testLen+1) 27 } 28 29 // Regression test: When going over the PageSize, it used to panic (gh#4851) 30 func (s *DockerSuite) TestLogsContainerBiggerThanPage(c *check.C) { 31 testLen := 32768 32 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)) 33 34 id := strings.TrimSpace(out) 35 dockerCmd(c, "wait", id) 36 37 out, _ = dockerCmd(c, "logs", id) 38 39 c.Assert(out, checker.HasLen, testLen+1) 40 } 41 42 // Regression test: When going much over the PageSize, it used to block (gh#4851) 43 func (s *DockerSuite) TestLogsContainerMuchBiggerThanPage(c *check.C) { 44 testLen := 33000 45 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen)) 46 47 id := strings.TrimSpace(out) 48 dockerCmd(c, "wait", id) 49 50 out, _ = dockerCmd(c, "logs", id) 51 52 c.Assert(out, checker.HasLen, testLen+1) 53 } 54 55 func (s *DockerSuite) TestLogsTimestamps(c *check.C) { 56 testLen := 100 57 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen)) 58 59 id := strings.TrimSpace(out) 60 dockerCmd(c, "wait", id) 61 62 out, _ = dockerCmd(c, "logs", "-t", id) 63 64 lines := strings.Split(out, "\n") 65 66 c.Assert(lines, checker.HasLen, testLen+1) 67 68 ts := regexp.MustCompile(`^.* `) 69 70 for _, l := range lines { 71 if l != "" { 72 _, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l)) 73 c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l)) 74 // ensure we have padded 0's 75 c.Assert(l[29], checker.Equals, uint8('Z')) 76 } 77 } 78 } 79 80 func (s *DockerSuite) TestLogsSeparateStderr(c *check.C) { 81 msg := "stderr_log" 82 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)) 83 84 id := strings.TrimSpace(out) 85 dockerCmd(c, "wait", id) 86 87 stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id) 88 89 c.Assert(stdout, checker.Equals, "") 90 91 stderr = strings.TrimSpace(stderr) 92 93 c.Assert(stderr, checker.Equals, msg) 94 } 95 96 func (s *DockerSuite) TestLogsStderrInStdout(c *check.C) { 97 // TODO Windows: Needs investigation why this fails. Obtained string includes 98 // a bunch of ANSI escape sequences before the "stderr_log" message. 99 testRequires(c, DaemonIsLinux) 100 msg := "stderr_log" 101 out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)) 102 103 id := strings.TrimSpace(out) 104 dockerCmd(c, "wait", id) 105 106 stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id) 107 c.Assert(stderr, checker.Equals, "") 108 109 stdout = strings.TrimSpace(stdout) 110 c.Assert(stdout, checker.Equals, msg) 111 } 112 113 func (s *DockerSuite) TestLogsTail(c *check.C) { 114 testLen := 100 115 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen)) 116 117 id := strings.TrimSpace(out) 118 dockerCmd(c, "wait", id) 119 120 out, _ = dockerCmd(c, "logs", "--tail", "5", id) 121 122 lines := strings.Split(out, "\n") 123 124 c.Assert(lines, checker.HasLen, 6) 125 126 out, _ = dockerCmd(c, "logs", "--tail", "all", id) 127 128 lines = strings.Split(out, "\n") 129 130 c.Assert(lines, checker.HasLen, testLen+1) 131 132 out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", id) 133 134 lines = strings.Split(out, "\n") 135 136 c.Assert(lines, checker.HasLen, testLen+1) 137 } 138 139 func (s *DockerSuite) TestLogsFollowStopped(c *check.C) { 140 dockerCmd(c, "run", "--name=test", "busybox", "echo", "hello") 141 id, err := getIDByName("test") 142 c.Assert(err, check.IsNil) 143 144 logsCmd := exec.Command(dockerBinary, "logs", "-f", id) 145 c.Assert(logsCmd.Start(), checker.IsNil) 146 147 errChan := make(chan error) 148 go func() { 149 errChan <- logsCmd.Wait() 150 close(errChan) 151 }() 152 153 select { 154 case err := <-errChan: 155 c.Assert(err, checker.IsNil) 156 case <-time.After(30 * time.Second): 157 c.Fatal("Following logs is hanged") 158 } 159 } 160 161 func (s *DockerSuite) TestLogsSince(c *check.C) { 162 name := "testlogssince" 163 dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done") 164 out, _ := dockerCmd(c, "logs", "-t", name) 165 166 log2Line := strings.Split(strings.Split(out, "\n")[1], " ") 167 t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written 168 c.Assert(err, checker.IsNil) 169 since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up 170 out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name) 171 172 // Skip 2 seconds 173 unexpected := []string{"log1", "log2"} 174 for _, v := range unexpected { 175 c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since)) 176 } 177 178 // Test to make sure a bad since format is caught by the client 179 out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name) 180 c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server")) 181 182 // Test with default value specified and parameter omitted 183 expected := []string{"log1", "log2", "log3"} 184 for _, cmd := range []*exec.Cmd{ 185 exec.Command(dockerBinary, "logs", "-t", name), 186 exec.Command(dockerBinary, "logs", "-t", "--since=0", name), 187 } { 188 out, _, err = runCommandWithOutput(cmd) 189 c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out)) 190 for _, v := range expected { 191 c.Assert(out, checker.Contains, v) 192 } 193 } 194 } 195 196 func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) { 197 // TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now. 198 testRequires(c, DaemonIsLinux) 199 name := "testlogssincefuturefollow" 200 out, _ := dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`) 201 202 // Extract one timestamp from the log file to give us a starting point for 203 // our `--since` argument. Because the log producer runs in the background, 204 // we need to check repeatedly for some output to be produced. 205 var timestamp string 206 for i := 0; i != 100 && timestamp == ""; i++ { 207 if out, _ = dockerCmd(c, "logs", "-t", name); out == "" { 208 time.Sleep(time.Millisecond * 100) // Retry 209 } else { 210 timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0] 211 } 212 } 213 214 c.Assert(timestamp, checker.Not(checker.Equals), "") 215 t, err := time.Parse(time.RFC3339Nano, timestamp) 216 c.Assert(err, check.IsNil) 217 218 since := t.Unix() + 2 219 out, _ = dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name) 220 c.Assert(out, checker.Not(checker.HasLen), 0, check.Commentf("cannot read from empty log")) 221 lines := strings.Split(strings.TrimSpace(out), "\n") 222 for _, v := range lines { 223 ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0]) 224 c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'", v)) 225 c.Assert(ts.Unix() >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts)) 226 } 227 } 228 229 // Regression test for #8832 230 func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) { 231 // TODO Windows: Fix this test for TP5. 232 testRequires(c, DaemonIsLinux) 233 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 600000;yes X | head -c 200000`) 234 235 id := strings.TrimSpace(out) 236 237 stopSlowRead := make(chan bool) 238 239 go func() { 240 exec.Command(dockerBinary, "wait", id).Run() 241 stopSlowRead <- true 242 }() 243 244 logCmd := exec.Command(dockerBinary, "logs", "-f", id) 245 stdout, err := logCmd.StdoutPipe() 246 c.Assert(err, checker.IsNil) 247 c.Assert(logCmd.Start(), checker.IsNil) 248 249 // First read slowly 250 bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead) 251 c.Assert(err, checker.IsNil) 252 253 // After the container has finished we can continue reading fast 254 bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil) 255 c.Assert(err, checker.IsNil) 256 257 actual := bytes1 + bytes2 258 expected := 200000 259 c.Assert(actual, checker.Equals, expected) 260 } 261 262 func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) { 263 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done") 264 id := strings.TrimSpace(out) 265 c.Assert(waitRun(id), checker.IsNil) 266 267 nroutines, err := getGoroutineNumber() 268 c.Assert(err, checker.IsNil) 269 cmd := exec.Command(dockerBinary, "logs", "-f", id) 270 r, w := io.Pipe() 271 cmd.Stdout = w 272 c.Assert(cmd.Start(), checker.IsNil) 273 274 // Make sure pipe is written to 275 chErr := make(chan error) 276 go func() { 277 b := make([]byte, 1) 278 _, err := r.Read(b) 279 chErr <- err 280 }() 281 c.Assert(<-chErr, checker.IsNil) 282 c.Assert(cmd.Process.Kill(), checker.IsNil) 283 284 // NGoroutines is not updated right away, so we need to wait before failing 285 c.Assert(waitForGoroutines(nroutines), checker.IsNil) 286 } 287 288 func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) { 289 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done") 290 id := strings.TrimSpace(out) 291 c.Assert(waitRun(id), checker.IsNil) 292 293 nroutines, err := getGoroutineNumber() 294 c.Assert(err, checker.IsNil) 295 cmd := exec.Command(dockerBinary, "logs", "-f", id) 296 c.Assert(cmd.Start(), checker.IsNil) 297 time.Sleep(200 * time.Millisecond) 298 c.Assert(cmd.Process.Kill(), checker.IsNil) 299 300 // NGoroutines is not updated right away, so we need to wait before failing 301 c.Assert(waitForGoroutines(nroutines), checker.IsNil) 302 } 303 304 func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) { 305 name := "testlogsnocontainer" 306 out, _, _ := dockerCmdWithError("logs", name) 307 message := fmt.Sprintf("Error: No such container: %s\n", name) 308 c.Assert(out, checker.Equals, message) 309 } 310 311 func (s *DockerSuite) TestLogsWithDetails(c *check.C) { 312 dockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello") 313 out, _ := dockerCmd(c, "logs", "--details", "--timestamps", "test") 314 315 logFields := strings.Fields(strings.TrimSpace(out)) 316 c.Assert(len(logFields), checker.Equals, 3, check.Commentf(out)) 317 318 details := strings.Split(logFields[1], ",") 319 c.Assert(details, checker.HasLen, 2) 320 c.Assert(details[0], checker.Equals, "baz=qux") 321 c.Assert(details[1], checker.Equals, "foo=bar") 322 }