Skip to content

Commit

Permalink
testutils helper updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rmfitzpatrick committed Jun 27, 2023
1 parent 097c034 commit af0e566
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
53 changes: 37 additions & 16 deletions tests/testutils/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Container struct {
Binds []string
WaitingFor []wait.Strategy
Mounts []testcontainers.ContainerMount
HostConfigModifiers []func(*dockerContainer.HostConfig)
Privileged bool
}

Expand All @@ -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){},
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion tests/testutils/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -39,6 +41,7 @@ type TestOption int

const (
OTLPReceiverSinkAllInterfaces TestOption = iota
OTLPReceiverSinkBindToBridgeGateway
)

type CollectorBuilder func(Collector) Collector
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit af0e566

Please sign in to comment.