2017-06-07 19:03:52 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"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-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/pkg/errors"
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/net/context"
|
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}))
|
2017-06-07 19:03:52 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
func TestGetExitStatus(t *testing.T) {
|
|
|
|
containerID := "the exec id"
|
|
|
|
expecatedErr := errors.New("unexpected error")
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
inspectError error
|
|
|
|
exitCode int
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inspectError: nil,
|
|
|
|
exitCode: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inspectError: expecatedErr,
|
|
|
|
expectedError: expecatedErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exitCode: 15,
|
|
|
|
expectedError: cli.StatusError{StatusCode: 15},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
client := &fakeClient{
|
|
|
|
inspectFunc: func(id string) (types.ContainerJSON, error) {
|
|
|
|
assert.Equal(t, containerID, id)
|
|
|
|
return types.ContainerJSON{
|
|
|
|
ContainerJSONBase: &types.ContainerJSONBase{
|
|
|
|
State: &types.ContainerState{ExitCode: testcase.exitCode},
|
|
|
|
},
|
|
|
|
}, testcase.inspectError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := getExitStatus(context.Background(), client, containerID)
|
|
|
|
assert.Equal(t, testcase.expectedError, err)
|
|
|
|
}
|
|
|
|
}
|