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