container_logs_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/api/types"
  14. "github.com/docker/docker/internal/testutil"
  15. "golang.org/x/net/context"
  16. )
  17. func TestContainerLogsNotFoundError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
  20. }
  21. _, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
  22. if !IsErrNotFound(err) {
  23. t.Fatalf("expected a not found error, got %v", err)
  24. }
  25. }
  26. func TestContainerLogsError(t *testing.T) {
  27. client := &Client{
  28. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  29. }
  30. _, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
  31. if err == nil || err.Error() != "Error response from daemon: Server error" {
  32. t.Fatalf("expected a Server Error, got %v", err)
  33. }
  34. _, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
  35. Since: "2006-01-02TZ",
  36. })
  37. testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
  38. _, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
  39. Until: "2006-01-02TZ",
  40. })
  41. testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
  42. }
  43. func TestContainerLogs(t *testing.T) {
  44. expectedURL := "/containers/container_id/logs"
  45. cases := []struct {
  46. options types.ContainerLogsOptions
  47. expectedQueryParams map[string]string
  48. }{
  49. {
  50. expectedQueryParams: map[string]string{
  51. "tail": "",
  52. },
  53. },
  54. {
  55. options: types.ContainerLogsOptions{
  56. Tail: "any",
  57. },
  58. expectedQueryParams: map[string]string{
  59. "tail": "any",
  60. },
  61. },
  62. {
  63. options: types.ContainerLogsOptions{
  64. ShowStdout: true,
  65. ShowStderr: true,
  66. Timestamps: true,
  67. Details: true,
  68. Follow: true,
  69. },
  70. expectedQueryParams: map[string]string{
  71. "tail": "",
  72. "stdout": "1",
  73. "stderr": "1",
  74. "timestamps": "1",
  75. "details": "1",
  76. "follow": "1",
  77. },
  78. },
  79. {
  80. options: types.ContainerLogsOptions{
  81. // An complete invalid date, timestamp or go duration will be
  82. // passed as is
  83. Since: "invalid but valid",
  84. },
  85. expectedQueryParams: map[string]string{
  86. "tail": "",
  87. "since": "invalid but valid",
  88. },
  89. },
  90. {
  91. options: types.ContainerLogsOptions{
  92. // An complete invalid date, timestamp or go duration will be
  93. // passed as is
  94. Until: "invalid but valid",
  95. },
  96. expectedQueryParams: map[string]string{
  97. "tail": "",
  98. "until": "invalid but valid",
  99. },
  100. },
  101. }
  102. for _, logCase := range cases {
  103. client := &Client{
  104. client: newMockClient(func(r *http.Request) (*http.Response, error) {
  105. if !strings.HasPrefix(r.URL.Path, expectedURL) {
  106. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
  107. }
  108. // Check query parameters
  109. query := r.URL.Query()
  110. for key, expected := range logCase.expectedQueryParams {
  111. actual := query.Get(key)
  112. if actual != expected {
  113. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  114. }
  115. }
  116. return &http.Response{
  117. StatusCode: http.StatusOK,
  118. Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
  119. }, nil
  120. }),
  121. }
  122. body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. defer body.Close()
  127. content, err := ioutil.ReadAll(body)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. if string(content) != "response" {
  132. t.Fatalf("expected response to contain 'response', got %s", string(content))
  133. }
  134. }
  135. }
  136. func ExampleClient_ContainerLogs_withTimeout() {
  137. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  138. defer cancel()
  139. client, _ := NewEnvClient()
  140. reader, err := client.ContainerLogs(ctx, "container_id", types.ContainerLogsOptions{})
  141. if err != nil {
  142. log.Fatal(err)
  143. }
  144. _, err = io.Copy(os.Stdout, reader)
  145. if err != nil && err != io.EOF {
  146. log.Fatal(err)
  147. }
  148. }