Skip to content

Commit

Permalink
test: enable logging with testcontainers (#11211)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Jun 1, 2022
1 parent b1918c1 commit 0147257
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions plugins/inputs/aerospike/aerospike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/outputs/cratedb/cratedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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) {
Expand Down
35 changes: 33 additions & 2 deletions testutil/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +33,7 @@ type Container struct {

Address string
Ports map[string]string
Logs TestLogConsumer

container testcontainers.Container
ctx context.Context
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

0 comments on commit 0147257

Please sign in to comment.