Skip to content

Commit

Permalink
chore: merge internal check tests with existing tests; revert mockStr…
Browse files Browse the repository at this point in the history
…ategyTarget to MockStrategyTarget to avoid export errors
  • Loading branch information
vchandela committed Sep 18, 2024
1 parent a0f5ab1 commit 5ff9e57
Showing 1 changed file with 19 additions and 85 deletions.
104 changes: 19 additions & 85 deletions wait/host_port_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package wait_test

import (
"bytes"
"context"
"errors"
"io"
"log"
"net"
"strconv"
"testing"
Expand Down Expand Up @@ -497,8 +499,17 @@ func TestHostPortStrategySucceedsGivenShellIsNotInstalled(t *testing.T) {
WithStartupTimeout(5 * time.Second).
WithPollInterval(100 * time.Millisecond)

oldWriter := log.Default().Writer()
var buf bytes.Buffer
log.Default().SetOutput(&buf)
t.Cleanup(func() {
log.Default().SetOutput(oldWriter)
})

err = wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)

require.Contains(t, buf.String(), "Shell not executable in container, only external port validated")
}

func TestHostPortStrategySucceedsGivenShellIsNotFound(t *testing.T) {
Expand Down Expand Up @@ -537,92 +548,15 @@ func TestHostPortStrategySucceedsGivenShellIsNotFound(t *testing.T) {
WithStartupTimeout(5 * time.Second).
WithPollInterval(100 * time.Millisecond)

err = wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)
}

func TestInternalCheckFailsGivenShellIsNotInstalled(t *testing.T) {
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
defer listener.Close()

rawPort := listener.Addr().(*net.TCPAddr).Port
port, err := nat.NewPort("tcp", strconv.Itoa(rawPort))
require.NoError(t, err)

target := &mockStrategyTarget{}
target.EXPECT().Host(anyContext).Return("localhost", nil)
target.EXPECT().Inspect(anyContext).Return(&types.ContainerJSON{
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap{
"80": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: port.Port(),
},
},
},
},
},
}, nil)
target.EXPECT().MappedPort(anyContext, nat.Port("80")).Return(port, nil)
target.EXPECT().State(anyContext).Return(&types.ContainerState{
Running: true,
}, nil)
target.EXPECT().Exec(anyContext, []string{"/bin/sh", "-c", "true && (\n\t\t\t\t\tcat /proc/net/tcp* | awk '{print $2}' | grep -i :0050 ||\n\t\t\t\t\tnc -vz -w 1 localhost 80 ||\n\t\t\t\t\t/bin/sh -c '</dev/tcp/localhost/80'\n\t\t\t\t)\n\t\t\t\t"}).Return(wait.ExitEaccess, nil, nil)
target.EXPECT().CopyFileFromContainer(anyContext, testFilename).Return(nil, errNotFound)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

{
err := wait.InternalCheck(ctx, "80", target)
require.Error(t, err)

require.Contains(t, err.Error(), wait.ErrShellNotExecutable.Error())
}
}

func TestInternalCheckFailsGivenShellIsNotFound(t *testing.T) {
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
defer listener.Close()
oldWriter := log.Default().Writer()
var buf bytes.Buffer
log.Default().SetOutput(&buf)
t.Cleanup(func() {
log.Default().SetOutput(oldWriter)
})

rawPort := listener.Addr().(*net.TCPAddr).Port
port, err := nat.NewPort("tcp", strconv.Itoa(rawPort))
err = wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)

target := &mockStrategyTarget{}
target.EXPECT().Host(anyContext).Return("localhost", nil)
target.EXPECT().Inspect(anyContext).Return(&types.ContainerJSON{
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap{
"80": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: port.Port(),
},
},
},
},
},
}, nil)
target.EXPECT().MappedPort(anyContext, nat.Port("80")).Return(port, nil)
target.EXPECT().State(anyContext).Return(&types.ContainerState{
Running: true,
}, nil)
target.EXPECT().Exec(anyContext, []string{"/bin/sh", "-c", "true && (\n\t\t\t\t\tcat /proc/net/tcp* | awk '{print $2}' | grep -i :0050 ||\n\t\t\t\t\tnc -vz -w 1 localhost 80 ||\n\t\t\t\t\t/bin/sh -c '</dev/tcp/localhost/80'\n\t\t\t\t)\n\t\t\t\t"}).Return(wait.ExitCmdNotFound, nil, nil)
target.EXPECT().CopyFileFromContainer(anyContext, testFilename).Return(nil, errNotFound)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

{
err := wait.InternalCheck(ctx, "80", target)
require.Error(t, err)

require.Contains(t, err.Error(), wait.ErrShellNotFound.Error())
}
require.Contains(t, buf.String(), "Shell not found in container")
}

0 comments on commit 5ff9e57

Please sign in to comment.