15 "github.com/docker/docker/api/types"
17 "golang.org/x/net/context"
20 func TestServiceLogsError(t *testing.T) {
22 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
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)
28 _, err = client.ServiceLogs(context.Background(), "service_id", types.ContainerLogsOptions{
29 Since: "2006-01-02TZ",
31 if err == nil || !strings.Contains(err.Error(), `parsing time "2006-01-02TZ"`) {
32 t.Fatalf("expected a 'parsing time' error, got %v", err)
36 func TestServiceLogs(t *testing.T) {
37 expectedURL := "/services/service_id/logs"
39 options types.ContainerLogsOptions
40 expectedQueryParams map[string]string
43 expectedQueryParams: map[string]string{
48 options: types.ContainerLogsOptions{
51 expectedQueryParams: map[string]string{
56 options: types.ContainerLogsOptions{
63 expectedQueryParams: map[string]string{
73 options: types.ContainerLogsOptions{
74 // An complete invalid date, timestamp or go duration will be
76 Since: "invalid but valid",
78 expectedQueryParams: map[string]string{
80 "since": "invalid but valid",
84 for _, logCase := range cases {
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)
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)
98 return &http.Response{
99 StatusCode: http.StatusOK,
100 Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
104 body, err := client.ServiceLogs(context.Background(), "service_id", logCase.options)
109 content, err := ioutil.ReadAll(body)
113 if string(content) != "response" {
114 t.Fatalf("expected response to contain 'response', got %s", string(content))
119 func ExampleClient_ServiceLogs_withTimeout() {
120 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
123 client, _ := NewEnvClient()
124 reader, err := client.ServiceLogs(ctx, "service_id", types.ContainerLogsOptions{})
129 _, err = io.Copy(os.Stdout, reader)
130 if err != nil && err != io.EOF {