Use checker assert for integration-cli/docker_cli_cp_* four files.

Signed-off-by: Jian Zhang <zhangjian.fnst@cn.fujitsu.com>
This commit is contained in:
Jian Zhang 2015-10-23 09:19:33 +08:00
parent 75128c46c7
commit f26a31e80c
4 changed files with 459 additions and 980 deletions

View File

@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@ -24,48 +25,37 @@ import (
// Test for error when SRC does not exist.
func (s *DockerSuite) TestCpFromErrSrcNotExists(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{})
tmpDir := getTestDir(c, "test-cp-from-err-src-not-exists")
defer os.RemoveAll(tmpDir)
err := runDockerCp(c, containerCpPath(cID, "file1"), tmpDir)
if err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
err := runDockerCp(c, containerCpPath(containerID, "file1"), tmpDir)
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
}
// Test for error when SRC ends in a trailing
// path separator but it exists as a file.
func (s *DockerSuite) TestCpFromErrSrcNotDir(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-err-src-not-dir")
defer os.RemoveAll(tmpDir)
err := runDockerCp(c, containerCpPathTrailingSep(cID, "file1"), tmpDir)
if err == nil {
c.Fatal("expected IsNotDir error, but got nil instead")
}
err := runDockerCp(c, containerCpPathTrailingSep(containerID, "file1"), tmpDir)
c.Assert(err, checker.NotNil)
if !isCpNotDir(err) {
c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
}
c.Assert(isCpNotDir(err), checker.True, check.Commentf("expected IsNotDir error, but got %T: %s", err, err))
}
// Test for error when SRC is a valid file or directory,
// bu the DST parent directory does not exist.
func (s *DockerSuite) TestCpFromErrDstParentNotExists(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-err-dst-parent-not-exists")
defer os.RemoveAll(tmpDir)
@ -73,36 +63,28 @@ func (s *DockerSuite) TestCpFromErrDstParentNotExists(c *check.C) {
makeTestContentInDir(c, tmpDir)
// Try with a file source.
srcPath := containerCpPath(cID, "/file1")
srcPath := containerCpPath(containerID, "/file1")
dstPath := cpPath(tmpDir, "notExists", "file1")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
// Try with a directory source.
srcPath = containerCpPath(cID, "/dir1")
srcPath = containerCpPath(containerID, "/dir1")
if err := runDockerCp(c, srcPath, dstPath); err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
err = runDockerCp(c, srcPath, dstPath)
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
}
// Test for error when DST ends in a trailing
// path separator but exists as a file.
func (s *DockerSuite) TestCpFromErrDstNotDir(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
defer os.RemoveAll(tmpDir)
@ -110,36 +92,28 @@ func (s *DockerSuite) TestCpFromErrDstNotDir(c *check.C) {
makeTestContentInDir(c, tmpDir)
// Try with a file source.
srcPath := containerCpPath(cID, "/file1")
srcPath := containerCpPath(containerID, "/file1")
dstPath := cpPathTrailingSep(tmpDir, "file1")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected IsNotDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotDir(err) {
c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
}
c.Assert(isCpNotDir(err), checker.True, check.Commentf("expected IsNotDir error, but got %T: %s", err, err))
// Try with a directory source.
srcPath = containerCpPath(cID, "/dir1")
srcPath = containerCpPath(containerID, "/dir1")
if err := runDockerCp(c, srcPath, dstPath); err == nil {
c.Fatal("expected IsNotDir error, but got nil instead")
}
err = runDockerCp(c, srcPath, dstPath)
c.Assert(err, checker.NotNil)
if !isCpNotDir(err) {
c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
}
c.Assert(isCpNotDir(err), checker.True, check.Commentf("expected IsNotDir error, but got %T: %s", err, err))
}
// Check that copying from a container to a local symlink copies to the symlink
// target and does not overwrite the local symlink itself.
func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
defer os.RemoveAll(tmpDir)
@ -148,79 +122,55 @@ func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
// First, copy a file from the container to a symlink to a file. This
// should overwrite the symlink target contents with the source contents.
srcPath := containerCpPath(cID, "/file2")
srcPath := containerCpPath(containerID, "/file2")
dstPath := cpPath(tmpDir, "symlinkToFile1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, dstPath, "file1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, dstPath, "file1"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(tmpDir, "file1"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(tmpDir, "file1"), "file2\n"), checker.IsNil)
// Next, copy a file from the container to a symlink to a directory. This
// should copy the file into the symlink target directory.
dstPath = cpPath(tmpDir, "symlinkToDir1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, dstPath, "dir1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, dstPath, "dir1"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(tmpDir, "file2"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(tmpDir, "file2"), "file2\n"), checker.IsNil)
// Next, copy a file from the container to a symlink to a file that does
// not exist (a broken symlink). This should create the target file with
// the contents of the source file.
dstPath = cpPath(tmpDir, "brokenSymlinkToFileX")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, dstPath, "fileX"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, dstPath, "fileX"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(tmpDir, "fileX"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(tmpDir, "fileX"), "file2\n"), checker.IsNil)
// Next, copy a directory from the container to a symlink to a local
// directory. This should copy the directory into the symlink target
// directory and not modify the symlink.
srcPath = containerCpPath(cID, "/dir2")
srcPath = containerCpPath(containerID, "/dir2")
dstPath = cpPath(tmpDir, "symlinkToDir1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, dstPath, "dir1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, dstPath, "dir1"), checker.IsNil)
// The directory should now contain a copy of "dir2".
if err := fileContentEquals(c, cpPath(tmpDir, "dir1/dir2/file2-1"), "file2-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(tmpDir, "dir1/dir2/file2-1"), "file2-1\n"), checker.IsNil)
// Next, copy a directory from the container to a symlink to a local
// directory that does not exist (a broken symlink). This should create
@ -228,19 +178,13 @@ func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
// should not modify the symlink.
dstPath = cpPath(tmpDir, "brokenSymlinkToDirX")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, dstPath, "dirX"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, dstPath, "dirX"), checker.IsNil)
// The "dirX" directory should now be a copy of "dir2".
if err := fileContentEquals(c, cpPath(tmpDir, "dirX/file2-1"), "file2-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(tmpDir, "dirX/file2-1"), "file2-1\n"), checker.IsNil)
}
// Possibilities are reduced to the remaining 10 cases:
@ -264,24 +208,19 @@ func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
// contents of the source file into it.
func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-a")
defer os.RemoveAll(tmpDir)
srcPath := containerCpPath(cID, "/root/file1")
srcPath := containerCpPath(containerID, "/root/file1")
dstPath := cpPath(tmpDir, "itWorks.txt")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
}
// B. SRC specifies a file and DST (with trailing path separator) doesn't
@ -289,54 +228,42 @@ func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
// create a directory when copying a single file.
func (s *DockerSuite) TestCpFromCaseB(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-case-b")
defer os.RemoveAll(tmpDir)
srcPath := containerCpPath(cID, "/file1")
srcPath := containerCpPath(containerID, "/file1")
dstDir := cpPathTrailingSep(tmpDir, "testDir")
err := runDockerCp(c, srcPath, dstDir)
if err == nil {
c.Fatal("expected DirNotExists error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpDirNotExist(err) {
c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
}
c.Assert(isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExists error, but got %T: %s", err, err))
}
// C. SRC specifies a file and DST exists as a file. This should overwrite
// the file at DST with the contents of the source file.
func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-c")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcPath := containerCpPath(cID, "/root/file1")
srcPath := containerCpPath(containerID, "/root/file1")
dstPath := cpPath(tmpDir, "file2")
// Ensure the local file starts with different content.
if err := fileContentEquals(c, dstPath, "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file2\n"), checker.IsNil)
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
}
// D. SRC specifies a file and DST exists as a directory. This should place
@ -344,50 +271,38 @@ func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
// this works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-case-d")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcPath := containerCpPath(cID, "/file1")
srcPath := containerCpPath(containerID, "/file1")
dstDir := cpPath(tmpDir, "dir1")
dstPath := filepath.Join(dstDir, "file1")
// Ensure that dstPath doesn't exist.
if _, err := os.Stat(dstPath); !os.IsNotExist(err) {
c.Fatalf("did not expect dstPath %q to exist", dstPath)
}
_, err := os.Stat(dstPath)
c.Assert(os.IsNotExist(err), checker.True, check.Commentf("did not expect dstPath %q to exist", dstPath))
if err := runDockerCp(c, srcPath, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
if err := os.RemoveAll(dstDir); err != nil {
c.Fatalf("unable to remove dstDir: %s", err)
}
// unable to remove dstDir
c.Assert(os.RemoveAll(dstDir), checker.IsNil)
if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
c.Fatalf("unable to make dstDir: %s", err)
}
// unable to make dstDir
c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
dstDir = cpPathTrailingSep(tmpDir, "dir1")
if err := runDockerCp(c, srcPath, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
}
// E. SRC specifies a directory and DST does not exist. This should create a
@ -396,66 +311,51 @@ func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
// not.
func (s *DockerSuite) TestCpFromCaseE(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-case-e")
defer os.RemoveAll(tmpDir)
srcDir := containerCpPath(cID, "dir1")
srcDir := containerCpPath(containerID, "dir1")
dstDir := cpPath(tmpDir, "testDir")
dstPath := filepath.Join(dstDir, "file1-1")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
if err := os.RemoveAll(dstDir); err != nil {
c.Fatalf("unable to remove dstDir: %s", err)
}
// unable to remove dstDir
c.Assert(os.RemoveAll(dstDir), checker.IsNil)
dstDir = cpPathTrailingSep(tmpDir, "testDir")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
}
// F. SRC specifies a directory and DST exists as a file. This should cause an
// error as it is not possible to overwrite a file with a directory.
func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-f")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcDir := containerCpPath(cID, "/root/dir1")
srcDir := containerCpPath(containerID, "/root/dir1")
dstFile := cpPath(tmpDir, "file1")
err := runDockerCp(c, srcDir, dstFile)
if err == nil {
c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyDir(err) {
c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
}
// G. SRC specifies a directory and DST exists as a directory. This should copy
@ -463,48 +363,37 @@ func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
// works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-g")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcDir := containerCpPath(cID, "/root/dir1")
srcDir := containerCpPath(containerID, "/root/dir1")
dstDir := cpPath(tmpDir, "dir2")
resultDir := filepath.Join(dstDir, "dir1")
dstPath := filepath.Join(resultDir, "file1-1")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
if err := os.RemoveAll(dstDir); err != nil {
c.Fatalf("unable to remove dstDir: %s", err)
}
// unable to remove dstDir
c.Assert(os.RemoveAll(dstDir), checker.IsNil)
if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
c.Fatalf("unable to make dstDir: %s", err)
}
// unable to make dstDir
c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
dstDir = cpPathTrailingSep(tmpDir, "dir2")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
}
// H. SRC specifies a directory's contents only and DST does not exist. This
@ -513,39 +402,29 @@ func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
// this works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-from-case-h")
defer os.RemoveAll(tmpDir)
srcDir := containerCpPathTrailingSep(cID, "dir1") + "."
srcDir := containerCpPathTrailingSep(containerID, "dir1") + "."
dstDir := cpPath(tmpDir, "testDir")
dstPath := filepath.Join(dstDir, "file1-1")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
if err := os.RemoveAll(dstDir); err != nil {
c.Fatalf("unable to remove resultDir: %s", err)
}
// unable to remove resultDir
c.Assert(os.RemoveAll(dstDir), checker.IsNil)
dstDir = cpPathTrailingSep(tmpDir, "testDir")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
}
// I. SRC specifies a directory's contents only and DST exists as a file. This
@ -553,27 +432,22 @@ func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
// directory.
func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-i")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
dstFile := cpPath(tmpDir, "file1")
err := runDockerCp(c, srcDir, dstFile)
if err == nil {
c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyDir(err) {
c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
}
// J. SRC specifies a directory's contents only and DST exists as a directory.
@ -582,45 +456,34 @@ func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
// trailing path separator or not.
func (s *DockerSuite) TestCpFromCaseJ(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-from-case-j")
defer os.RemoveAll(tmpDir)
makeTestContentInDir(c, tmpDir)
srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
dstDir := cpPath(tmpDir, "dir2")
dstPath := filepath.Join(dstDir, "file1-1")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
if err := os.RemoveAll(dstDir); err != nil {
c.Fatalf("unable to remove dstDir: %s", err)
}
// unable to remove dstDir
c.Assert(os.RemoveAll(dstDir), checker.IsNil)
if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
c.Fatalf("unable to make dstDir: %s", err)
}
// unable to make dstDir
c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
dstDir = cpPathTrailingSep(tmpDir, "dir2")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
}

