diff --git a/docker.go b/docker.go index d2f7ddbfd5..de6ac93e26 100644 --- a/docker.go +++ b/docker.go @@ -104,6 +104,13 @@ func (c *DockerContainer) Host(ctx context.Context) (string, error) { // MappedPort gets externally mapped port for a container port func (c *DockerContainer) MappedPort(ctx context.Context, port nat.Port) (nat.Port, error) { + inspect, err := c.inspectContainer(ctx) + if err != nil { + return "", err + } + if inspect.ContainerJSONBase.HostConfig.NetworkMode == "host" { + return port, nil + } ports, err := c.Ports(ctx) if err != nil { return "", err diff --git a/docker_test.go b/docker_test.go index 39b69044f6..49ac840642 100644 --- a/docker_test.go +++ b/docker_test.go @@ -92,11 +92,12 @@ func TestContainerAttachedToNewNetwork(t *testing.T) { func TestContainerWithHostNetworkOptions(t *testing.T) { ctx := context.Background() - gcr := GenericContainerRequest{ContainerRequest: ContainerRequest{ - Image: "nginx", - SkipReaper: true, - NetworkMode: "host", - }, + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + }, Started: true, } @@ -120,12 +121,13 @@ func TestContainerWithHostNetworkOptions(t *testing.T) { func TestContainerWithNetworkModeAndNetworkTogether(t *testing.T) { ctx := context.Background() - gcr := GenericContainerRequest{ContainerRequest: ContainerRequest{ - Image: "nginx", - SkipReaper: true, - NetworkMode: "host", - Networks: []string{"new-network"}, - }, + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + Networks: []string{"new-network"}, + }, Started: true, } @@ -136,6 +138,100 @@ func TestContainerWithNetworkModeAndNetworkTogether(t *testing.T) { } } +func TestContainerWithHostNetworkOptionsAndWaitStrategy(t *testing.T) { + ctx := context.Background() + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort("80/tcp"), + }, + Started: true, + } + + nginxC, err := GenericContainer(ctx, gcr) + if err != nil { + t.Fatal(err) + } + + defer nginxC.Terminate(ctx) + + host, err := nginxC.Host(ctx) + if err != nil { + t.Errorf("Expected host %s. Got '%d'.", host, err) + } + + _, err = http.Get("http://" + host + ":80") + if err != nil { + t.Errorf("Expected OK response. Got '%d'.", err) + } +} + +func TestContainerWithHostNetworkAndEndpoint(t *testing.T) { + nginxPort := "80/tcp" + ctx := context.Background() + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort(nat.Port(nginxPort)), + }, + Started: true, + } + + nginxC, err := GenericContainer(ctx, gcr) + if err != nil { + t.Fatal(err) + } + + defer nginxC.Terminate(ctx) + + hostN, err := nginxC.Endpoint(ctx, "") + if err != nil { + t.Errorf("Expected host %s. Got '%d'.", hostN, err) + } + t.Log(hostN) + + _, err = http.Get("http://" + hostN) + if err != nil { + t.Errorf("Expected OK response. Got '%d'.", err) + } +} + +func TestContainerWithHostNetworkAndPortEndpoint(t *testing.T) { + nginxPort := "80/tcp" + ctx := context.Background() + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort(nat.Port(nginxPort)), + }, + Started: true, + } + + nginxC, err := GenericContainer(ctx, gcr) + if err != nil { + t.Fatal(err) + } + + defer nginxC.Terminate(ctx) + + origin, err := nginxC.PortEndpoint(ctx, nat.Port(nginxPort), "http") + if err != nil { + t.Errorf("Expected host %s. Got '%d'.", origin, err) + } + t.Log(origin) + + _, err = http.Get(origin) + if err != nil { + t.Errorf("Expected OK response. Got '%d'.", err) + } +} + func TestContainerReturnItsContainerID(t *testing.T) { ctx := context.Background() nginxA, err := GenericContainer(ctx, GenericContainerRequest{