From 4f3436d20e484cb81cb2d5348f8971d326176fe0 Mon Sep 17 00:00:00 2001 From: Bablzz Date: Tue, 11 Feb 2020 13:50:59 +0300 Subject: [PATCH 1/2] #150 If networkMode host ForListeningPort does not work --- docker.go | 7 +++++++ docker_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/docker.go b/docker.go index 1e01e61c26..91fcab6631 100644 --- a/docker.go +++ b/docker.go @@ -101,6 +101,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..d4d21a98a7 100644 --- a/docker_test.go +++ b/docker_test.go @@ -136,6 +136,35 @@ 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 TestContainerReturnItsContainerID(t *testing.T) { ctx := context.Background() nginxA, err := GenericContainer(ctx, GenericContainerRequest{ From 3aaae5aedc5640a0bcab19e50d607d73e8b050c6 Mon Sep 17 00:00:00 2001 From: Bablzz Date: Fri, 14 Feb 2020 10:29:10 +0300 Subject: [PATCH 2/2] PR fix. Reformat code --- docker_test.go | 63 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/docker_test.go b/docker_test.go index 69e871a045..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, } @@ -138,12 +140,13 @@ 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"), - }, + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort("80/tcp"), + }, Started: true, } @@ -168,12 +171,13 @@ func TestContainerWithHostNetworkOptionsAndWaitStrategy(t *testing.T) { 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)), - }, + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort(nat.Port(nginxPort)), + }, Started: true, } @@ -199,12 +203,13 @@ func TestContainerWithHostNetworkAndEndpoint(t *testing.T) { 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)), - }, + gcr := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort(nat.Port(nginxPort)), + }, Started: true, }