github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/client/events_test.go (about)

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