Skip to content

Commit

Permalink
chore: allow overriding the bind ip address in docker containers (#647)
Browse files Browse the repository at this point in the history
Co-authored-by: Aris Tzoumas <atzoumas@rudderstack.com>
  • Loading branch information
debanjan97 and atzoum authored Sep 23, 2024
1 parent 1a4a26b commit 6bb5880
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
40 changes: 28 additions & 12 deletions testhelper/docker/resource/internal/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime"

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

const (
Expand All @@ -12,29 +13,44 @@ const (
)

// 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
}
7 changes: 7 additions & 0 deletions testhelper/docker/resource/minio/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ func WithOptions(options ...string) Opt {
}
}

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

type Config struct {
Tag string
Network *docker.Network
Options []string
BindIP string
}
4 changes: 2 additions & 2 deletions testhelper/docker/resource/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,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: internal.IPv4PortBindings([]string{"9000"}),
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 All @@ -82,7 +82,7 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*R
}
})

endpoint := fmt.Sprintf("localhost:%s", minioContainer.GetPort("9000/tcp"))
endpoint := minioContainer.GetHostPort("9000/tcp")

// check if minio server is up & running.
if err := pool.Retry(func() error {
Expand Down
2 changes: 1 addition & 1 deletion testhelper/docker/resource/sshserver/sshserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Setup(pool *dockertest.Pool, cln resource.Cleaner, opts ...Option) (*Resour

var (
buf *bytes.Buffer
timeout = time.After(30 * time.Second)
timeout = time.After(60 * time.Second)
ticker = time.NewTicker(200 * time.Millisecond)
)
loop:
Expand Down

0 comments on commit 6bb5880

Please sign in to comment.