gopkg.in/docker/docker.v20@v20.10.27/client/events_test.go (about)

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