service_logs_test.go 3.5 KB

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