2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-08-09 20:34:07 +00:00
|
|
|
"encoding/json"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
2016-08-09 20:34:07 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2016-08-09 20:34:07 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2016-08-09 20:34:07 +00:00
|
|
|
_, errs := client.Events(context.Background(), e.options)
|
|
|
|
err := <-errs
|
2016-09-06 18:46:37 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), e.expectedError) {
|
2016-09-04 07:17:58 +00:00
|
|
|
t.Fatalf("expected an error %q, got %v", e.expectedError, err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventsErrorFromServer(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2016-08-09 20:34:07 +00:00
|
|
|
_, errs := client.Events(context.Background(), types.EventsOptions{})
|
|
|
|
err := <-errs
|
2016-09-06 18:46:37 +00:00
|
|
|
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
|
|
|
t.Fatalf("expected a Server Error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvents(t *testing.T) {
|
2016-08-09 20:34:07 +00:00
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
expectedURL := "/events"
|
|
|
|
|
|
|
|
filters := filters.NewArgs()
|
2016-08-09 20:34:07 +00:00
|
|
|
filters.Add("type", events.ContainerEventType)
|
|
|
|
expectedFiltersJSON := fmt.Sprintf(`{"type":{"%s":true}}`, events.ContainerEventType)
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
eventsCases := []struct {
|
|
|
|
options types.EventsOptions
|
2016-08-09 20:34:07 +00:00
|
|
|
events []events.Message
|
|
|
|
expectedEvents map[string]bool
|
2016-09-06 18:46:37 +00:00
|
|
|
expectedQueryParams map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
options: types.EventsOptions{
|
2016-08-09 20:34:07 +00:00
|
|
|
Filters: filters,
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
|
|
|
expectedQueryParams: map[string]string{
|
2016-08-09 20:34:07 +00:00
|
|
|
"filters": expectedFiltersJSON,
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
2016-08-09 20:34:07 +00:00
|
|
|
events: []events.Message{},
|
|
|
|
expectedEvents: make(map[string]bool),
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
options: types.EventsOptions{
|
|
|
|
Filters: filters,
|
|
|
|
},
|
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"filters": expectedFiltersJSON,
|
|
|
|
},
|
2016-08-09 20:34:07 +00:00
|
|
|
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,
|
|
|
|
},
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, eventsCase := range eventsCases {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
2016-08-09 20:34:07 +00:00
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 20:34:07 +00:00
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
for _, e := range eventsCase.events {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
buffer.Write(b)
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2016-08-09 20:34:07 +00:00
|
|
|
Body: ioutil.NopCloser(buffer),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
2016-08-09 20:34:07 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|