github.com/getong/docker@v1.13.1/integration-cli/docker_api_logs_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "fmt" 7 "net/http" 8 "strings" 9 "time" 10 11 "github.com/docker/docker/pkg/integration/checker" 12 "github.com/go-check/check" 13 ) 14 15 func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) { 16 out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done") 17 id := strings.TrimSpace(out) 18 c.Assert(waitRun(id), checker.IsNil) 19 20 type logOut struct { 21 out string 22 res *http.Response 23 err error 24 } 25 chLog := make(chan logOut) 26 27 go func() { 28 res, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "") 29 if err != nil { 30 chLog <- logOut{"", nil, err} 31 return 32 } 33 defer body.Close() 34 out, err := bufio.NewReader(body).ReadString('\n') 35 if err != nil { 36 chLog <- logOut{"", nil, err} 37 return 38 } 39 chLog <- logOut{strings.TrimSpace(out), res, err} 40 }() 41 42 select { 43 case l := <-chLog: 44 c.Assert(l.err, checker.IsNil) 45 c.Assert(l.res.StatusCode, checker.Equals, http.StatusOK) 46 if !strings.HasSuffix(l.out, "hello") { 47 c.Fatalf("expected log output to container 'hello', but it does not") 48 } 49 case <-time.After(20 * time.Second): 50 c.Fatal("timeout waiting for logs to exit") 51 } 52 } 53 54 func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) { 55 name := "logs_test" 56 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 57 58 status, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs", name), nil) 59 c.Assert(status, checker.Equals, http.StatusBadRequest) 60 c.Assert(err, checker.IsNil) 61 62 expected := "Bad parameters: you must choose at least one stream" 63 c.Assert(getErrorMessage(c, body), checker.Contains, expected) 64 } 65 66 // Regression test for #12704 67 func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) { 68 name := "logs_test" 69 t0 := time.Now() 70 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10") 71 72 _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "") 73 t1 := time.Now() 74 c.Assert(err, checker.IsNil) 75 body.Close() 76 elapsed := t1.Sub(t0).Seconds() 77 if elapsed > 20.0 { 78 c.Fatalf("HTTP response was not immediate (elapsed %.1fs)", elapsed) 79 } 80 } 81 82 func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) { 83 name := "nonExistentContainer" 84 resp, _, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "") 85 c.Assert(err, checker.IsNil) 86 c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound) 87 }