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

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