Skip to content

Commit

Permalink
chore: allow to override the bind ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Sep 23, 2024
1 parent 7386e13 commit 0e1ad98
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 45 deletions.
58 changes: 28 additions & 30 deletions testhelper/docker/resource/internal/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,53 @@ import (
"runtime"

"github.com/ory/dockertest/v3/docker"
"github.com/samber/lo"
)

const (
BindHostIP = "127.0.0.1"
BindInternalHost = "host.docker.internal"
BindAllIP = "0.0.0.0"
)

func CreatePortBindingsForAllInterfaces(exposedPorts []string) map[docker.Port][]docker.PortBinding {
portBindings := make(map[docker.Port][]docker.PortBinding)
bindings := []docker.PortBinding{
{
HostIP: BindAllIP,
HostPort: "0",
},
}

for _, p := range exposedPorts {
portBindings[docker.Port(p)] = bindings
}

return portBindings
}

// IPv4PortBindings returns the port bindings for the given exposed ports forcing ipv4 address.
func IPv4PortBindings(exposedPorts []string) map[docker.Port][]docker.PortBinding {
func IPv4PortBindings(exposedPorts []string, opts ...IPv4PortBindingsOpt) map[docker.Port][]docker.PortBinding {
portBindings := make(map[docker.Port][]docker.PortBinding)

bindings := []docker.PortBinding{
{
HostIP: BindHostIP,
HostPort: "0",
},
c := &ipv4PortBindingsConfig{
ips: []string{BindHostIP},
}

if runtime.GOOS == "linux" {
bindings = append(bindings, docker.PortBinding{
HostIP: BindInternalHost,
HostPort: "0",
})
c.ips = append(c.ips, BindInternalHost)
}

for _, opt := range opts {
opt(c)
}
bindings := lo.Map(c.ips, func(ip string, _ int) docker.PortBinding {
return docker.PortBinding{
HostIP: ip,
HostPort: "0",
}
})
for _, exposedPort := range exposedPorts {
portBindings[docker.Port(exposedPort)+"/tcp"] = bindings
}

return portBindings
}

type IPv4PortBindingsOpt func(*ipv4PortBindingsConfig)

type ipv4PortBindingsConfig struct {
ips []string
}

func WithBindIP(ip string) IPv4PortBindingsOpt {
return func(c *ipv4PortBindingsConfig) {
if ip != "" {
c.ips = []string{ip}
}
}
}

func DefaultHostConfig(hc *docker.HostConfig) {
hc.PublishAllPorts = false
}
8 changes: 3 additions & 5 deletions testhelper/docker/resource/minio/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package minio

import (
"github.com/ory/dockertest/v3/docker"

"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)

type Opt func(*Config)
Expand All @@ -26,15 +24,15 @@ func WithOptions(options ...string) Opt {
}
}

func WithNetworkBindingConfig(cfg resource.NetworkBindingConfig) Opt {
func WithBindIP(ip string) Opt {
return func(c *Config) {
c.NetworkBindingConfig = cfg
c.BindIP = ip
}
}

type Config struct {
resource.NetworkBindingConfig
Tag string
Network *docker.Network
Options []string
BindIP string
}
7 changes: 1 addition & 6 deletions testhelper/docker/resource/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*R
networkID = c.Network.ID
}

portBindings := internal.IPv4PortBindings([]string{"9000"})
if c.BindToAllInterfaces {
portBindings = internal.CreatePortBindingsForAllInterfaces([]string{"9000"})
}

minioContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "minio/minio",
Tag: c.Tag,
Expand All @@ -76,7 +71,7 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*R
fmt.Sprintf("MINIO_SITE_REGION=%s", region),
"MINIO_API_SELECT_PARQUET=on",
}, c.Options...),
PortBindings: portBindings,
PortBindings: internal.IPv4PortBindings([]string{"9000"}, internal.WithBindIP(c.BindIP)),
}, internal.DefaultHostConfig)
if err != nil {
return nil, fmt.Errorf("could not start resource: %s", err)
Expand Down
4 changes: 0 additions & 4 deletions testhelper/docker/resource/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ type NOPLogger struct{}

// Log for the NOP Logger does nothing.
func (*NOPLogger) Log(...interface{}) {}

type NetworkBindingConfig struct {
BindToAllInterfaces bool
}

0 comments on commit 0e1ad98

Please sign in to comment.