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

View File

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

View File

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

View File

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