github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/client/events_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "io/ioutil" 9 "net/http" 10 "strings" 11 "testing" 12 13 "golang.org/x/net/context" 14 15 "github.com/docker/docker/api/types" 16 "github.com/docker/docker/api/types/events" 17 "github.com/docker/docker/api/types/filters" 18 ) 19 20 func TestEventsErrorInOptions(t *testing.T) { 21 errorCases := []struct { 22 options types.EventsOptions 23 expectedError string 24 }{ 25 { 26 options: types.EventsOptions{ 27 Since: "2006-01-02TZ", 28 }, 29 expectedError: `parsing time "2006-01-02TZ"`, 30 }, 31 { 32 options: types.EventsOptions{ 33 Until: "2006-01-02TZ", 34 }, 35 expectedError: `parsing time "2006-01-02TZ"`, 36 }, 37 } 38 for _, e := range errorCases { 39 client := &Client{ 40 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 41 } 42 _, errs := client.Events(context.Background(), e.options) 43 err := <-errs 44 if err == nil || !strings.Contains(err.Error(), e.expectedError) { 45 t.Fatalf("expected an error %q, got %v", e.expectedError, err) 46 } 47 } 48 } 49 50 func TestEventsErrorFromServer(t *testing.T) { 51 client := &Client{ 52 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 53 } 54 _, errs := client.Events(context.Background(), types.EventsOptions{}) 55 err := <-errs 56 if err == nil || err.Error() != "Error response from daemon: Server error" { 57 t.Fatalf("expected a Server Error, got %v", err) 58 } 59 } 60 61 func TestEvents(t *testing.T) { 62 63 expectedURL := "/events" 64 65 filters := filters.NewArgs() 66 filters.Add("type", events.ContainerEventType) 67 expectedFiltersJSON := fmt.Sprintf(`{"type":{"%s":true}}`, events.ContainerEventType) 68 69 eventsCases := []struct { 70 options types.EventsOptions 71 events []events.Message 72 expectedEvents map[string]bool 73 expectedQueryParams map[string]string 74 }{ 75 { 76 options: types.EventsOptions{ 77 Filters: filters, 78 }, 79 expectedQueryParams: map[string]string{ 80 "filters": expectedFiltersJSON, 81 }, 82 events: []events.Message{}, 83 expectedEvents: make(map[string]bool), 84 }, 85 { 86 options: types.EventsOptions{ 87 Filters: filters, 88 }, 89 expectedQueryParams: map[string]string{ 90 "filters": expectedFiltersJSON, 91 }, 92 events: []events.Message{ 93 { 94 Type: "container", 95 ID: "1", 96 Action: "create", 97 }, 98 { 99 Type: "container", 100 ID: "2", 101 Action: "die", 102 }, 103 { 104 Type: "container", 105 ID: "3", 106 Action: "create", 107 }, 108 }, 109 expectedEvents: map[string]bool{ 110 "1": true, 111 "2": true, 112 "3": true, 113 }, 114 }, 115 } 116 117 for _, eventsCase := range eventsCases { 118 client := &Client{ 119 client: newMockClient(func(req *http.Request) (*http.Response, error) { 120 if !strings.HasPrefix(req.URL.Path, expectedURL) { 121 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 122 } 123 query := req.URL.Query() 124 125 for key, expected := range eventsCase.expectedQueryParams { 126 actual := query.Get(key) 127 if actual != expected { 128 return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual) 129 } 130 } 131 132 buffer := new(bytes.Buffer) 133 134 for _, e := range eventsCase.events { 135 b, _ := json.Marshal(e) 136 buffer.Write(b) 137 } 138 139 return &http.Response{ 140 StatusCode: http.StatusOK, 141 Body: ioutil.NopCloser(buffer), 142 }, nil 143 }), 144 } 145 146 messages, errs := client.Events(context.Background(), eventsCase.options) 147 148 loop: 149 for { 150 select { 151 case err := <-errs: 152 if err != nil && err != io.EOF { 153 t.Fatal(err) 154 } 155 156 break loop 157 case e := <-messages: 158 _, ok := eventsCase.expectedEvents[e.ID] 159 if !ok { 160 t.Fatalf("event received not expected with action %s & id %s", e.Action, e.ID) 161 } 162 } 163 } 164 } 165 }