client/events_test.go
4f0d95fa
 package client // import "github.com/docker/docker/client"
7c36a1af
 
 import (
 	"bytes"
7d62e40f
 	"context"
d6bd79c1
 	"encoding/json"
7c36a1af
 	"fmt"
d6bd79c1
 	"io"
7c36a1af
 	"io/ioutil"
 	"net/http"
 	"strings"
 	"testing"
 
 	"github.com/docker/docker/api/types"
d6bd79c1
 	"github.com/docker/docker/api/types/events"
7c36a1af
 	"github.com/docker/docker/api/types/filters"
161e0a90
 	"github.com/docker/docker/errdefs"
7c36a1af
 )
 
 func TestEventsErrorInOptions(t *testing.T) {
 	errorCases := []struct {
 		options       types.EventsOptions
 		expectedError string
 	}{
 		{
 			options: types.EventsOptions{
 				Since: "2006-01-02TZ",
 			},
 			expectedError: `parsing time "2006-01-02TZ"`,
 		},
 		{
 			options: types.EventsOptions{
 				Until: "2006-01-02TZ",
 			},
 			expectedError: `parsing time "2006-01-02TZ"`,
 		},
 	}
 	for _, e := range errorCases {
 		client := &Client{
9a072adf
 			client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
7c36a1af
 		}
d6bd79c1
 		_, errs := client.Events(context.Background(), e.options)
 		err := <-errs
7c36a1af
 		if err == nil || !strings.Contains(err.Error(), e.expectedError) {
9279a93f
 			t.Fatalf("expected an error %q, got %v", e.expectedError, err)
7c36a1af
 		}
 	}
 }
 
 func TestEventsErrorFromServer(t *testing.T) {
 	client := &Client{
9a072adf
 		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
7c36a1af
 	}
d6bd79c1
 	_, errs := client.Events(context.Background(), types.EventsOptions{})
 	err := <-errs
7c36a1af
 	if err == nil || err.Error() != "Error response from daemon: Server error" {
 		t.Fatalf("expected a Server Error, got %v", err)
 	}
161e0a90
 	if !errdefs.IsSystem(err) {
 		t.Fatalf("expected a Server Error, got %T", err)
 	}
7c36a1af
 }
 
 func TestEvents(t *testing.T) {
d6bd79c1
 
7c36a1af
 	expectedURL := "/events"
 
 	filters := filters.NewArgs()
d6bd79c1
 	filters.Add("type", events.ContainerEventType)
 	expectedFiltersJSON := fmt.Sprintf(`{"type":{"%s":true}}`, events.ContainerEventType)
7c36a1af
 
 	eventsCases := []struct {
 		options             types.EventsOptions
d6bd79c1
 		events              []events.Message
 		expectedEvents      map[string]bool
7c36a1af
 		expectedQueryParams map[string]string
 	}{
 		{
 			options: types.EventsOptions{
d6bd79c1
 				Filters: filters,
7c36a1af
 			},
 			expectedQueryParams: map[string]string{
d6bd79c1
 				"filters": expectedFiltersJSON,
7c36a1af
 			},
d6bd79c1
 			events:         []events.Message{},
 			expectedEvents: make(map[string]bool),
7c36a1af
 		},
 		{
 			options: types.EventsOptions{
 				Filters: filters,
 			},
 			expectedQueryParams: map[string]string{
 				"filters": expectedFiltersJSON,
 			},
d6bd79c1
 			events: []events.Message{
 				{
 					Type:   "container",
 					ID:     "1",
 					Action: "create",
 				},
 				{
 					Type:   "container",
 					ID:     "2",
 					Action: "die",
 				},
 				{
 					Type:   "container",
 					ID:     "3",
 					Action: "create",
 				},
 			},
 			expectedEvents: map[string]bool{
 				"1": true,
 				"2": true,
 				"3": true,
 			},
7c36a1af
 		},
 	}
 
 	for _, eventsCase := range eventsCases {
 		client := &Client{
9a072adf
 			client: newMockClient(func(req *http.Request) (*http.Response, error) {
7c36a1af
 				if !strings.HasPrefix(req.URL.Path, expectedURL) {
 					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
 				}
 				query := req.URL.Query()
d6bd79c1
 
7c36a1af
 				for key, expected := range eventsCase.expectedQueryParams {
 					actual := query.Get(key)
 					if actual != expected {
 						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
 					}
 				}
d6bd79c1
 
 				buffer := new(bytes.Buffer)
 
 				for _, e := range eventsCase.events {
 					b, _ := json.Marshal(e)
 					buffer.Write(b)
 				}
 
7c36a1af
 				return &http.Response{
 					StatusCode: http.StatusOK,
d6bd79c1
 					Body:       ioutil.NopCloser(buffer),
7c36a1af
 				}, nil
 			}),
 		}
d6bd79c1
 
 		messages, errs := client.Events(context.Background(), eventsCase.options)
 
 	loop:
 		for {
 			select {
 			case err := <-errs:
 				if err != nil && err != io.EOF {
 					t.Fatal(err)
 				}
 
 				break loop
 			case e := <-messages:
 				_, ok := eventsCase.expectedEvents[e.ID]
 				if !ok {
 					t.Fatalf("event received not expected with action %s & id %s", e.Action, e.ID)
 				}
 			}
7c36a1af
 		}
 	}
 }