Skip to content

Commit

Permalink
chore: add internalCheck() tests for exit code = 126/127
Browse files Browse the repository at this point in the history
  • Loading branch information
vchandela committed Sep 14, 2024
1 parent a9757d3 commit 36a4e89
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion wait/host_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,106 @@ func TestHostPortStrategySucceedsGivenShellIsNotFound(t *testing.T) {

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())
}
}

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))
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())
}
}

0 comments on commit 36a4e89

Please sign in to comment.