From c06bf9e82adafce37b278dd80b1f06eece7ee76b Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 27 Oct 2026 14:26:07 +0100 Subject: [PATCH] test(monitor): lock which containers the monitor follows The monitor ends with the application it follows: pin that its initial listing and its event subscription are scoped to the project's own containers (no one-offs, no hook runners without a config hash), and that a provider's relay, which never terminates, is not waited for, whether it is there at the start or created later. Part of #14081. Signed-off-by: Nicolas De Loof --- pkg/compose/monitor_test.go | 80 ++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/pkg/compose/monitor_test.go b/pkg/compose/monitor_test.go index 113204bc8..c5fdfd837 100644 --- a/pkg/compose/monitor_test.go +++ b/pkg/compose/monitor_test.go @@ -266,6 +266,14 @@ func monitorDieEvent(exitCode int) events.Message { // newMonitorTestFixture wires a monitor against a mocked API client, with the // goroutine-leak guard and the standard initial ContainerList expectation. func newMonitorTestFixture(t *testing.T) (*logstream.Monitor, *mocks.MockAPIClient) { + t.Helper() + return newMonitorTestFixtureListing(t, []container.Summary{testContainer("service1", "123", false)}, nil) +} + +// newMonitorTestFixtureListing is newMonitorTestFixture for given containers +// in the initial listing, whose request it records in listed when it is not +// nil. +func newMonitorTestFixtureListing(t *testing.T, initial []container.Summary, listed *client.ContainerListOptions) (*monitor, *mocks.MockAPIClient) { t.Helper() ignoreExisting := goleak.IgnoreCurrent() t.Cleanup(func() { @@ -277,7 +285,12 @@ func newMonitorTestFixture(t *testing.T) (*logstream.Monitor, *mocks.MockAPIClie expectNowAsDaemonTime(apiMock) apiMock.EXPECT().ContainerList(gomock.Any(), gomock.Any()). - Return(client.ContainerListResult{Items: []container.Summary{testContainer("service1", "123", false)}}, nil) + DoAndReturn(func(_ context.Context, opts client.ContainerListOptions) (client.ContainerListResult, error) { + if listed != nil { + *listed = opts + } + return client.ContainerListResult{Items: initial}, nil + }) m := logstream.NewMonitor(apiMock, strings.ToLower(testProject)) return m, apiMock @@ -954,3 +967,68 @@ func TestMonitorReadDaemonTimeOnce(t *testing.T) { assert.DeepEqual(t, log.since, []string{"981153306.789012345"}) } + +// TestMonitorFollowsTheContainersOfTheApplicationOnly pins which containers +// the monitor follows: the project's containers that are not one-off, and, in +// the initial listing, that carry a config hash, running or not. The hook +// runners of pre_start carry none: a runner's exit is not the end of the +// application. +func TestMonitorFollowsTheContainersOfTheApplicationOnly(t *testing.T) { + var listed client.ContainerListOptions + m, apiMock := newMonitorTestFixtureListing(t, []container.Summary{testContainer("service1", "123", false)}, &listed) + var subscribed client.EventsListOptions + messages := make(chan events.Message, 1) + messages <- monitorEvent(events.ActionDestroy) + apiMock.EXPECT().Events(gomock.Any(), gomock.Any()). + DoAndReturn(func(_ context.Context, opts client.EventsListOptions) client.EventsResult { + subscribed = opts + return client.EventsResult{Messages: messages, Err: make(chan error)} + }) + + _, err := runMonitor(t, m) + + assert.NilError(t, err) + project := labelFilter(api.ProjectLabel, strings.ToLower(testProject)) + assert.Assert(t, listed.All, "stopped containers are part of the application") + labels := listed.Filters["label"] + assert.Assert(t, labels[project], "listing filters: %v", listed.Filters) + assert.Assert(t, labels[oneOffFilter(false)], "one-off containers are not part of the application: %v", listed.Filters) + assert.Assert(t, labels[api.ConfigHashLabel], "a container without a config hash (a hook runner) is not: %v", listed.Filters) + labels = subscribed.Filters["label"] + assert.Assert(t, subscribed.Filters["type"]["container"], "events filters: %v", subscribed.Filters) + assert.Assert(t, labels[project], "events filters: %v", subscribed.Filters) + assert.Assert(t, labels[oneOffFilter(false)], "the events of one-off containers are not wanted: %v", subscribed.Filters) +} + +// TestMonitorDoesNotWaitForARelay pins that the relay container a provider +// service may leave in the project -- it never terminates on its own -- is not +// part of the application the monitor follows, whether it is there at the +// start or created later: the monitor ends with the application's own +// containers, instead of waiting forever for the relay. +func TestMonitorDoesNotWaitForARelay(t *testing.T) { + relayLabels := func() map[string]string { + labels := containerLabels("provider", false) + labels[api.RelayLabel] = "relay-identity" + return labels + } + + t.Run("already there at the start", func(t *testing.T) { + relay := container.Summary{ID: "relay", Labels: relayLabels()} + m, apiMock := newMonitorTestFixtureListing(t, []container.Summary{testContainer("service1", "123", false), relay}, nil) + expectEvents(apiMock, monitorEvent(events.ActionDestroy)) + + _, err := runMonitor(t, m) + + assert.NilError(t, err) + }) + + t.Run("created while the application runs", func(t *testing.T) { + m, apiMock := newMonitorTestFixture(t) + relayCreated := containerMessage(events.ActionCreate, "relay", "testproject-provider-1", "provider", map[string]string{api.RelayLabel: "relay-identity"}) + expectEvents(apiMock, relayCreated, monitorEvent(events.ActionDestroy)) + + _, err := runMonitor(t, m) + + assert.NilError(t, err) + }) +}