diff --git a/plugins/inputs/aerospike/aerospike_test.go b/plugins/inputs/aerospike/aerospike_test.go index e0e6be444ce80..d80860a977dbf 100644 --- a/plugins/inputs/aerospike/aerospike_test.go +++ b/plugins/inputs/aerospike/aerospike_test.go @@ -14,7 +14,7 @@ import ( const servicePort = "3000" -func launchTestServer(t *testing.T) testutil.Container { +func launchTestServer(t *testing.T) *testutil.Container { container := testutil.Container{ Image: "aerospike:ce-6.0.0.1", ExposedPorts: []string{servicePort}, @@ -23,7 +23,7 @@ func launchTestServer(t *testing.T) testutil.Container { err := container.Start() require.NoError(t, err, "failed to start container") - return container + return &container } func TestAerospikeStatisticsIntegration(t *testing.T) { diff --git a/plugins/outputs/cratedb/cratedb_test.go b/plugins/outputs/cratedb/cratedb_test.go index 149a8722b90d8..8fe4d7ff715d0 100644 --- a/plugins/outputs/cratedb/cratedb_test.go +++ b/plugins/outputs/cratedb/cratedb_test.go @@ -18,7 +18,7 @@ import ( const servicePort = "5432" -func createTestContainer(t *testing.T) testutil.Container { +func createTestContainer(t *testing.T) *testutil.Container { container := testutil.Container{ Image: "crate", ExposedPorts: []string{servicePort}, @@ -34,7 +34,7 @@ func createTestContainer(t *testing.T) testutil.Container { err := container.Start() require.NoError(t, err, "failed to start container") - return container + return &container } func TestConnectAndWriteIntegration(t *testing.T) { diff --git a/testutil/container.go b/testutil/container.go index 8c6b7cccce000..936b66a44fc6e 100644 --- a/testutil/container.go +++ b/testutil/container.go @@ -13,6 +13,14 @@ import ( "github.com/testcontainers/testcontainers-go/wait" ) +type TestLogConsumer struct { + Msgs []string +} + +func (g *TestLogConsumer) Accept(l testcontainers.Log) { + g.Msgs = append(g.Msgs, string(l.Content)) +} + type Container struct { BindMounts map[string]string Entrypoint []string @@ -25,6 +33,7 @@ type Container struct { Address string Ports map[string]string + Logs TestLogConsumer container testcontainers.Container ctx context.Context @@ -53,6 +62,15 @@ func (c *Container) Start() error { } c.container = container + c.Logs = TestLogConsumer{ + Msgs: []string{}, + } + c.container.FollowOutput(&c.Logs) + err = c.container.StartLogProducer(c.ctx) + if err != nil { + return fmt.Errorf("log producer failed: %s", err) + } + c.Address = "localhost" err = c.LookupMappedPorts() @@ -96,11 +114,24 @@ func (c *Container) LookupMappedPorts() error { return nil } +func (c *Container) PrintLogs() { + fmt.Println("--- Container Logs Start ---") + for _, msg := range c.Logs.Msgs { + fmt.Print(msg) + } + fmt.Println("--- Container Logs End ---") +} + func (c *Container) Terminate() error { err := c.container.Terminate(c.ctx) if err != nil { - return fmt.Errorf("failed to terminate the container: %s", err) + fmt.Printf("failed to terminate the container: %s", err) } - return nil + // this needs to happen after the container is terminated otherwise there + // is a huge time penalty on the order of 50% increase in test time + _ = c.container.StopLogProducer() + c.PrintLogs() + + return err }