From 826b23c2cad2472f9783228ebff4e97db9ead36e Mon Sep 17 00:00:00 2001 From: Ryan Min Date: Thu, 9 Jan 2025 15:40:53 -0500 Subject: [PATCH] fix integration tests --- .../dockerstatsreceiver/integration_test.go | 28 +++++++++++++++---- receiver/dockerstatsreceiver/logs_receiver.go | 11 +++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/receiver/dockerstatsreceiver/integration_test.go b/receiver/dockerstatsreceiver/integration_test.go index 789f1f242851..9e3b88aa1ac9 100644 --- a/receiver/dockerstatsreceiver/integration_test.go +++ b/receiver/dockerstatsreceiver/integration_test.go @@ -219,8 +219,8 @@ func TestContainerLifecycleEventsIntegration(t *testing.T) { // no events should be received before container starts assert.Never(t, func() bool { - return len(consumer.AllLogs()) > 0 - }, 5*time.Second, 1*time.Second, "received unexpected events") + return consumer.LogRecordCount() > 0 + }, 5*time.Second, 1*time.Second, "received %d unexpected events, expected 0", consumer.LogRecordCount()) nginxContainer := createNginxContainer(ctx, t) nginxID := nginxContainer.GetContainerID() @@ -250,13 +250,21 @@ func TestContainerLifecycleEventsIntegration(t *testing.T) { require.NoError(t, nginxContainer.Terminate(ctx)) assert.Eventuallyf(t, func() bool { - return hasDockerEvents(consumer.AllLogs(), redisID, []string{ + return hasDockerEvents(consumer.AllLogs(), nginxID, []string{ "docker.container.die", "docker.container.stop", }) }, 5*time.Second, 1*time.Second, "failed to receive container stop/die events") + consumer.Reset() require.NoError(t, redisContainer.Terminate(ctx)) + + assert.Eventuallyf(t, func() bool { + return hasDockerEvents(consumer.AllLogs(), redisID, []string{ + "docker.container.die", + "docker.container.stop", + }) + }, 5*time.Second, 1*time.Second, "failed to receive container stop/die events") assert.NoError(t, recv.Shutdown(ctx)) } @@ -268,7 +276,7 @@ func TestFilteredContainerEventsIntegration(t *testing.T) { f, config := factory() // Only receive events from redis containers config.Logs.Filters = map[string][]string{ - "image": {"*redis*"}, + "image": {"docker.io/library/redis:latest"}, } consumer := new(consumertest.LogsSink) @@ -282,8 +290,8 @@ func TestFilteredContainerEventsIntegration(t *testing.T) { nginxContainer := createNginxContainer(ctx, t) assert.Never(t, func() bool { - return len(consumer.AllLogs()) > 0 - }, 5*time.Second, 1*time.Second, "received events for excluded container") + return consumer.LogRecordCount() > 0 + }, 5*time.Second, 1*time.Second, "received %d unexpected events, expected 0", consumer.LogRecordCount()) redisContainer := createRedisContainer(ctx, t) redisID := redisContainer.GetContainerID() @@ -295,8 +303,16 @@ func TestFilteredContainerEventsIntegration(t *testing.T) { }) }, 5*time.Second, 1*time.Second, "failed to receive redis container events") + consumer.Reset() require.NoError(t, nginxContainer.Terminate(ctx)) require.NoError(t, redisContainer.Terminate(ctx)) + assert.Eventuallyf(t, func() bool { + return hasDockerEvents(consumer.AllLogs(), redisID, []string{ + "docker.container.die", + "docker.container.stop", + }) + }, 5*time.Second, 1*time.Second, "failed to receive container stop/die events") + assert.NoError(t, recv.Shutdown(ctx)) } diff --git a/receiver/dockerstatsreceiver/logs_receiver.go b/receiver/dockerstatsreceiver/logs_receiver.go index ded4ef8df108..0b0a61d1d3ec 100644 --- a/receiver/dockerstatsreceiver/logs_receiver.go +++ b/receiver/dockerstatsreceiver/logs_receiver.go @@ -101,10 +101,13 @@ func newDockerEventPoller( } func (d *dockerEventPoller) Start(ctx context.Context) { - filterArgs := filters.NewArgs() - for k, v := range d.config.Logs.Filters { - for _, elem := range v { - filterArgs.Add(k, elem) + filterArgs := filters.Args{} + if len(d.config.Logs.Filters) > 0 { + filterArgs = filters.NewArgs() + for k, v := range d.config.Logs.Filters { + for _, elem := range v { + filterArgs.Add(k, elem) + } } } for {