-
Notifications
You must be signed in to change notification settings - Fork 589
/
ports.go
21 lines (17 loc) · 861 Bytes
/
ports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package containers
import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/kong/kubernetes-ingress-controller/v3/test/helpers"
)
// MappedLocalPort returns a port mapping for a container port that can be used to access the
// container from the host. The returned string is in the format expected by testcontainers.ContainerRequest ExposedPorts.
//
// This is a workaround for a bug in docker that causes IPv4 and IPv6 port mappings to overlap with
// different containers on the same host. See: https://github.com/moby/moby/issues/42442.
func MappedLocalPort(t *testing.T, containerPort nat.Port) string {
// Return the port mapping in the format expected by testcontainers.ContainerRequest ExposedPorts:
// <host>:<host-port>:<container-port>.
return fmt.Sprintf("0.0.0.0:%d:%s", helpers.GetFreePort(t), containerPort.Port())
}