2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-06-07 19:03:52 -04:00
|
|
|
"io/ioutil"
|
2016-09-08 13:11:39 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-05-09 18:35:25 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func withDefaultOpts(options execOptions) execOptions {
|
|
|
|
options.env = opts.NewListOpts(opts.ValidateEnv)
|
|
|
|
if len(options.command) == 0 {
|
|
|
|
options.command = []string{"command"}
|
|
|
|
}
|
|
|
|
return options
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseExec(t *testing.T) {
|
2017-05-09 18:35:25 -04:00
|
|
|
testcases := []struct {
|
|
|
|
options execOptions
|
|
|
|
configFile configfile.ConfigFile
|
|
|
|
expected types.ExecConfig
|
|
|
|
}{
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2017-05-09 18:35:25 -04:00
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
},
|
|
|
|
options: withDefaultOpts(execOptions{}),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2017-05-09 18:35:25 -04:00
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command1", "command2"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
},
|
|
|
|
options: withDefaultOpts(execOptions{
|
|
|
|
command: []string{"command1", "command2"},
|
|
|
|
}),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2017-05-09 18:35:25 -04:00
|
|
|
options: withDefaultOpts(execOptions{
|
2016-09-08 13:11:39 -04:00
|
|
|
interactive: true,
|
|
|
|
tty: true,
|
|
|
|
user: "uid",
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
User: "uid",
|
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"command"},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2017-05-09 18:35:25 -04:00
|
|
|
options: withDefaultOpts(execOptions{detach: true}),
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Detach: true,
|
|
|
|
Cmd: []string{"command"},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2017-05-09 18:35:25 -04:00
|
|
|
options: withDefaultOpts(execOptions{
|
2016-09-08 13:11:39 -04:00
|
|
|
tty: true,
|
|
|
|
interactive: true,
|
|
|
|
detach: true,
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Detach: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
options: withDefaultOpts(execOptions{detach: true}),
|
|
|
|
configFile: configfile.ConfigFile{DetachKeys: "de"},
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
DetachKeys: "de",
|
|
|
|
Detach: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
options: withDefaultOpts(execOptions{
|
|
|
|
detach: true,
|
|
|
|
detachKeys: "ab",
|
|
|
|
}),
|
|
|
|
configFile: configfile.ConfigFile{DetachKeys: "de"},
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
DetachKeys: "ab",
|
|
|
|
Detach: true,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
for _, testcase := range testcases {
|
|
|
|
execConfig := parseExec(testcase.options, &testcase.configFile)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(testcase.expected, *execConfig))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func TestRunExec(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
options execOptions
|
|
|
|
client fakeClient
|
|
|
|
expectedError string
|
|
|
|
expectedOut string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "successful detach",
|
|
|
|
options: withDefaultOpts(execOptions{
|
|
|
|
container: "thecontainer",
|
|
|
|
detach: true,
|
|
|
|
}),
|
|
|
|
client: fakeClient{execCreateFunc: execCreateWithID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "inspect error",
|
|
|
|
options: newExecOptions(),
|
|
|
|
client: fakeClient{
|
|
|
|
inspectFunc: func(string) (types.ContainerJSON, error) {
|
|
|
|
return types.ContainerJSON{}, errors.New("failed inspect")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "failed inspect",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "missing exec ID",
|
|
|
|
options: newExecOptions(),
|
|
|
|
expectedError: "exec ID empty",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Run(testcase.doc, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&testcase.client)
|
|
|
|
|
|
|
|
err := runExec(cli, testcase.options)
|
|
|
|
if testcase.expectedError != "" {
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, testcase.expectedError)
|
2017-05-09 18:35:25 -04:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
if !assert.Check(t, err) {
|
2017-05-09 18:35:25 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String()))
|
2017-05-09 18:35:25 -04:00
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func execCreateWithID(_ string, _ types.ExecConfig) (types.IDResponse, error) {
|
|
|
|
return types.IDResponse{ID: "execid"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetExecExitStatus(t *testing.T) {
|
|
|
|
execID := "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},
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
client := &fakeClient{
|
|
|
|
execInspectFunc: func(id string) (types.ContainerExecInspect, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(execID, id))
|
2017-05-09 18:35:25 -04:00
|
|
|
return types.ContainerExecInspect{ExitCode: testcase.exitCode}, testcase.inspectError
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
err := getExecExitStatus(context.Background(), client, execID)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expectedError, err))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 19:03:52 -04:00
|
|
|
|
|
|
|
func TestNewExecCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
containerInspectFunc func(img string) (types.ContainerJSON, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "client-error",
|
|
|
|
args: []string{"5cb5bb5e4a3b", "-t", "-i", "bash"},
|
|
|
|
expectedError: "something went wrong",
|
|
|
|
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
|
|
|
|
return types.ContainerJSON{}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-05-09 18:35:25 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc})
|
2017-06-07 19:03:52 -04:00
|
|
|
cmd := NewExecCommand(cli)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-06-07 19:03:52 -04:00
|
|
|
}
|
|
|
|
}
|