View File

@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@ -26,107 +27,77 @@ const (
// Ensure that an all-local path case returns an error.
func (s *DockerSuite) TestCpLocalOnly(c *check.C) {
err := runDockerCp(c, "foo", "bar")
if err == nil {
c.Fatal("expected failure, got success")
}
c.Assert(err, checker.NotNil)
if !strings.Contains(err.Error(), "must specify at least one container source") {
c.Fatalf("unexpected output: %s", err.Error())
}
c.Assert(err.Error(), checker.Contains, "must specify at least one container source")
}
// Test for #5656
// Check that garbage paths don't escape the container's rootfs
func (s *DockerSuite) TestCpGarbagePath(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
hostFile, err := os.Create(cpFullPath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
path := path.Join("../../../../../../../../../../../../", cpFullPath)
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if string(test) == cpHostContents {
c.Errorf("output matched host file -- garbage path can escape container rootfs")
}
if string(test) != cpContainerContents {
c.Errorf("output doesn't match the input for garbage path")
}
// output matched host file -- garbage path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
// output doesn't match the input for garbage path
c.Assert(string(test), checker.Equals, cpContainerContents)
}
// Check that relative paths are relative to the container's rootfs
func (s *DockerSuite) TestCpRelativePath(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
hostFile, err := os.Create(cpFullPath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
@ -136,200 +107,151 @@ func (s *DockerSuite) TestCpRelativePath(c *check.C) {
// normally this is `filepath.Rel("/", cpFullPath)` but we cannot
// get this unix-path manipulation on windows with filepath.
relPath = cpFullPath[1:]
} else {
c.Fatalf("path %s was assumed to be an absolute path", cpFullPath)
}
c.Assert(path.IsAbs(cpFullPath), checker.True, check.Commentf("path %s was assumed to be an absolute path", cpFullPath))
dockerCmd(c, "cp", cleanedContainerID+":"+relPath, tmpdir)
dockerCmd(c, "cp", containerID+":"+relPath, tmpdir)
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if string(test) == cpHostContents {
c.Errorf("output matched host file -- relative path can escape container rootfs")
}
if string(test) != cpContainerContents {
c.Errorf("output doesn't match the input for relative path")
}
// output matched host file -- relative path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
// output doesn't match the input for relative path
c.Assert(string(test), checker.Equals, cpContainerContents)
}
// Check that absolute paths are relative to the container's rootfs
func (s *DockerSuite) TestCpAbsolutePath(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
hostFile, err := os.Create(cpFullPath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
path := cpFullPath
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if string(test) == cpHostContents {
c.Errorf("output matched host file -- absolute path can escape container rootfs")
}
if string(test) != cpContainerContents {
c.Errorf("output doesn't match the input for absolute path")
}
// output matched host file -- absolute path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
// output doesn't match the input for absolute path
c.Assert(string(test), checker.Equals, cpContainerContents)
}
// Test for #5619
// Check that absolute symlinks are still relative to the container's rootfs
func (s *DockerSuite) TestCpAbsoluteSymlink(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
hostFile, err := os.Create(cpFullPath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
tmpname := filepath.Join(tmpdir, "container_path")
defer os.RemoveAll(tmpdir)
path := path.Join("/", "container_path")
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
// We should have copied a symlink *NOT* the file itself!
linkTarget, err := os.Readlink(tmpname)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if linkTarget != filepath.FromSlash(cpFullPath) {
c.Errorf("symlink target was %q, but expected: %q", linkTarget, cpFullPath)
}
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpFullPath))
}
// Check that symlinks to a directory behave as expected when copying one from
// a container.
func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
testDir, err := ioutil.TempDir("", "test-cp-from-symlink-to-dir-")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(testDir)
// This copy command should copy the symlink, not the target, into the
// temporary directory.
dockerCmd(c, "cp", cleanedContainerID+":"+"/dir_link", testDir)
dockerCmd(c, "cp", containerID+":"+"/dir_link", testDir)
expectedPath := filepath.Join(testDir, "dir_link")
linkTarget, err := os.Readlink(expectedPath)
if err != nil {
c.Fatalf("unable to read symlink at %q: %v", expectedPath, err)
}
c.Assert(err, checker.IsNil)
if linkTarget != filepath.FromSlash(cpTestPathParent) {
c.Errorf("symlink target was %q, but expected: %q", linkTarget, cpTestPathParent)
}
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpTestPathParent))
os.Remove(expectedPath)
// This copy command should resolve the symlink (note the trailing
// separator), copying the target into the temporary directory.
dockerCmd(c, "cp", cleanedContainerID+":"+"/dir_link/", testDir)
dockerCmd(c, "cp", containerID+":"+"/dir_link/", testDir)
// It *should not* have copied the directory using the target's name, but
// used the given name instead.
unexpectedPath := filepath.Join(testDir, cpTestPathParent)
if stat, err := os.Lstat(unexpectedPath); err == nil {
c.Fatalf("target name was copied: %q - %q", stat.Mode(), stat.Name())
stat, err := os.Lstat(unexpectedPath)
if err == nil {
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
}
c.Assert(err, checker.NotNil, check.Commentf(out))
// It *should* have copied the directory using the asked name "dir_link".
stat, err := os.Lstat(expectedPath)
if err != nil {
c.Fatalf("unable to stat resource at %q: %v", expectedPath, err)
}
stat, err = os.Lstat(expectedPath)
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
if !stat.IsDir() {
c.Errorf("should have copied a directory but got %q instead", stat.Mode())
}
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
}
// Check that symlinks to a directory behave as expected when copying one to a
@ -339,65 +261,46 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) {
testRequires(c, SameHostDaemon) // Requires local volume mount bind.
testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(testVol)
// Create a test container with a local volume. We will test by copying
// to the volume path in the container which we can then verify locally.
out, exitCode := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
// Create a temp directory to hold a test file nested in a direcotry.
testDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(testDir)
// This file will be at "/testDir/some/path/test" and will be copied into
// the test volume later.
hostTestFilename := filepath.Join(testDir, cpFullPath)
if err := os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)); err != nil {
c.Fatal(err)
}
if err := ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)), checker.IsNil)
c.Assert(ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)), checker.IsNil)
// Now create another temp directory to hold a symlink to the
// "/testDir/some" directory.
linkDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(linkDir)
// Then symlink "/linkDir/dir_link" to "/testdir/some".
linkTarget := filepath.Join(testDir, cpTestPathParent)
localLink := filepath.Join(linkDir, "dir_link")
if err := os.Symlink(linkTarget, localLink); err != nil {
c.Fatal(err)
}
c.Assert(os.Symlink(linkTarget, localLink), checker.IsNil)
// Now copy that symlink into the test volume in the container.
dockerCmd(c, "cp", localLink, cleanedContainerID+":/testVol")
dockerCmd(c, "cp", localLink, containerID+":/testVol")
// This copy command should have copied the symlink *not* the target.
expectedPath := filepath.Join(testVol, "dir_link")
actualLinkTarget, err := os.Readlink(expectedPath)
if err != nil {
c.Fatalf("unable to read symlink at %q: %v", expectedPath, err)
}
c.Assert(err, checker.IsNil, check.Commentf("unable to read symlink at %q", expectedPath))
if actualLinkTarget != linkTarget {
c.Errorf("symlink target was %q, but expected: %q", actualLinkTarget, linkTarget)
}
c.Assert(actualLinkTarget, checker.Equals, linkTarget)
// Good, now remove that copied link for the next test.
os.Remove(expectedPath)
@ -405,62 +308,48 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) {
// This copy command should resolve the symlink (note the trailing
// separator), copying the target into the test volume directory in the
// container.
dockerCmd(c, "cp", localLink+"/", cleanedContainerID+":/testVol")
dockerCmd(c, "cp", localLink+"/", containerID+":/testVol")
// It *should not* have copied the directory using the target's name, but
// used the given name instead.
unexpectedPath := filepath.Join(testVol, cpTestPathParent)
if stat, err := os.Lstat(unexpectedPath); err == nil {
c.Fatalf("target name was copied: %q - %q", stat.Mode(), stat.Name())
stat, err := os.Lstat(unexpectedPath)
if err == nil {
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
}
c.Assert(err, checker.NotNil, check.Commentf(out))
// It *should* have copied the directory using the asked name "dir_link".
stat, err := os.Lstat(expectedPath)
if err != nil {
c.Fatalf("unable to stat resource at %q: %v", expectedPath, err)
}
stat, err = os.Lstat(expectedPath)
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
if !stat.IsDir() {
c.Errorf("should have copied a directory but got %q instead", stat.Mode())
}
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
// And this directory should contain the file copied from the host at the
// expected location: "/testVol/dir_link/path/test"
expectedFilepath := filepath.Join(testVol, "dir_link/path/test")
fileContents, err := ioutil.ReadFile(expectedFilepath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if string(fileContents) != cpHostContents {
c.Fatalf("file contains %q but expected %q", string(fileContents), cpHostContents)
}
c.Assert(string(fileContents), checker.Equals, cpHostContents)
}
// Test for #5619
// Check that symlinks which are part of the resource path are still relative to the container's rootfs
func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
c.Fatal(err)
}
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
hostFile, err := os.Create(cpFullPath)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
@ -468,33 +357,26 @@ func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) {
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
path := path.Join("/", "container_path", cpTestName)
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
if string(test) == cpHostContents {
c.Errorf("output matched host file -- symlink path component can escape container rootfs")
}
if string(test) != cpContainerContents {
c.Errorf("output doesn't match the input for symlink path component")
}
// output matched host file -- symlink path component can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
// output doesn't match the input for symlink path component
c.Assert(string(test), checker.Equals, cpContainerContents)
}
// Check that cp with unprivileged user doesn't return any error
@ -502,36 +384,25 @@ func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) {
testRequires(c, DaemonIsLinux)
testRequires(c, UnixCli) // uses chmod/su: not available on windows
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpdir)
if err = os.Chmod(tmpdir, 0777); err != nil {
c.Fatal(err)
}
c.Assert(os.Chmod(tmpdir, 0777), checker.IsNil)
path := cpTestName
_, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+cleanedContainerID+":"+path+" "+tmpdir))
if err != nil {
c.Fatalf("couldn't copy with unprivileged user: %s:%s %s", cleanedContainerID, path, err)
}
_, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+containerID+":"+path+" "+tmpdir))
c.Assert(err, checker.IsNil, check.Commentf("couldn't copy with unprivileged user: %s:%s", containerID, path))
}
func (s *DockerSuite) TestCpSpecialFiles(c *check.C) {
@ -539,53 +410,43 @@ func (s *DockerSuite) TestCpSpecialFiles(c *check.C) {
testRequires(c, SameHostDaemon)
outDir, err := ioutil.TempDir("", "cp-test-special-files")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(outDir)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
// Copy actual /etc/resolv.conf
dockerCmd(c, "cp", cleanedContainerID+":/etc/resolv.conf", outDir)
dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
expected, err := readContainerFile(cleanedContainerID, "resolv.conf")
expected, err := readContainerFile(containerID, "resolv.conf")
actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
if !bytes.Equal(actual, expected) {
c.Fatalf("Expected copied file to be duplicate of the container resolvconf")
}
// Expected copied file to be duplicate of the container resolvconf
c.Assert(bytes.Equal(actual, expected), checker.True)
// Copy actual /etc/hosts
dockerCmd(c, "cp", cleanedContainerID+":/etc/hosts", outDir)
dockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
expected, err = readContainerFile(cleanedContainerID, "hosts")
expected, err = readContainerFile(containerID, "hosts")
actual, err = ioutil.ReadFile(outDir + "/hosts")
if !bytes.Equal(actual, expected) {
c.Fatalf("Expected copied file to be duplicate of the container hosts")
}
// Expected copied file to be duplicate of the container hosts
c.Assert(bytes.Equal(actual, expected), checker.True)
// Copy actual /etc/resolv.conf
dockerCmd(c, "cp", cleanedContainerID+":/etc/hostname", outDir)
dockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
expected, err = readContainerFile(cleanedContainerID, "hostname")
expected, err = readContainerFile(containerID, "hostname")
actual, err = ioutil.ReadFile(outDir + "/hostname")
if !bytes.Equal(actual, expected) {
c.Fatalf("Expected copied file to be duplicate of the container resolvconf")
}
// Expected copied file to be duplicate of the container resolvconf
c.Assert(bytes.Equal(actual, expected), checker.True)
}
func (s *DockerSuite) TestCpVolumePath(c *check.C) {
@ -595,216 +456,148 @@ func (s *DockerSuite) TestCpVolumePath(c *check.C) {
testRequires(c, SameHostDaemon)
tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpDir)
outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(outDir)
_, err = os.Create(tmpDir + "/test")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
out, exitCode := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
// Copy actual volume path
dockerCmd(c, "cp", cleanedContainerID+":/foo", outDir)
dockerCmd(c, "cp", containerID+":/foo", outDir)
stat, err := os.Stat(outDir + "/foo")
if err != nil {
c.Fatal(err)
}
if !stat.IsDir() {
c.Fatal("expected copied content to be dir")
}
c.Assert(err, checker.IsNil)
// expected copied content to be dir
c.Assert(stat.IsDir(), checker.True)
stat, err = os.Stat(outDir + "/foo/bar")
if err != nil {
c.Fatal(err)
}
if stat.IsDir() {
c.Fatal("Expected file `bar` to be a file")
}
c.Assert(err, checker.IsNil)
// Expected file `bar` to be a file
c.Assert(stat.IsDir(), checker.False)
// Copy file nested in volume
dockerCmd(c, "cp", cleanedContainerID+":/foo/bar", outDir)
dockerCmd(c, "cp", containerID+":/foo/bar", outDir)
stat, err = os.Stat(outDir + "/bar")
if err != nil {
c.Fatal(err)
}
if stat.IsDir() {
c.Fatal("Expected file `bar` to be a file")
}
c.Assert(err, checker.IsNil)
// Expected file `bar` to be a file
c.Assert(stat.IsDir(), checker.False)
// Copy Bind-mounted dir
dockerCmd(c, "cp", cleanedContainerID+":/baz", outDir)
dockerCmd(c, "cp", containerID+":/baz", outDir)
stat, err = os.Stat(outDir + "/baz")
if err != nil {
c.Fatal(err)
}
if !stat.IsDir() {
c.Fatal("Expected `baz` to be a dir")
}
c.Assert(err, checker.IsNil)
// Expected `baz` to be a dir
c.Assert(stat.IsDir(), checker.True)
// Copy file nested in bind-mounted dir
dockerCmd(c, "cp", cleanedContainerID+":/baz/test", outDir)
dockerCmd(c, "cp", containerID+":/baz/test", outDir)
fb, err := ioutil.ReadFile(outDir + "/baz/test")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
fb2, err := ioutil.ReadFile(tmpDir + "/test")
if err != nil {
c.Fatal(err)
}
if !bytes.Equal(fb, fb2) {
c.Fatalf("Expected copied file to be duplicate of bind-mounted file")
}
c.Assert(err, checker.IsNil)
// Expected copied file to be duplicate of bind-mounted file
c.Assert(bytes.Equal(fb, fb2), checker.True)
// Copy bind-mounted file
dockerCmd(c, "cp", cleanedContainerID+":/test", outDir)
dockerCmd(c, "cp", containerID+":/test", outDir)
fb, err = ioutil.ReadFile(outDir + "/test")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
fb2, err = ioutil.ReadFile(tmpDir + "/test")
if err != nil {
c.Fatal(err)
}
if !bytes.Equal(fb, fb2) {
c.Fatalf("Expected copied file to be duplicate of bind-mounted file")
}
c.Assert(err, checker.IsNil)
// Expected copied file to be duplicate of bind-mounted file
c.Assert(bytes.Equal(fb, fb2), checker.True)
}
func (s *DockerSuite) TestCpToDot(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpdir)
cwd, err := os.Getwd()
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.Chdir(cwd)
if err := os.Chdir(tmpdir); err != nil {
c.Fatal(err)
}
dockerCmd(c, "cp", cleanedContainerID+":/test", ".")
c.Assert(os.Chdir(tmpdir), checker.IsNil)
dockerCmd(c, "cp", containerID+":/test", ".")
content, err := ioutil.ReadFile("./test")
if string(content) != "lololol\n" {
c.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
}
c.Assert(string(content), checker.Equals, "lololol\n")
}
func (s *DockerSuite) TestCpToStdout(c *check.C) {
testRequires(c, DaemonIsLinux)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
if exitCode != 0 {
c.Fatalf("failed to create a container:%s\n", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
cID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cID)
if strings.TrimSpace(out) != "0" {
c.Fatalf("failed to set up container:%s\n", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
out, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "cp", cID+":/test", "-"),
exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
exec.Command("tar", "-vtf", "-"))
if err != nil {
c.Fatalf("Failed to run commands: %s", err)
}
c.Assert(err, checker.IsNil)
if !strings.Contains(out, "test") || !strings.Contains(out, "-rw") {
c.Fatalf("Missing file from tar TOC:\n%s", out)
}
c.Assert(out, checker.Contains, "test")
c.Assert(out, checker.Contains, "-rw")
}
func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
testRequires(c, SameHostDaemon)
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
cleanedContainerID := strings.TrimSpace(out)
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
tmpdir, err := ioutil.TempDir("", "docker-integration")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpdir)
dockerCmd(c, "cp", cleanedContainerID+":/te:s:t", tmpdir)
dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
if string(content) != "lololol\n" {
c.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
}
c.Assert(string(content), checker.Equals, "lololol\n")
}
func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
testRequires(c, DaemonIsLinux)
expectedMsg := "hello"
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg)
id := strings.TrimSpace(string(out))
containerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", id)
status := strings.TrimSpace(out)
if status != "0" {
c.Fatalf("container exited with status %s", status)
}
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
if err != nil {
c.Fatalf("unable to make temporary directory: %s", err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpDir)
dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", id), tmpDir)
dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir)
out, _ = dockerCmd(c, "start", "-a", id)
out, _ = dockerCmd(c, "start", "-a", containerID)
msg := strings.TrimSpace(out)
if msg != expectedMsg {
c.Fatalf("expected %q but got %q", expectedMsg, msg)
}
c.Assert(strings.TrimSpace(out), checker.Equals, expectedMsg)
}
func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
@ -812,9 +605,7 @@ func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
c.Fatalf("unable to make temporary directory: %s", err)
}
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpDir)
dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
}

