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

     1  package events
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Prakhar-Agarwal-byte/moby/api/types/events"
     9  	timetypes "github.com/Prakhar-Agarwal-byte/moby/api/types/time"
    10  	eventstestutils "github.com/Prakhar-Agarwal-byte/moby/daemon/events/testutils"
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  )
    14  
    15  // validateLegacyFields validates that the legacy "Status", "ID", and "From"
    16  // fields are set to the same value as their "current" (non-legacy) fields.
    17  //
    18  // These fields were deprecated since v1.10 (https://github.com/moby/moby/pull/18888).
    19  //
    20  // TODO remove this once we removed the deprecated `ID`, `Status`, and `From` fields.
    21  func validateLegacyFields(t *testing.T, msg events.Message) {
    22  	t.Helper()
    23  	assert.Check(t, is.Equal(msg.Status, string(msg.Action)), "Legacy Status field does not match Action")
    24  	assert.Check(t, is.Equal(msg.ID, msg.Actor.ID), "Legacy ID field does not match Actor.ID")
    25  	assert.Check(t, is.Equal(msg.From, msg.Actor.Attributes["image"]), "Legacy From field does not match Actor.Attributes.image")
    26  }
    27  
    28  func TestEventsLog(t *testing.T) {
    29  	e := New()
    30  	_, l1, _ := e.Subscribe()
    31  	_, l2, _ := e.Subscribe()
    32  	defer e.Evict(l1)
    33  	defer e.Evict(l2)
    34  	subscriberCount := e.SubscribersCount()
    35  	assert.Check(t, is.Equal(subscriberCount, 2))
    36  
    37  	e.Log("test", events.ContainerEventType, events.Actor{
    38  		ID:         "cont",
    39  		Attributes: map[string]string{"image": "image"},
    40  	})
    41  	select {
    42  	case msg := <-l1:
    43  		assert.Check(t, is.Len(e.events, 1))
    44  
    45  		jmsg, ok := msg.(events.Message)
    46  		assert.Assert(t, ok, "unexpected type: %T", msg)
    47  		validateLegacyFields(t, jmsg)
    48  		assert.Check(t, is.Equal(jmsg.Action, events.Action("test")))
    49  		assert.Check(t, is.Equal(jmsg.Actor.ID, "cont"))
    50  		assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image"))
    51  	case <-time.After(1 * time.Second):
    52  		t.Fatal("Timeout waiting for broadcasted message")
    53  	}
    54  	select {
    55  	case msg := <-l2:
    56  		assert.Check(t, is.Len(e.events, 1))
    57  
    58  		jmsg, ok := msg.(events.Message)
    59  		assert.Assert(t, ok, "unexpected type: %T", msg)
    60  		validateLegacyFields(t, jmsg)
    61  		assert.Check(t, is.Equal(jmsg.Action, events.Action("test")))
    62  		assert.Check(t, is.Equal(jmsg.Actor.ID, "cont"))
    63  		assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image"))
    64  	case <-time.After(1 * time.Second):
    65  		t.Fatal("Timeout waiting for broadcasted message")
    66  	}
    67  }
    68  
    69  func TestEventsLogTimeout(t *testing.T) {
    70  	e := New()
    71  	_, l, _ := e.Subscribe()
    72  	defer e.Evict(l)
    73  
    74  	c := make(chan struct{})
    75  	go func() {
    76  		e.Log("test", events.ImageEventType, events.Actor{
    77  			ID: "image",
    78  		})
    79  		close(c)
    80  	}()
    81  
    82  	select {
    83  	case <-c:
    84  	case <-time.After(time.Second):
    85  		t.Fatal("Timeout publishing message")
    86  	}
    87  }
    88  
    89  func TestLogEvents(t *testing.T) {
    90  	e := New()
    91  
    92  	for i := 0; i < eventsLimit+16; i++ {
    93  		num := strconv.Itoa(i)
    94  		e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
    95  			ID:         "cont_" + num,
    96  			Attributes: map[string]string{"image": "image_" + num},
    97  		})
    98  	}
    99  	time.Sleep(50 * time.Millisecond)
   100  	current, l, _ := e.Subscribe()
   101  	for i := 0; i < 10; i++ {
   102  		num := strconv.Itoa(i + eventsLimit + 16)
   103  		e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
   104  			ID:         "cont_" + num,
   105  			Attributes: map[string]string{"image": "image_" + num},
   106  		})
   107  	}
   108  	assert.Assert(t, is.Len(e.events, eventsLimit))
   109  
   110  	var msgs []events.Message
   111  	for len(msgs) < 10 {
   112  		m := <-l
   113  		jm, ok := (m).(events.Message)
   114  		if !ok {
   115  			t.Fatalf("Unexpected type %T", m)
   116  		}
   117  		msgs = append(msgs, jm)
   118  	}
   119  
   120  	assert.Assert(t, is.Len(current, eventsLimit))
   121  
   122  	first := current[0]
   123  	validateLegacyFields(t, first)
   124  	assert.Check(t, is.Equal(first.Action, events.Action("action_16")))
   125  
   126  	last := current[len(current)-1]
   127  	assert.Check(t, is.Equal(last.Action, events.Action("action_271")))
   128  
   129  	firstC := msgs[0]
   130  	assert.Check(t, is.Equal(firstC.Action, events.Action("action_272")))
   131  
   132  	lastC := msgs[len(msgs)-1]
   133  	assert.Check(t, is.Equal(lastC.Action, events.Action("action_281")))
   134  }
   135  
   136  // Regression-test for https://github.com/moby/moby/issues/20999
   137  //
   138  // Fixtures:
   139  //
   140  //	2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   141  //	2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)
   142  //	2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   143  func TestLoadBufferedEvents(t *testing.T) {
   144  	now := time.Now()
   145  	f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
   146  	assert.NilError(t, err)
   147  
   148  	s, sNano, err := timetypes.ParseTimestamps(f, -1)
   149  	assert.NilError(t, err)
   150  
   151  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   152  	assert.NilError(t, err)
   153  
   154  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   155  	assert.NilError(t, err)
   156  
   157  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   158  	assert.NilError(t, err)
   159  
   160  	evts := &Events{
   161  		events: []events.Message{*m1, *m2, *m3},
   162  	}
   163  
   164  	since := time.Unix(s, sNano)
   165  	until := time.Time{}
   166  
   167  	messages := evts.loadBufferedEvents(since, until, nil)
   168  	assert.Assert(t, is.Len(messages, 1))
   169  }
   170  
   171  func TestLoadBufferedEventsOnlyFromPast(t *testing.T) {
   172  	now := time.Now()
   173  	f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.090000000+02:00", now)
   174  	assert.NilError(t, err)
   175  
   176  	s, sNano, err := timetypes.ParseTimestamps(f, 0)
   177  	assert.NilError(t, err)
   178  
   179  	f, err = timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
   180  	assert.NilError(t, err)
   181  
   182  	u, uNano, err := timetypes.ParseTimestamps(f, 0)
   183  	assert.NilError(t, err)
   184  
   185  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   186  	assert.NilError(t, err)
   187  
   188  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   189  	assert.NilError(t, err)
   190  
   191  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   192  	assert.NilError(t, err)
   193  
   194  	evts := &Events{
   195  		events: []events.Message{*m1, *m2, *m3},
   196  	}
   197  
   198  	since := time.Unix(s, sNano)
   199  	until := time.Unix(u, uNano)
   200  
   201  	messages := evts.loadBufferedEvents(since, until, nil)
   202  	assert.Assert(t, is.Len(messages, 1))
   203  	assert.Check(t, is.Equal(messages[0].Type, events.NetworkEventType))
   204  }
   205  
   206  // Regression-test for https://github.com/moby/moby/issues/13753
   207  func TestIgnoreBufferedWhenNoTimes(t *testing.T) {
   208  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   209  	assert.NilError(t, err)
   210  
   211  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   212  	assert.NilError(t, err)
   213  
   214  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   215  	assert.NilError(t, err)
   216  
   217  	evts := &Events{
   218  		events: []events.Message{*m1, *m2, *m3},
   219  	}
   220  
   221  	since := time.Time{}
   222  	until := time.Time{}
   223  
   224  	messages := evts.loadBufferedEvents(since, until, nil)
   225  	assert.Assert(t, is.Len(messages, 0))
   226  }