container_logs_test.go 4.5 KB

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