service_logs_test.go 3.2 KB

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