test: enable logging with testcontainers (#11211)

This commit is contained in:
Joshua Powers 2022-06-01 07:42:46 -06:00 committed by GitHub
parent b1918c1317
commit 0147257358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -14,7 +14,7 @@ import (
const servicePort = "3000" const servicePort = "3000"
func launchTestServer(t *testing.T) testutil.Container { func launchTestServer(t *testing.T) *testutil.Container {
container := testutil.Container{ container := testutil.Container{
Image: "aerospike:ce-6.0.0.1", Image: "aerospike:ce-6.0.0.1",
ExposedPorts: []string{servicePort}, ExposedPorts: []string{servicePort},
@ -23,7 +23,7 @@ func launchTestServer(t *testing.T) testutil.Container {
err := container.Start() err := container.Start()
require.NoError(t, err, "failed to start container") require.NoError(t, err, "failed to start container")
return container return &container
} }
func TestAerospikeStatisticsIntegration(t *testing.T) { func TestAerospikeStatisticsIntegration(t *testing.T) {

View File

@ -18,7 +18,7 @@ import (
const servicePort = "5432" const servicePort = "5432"
func createTestContainer(t *testing.T) testutil.Container { func createTestContainer(t *testing.T) *testutil.Container {
container := testutil.Container{ container := testutil.Container{
Image: "crate", Image: "crate",
ExposedPorts: []string{servicePort}, ExposedPorts: []string{servicePort},
@ -34,7 +34,7 @@ func createTestContainer(t *testing.T) testutil.Container {
err := container.Start() err := container.Start()
require.NoError(t, err, "failed to start container") require.NoError(t, err, "failed to start container")
return container return &container
} }
func TestConnectAndWriteIntegration(t *testing.T) { func TestConnectAndWriteIntegration(t *testing.T) {

View File

@ -13,6 +13,14 @@ import (
"github.com/testcontainers/testcontainers-go/wait" "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 { type Container struct {
BindMounts map[string]string BindMounts map[string]string
Entrypoint []string Entrypoint []string
@ -25,6 +33,7 @@ type Container struct {
Address string Address string
Ports map[string]string Ports map[string]string
Logs TestLogConsumer
container testcontainers.Container container testcontainers.Container
ctx context.Context ctx context.Context
@ -53,6 +62,15 @@ func (c *Container) Start() error {
} }
c.container = container 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" c.Address = "localhost"
err = c.LookupMappedPorts() err = c.LookupMappedPorts()
@ -96,11 +114,24 @@ func (c *Container) LookupMappedPorts() error {
return nil 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 { func (c *Container) Terminate() error {
err := c.container.Terminate(c.ctx) err := c.container.Terminate(c.ctx)
if err != nil { 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
} }