Skip to content

Commit

Permalink
Merge pull request #151 from Bablzz/master
Browse files Browse the repository at this point in the history
#150 If networkMode host ForListeningPort does not work
  • Loading branch information
gianarb committed Feb 19, 2020
2 parents b028444 + 3aaae5a commit f64b20d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 11 deletions.
7 changes: 7 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
118 changes: 107 additions & 11 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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,
}

Expand All @@ -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{
Expand Down

0 comments on commit f64b20d

Please sign in to comment.