2023-08-26 11:48:49 +00:00
|
|
|
package events
|
2015-04-03 20:58:56 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-26 11:48:49 +00:00
|
|
|
"strconv"
|
2015-04-03 20:58:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2016-09-12 07:34:19 +00:00
|
|
|
eventstestutils "github.com/docker/docker/daemon/events/testutils"
|
2023-08-26 11:48:49 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-04-03 20:58:56 +00:00
|
|
|
)
|
|
|
|
|
2023-08-26 12:03:14 +00:00
|
|
|
// validateLegacyFields validates that the legacy "Status", "ID", and "From"
|
|
|
|
// fields are set to the same value as their "current" (non-legacy) fields.
|
|
|
|
//
|
|
|
|
// These fields were deprecated since v1.10 (https://github.com/moby/moby/pull/18888).
|
|
|
|
//
|
|
|
|
// TODO remove this once we removed the deprecated `ID`, `Status`, and `From` fields.
|
|
|
|
func validateLegacyFields(t *testing.T, msg events.Message) {
|
|
|
|
t.Helper()
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(msg.Status, string(msg.Action)), "Legacy Status field does not match Action")
|
2023-08-26 12:03:14 +00:00
|
|
|
assert.Check(t, is.Equal(msg.ID, msg.Actor.ID), "Legacy ID field does not match Actor.ID")
|
|
|
|
assert.Check(t, is.Equal(msg.From, msg.Actor.Attributes["image"]), "Legacy From field does not match Actor.Attributes.image")
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:58:56 +00:00
|
|
|
func TestEventsLog(t *testing.T) {
|
|
|
|
e := New()
|
2015-10-12 18:54:46 +00:00
|
|
|
_, l1, _ := e.Subscribe()
|
|
|
|
_, l2, _ := e.Subscribe()
|
2015-04-03 20:58:56 +00:00
|
|
|
defer e.Evict(l1)
|
|
|
|
defer e.Evict(l2)
|
2023-08-26 11:48:49 +00:00
|
|
|
subscriberCount := e.SubscribersCount()
|
|
|
|
assert.Check(t, is.Equal(subscriberCount, 2))
|
|
|
|
|
|
|
|
e.Log("test", events.ContainerEventType, events.Actor{
|
2015-12-21 22:55:23 +00:00
|
|
|
ID: "cont",
|
|
|
|
Attributes: map[string]string{"image": "image"},
|
2023-08-26 11:48:49 +00:00
|
|
|
})
|
2015-04-03 20:58:56 +00:00
|
|
|
select {
|
|
|
|
case msg := <-l1:
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Check(t, is.Len(e.events, 1))
|
|
|
|
|
2015-12-21 22:55:23 +00:00
|
|
|
jmsg, ok := msg.(events.Message)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Assert(t, ok, "unexpected type: %T", msg)
|
2023-08-26 12:03:14 +00:00
|
|
|
validateLegacyFields(t, jmsg)
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(jmsg.Action, events.Action("test")))
|
2023-08-26 12:03:14 +00:00
|
|
|
assert.Check(t, is.Equal(jmsg.Actor.ID, "cont"))
|
|
|
|
assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image"))
|
2015-04-03 20:58:56 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Timeout waiting for broadcasted message")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case msg := <-l2:
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Check(t, is.Len(e.events, 1))
|
|
|
|
|
2015-12-21 22:55:23 +00:00
|
|
|
jmsg, ok := msg.(events.Message)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Assert(t, ok, "unexpected type: %T", msg)
|
2023-08-26 12:03:14 +00:00
|
|
|
validateLegacyFields(t, jmsg)
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(jmsg.Action, events.Action("test")))
|
2023-08-26 12:03:14 +00:00
|
|
|
assert.Check(t, is.Equal(jmsg.Actor.ID, "cont"))
|
|
|
|
assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image"))
|
2015-04-03 20:58:56 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Timeout waiting for broadcasted message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventsLogTimeout(t *testing.T) {
|
|
|
|
e := New()
|
2015-10-12 18:54:46 +00:00
|
|
|
_, l, _ := e.Subscribe()
|
2015-04-03 20:58:56 +00:00
|
|
|
defer e.Evict(l)
|
|
|
|
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
2023-08-26 11:48:49 +00:00
|
|
|
e.Log("test", events.ImageEventType, events.Actor{
|
2015-12-21 22:55:23 +00:00
|
|
|
ID: "image",
|
2023-08-26 11:48:49 +00:00
|
|
|
})
|
2015-04-03 20:58:56 +00:00
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout publishing message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:53:13 +00:00
|
|
|
func TestLogEvents(t *testing.T) {
|
2015-04-03 20:58:56 +00:00
|
|
|
e := New()
|
2015-08-19 18:51:14 +00:00
|
|
|
|
2015-09-15 19:53:13 +00:00
|
|
|
for i := 0; i < eventsLimit+16; i++ {
|
2023-08-26 11:48:49 +00:00
|
|
|
num := strconv.Itoa(i)
|
2023-08-26 13:24:46 +00:00
|
|
|
e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
|
2023-08-26 11:48:49 +00:00
|
|
|
ID: "cont_" + num,
|
|
|
|
Attributes: map[string]string{"image": "image_" + num},
|
|
|
|
})
|
2015-09-15 19:53:13 +00:00
|
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-10-12 18:54:46 +00:00
|
|
|
current, l, _ := e.Subscribe()
|
2015-09-15 19:53:13 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2023-08-26 11:48:49 +00:00
|
|
|
num := strconv.Itoa(i + eventsLimit + 16)
|
2023-08-26 13:24:46 +00:00
|
|
|
e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
|
2023-08-26 11:48:49 +00:00
|
|
|
ID: "cont_" + num,
|
|
|
|
Attributes: map[string]string{"image": "image_" + num},
|
|
|
|
})
|
2015-04-03 20:58:56 +00:00
|
|
|
}
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Assert(t, is.Len(e.events, eventsLimit))
|
2015-04-03 20:58:56 +00:00
|
|
|
|
2015-12-21 22:55:23 +00:00
|
|
|
var msgs []events.Message
|
2015-04-03 20:58:56 +00:00
|
|
|
for len(msgs) < 10 {
|
2015-09-15 19:53:13 +00:00
|
|
|
m := <-l
|
2015-12-21 22:55:23 +00:00
|
|
|
jm, ok := (m).(events.Message)
|
2015-09-15 19:53:13 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected type %T", m)
|
2015-04-03 20:58:56 +00:00
|
|
|
}
|
2015-09-15 19:53:13 +00:00
|
|
|
msgs = append(msgs, jm)
|
|
|
|
}
|
2017-12-20 11:49:51 +00:00
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.Assert(t, is.Len(current, eventsLimit))
|
|
|
|
|
|
|
|
first := current[0]
|
2023-08-26 12:03:14 +00:00
|
|
|
validateLegacyFields(t, first)
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(first.Action, events.Action("action_16")))
|
2017-12-20 11:49:51 +00:00
|
|
|
|
2015-09-15 19:53:13 +00:00
|
|
|
last := current[len(current)-1]
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(last.Action, events.Action("action_271")))
|
2015-09-15 19:53:13 +00:00
|
|
|
|
|
|
|
firstC := msgs[0]
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(firstC.Action, events.Action("action_272")))
|
2023-08-26 11:48:49 +00:00
|
|
|
|
2015-09-15 19:53:13 +00:00
|
|
|
lastC := msgs[len(msgs)-1]
|
2023-08-26 13:24:46 +00:00
|
|
|
assert.Check(t, is.Equal(lastC.Action, events.Action("action_281")))
|
2015-04-03 20:58:56 +00:00
|
|
|
}
|
2016-03-08 00:02:35 +00:00
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
// Regression-test for https://github.com/moby/moby/issues/20999
|
|
|
|
//
|
2016-03-08 00:02:35 +00:00
|
|
|
// Fixtures:
|
|
|
|
//
|
2023-08-26 11:48:49 +00:00
|
|
|
// 2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
|
|
|
|
// 2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)
|
|
|
|
// 2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
|
2016-03-08 00:02:35 +00:00
|
|
|
func TestLoadBufferedEvents(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
s, sNano, err := timetypes.ParseTimestamps(f, -1)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-03-08 00:02:35 +00:00
|
|
|
|
|
|
|
m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-03-08 00:02:35 +00:00
|
|
|
m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-03-08 00:02:35 +00:00
|
|
|
m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-03-08 00:02:35 +00:00
|
|
|
|
2023-08-27 20:23:29 +00:00
|
|
|
evts := &Events{
|
2016-04-11 18:52:34 +00:00
|
|
|
events: []events.Message{*m1, *m2, *m3},
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Unix(s, sNano)
|
|
|
|
until := time.Time{}
|
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
messages := evts.loadBufferedEvents(since, until, nil)
|
|
|
|
assert.Assert(t, is.Len(messages, 1))
|
2016-04-11 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBufferedEventsOnlyFromPast(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.090000000+02:00", now)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
s, sNano, err := timetypes.ParseTimestamps(f, 0)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-04-11 18:52:34 +00:00
|
|
|
|
|
|
|
f, err = timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
u, uNano, err := timetypes.ParseTimestamps(f, 0)
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-04-11 18:52:34 +00:00
|
|
|
|
|
|
|
m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-03-08 00:02:35 +00:00
|
|
|
|
2023-08-27 20:23:29 +00:00
|
|
|
evts := &Events{
|
2016-04-11 18:52:34 +00:00
|
|
|
events: []events.Message{*m1, *m2, *m3},
|
2016-03-08 00:02:35 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
since := time.Unix(s, sNano)
|
|
|
|
until := time.Unix(u, uNano)
|
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
messages := evts.loadBufferedEvents(since, until, nil)
|
|
|
|
assert.Assert(t, is.Len(messages, 1))
|
2023-08-28 20:06:31 +00:00
|
|
|
assert.Check(t, is.Equal(messages[0].Type, events.NetworkEventType))
|
2016-04-11 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
// Regression-test for https://github.com/moby/moby/issues/13753
|
2017-05-21 23:24:07 +00:00
|
|
|
func TestIgnoreBufferedWhenNoTimes(t *testing.T) {
|
2016-04-11 18:52:34 +00:00
|
|
|
m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2016-04-11 18:52:34 +00:00
|
|
|
m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
|
2023-08-26 11:48:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-04-11 18:52:34 +00:00
|
|
|
|
2023-08-27 20:23:29 +00:00
|
|
|
evts := &Events{
|
2016-04-11 18:52:34 +00:00
|
|
|
events: []events.Message{*m1, *m2, *m3},
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Time{}
|
|
|
|
until := time.Time{}
|
|
|
|
|
2023-08-26 11:48:49 +00:00
|
|
|
messages := evts.loadBufferedEvents(since, until, nil)
|
|
|
|
assert.Assert(t, is.Len(messages, 0))
|
2016-03-08 00:02:35 +00:00
|
|
|
}
|