View File

@ -3,6 +3,7 @@ package main
import (
"os"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@ -23,31 +24,25 @@ import (
// Test for error when SRC does not exist.
func (s *DockerSuite) TestCpToErrSrcNotExists(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{})
tmpDir := getTestDir(c, "test-cp-to-err-src-not-exists")
defer os.RemoveAll(tmpDir)
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "file1")
dstPath := containerCpPath(containerID, "file1")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
}
// Test for error when SRC ends in a trailing
// path separator but it exists as a file.
func (s *DockerSuite) TestCpToErrSrcNotDir(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{})
tmpDir := getTestDir(c, "test-cp-to-err-src-not-dir")
defer os.RemoveAll(tmpDir)
@ -55,24 +50,19 @@ func (s *DockerSuite) TestCpToErrSrcNotDir(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcPath := cpPathTrailingSep(tmpDir, "file1")
dstPath := containerCpPath(cID, "testDir")
dstPath := containerCpPath(containerID, "testDir")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected IsNotDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotDir(err) {
c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
}
c.Assert(isCpNotDir(err), checker.True, check.Commentf("expected IsNotDir error, but got %T: %s", err, err))
}
// Test for error when SRC is a valid file or directory,
// bu the DST parent directory does not exist.
func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-to-err-dst-parent-not-exists")
defer os.RemoveAll(tmpDir)
@ -81,27 +71,19 @@ func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
// Try with a file source.
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "/notExists", "file1")
dstPath := containerCpPath(containerID, "/notExists", "file1")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
// Try with a directory source.
srcPath = cpPath(tmpDir, "dir1")
if err := runDockerCp(c, srcPath, dstPath); err == nil {
c.Fatal("expected IsNotExist error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpNotExist(err) {
c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpNotExist(err), checker.True, check.Commentf("expected IsNotExist error, but got %T: %s", err, err))
}
// Test for error when DST ends in a trailing path separator but exists as a
@ -109,8 +91,7 @@ func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
// non-directory and cannot overwrite an existing
func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{addContent: true})
defer deleteContainer(cID)
containerID := makeTestContainer(c, testContainerOptions{addContent: true})
tmpDir := getTestDir(c, "test-cp-to-err-dst-not-dir")
defer os.RemoveAll(tmpDir)
@ -119,19 +100,15 @@ func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
// Try with a file source.
srcPath := cpPath(tmpDir, "dir1/file1-1")
dstPath := containerCpPathTrailingSep(cID, "file1")
dstPath := containerCpPathTrailingSep(containerID, "file1")
// The client should encounter an error trying to stat the destination
// and then be unable to copy since the destination is asserted to be a
// directory but does not exist.
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected DirNotExist error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpDirNotExist(err) {
c.Fatalf("expected DirNotExist error, but got %T: %s", err, err)
}
c.Assert(isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExist error, but got %T: %s", err, err))
// Try with a directory source.
srcPath = cpPath(tmpDir, "dir1")
@ -141,13 +118,9 @@ func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
// name in the source archive, but this directory would overwrite the
// existing file with the same name.
err = runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected CannotOverwriteNonDirWithDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCannotOverwriteNonDirWithDir(err) {
c.Fatalf("expected CannotOverwriteNonDirWithDir error, but got %T: %s", err, err)
}
c.Assert(isCannotOverwriteNonDirWithDir(err), checker.True, check.Commentf("expected CannotOverwriteNonDirWithDir error, but got %T: %s", err, err))
}
// Check that copying from a local path to a symlink in a container copies to
@ -163,106 +136,75 @@ func (s *DockerSuite) TestCpToSymlinkDestination(c *check.C) {
makeTestContentInDir(c, testVol)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
})
defer deleteContainer(cID)
// First, copy a local file to a symlink to a file in the container. This
// should overwrite the symlink target contents with the source contents.
srcPath := cpPath(testVol, "file2")
dstPath := containerCpPath(cID, "/vol2/symlinkToFile1")
dstPath := containerCpPath(containerID, "/vol2/symlinkToFile1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(testVol, "file1"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(testVol, "file1"), "file2\n"), checker.IsNil)
// Next, copy a local file to a symlink to a directory in the container.
// This should copy the file into the symlink target directory.
dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(testVol, "file2"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(testVol, "file2"), "file2\n"), checker.IsNil)
// Next, copy a file to a symlink to a file that does not exist (a broken
// symlink) in the container. This should create the target file with the
// contents of the source file.
dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToFileX")
dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToFileX")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX"), checker.IsNil)
// The file should have the contents of "file2" now.
if err := fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n"), checker.IsNil)
// Next, copy a local directory to a symlink to a directory in the
// container. This should copy the directory into the symlink target
// directory and not modify the symlink.
srcPath = cpPath(testVol, "/dir2")
dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"), checker.IsNil)
// The directory should now contain a copy of "dir2".
if err := fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n"), checker.IsNil)
// Next, copy a local directory to a symlink to a local directory that does
// not exist (a broken symlink) in the container. This should create the
// target as a directory with the contents of the source directory. It
// should not modify the symlink.
dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToDirX")
dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToDirX")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// The symlink should not have been modified.
if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX"); err != nil {
c.Fatal(err)
}
c.Assert(symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX"), checker.IsNil)
// The "dirX" directory should now be a copy of "dir2".
if err := fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n"), checker.IsNil)
}
// Possibilities are reduced to the remaining 10 cases:
@ -286,10 +228,9 @@ func (s *DockerSuite) TestCpToSymlinkDestination(c *check.C) {
// contents of the source file into it.
func (s *DockerSuite) TestCpToCaseA(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-a")
defer os.RemoveAll(tmpDir)
@ -297,15 +238,11 @@ func (s *DockerSuite) TestCpToCaseA(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "/root/itWorks.txt")
dstPath := containerCpPath(containerID, "/root/itWorks.txt")
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
}
// B. SRC specifies a file and DST (with trailing path separator) doesn't
@ -313,10 +250,9 @@ func (s *DockerSuite) TestCpToCaseA(c *check.C) {
// create a directory when copying a single file.
func (s *DockerSuite) TestCpToCaseB(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("testDir/file1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-b")
defer os.RemoveAll(tmpDir)
@ -324,27 +260,22 @@ func (s *DockerSuite) TestCpToCaseB(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcPath := cpPath(tmpDir, "file1")
dstDir := containerCpPathTrailingSep(cID, "testDir")
dstDir := containerCpPathTrailingSep(containerID, "testDir")
err := runDockerCp(c, srcPath, dstDir)
if err == nil {
c.Fatal("expected DirNotExists error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpDirNotExist(err) {
c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
}
c.Assert(isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExists error, but got %T: %s", err, err))
}
// C. SRC specifies a file and DST exists as a file. This should overwrite
// the file at DST with the contents of the source file.
func (s *DockerSuite) TestCpToCaseC(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
command: makeCatFileCommand("file2"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-c")
defer os.RemoveAll(tmpDir)
@ -352,21 +283,15 @@ func (s *DockerSuite) TestCpToCaseC(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "/root/file2")
dstPath := containerCpPath(containerID, "/root/file2")
// Ensure the container's file starts with the original content.
if err := containerStartOutputEquals(c, cID, "file2\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file2\n"), checker.IsNil)
if err := runDockerCp(c, srcPath, dstPath); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstPath), checker.IsNil)
// Should now contain file1's contents.
if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
}
// D. SRC specifies a file and DST exists as a directory. This should place
@ -374,11 +299,10 @@ func (s *DockerSuite) TestCpToCaseC(c *check.C) {
// this works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpToCaseD(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true,
command: makeCatFileCommand("/dir1/file1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-d")
defer os.RemoveAll(tmpDir)
@ -386,46 +310,33 @@ func (s *DockerSuite) TestCpToCaseD(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcPath := cpPath(tmpDir, "file1")
dstDir := containerCpPath(cID, "dir1")
dstDir := containerCpPath(containerID, "dir1")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcPath, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstDir), checker.IsNil)
// Should now contain file1's contents.
if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
// Make new destination container.
cID = makeTestContainer(c, testContainerOptions{
containerID = makeTestContainer(c, testContainerOptions{
addContent: true,
command: makeCatFileCommand("/dir1/file1"),
})
defer deleteContainer(cID)
dstDir = containerCpPathTrailingSep(cID, "dir1")
dstDir = containerCpPathTrailingSep(containerID, "dir1")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcPath, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcPath, dstDir), checker.IsNil)
// Should now contain file1's contents.
if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
}
// E. SRC specifies a directory and DST does not exist. This should create a
@ -434,10 +345,9 @@ func (s *DockerSuite) TestCpToCaseD(c *check.C) {
// not.
func (s *DockerSuite) TestCpToCaseE(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("/testDir/file1-1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-e")
defer os.RemoveAll(tmpDir)
@ -445,46 +355,35 @@ func (s *DockerSuite) TestCpToCaseE(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPath(tmpDir, "dir1")
dstDir := containerCpPath(cID, "testDir")
dstDir := containerCpPath(containerID, "testDir")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
// Make new destination container.
cID = makeTestContainer(c, testContainerOptions{
containerID = makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("/testDir/file1-1"),
})
defer deleteContainer(cID)
dstDir = containerCpPathTrailingSep(cID, "testDir")
dstDir = containerCpPathTrailingSep(containerID, "testDir")
err := runDockerCp(c, srcDir, dstDir)
if err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
}
// F. SRC specifies a directory and DST exists as a file. This should cause an
// error as it is not possible to overwrite a file with a directory.
func (s *DockerSuite) TestCpToCaseF(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-f")
defer os.RemoveAll(tmpDir)
@ -492,16 +391,12 @@ func (s *DockerSuite) TestCpToCaseF(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPath(tmpDir, "dir1")
dstFile := containerCpPath(cID, "/root/file1")
dstFile := containerCpPath(containerID, "/root/file1")
err := runDockerCp(c, srcDir, dstFile)
if err == nil {
c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyDir(err) {
c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
}
// G. SRC specifies a directory and DST exists as a directory. This should copy
@ -509,11 +404,10 @@ func (s *DockerSuite) TestCpToCaseF(c *check.C) {
// works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpToCaseG(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
command: makeCatFileCommand("dir2/dir1/file1-1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-g")
defer os.RemoveAll(tmpDir)
@ -521,46 +415,33 @@ func (s *DockerSuite) TestCpToCaseG(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPath(tmpDir, "dir1")
dstDir := containerCpPath(cID, "/root/dir2")
dstDir := containerCpPath(containerID, "/root/dir2")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
// Make new destination container.
cID = makeTestContainer(c, testContainerOptions{
containerID = makeTestContainer(c, testContainerOptions{
addContent: true,
command: makeCatFileCommand("/dir2/dir1/file1-1"),
})
defer deleteContainer(cID)
dstDir = containerCpPathTrailingSep(cID, "/dir2")
dstDir = containerCpPathTrailingSep(containerID, "/dir2")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
}
// H. SRC specifies a directory's contents only and DST does not exist. This
@ -569,10 +450,9 @@ func (s *DockerSuite) TestCpToCaseG(c *check.C) {
// this works whether DST has a trailing path separator or not.
func (s *DockerSuite) TestCpToCaseH(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("/testDir/file1-1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-h")
defer os.RemoveAll(tmpDir)
@ -580,35 +460,26 @@ func (s *DockerSuite) TestCpToCaseH(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
dstDir := containerCpPath(cID, "testDir")
dstDir := containerCpPath(containerID, "testDir")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
// Make new destination container.
cID = makeTestContainer(c, testContainerOptions{
containerID = makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("/testDir/file1-1"),
})
defer deleteContainer(cID)
dstDir = containerCpPathTrailingSep(cID, "testDir")
dstDir = containerCpPathTrailingSep(containerID, "testDir")
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
}
// I. SRC specifies a directory's contents only and DST exists as a file. This
@ -616,10 +487,9 @@ func (s *DockerSuite) TestCpToCaseH(c *check.C) {
// directory.
func (s *DockerSuite) TestCpToCaseI(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-i")
defer os.RemoveAll(tmpDir)
@ -627,16 +497,12 @@ func (s *DockerSuite) TestCpToCaseI(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
dstFile := containerCpPath(cID, "/root/file1")
dstFile := containerCpPath(containerID, "/root/file1")
err := runDockerCp(c, srcDir, dstFile)
if err == nil {
c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyDir(err) {
c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
}
// J. SRC specifies a directory's contents only and DST exists as a directory.
@ -645,11 +511,10 @@ func (s *DockerSuite) TestCpToCaseI(c *check.C) {
// trailing path separator or not.
func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
testRequires(c, DaemonIsLinux)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
addContent: true, workDir: "/root",
command: makeCatFileCommand("/dir2/file1-1"),
})
defer deleteContainer(cID)
tmpDir := getTestDir(c, "test-cp-to-case-j")
defer os.RemoveAll(tmpDir)
@ -657,45 +522,32 @@ func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
makeTestContentInDir(c, tmpDir)
srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
dstDir := containerCpPath(cID, "/dir2")
dstDir := containerCpPath(containerID, "/dir2")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
// Now try again but using a trailing path separator for dstDir.
// Make new destination container.
cID = makeTestContainer(c, testContainerOptions{
containerID = makeTestContainer(c, testContainerOptions{
command: makeCatFileCommand("/dir2/file1-1"),
})
defer deleteContainer(cID)
dstDir = containerCpPathTrailingSep(cID, "/dir2")
dstDir = containerCpPathTrailingSep(containerID, "/dir2")
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
if err := runDockerCp(c, srcDir, dstDir); err != nil {
c.Fatalf("unexpected error %T: %s", err, err)
}
c.Assert(runDockerCp(c, srcDir, dstDir), checker.IsNil)
// Should now contain file1-1's contents.
if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
}
// The `docker cp` command should also ensure that you cannot
@ -708,28 +560,21 @@ func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
makeTestContentInDir(c, tmpDir)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
readOnly: true, workDir: "/root",
command: makeCatFileCommand("shouldNotExist"),
})
defer deleteContainer(cID)
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "/root/shouldNotExist")
dstPath := containerCpPath(containerID, "/root/shouldNotExist")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected ErrContainerRootfsReadonly error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyReadOnly(err) {
c.Fatalf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err))
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
}
// The `docker cp` command should also ensure that you
@ -742,26 +587,19 @@ func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
makeTestContentInDir(c, tmpDir)
cID := makeTestContainer(c, testContainerOptions{
containerID := makeTestContainer(c, testContainerOptions{
volumes: defaultVolumes(tmpDir), workDir: "/root",
command: makeCatFileCommand("/vol_ro/shouldNotExist"),
})
defer deleteContainer(cID)
srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(cID, "/vol_ro/shouldNotExist")
dstPath := containerCpPath(containerID, "/vol_ro/shouldNotExist")
err := runDockerCp(c, srcPath, dstPath)
if err == nil {
c.Fatal("expected ErrVolumeReadonly error, but got nil instead")
}
c.Assert(err, checker.NotNil)
if !isCpCannotCopyReadOnly(err) {
c.Fatalf("expected ErrVolumeReadonly error, but got %T: %s", err, err)
}
c.Assert(isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrVolumeReadonly error, but got %T: %s", err, err))
// Ensure that dstPath doesn't exist.
if err := containerStartOutputEquals(c, cID, ""); err != nil {
c.Fatal(err)
}
c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
}

View File

@ -10,6 +10,7 @@ import (
"strings"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@ -90,17 +91,11 @@ func makeTestContentInDir(c *check.C, dir string) {
path := filepath.Join(dir, filepath.FromSlash(fd.path))
switch fd.filetype {
case ftRegular:
if err := ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)); err != nil {
c.Fatal(err)
}
c.Assert(ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)), checker.IsNil)
case ftDir:
if err := os.Mkdir(path, os.FileMode(0777)); err != nil {
c.Fatal(err)
}
c.Assert(os.Mkdir(path, os.FileMode(0777)), checker.IsNil)
case ftSymlink:
if err := os.Symlink(fd.contents, path); err != nil {
c.Fatal(err)
}
c.Assert(os.Symlink(fd.contents, path), checker.IsNil)
}
}
}
@ -143,25 +138,17 @@ func makeTestContainer(c *check.C, options testContainerOptions) (containerID st
args = append(args, "busybox", "/bin/sh", "-c", options.command)
out, status := dockerCmd(c, args...)
if status != 0 {
c.Fatalf("failed to run container, status %d: %s", status, out)
}
out, _ := dockerCmd(c, args...)
containerID = strings.TrimSpace(out)
out, status = dockerCmd(c, "wait", containerID)
if status != 0 {
c.Fatalf("failed to wait for test container container, status %d: %s", status, out)
}
out, _ = dockerCmd(c, "wait", containerID)
if exitCode := strings.TrimSpace(out); exitCode != "0" {
logs, status := dockerCmd(c, "logs", containerID)
if status != 0 {
logs = "UNABLE TO GET LOGS"
}
c.Fatalf("failed to make test container, exit code (%s): %s", exitCode, logs)
exitCode := strings.TrimSpace(out)
if exitCode != "0" {
out, _ = dockerCmd(c, "logs", containerID)
}
c.Assert(exitCode, checker.Equals, "0", check.Commentf("failed to make test container: %s", out))
return
}
@ -204,10 +191,10 @@ func runDockerCp(c *check.C, src, dst string) (err error) {
return
}
func startContainerGetOutput(c *check.C, cID string) (out string, err error) {
c.Logf("running `docker start -a %s`", cID)
func startContainerGetOutput(c *check.C, containerID string) (out string, err error) {
c.Logf("running `docker start -a %s`", containerID)
args := []string{"start", "-a", cID}
args := []string{"start", "-a", containerID}
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, args...))
if err != nil {
@ -220,9 +207,9 @@ func startContainerGetOutput(c *check.C, cID string) (out string, err error) {
func getTestDir(c *check.C, label string) (tmpDir string) {
var err error
if tmpDir, err = ioutil.TempDir("", label); err != nil {
c.Fatalf("unable to make temporary directory: %s", err)
}
tmpDir, err = ioutil.TempDir("", label)
// unable to make temporary directory
c.Assert(err, checker.IsNil)
return
}
@ -276,22 +263,22 @@ func symlinkTargetEquals(c *check.C, symlink, expectedTarget string) (err error)
actualTarget, err := os.Readlink(symlink)
if err != nil {
return err
return
}
if actualTarget != expectedTarget {
return fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
err = fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
}
return nil
return
}
func containerStartOutputEquals(c *check.C, cID, contents string) (err error) {
c.Logf("checking that container %q start output contains %q\n", cID, contents)
func containerStartOutputEquals(c *check.C, containerID, contents string) (err error) {
c.Logf("checking that container %q start output contains %q\n", containerID, contents)
out, err := startContainerGetOutput(c, cID)
out, err := startContainerGetOutput(c, containerID)
if err != nil {
return err
return
}
if out != contents {