From 62ec14d9ecd55c6e0eadaf60052b84456d93248d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Aug 2023 14:03:14 +0200 Subject: [PATCH] daemon/events: verify non-deprecated fields Some tests were testing the deprecated fields, instead of their non-deprecated alternatives. This patch adds a utility to verify that they match, and rewrites the tests to check the non-deprecated fields instead. Signed-off-by: Sebastiaan van Stijn --- daemon/events/events_test.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/daemon/events/events_test.go b/daemon/events/events_test.go index 54f2eac8c7..7be0fea978 100644 --- a/daemon/events/events_test.go +++ b/daemon/events/events_test.go @@ -12,6 +12,19 @@ import ( is "gotest.tools/v3/assert/cmp" ) +// 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() + assert.Check(t, is.Equal(msg.Status, msg.Action), "Legacy Status field does not match Action") + 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") +} + func TestEventsLog(t *testing.T) { e := New() _, l1, _ := e.Subscribe() @@ -31,9 +44,10 @@ func TestEventsLog(t *testing.T) { jmsg, ok := msg.(events.Message) assert.Assert(t, ok, "unexpected type: %T", msg) - assert.Check(t, is.Equal(jmsg.Status, "test")) - assert.Check(t, is.Equal(jmsg.ID, "cont")) - assert.Check(t, is.Equal(jmsg.From, "image")) + validateLegacyFields(t, jmsg) + assert.Check(t, is.Equal(jmsg.Action, "test")) + assert.Check(t, is.Equal(jmsg.Actor.ID, "cont")) + assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image")) case <-time.After(1 * time.Second): t.Fatal("Timeout waiting for broadcasted message") } @@ -43,9 +57,10 @@ func TestEventsLog(t *testing.T) { jmsg, ok := msg.(events.Message) assert.Assert(t, ok, "unexpected type: %T", msg) - assert.Check(t, is.Equal(jmsg.Status, "test")) - assert.Check(t, is.Equal(jmsg.ID, "cont")) - assert.Check(t, is.Equal(jmsg.From, "image")) + validateLegacyFields(t, jmsg) + assert.Check(t, is.Equal(jmsg.Action, "test")) + assert.Check(t, is.Equal(jmsg.Actor.ID, "cont")) + assert.Check(t, is.Equal(jmsg.Actor.Attributes["image"], "image")) case <-time.After(1 * time.Second): t.Fatal("Timeout waiting for broadcasted message") } @@ -105,9 +120,8 @@ func TestLogEvents(t *testing.T) { assert.Assert(t, is.Len(current, eventsLimit)) first := current[0] + validateLegacyFields(t, first) assert.Check(t, is.Equal(first.Action, "action_16")) - // TODO remove this once we removed the deprecated `ID`, `Status`, and `From` fields - assert.Check(t, is.Equal(first.Action, first.Status), "Action does not match Status") last := current[len(current)-1] assert.Check(t, is.Equal(last.Action, "action_271"))