Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: enable logging with testcontainers #11211

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}