test: enable logging with testcontainers (#11211)
This commit is contained in:
parent
b1918c1317
commit
0147257358
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue