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; run make lint
  • Loading branch information
vchandela committed Sep 18, 2024
1 parent 79b4a85 commit d76ecc1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 104 deletions.
8 changes: 5 additions & 3 deletions wait/host_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ var (
_ StrategyTimeout = (*HostPortStrategy)(nil)
)

var errShellNotExecutable = errors.New("/bin/sh command not executable")
var errShellNotFound = errors.New("/bin/sh command not found")
var (
errShellNotExecutable = errors.New("/bin/sh command not executable")
errShellNotFound = errors.New("/bin/sh command not found")
)

type HostPortStrategy struct {
// Port is a string containing port number and protocol in the format "80/tcp"
Expand Down Expand Up @@ -167,7 +169,7 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT
default:
return fmt.Errorf("internal check: %w", err)
}
}
}

return nil
}
Expand Down
120 changes: 19 additions & 101 deletions wait/host_port_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package wait

import (
"bytes"
"context"
"io"
"log"
"net"
"strconv"
"testing"
Expand Down Expand Up @@ -501,8 +503,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 @@ -552,108 +563,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{
HostImpl: func(_ context.Context) (string, error) {
return "localhost", nil
},
InspectImpl: func(_ context.Context) (*types.ContainerJSON, error) {
return &types.ContainerJSON{
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap{
"80": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: port.Port(),
},
},
},
},
},
}, nil
},
MappedPortImpl: func(_ context.Context, _ nat.Port) (nat.Port, error) {
return port, nil
},
StateImpl: func(_ context.Context) (*types.ContainerState, error) {
return &types.ContainerState{
Running: true,
}, nil
},
ExecImpl: func(_ context.Context, _ []string, _ ...exec.ProcessOption) (int, io.Reader, error) {
// This is the error that would be returned if the shell is not installed.
return exitEaccess, nil, nil
},
}

{
err := internalCheck(context.Background(), "80", target)
require.Error(t, err)

require.Contains(t, err.Error(), errShellNotExecutable.Error())
}
}
oldWriter := log.Default().Writer()
var buf bytes.Buffer
log.Default().SetOutput(&buf)
t.Cleanup(func() {
log.Default().SetOutput(oldWriter)
})

func TestInternalCheckFailsGivenShellIsNotFound(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))
err = wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)

target := &MockStrategyTarget{
HostImpl: func(_ context.Context) (string, error) {
return "localhost", nil
},
InspectImpl: func(_ context.Context) (*types.ContainerJSON, error) {
return &types.ContainerJSON{
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap{
"80": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: port.Port(),
},
},
},
},
},
}, nil
},
MappedPortImpl: func(_ context.Context, _ nat.Port) (nat.Port, error) {
return port, nil
},
StateImpl: func(_ context.Context) (*types.ContainerState, error) {
return &types.ContainerState{
Running: true,
}, nil
},
ExecImpl: func(_ context.Context, _ []string, _ ...exec.ProcessOption) (int, io.Reader, error) {
// This is the error that would be returned if the shell is not found.
return exitCmdNotFound, nil, nil
},
}

{
err := internalCheck(context.Background(), "80", target)
require.Error(t, err)

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

0 comments on commit d76ecc1

Please sign in to comment.