Skip to content

Commit

Permalink
Merge pull request #129 from mdelapenya/128-bash-host-port
Browse files Browse the repository at this point in the history
(#128) Throw an error when bash is not installed in the target container
  • Loading branch information
gianarb authored Feb 7, 2020
2 parents 8426741 + d121153 commit 0a03bcf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
46 changes: 46 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 5 additions & 3 deletions wait/host_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -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 '</dev/tcp/localhost/%d'
/bin/sh -c '</dev/tcp/localhost/%d'
)
`
return "true && " + fmt.Sprintf(command, internalPort, internalPort, internalPort)
Expand Down

0 comments on commit 0a03bcf

Please sign in to comment.