From c4ea467351a26110a0688d8543cd017104cd1bf5 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Fri, 22 Sep 2023 13:11:19 +0200 Subject: [PATCH] nettest: small changes and cleanup --- .../protocol/nettest/redis/iptables_test.go | 21 ++++++++++++++----- pkg/agent/protocol/nettest/tlog/tlog.go | 11 ++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pkg/agent/protocol/nettest/redis/iptables_test.go b/pkg/agent/protocol/nettest/redis/iptables_test.go index aac29cf3..8cb68623 100644 --- a/pkg/agent/protocol/nettest/redis/iptables_test.go +++ b/pkg/agent/protocol/nettest/redis/iptables_test.go @@ -10,6 +10,8 @@ import ( "github.com/docker/docker/api/types/container" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" + + "github.com/grafana/xk6-disruptor/pkg/agent/protocol/nettest/tlog" ) const iptablesRule = "INPUT -p tcp --dport 6379 -j REJECT --reject-with tcp-reset" @@ -26,7 +28,6 @@ func Test_Redis(t *testing.T) { redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ProviderType: testcontainers.ProviderDocker, ContainerRequest: testcontainers.ContainerRequest{ - Networks: []string{}, Image: "redis", ExposedPorts: []string{"6379/tcp"}, WaitingFor: wait.ForExposedPort(), @@ -79,11 +80,21 @@ func Test_Redis(t *testing.T) { t.Fatalf("failed to create agent container %v", err) } - t.Cleanup(func() { - _ = redisGo.Terminate(ctx) - }) + // TODO: Calling terminate with a log attached makes the test hang. + // See: https://github.com/testcontainers/testcontainers-go/issues/1669 + // t.Cleanup(func() { + // _ = redisGo.Terminate(ctx) + // }) - // TODO:Follow container under test logs. Currently doing so hangs out the test forever. + redisGo.FollowOutput(tlog.Mirror{T: t, Name: "redis-go"}) + err = redisGo.StartLogProducer(ctx) + if err != nil { + t.Fatal(err) + } + // TODO: See above. + // t.Cleanup(func() { + // redisGo.StopLogProducer() + // }) redisGoStatus, err := redisGo.State(ctx) if err != nil { diff --git a/pkg/agent/protocol/nettest/tlog/tlog.go b/pkg/agent/protocol/nettest/tlog/tlog.go index ff9d9aae..7ec06e0c 100644 --- a/pkg/agent/protocol/nettest/tlog/tlog.go +++ b/pkg/agent/protocol/nettest/tlog/tlog.go @@ -9,10 +9,17 @@ import ( // Mirror is a testcontainers log adapter that mirrors container output to testing.T.Log. type Mirror struct { - T *testing.T + T *testing.T + Name string } // Accept implements the testcontainers adapter interface by writing received output to the test logger. func (m Mirror) Accept(log testcontainers.Log) { - m.T.Logf("%s: %s", log.LogType, log.Content) + prefix := "" + if m.Name != "" { + prefix += m.Name + "/" + } + prefix += log.LogType + + m.T.Logf("%s: %s", prefix, log.Content) }