Skip to content

Commit

Permalink
#150 If networkMode host ForListeningPort does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
Bablzz committed Feb 11, 2020
1 parent 0a03bcf commit 4f3436d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 4f3436d

Please sign in to comment.