container_logs_test.go 4.4 KB

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