Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#128) Throw an error when bash is not installed in the target container #129

Merged
merged 9 commits into from
Feb 7, 2020
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 @@ -92,13 +92,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})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was a bug, yes, sorry for that

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")
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -107,9 +109,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 ||
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the clue!! At least, WOMC! Let's 🤞 for Travis :)

nc -vz -w 1 localhost %d ||
/bin/bash -c '</dev/tcp/localhost/%d'
/bin/sh -c '</dev/tcp/localhost/%d'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary, I believe. External command fix should be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for the late response. I'll revert this change and resend. TBH, not sure why the travis tests fail tough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmm, thinking about it, the internal command should be sh, because it's very likely that bash is not present in the container (i.e. Alpine-based images)

)
`
return "true && " + fmt.Sprintf(command, internalPort, internalPort, internalPort)
Expand Down