docker_api_logs_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "os/exec"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
  10. name := "logs_test"
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "bin/sh", "-c", "sleep 10 && echo "+name)
  12. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  13. c.Fatal(out, err)
  14. }
  15. statusCode, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", name), nil)
  16. if err != nil || statusCode != http.StatusOK {
  17. c.Fatalf("Expected %d from logs request, got %d", http.StatusOK, statusCode)
  18. }
  19. if !bytes.Contains(body, []byte(name)) {
  20. c.Fatalf("Expected %s, got %s", name, string(body[:]))
  21. }
  22. }
  23. func (s *DockerSuite) TestLogsApiNoStdoutNorStderr(c *check.C) {
  24. name := "logs_test"
  25. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  26. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  27. c.Fatal(out, err)
  28. }
  29. statusCode, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs", name), nil)
  30. if err == nil || statusCode != http.StatusBadRequest {
  31. c.Fatalf("Expected %d from logs request, got %d", http.StatusBadRequest, statusCode)
  32. }
  33. expected := "Bad parameters: you must choose at least one stream"
  34. if !bytes.Contains(body, []byte(expected)) {
  35. c.Fatalf("Expected %s, got %s", expected, string(body[:]))
  36. }
  37. }