2017-10-30 17:52:08 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2023-09-09 18:27:44 -04:00
|
|
|
"context"
|
2017-10-30 17:52:08 -04:00
|
|
|
"io"
|
2018-02-27 16:38:02 -05:00
|
|
|
"os"
|
2017-10-30 17:52:08 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-10-30 17:52:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunCopyWithInvalidArguments(t *testing.T) {
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-10-30 17:52:08 -04:00
|
|
|
doc string
|
|
|
|
options copyOptions
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "copy between container",
|
|
|
|
options: copyOptions{
|
|
|
|
source: "first:/path",
|
|
|
|
destination: "second:/path",
|
|
|
|
},
|
|
|
|
expectedErr: "copying between containers is not supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "copy without a container",
|
|
|
|
options: copyOptions{
|
|
|
|
source: "./source",
|
|
|
|
destination: "./dest",
|
|
|
|
},
|
|
|
|
expectedErr: "must specify at least one container source",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Run(testcase.doc, func(t *testing.T) {
|
2023-09-09 18:27:44 -04:00
|
|
|
err := runCopy(context.TODO(), test.NewFakeCli(nil), testcase.options)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expectedErr)
|
2017-10-30 17:52:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunCopyFromContainerToStdout(t *testing.T) {
|
|
|
|
tarContent := "the tar content"
|
|
|
|
|
2024-06-09 08:06:13 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
|
|
|
|
assert.Check(t, is.Equal("container", ctr))
|
2022-02-25 07:05:59 -05:00
|
|
|
return io.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil
|
2017-10-30 17:52:08 -04:00
|
|
|
},
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
|
|
|
source: "container:/path",
|
|
|
|
destination: "-",
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(tarContent, cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
|
2017-10-30 17:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunCopyFromContainerToFilesystem(t *testing.T) {
|
|
|
|
destDir := fs.NewDir(t, "cp-test",
|
|
|
|
fs.WithFile("file1", "content\n"))
|
|
|
|
defer destDir.Remove()
|
|
|
|
|
2024-06-09 08:06:13 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
|
|
|
|
assert.Check(t, is.Equal("container", ctr))
|
2017-10-30 17:52:08 -04:00
|
|
|
readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{})
|
|
|
|
return readCloser, types.ContainerPathStat{}, err
|
|
|
|
},
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
|
|
|
source: "container:/path",
|
|
|
|
destination: destDir.Path(),
|
|
|
|
quiet: true,
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("", cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
|
2017-10-30 17:52:08 -04:00
|
|
|
|
2022-02-25 07:05:59 -05:00
|
|
|
content, err := os.ReadFile(destDir.Join("file1"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("content\n", string(content)))
|
2017-10-30 17:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunCopyFromContainerToFilesystemMissingDestinationDirectory(t *testing.T) {
|
|
|
|
destDir := fs.NewDir(t, "cp-test",
|
|
|
|
fs.WithFile("file1", "content\n"))
|
|
|
|
defer destDir.Remove()
|
|
|
|
|
2024-06-09 08:06:13 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
|
|
|
|
assert.Check(t, is.Equal("container", ctr))
|
2017-10-30 17:52:08 -04:00
|
|
|
readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{})
|
|
|
|
return readCloser, types.ContainerPathStat{}, err
|
|
|
|
},
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
2017-10-30 17:52:08 -04:00
|
|
|
source: "container:/path",
|
|
|
|
destination: destDir.Join("missing", "foo"),
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, destDir.Join("missing"))
|
2017-10-30 17:52:08 -04:00
|
|
|
}
|
|
|
|
|
2018-02-27 16:38:02 -05:00
|
|
|
func TestRunCopyToContainerFromFileWithTrailingSlash(t *testing.T) {
|
|
|
|
srcFile := fs.NewFile(t, t.Name())
|
|
|
|
defer srcFile.Remove()
|
|
|
|
|
2024-06-09 08:06:13 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
2018-02-27 16:38:02 -05:00
|
|
|
source: srcFile.Path() + string(os.PathSeparator),
|
|
|
|
destination: "container:/path",
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
2018-02-27 10:54:36 -05:00
|
|
|
|
|
|
|
expectedError := "not a directory"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedError = "The filename, directory name, or volume label syntax is incorrect"
|
|
|
|
}
|
|
|
|
assert.ErrorContains(t, err, expectedError)
|
2018-02-27 16:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunCopyToContainerSourceDoesNotExist(t *testing.T) {
|
2024-06-09 08:06:13 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
2018-02-27 16:38:02 -05:00
|
|
|
source: "/does/not/exist",
|
|
|
|
destination: "container:/path",
|
2024-06-09 08:06:13 -04:00
|
|
|
})
|
2018-02-27 16:38:02 -05:00
|
|
|
expected := "no such file or directory"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = "cannot find the file specified"
|
|
|
|
}
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, expected)
|
2018-02-27 16:38:02 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:52:08 -04:00
|
|
|
func TestSplitCpArg(t *testing.T) {
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-10-30 17:52:08 -04:00
|
|
|
doc string
|
|
|
|
path string
|
|
|
|
os string
|
|
|
|
expectedContainer string
|
|
|
|
expectedPath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "absolute path with colon",
|
|
|
|
os: "linux",
|
|
|
|
path: "/abs/path:withcolon",
|
|
|
|
expectedPath: "/abs/path:withcolon",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "relative path with colon",
|
|
|
|
path: "./relative:path",
|
|
|
|
expectedPath: "./relative:path",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "absolute path with drive",
|
|
|
|
os: "windows",
|
|
|
|
path: `d:\abs\path`,
|
|
|
|
expectedPath: `d:\abs\path`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "no separator",
|
|
|
|
path: "relative/path",
|
|
|
|
expectedPath: "relative/path",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "with separator",
|
|
|
|
path: "container:/opt/foo",
|
|
|
|
expectedPath: "/opt/foo",
|
|
|
|
expectedContainer: "container",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Run(testcase.doc, func(t *testing.T) {
|
2018-06-08 12:24:26 -04:00
|
|
|
skip.If(t, testcase.os != "" && testcase.os != runtime.GOOS)
|
2017-10-30 17:52:08 -04:00
|
|
|
|
2024-06-09 08:06:13 -04:00
|
|
|
ctr, path := splitCpArg(testcase.path)
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedContainer, ctr))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expectedPath, path))
|
2017-10-30 17:52:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 03:17:35 -05:00
|
|
|
|
|
|
|
func TestRunCopyFromContainerToFilesystemIrregularDestination(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(nil)
|
2024-06-09 08:06:13 -04:00
|
|
|
err := runCopy(context.TODO(), cli, copyOptions{
|
|
|
|
source: "container:/dev/null",
|
|
|
|
destination: "/dev/random",
|
|
|
|
})
|
2019-02-07 03:17:35 -05:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
expected := `"/dev/random" must be a directory or a regular file`
|
|
|
|
assert.ErrorContains(t, err, expected)
|
|
|
|
}
|