diff --git a/docker_test.go b/docker_test.go index bf09265308..39b69044f6 100644 --- a/docker_test.go +++ b/docker_test.go @@ -793,6 +793,52 @@ func TestContainerCreationWaitsForLogAndPortContextTimeout(t *testing.T) { } +func TestContainerCreationWaitingForHostPort(t *testing.T) { + ctx := context.Background() + req := ContainerRequest{ + Image: "nginx:1.17.6", + ExposedPorts: []string{"80/tcp"}, + WaitingFor: wait.ForListeningPort("80/tcp"), + } + nginx, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + defer func() { + err := nginx.Terminate(ctx) + if err != nil { + t.Fatal(err) + } + t.Log("terminated nginx container") + }() + if err != nil { + t.Fatal(err) + } +} + +func TestContainerCreationWaitingForHostPortWithoutBashThrowsAnError(t *testing.T) { + ctx := context.Background() + req := ContainerRequest{ + Image: "nginx:1.17.6-alpine", + ExposedPorts: []string{"80/tcp"}, + WaitingFor: wait.ForListeningPort("80/tcp"), + } + nginx, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + defer func() { + err := nginx.Terminate(ctx) + if err != nil { + t.Fatal(err) + } + t.Log("terminated nginx container") + }() + if err != nil { + t.Fatal(err) + } +} + func TestContainerCreationWaitsForLogAndPort(t *testing.T) { ctx := context.Background() req := ContainerRequest{ diff --git a/wait/host_port.go b/wait/host_port.go index b639b8e84e..dd67503366 100644 --- a/wait/host_port.go +++ b/wait/host_port.go @@ -90,13 +90,15 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT //internal check command := buildInternalCheckCommand(hp.Port.Int()) for { - exitCode, err := target.Exec(ctx, []string{"/bin/bash", "-c", command}) + exitCode, err := target.Exec(ctx, []string{"/bin/sh", "-c", command}) if err != nil { return errors.Wrapf(err, "host port waiting failed") } if exitCode == 0 { break + } else if exitCode == 126 { + return errors.New("/bin/sh command not executable") } } @@ -105,9 +107,9 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT func buildInternalCheckCommand(internalPort int) string { command := `( - cat /proc/net/tcp{,6} | awk '{print $2}' | grep -i :%x || + cat /proc/net/tcp* | awk '{print $2}' | grep -i :%04x || nc -vz -w 1 localhost %d || - /bin/bash -c '