2017-06-07 19:03:52 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2017-11-15 22:18:23 -05:00
|
|
|
"fmt"
|
2022-02-25 07:05:59 -05:00
|
|
|
"io"
|
2017-06-07 19:03:52 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/docker/cli/cli"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-11-15 22:18:23 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2017-06-07 19:03:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAttachCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
containerInspectFunc func(img string) (types.ContainerJSON, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "client-error",
|
|
|
|
args: []string{"5cb5bb5e4a3b"},
|
|
|
|
expectedError: "something went wrong",
|
|
|
|
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
|
|
|
|
return types.ContainerJSON{}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "client-stopped",
|
|
|
|
args: []string{"5cb5bb5e4a3b"},
|
|
|
|
expectedError: "You cannot attach to a stopped container",
|
|
|
|
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
|
|
|
|
c := types.ContainerJSON{}
|
|
|
|
c.ContainerJSONBase = &types.ContainerJSONBase{}
|
|
|
|
c.ContainerJSONBase.State = &types.ContainerState{Running: false}
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "client-paused",
|
|
|
|
args: []string{"5cb5bb5e4a3b"},
|
|
|
|
expectedError: "You cannot attach to a paused container",
|
|
|
|
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
|
|
|
|
c := types.ContainerJSON{}
|
|
|
|
c.ContainerJSONBase = &types.ContainerJSONBase{}
|
|
|
|
c.ContainerJSONBase.State = &types.ContainerState{
|
|
|
|
Running: true,
|
|
|
|
Paused: true,
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
2017-06-07 19:12:39 -04:00
|
|
|
{
|
|
|
|
name: "client-restarting",
|
|
|
|
args: []string{"5cb5bb5e4a3b"},
|
|
|
|
expectedError: "You cannot attach to a restarting container",
|
|
|
|
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
|
|
|
|
c := types.ContainerJSON{}
|
|
|
|
c.ContainerJSONBase = &types.ContainerJSONBase{}
|
|
|
|
c.ContainerJSONBase.State = &types.ContainerState{
|
|
|
|
Running: true,
|
|
|
|
Paused: false,
|
|
|
|
Restarting: true,
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
2017-06-07 19:03:52 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-05-09 18:35:25 -04:00
|
|
|
cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc}))
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-06-07 19:03:52 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-06-07 19:03:52 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
func TestGetExitStatus(t *testing.T) {
|
2017-11-15 22:18:23 -05:00
|
|
|
var (
|
|
|
|
expectedErr = fmt.Errorf("unexpected error")
|
|
|
|
errC = make(chan error, 1)
|
2022-04-29 13:26:50 -04:00
|
|
|
resultC = make(chan container.WaitResponse, 1)
|
2017-11-15 22:18:23 -05:00
|
|
|
)
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
testcases := []struct {
|
2022-04-29 13:26:50 -04:00
|
|
|
result *container.WaitResponse
|
2017-11-15 22:18:23 -05:00
|
|
|
err error
|
2017-05-09 18:35:25 -04:00
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
2022-04-29 13:26:50 -04:00
|
|
|
result: &container.WaitResponse{
|
2017-11-15 22:18:23 -05:00
|
|
|
StatusCode: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
err: expectedErr,
|
|
|
|
expectedError: expectedErr,
|
2017-05-09 18:35:25 -04:00
|
|
|
},
|
|
|
|
{
|
2022-04-29 13:26:50 -04:00
|
|
|
result: &container.WaitResponse{
|
|
|
|
Error: &container.WaitExitError{Message: expectedErr.Error()},
|
2017-11-15 22:18:23 -05:00
|
|
|
},
|
|
|
|
expectedError: expectedErr,
|
2017-05-09 18:35:25 -04:00
|
|
|
},
|
|
|
|
{
|
2022-04-29 13:26:50 -04:00
|
|
|
result: &container.WaitResponse{
|
2017-11-15 22:18:23 -05:00
|
|
|
StatusCode: 15,
|
|
|
|
},
|
2017-05-09 18:35:25 -04:00
|
|
|
expectedError: cli.StatusError{StatusCode: 15},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
2017-11-15 22:18:23 -05:00
|
|
|
if testcase.err != nil {
|
|
|
|
errC <- testcase.err
|
|
|
|
}
|
|
|
|
if testcase.result != nil {
|
|
|
|
resultC <- *testcase.result
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
2017-11-15 22:18:23 -05:00
|
|
|
err := getExitStatus(errC, resultC)
|
2017-12-21 16:27:57 -05:00
|
|
|
if testcase.expectedError == nil {
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-12-21 16:27:57 -05:00
|
|
|
} else {
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expectedError.Error())
|
2017-12-21 16:27:57 -05:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
}
|