From af0e5660213b2d508f113de9ffce836537ab417c Mon Sep 17 00:00:00 2001 From: Ryan Fitzpatrick Date: Fri, 23 Jun 2023 19:30:24 +0000 Subject: [PATCH] testutils helper updates --- tests/testutils/container.go | 53 +++++++++++++++++++++++++----------- tests/testutils/testcase.go | 18 +++++++++++- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/tests/testutils/container.go b/tests/testutils/container.go index d652aa343d..311f856e2a 100644 --- a/tests/testutils/container.go +++ b/tests/testutils/container.go @@ -58,6 +58,7 @@ type Container struct { Binds []string WaitingFor []wait.Strategy Mounts []testcontainers.ContainerMount + HostConfigModifiers []func(*dockerContainer.HostConfig) Privileged bool } @@ -67,7 +68,8 @@ var _ testcontainers.Container = (*Container)(nil) // implements a testcontainers.Container. func NewContainer() Container { return Container{ - Env: map[string]string{}, + Env: map[string]string{}, + HostConfigModifiers: []func(*dockerContainer.HostConfig){}, } } @@ -111,6 +113,15 @@ func (container Container) WithStartupTimeout(startupTimeout time.Duration) Cont return container } +func (container Container) WithHostConfigModifier(cm func(*dockerContainer.HostConfig)) Container { + // copy current modifiers since we are in a builder + var hcm []func(*dockerContainer.HostConfig) + hcm = append(hcm, container.HostConfigModifiers...) + hcm = append(hcm, cm) + container.HostConfigModifiers = hcm + return container +} + func copyMap(m map[string]string) map[string]string { returned := map[string]string{} for k, v := range m { @@ -222,22 +233,32 @@ func (container Container) Build() *Container { startupTimeout = *container.startupTimeout } + var hostConfigModifier func(config *dockerContainer.HostConfig) + if len(container.HostConfigModifiers) != 0 { + hostConfigModifier = func(config *dockerContainer.HostConfig) { + for _, cm := range container.HostConfigModifiers { + cm(config) + } + } + } + container.req = &testcontainers.ContainerRequest{ - Binds: container.Binds, - User: container.User, - Image: container.Image, - FromDockerfile: container.Dockerfile, - Cmd: container.Cmd, - Entrypoint: container.Entrypoint, - Env: container.Env, - ExposedPorts: container.ExposedPorts, - Name: container.ContainerName, - Networks: container.ContainerNetworks, - Mounts: container.Mounts, - NetworkMode: networkMode, - Labels: container.Labels, - Privileged: container.Privileged, - WaitingFor: wait.ForAll(container.WaitingFor...).WithDeadline(startupTimeout), + Binds: container.Binds, + User: container.User, + Image: container.Image, + FromDockerfile: container.Dockerfile, + Cmd: container.Cmd, + Entrypoint: container.Entrypoint, + Env: container.Env, + ExposedPorts: container.ExposedPorts, + Name: container.ContainerName, + Networks: container.ContainerNetworks, + Mounts: container.Mounts, + NetworkMode: networkMode, + Labels: container.Labels, + Privileged: container.Privileged, + HostConfigModifier: hostConfigModifier, + WaitingFor: wait.ForAll(container.WaitingFor...).WithDeadline(startupTimeout), } return &container } diff --git a/tests/testutils/testcase.go b/tests/testutils/testcase.go index d7fbc4c5b6..4370dd4286 100644 --- a/tests/testutils/testcase.go +++ b/tests/testutils/testcase.go @@ -25,6 +25,8 @@ import ( "testing" "time" + "github.com/docker/docker/api/types" + docker "github.com/docker/docker/client" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -39,6 +41,7 @@ type TestOption int const ( OTLPReceiverSinkAllInterfaces TestOption = iota + OTLPReceiverSinkBindToBridgeGateway ) type CollectorBuilder func(Collector) Collector @@ -89,8 +92,21 @@ func NewTestcase(t testing.TB, opts ...TestOption) *Testcase { func (t *Testcase) setOTLPEndpoint(opts []TestOption) { otlpPort := GetAvailablePort(t) otlpHost := "localhost" - if HasTestOption(OTLPReceiverSinkAllInterfaces, opts) { + switch { + case HasTestOption(OTLPReceiverSinkAllInterfaces, opts): otlpHost = "0.0.0.0" + case HasTestOption(OTLPReceiverSinkBindToBridgeGateway, opts): + client, err := docker.NewClientWithOpts(docker.FromEnv) + require.NoError(t, err) + client.NegotiateAPIVersion(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + network, err := client.NetworkInspect(ctx, "bridge", types.NetworkInspectOptions{}) + require.NoError(t, err) + for _, ipam := range network.IPAM.Config { + otlpHost = ipam.Gateway + } + require.NotEmpty(t, otlpHost, "no bridge network gateway detected. Host IP is inaccessible.") } t.OTLPEndpoint = fmt.Sprintf("%s:%d", otlpHost, otlpPort) t.OTLPEndpointForCollector = t.OTLPEndpoint