2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:05:59 -05:00
|
|
|
"io"
|
2020-06-29 18:32:44 -04:00
|
|
|
"os"
|
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"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2020-06-29 18:32:44 -04:00
|
|
|
"gotest.tools/v3/fs"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
func withDefaultOpts(options ExecOptions) ExecOptions {
|
|
|
|
options.Env = opts.NewListOpts(opts.ValidateEnv)
|
|
|
|
options.EnvFile = opts.NewListOpts(nil)
|
|
|
|
if len(options.Command) == 0 {
|
|
|
|
options.Command = []string{"command"}
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
return options
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseExec(t *testing.T) {
|
2020-06-29 18:32:44 -04:00
|
|
|
content := `ONE=1
|
|
|
|
TWO=2
|
|
|
|
`
|
|
|
|
|
|
|
|
tmpFile := fs.NewFile(t, t.Name(), fs.WithContent(content))
|
|
|
|
defer tmpFile.Remove()
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
testcases := []struct {
|
2022-02-22 04:36:43 -05:00
|
|
|
options ExecOptions
|
2017-05-09 18:35:25 -04:00
|
|
|
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,
|
|
|
|
},
|
2022-02-22 04:36:43 -05:00
|
|
|
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,
|
|
|
|
},
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{
|
|
|
|
Command: []string{"command1", "command2"},
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{
|
|
|
|
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
|
|
|
{
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{Detach: true}),
|
2017-05-09 18:35:25 -04:00
|
|
|
expected: types.ExecConfig{
|
|
|
|
Detach: true,
|
|
|
|
Cmd: []string{"command"},
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2017-05-02 15:10:03 -04:00
|
|
|
{
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{
|
|
|
|
TTY: true,
|
|
|
|
Interactive: true,
|
|
|
|
Detach: true,
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Detach: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{Detach: true}),
|
2017-05-09 18:35:25 -04:00
|
|
|
configFile: configfile.ConfigFile{DetachKeys: "de"},
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
DetachKeys: "de",
|
|
|
|
Detach: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{
|
|
|
|
Detach: true,
|
|
|
|
DetachKeys: "ab",
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
|
|
|
configFile: configfile.ConfigFile{DetachKeys: "de"},
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
DetachKeys: "ab",
|
|
|
|
Detach: true,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2020-06-29 18:32:44 -04:00
|
|
|
{
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Env: []string{"ONE=1", "TWO=2"},
|
|
|
|
},
|
2022-02-22 04:36:43 -05:00
|
|
|
options: func() ExecOptions {
|
|
|
|
o := withDefaultOpts(ExecOptions{})
|
|
|
|
o.EnvFile.Set(tmpFile.Path())
|
2020-06-29 18:32:44 -04:00
|
|
|
return o
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: types.ExecConfig{
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Env: []string{"ONE=1", "TWO=2", "ONE=override"},
|
|
|
|
},
|
2022-02-22 04:36:43 -05:00
|
|
|
options: func() ExecOptions {
|
|
|
|
o := withDefaultOpts(ExecOptions{})
|
|
|
|
o.EnvFile.Set(tmpFile.Path())
|
|
|
|
o.Env.Set("ONE=override")
|
2020-06-29 18:32:44 -04:00
|
|
|
return o
|
|
|
|
}(),
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
for _, testcase := range testcases {
|
2020-06-29 18:32:44 -04:00
|
|
|
execConfig, err := parseExec(testcase.options, &testcase.configFile)
|
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(testcase.expected, *execConfig))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 18:32:44 -04:00
|
|
|
func TestParseExecNoSuchFile(t *testing.T) {
|
2022-02-22 04:36:43 -05:00
|
|
|
execOpts := withDefaultOpts(ExecOptions{})
|
|
|
|
execOpts.EnvFile.Set("no-such-env-file")
|
2020-06-29 18:32:44 -04:00
|
|
|
execConfig, err := parseExec(execOpts, &configfile.ConfigFile{})
|
|
|
|
assert.ErrorContains(t, err, "no-such-env-file")
|
|
|
|
assert.Check(t, os.IsNotExist(err))
|
|
|
|
assert.Check(t, execConfig == nil)
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func TestRunExec(t *testing.T) {
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-05-09 18:35:25 -04:00
|
|
|
doc string
|
2022-02-22 04:36:43 -05:00
|
|
|
options ExecOptions
|
2017-05-09 18:35:25 -04:00
|
|
|
client fakeClient
|
|
|
|
expectedError string
|
|
|
|
expectedOut string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "successful detach",
|
2022-02-22 04:36:43 -05:00
|
|
|
options: withDefaultOpts(ExecOptions{
|
2023-11-08 08:32:55 -05:00
|
|
|
Detach: true,
|
2017-05-09 18:35:25 -04:00
|
|
|
}),
|
|
|
|
client: fakeClient{execCreateFunc: execCreateWithID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "inspect error",
|
2022-02-22 04:36:43 -05:00
|
|
|
options: NewExecOptions(),
|
2017-05-09 18:35:25 -04:00
|
|
|
client: fakeClient{
|
|
|
|
inspectFunc: func(string) (types.ContainerJSON, error) {
|
|
|
|
return types.ContainerJSON{}, errors.New("failed inspect")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "failed inspect",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "missing exec ID",
|
2022-02-22 04:36:43 -05:00
|
|
|
options: NewExecOptions(),
|
2017-05-09 18:35:25 -04:00
|
|
|
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) {
|
2023-09-09 18:27:44 -04:00
|
|
|
fakeCLI := test.NewFakeCli(&testcase.client)
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
err := RunExec(context.TODO(), fakeCLI, "thecontainer", testcase.options)
|
2017-05-09 18:35:25 -04:00
|
|
|
if testcase.expectedError != "" {
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, testcase.expectedError)
|
2023-11-20 07:54:53 -05:00
|
|
|
} else if !assert.Check(t, err) {
|
|
|
|
return
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
2023-09-09 18:27:44 -04:00
|
|
|
assert.Check(t, is.Equal(testcase.expectedOut, fakeCLI.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedErr, fakeCLI.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"
|
2021-12-16 23:54:28 -05:00
|
|
|
expectedErr := errors.New("unexpected error")
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
inspectError error
|
|
|
|
exitCode int
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inspectError: nil,
|
|
|
|
exitCode: 0,
|
|
|
|
},
|
|
|
|
{
|
2021-12-16 23:54:28 -05:00
|
|
|
inspectError: expectedErr,
|
|
|
|
expectedError: expectedErr,
|
2017-05-09 18:35:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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 {
|
2023-11-20 11:38:50 -05:00
|
|
|
fakeCLI := test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc})
|
|
|
|
cmd := NewExecCommand(fakeCLI)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-06-07 19:03:52 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|