79ca77f3 |
package main
import ( |
ef98fe07 |
"bytes" |
79ca77f3 |
"fmt"
"io/ioutil"
"os" |
5eef0a28 |
"os/exec" |
c5b312dc |
"path" |
79ca77f3 |
"path/filepath" |
f3d96e81 |
"strings" |
dc944ea7 |
|
f26a31e8 |
"github.com/docker/docker/pkg/integration/checker" |
dc944ea7 |
"github.com/go-check/check" |
79ca77f3 |
)
const (
cpTestPathParent = "/some"
cpTestPath = "/some/path"
cpTestName = "test"
cpFullPath = "/some/path/test"
cpContainerContents = "holla, i am the container"
cpHostContents = "hello, i am the host"
)
|
418135e7 |
// Ensure that an all-local path case returns an error.
func (s *DockerSuite) TestCpLocalOnly(c *check.C) {
err := runDockerCp(c, "foo", "bar") |
f26a31e8 |
c.Assert(err, checker.NotNil) |
418135e7 |
|
f26a31e8 |
c.Assert(err.Error(), checker.Contains, "must specify at least one container source") |
418135e7 |
}
|
79ca77f3 |
// Test for #5656
// Check that garbage paths don't escape the container's rootfs |
dc944ea7 |
func (s *DockerSuite) TestCpGarbagePath(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath) |
79ca77f3 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
79ca77f3 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
79ca77f3 |
|
f26a31e8 |
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil) |
79ca77f3 |
hostFile, err := os.Create(cpFullPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
|
c5b312dc |
path := path.Join("../../../../../../../../../../../../", cpFullPath) |
79ca77f3 |
|
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+path, tmpdir) |
79ca77f3 |
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
|
f26a31e8 |
// output matched host file -- garbage path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents) |
79ca77f3 |
|
f26a31e8 |
// output doesn't match the input for garbage path
c.Assert(string(test), checker.Equals, cpContainerContents) |
79ca77f3 |
}
// Check that relative paths are relative to the container's rootfs |
dc944ea7 |
func (s *DockerSuite) TestCpRelativePath(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath) |
79ca77f3 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
79ca77f3 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
79ca77f3 |
|
f26a31e8 |
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil) |
79ca77f3 |
hostFile, err := os.Create(cpFullPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
|
c5b312dc |
var relPath string
if path.IsAbs(cpFullPath) {
// normally this is `filepath.Rel("/", cpFullPath)` but we cannot
// get this unix-path manipulation on windows with filepath.
relPath = cpFullPath[1:]
} |
f26a31e8 |
c.Assert(path.IsAbs(cpFullPath), checker.True, check.Commentf("path %s was assumed to be an absolute path", cpFullPath)) |
79ca77f3 |
|
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+relPath, tmpdir) |
79ca77f3 |
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
|
f26a31e8 |
// output matched host file -- relative path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents) |
79ca77f3 |
|
f26a31e8 |
// output doesn't match the input for relative path
c.Assert(string(test), checker.Equals, cpContainerContents) |
79ca77f3 |
}
// Check that absolute paths are relative to the container's rootfs |
dc944ea7 |
func (s *DockerSuite) TestCpAbsolutePath(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath) |
79ca77f3 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
79ca77f3 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
79ca77f3 |
|
f26a31e8 |
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil) |
79ca77f3 |
hostFile, err := os.Create(cpFullPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
path := cpFullPath
|
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+path, tmpdir) |
79ca77f3 |
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
79ca77f3 |
|
f26a31e8 |
// output matched host file -- absolute path can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents) |
79ca77f3 |
|
f26a31e8 |
// output doesn't match the input for absolute path
c.Assert(string(test), checker.Equals, cpContainerContents) |
79ca77f3 |
} |
5eef0a28 |
|
ff24a328 |
// Test for #5619
// Check that absolute symlinks are still relative to the container's rootfs |
dc944ea7 |
func (s *DockerSuite) TestCpAbsoluteSymlink(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path") |
ff24a328 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
ff24a328 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
ff24a328 |
|
f26a31e8 |
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil) |
ff24a328 |
hostFile, err := os.Create(cpFullPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
|
75f6929b |
tmpname := filepath.Join(tmpdir, "container_path") |
ff24a328 |
defer os.RemoveAll(tmpdir)
|
c5b312dc |
path := path.Join("/", "container_path") |
ff24a328 |
|
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+path, tmpdir) |
ff24a328 |
|
75f6929b |
// We should have copied a symlink *NOT* the file itself!
linkTarget, err := os.Readlink(tmpname) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
|
f26a31e8 |
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpFullPath)) |
75f6929b |
}
// Check that symlinks to a directory behave as expected when copying one from
// a container.
func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link") |
75f6929b |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
75f6929b |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
75f6929b |
testDir, err := ioutil.TempDir("", "test-cp-from-symlink-to-dir-") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
75f6929b |
defer os.RemoveAll(testDir) |
ff24a328 |
|
75f6929b |
// This copy command should copy the symlink, not the target, into the
// temporary directory. |
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+"/dir_link", testDir) |
75f6929b |
expectedPath := filepath.Join(testDir, "dir_link")
linkTarget, err := os.Readlink(expectedPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
|
f26a31e8 |
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpTestPathParent)) |
75f6929b |
os.Remove(expectedPath)
// This copy command should resolve the symlink (note the trailing |
51462327 |
// separator), copying the target into the temporary directory. |
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+"/dir_link/", testDir) |
75f6929b |
// It *should not* have copied the directory using the target's name, but
// used the given name instead.
unexpectedPath := filepath.Join(testDir, cpTestPathParent) |
f26a31e8 |
stat, err := os.Lstat(unexpectedPath)
if err == nil {
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name()) |
75f6929b |
} |
f26a31e8 |
c.Assert(err, checker.NotNil, check.Commentf(out)) |
75f6929b |
// It *should* have copied the directory using the asked name "dir_link". |
f26a31e8 |
stat, err = os.Lstat(expectedPath)
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath)) |
75f6929b |
|
f26a31e8 |
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode())) |
75f6929b |
}
// Check that symlinks to a directory behave as expected when copying one to a
// container.
func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
75f6929b |
testRequires(c, SameHostDaemon) // Requires local volume mount bind.
testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
75f6929b |
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. |
f26a31e8 |
out, _ := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox") |
75f6929b |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
75f6929b |
// Create a temp directory to hold a test file nested in a direcotry.
testDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
75f6929b |
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) |
f26a31e8 |
c.Assert(os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)), checker.IsNil)
c.Assert(ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)), checker.IsNil) |
75f6929b |
// Now create another temp directory to hold a symlink to the
// "/testDir/some" directory.
linkDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
75f6929b |
defer os.RemoveAll(linkDir)
// Then symlink "/linkDir/dir_link" to "/testdir/some".
linkTarget := filepath.Join(testDir, cpTestPathParent)
localLink := filepath.Join(linkDir, "dir_link") |
f26a31e8 |
c.Assert(os.Symlink(linkTarget, localLink), checker.IsNil) |
ff24a328 |
|
75f6929b |
// Now copy that symlink into the test volume in the container. |
f26a31e8 |
dockerCmd(c, "cp", localLink, containerID+":/testVol") |
75f6929b |
// This copy command should have copied the symlink *not* the target.
expectedPath := filepath.Join(testVol, "dir_link")
actualLinkTarget, err := os.Readlink(expectedPath) |
f26a31e8 |
c.Assert(err, checker.IsNil, check.Commentf("unable to read symlink at %q", expectedPath)) |
75f6929b |
|
f26a31e8 |
c.Assert(actualLinkTarget, checker.Equals, linkTarget) |
75f6929b |
// Good, now remove that copied link for the next test.
os.Remove(expectedPath)
// This copy command should resolve the symlink (note the trailing |
51462327 |
// separator), copying the target into the test volume directory in the |
75f6929b |
// container. |
f26a31e8 |
dockerCmd(c, "cp", localLink+"/", containerID+":/testVol") |
75f6929b |
// It *should not* have copied the directory using the target's name, but
// used the given name instead.
unexpectedPath := filepath.Join(testVol, cpTestPathParent) |
f26a31e8 |
stat, err := os.Lstat(unexpectedPath)
if err == nil {
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name()) |
75f6929b |
} |
f26a31e8 |
c.Assert(err, checker.NotNil, check.Commentf(out)) |
75f6929b |
// It *should* have copied the directory using the asked name "dir_link". |
f26a31e8 |
stat, err = os.Lstat(expectedPath)
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath)) |
75f6929b |
|
f26a31e8 |
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode())) |
75f6929b |
// 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) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
75f6929b |
|
f26a31e8 |
c.Assert(string(fileContents), checker.Equals, cpHostContents) |
ff24a328 |
}
// Test for #5619
// Check that symlinks which are part of the resource path are still relative to the container's rootfs |
dc944ea7 |
func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path") |
ff24a328 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
ff24a328 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
ff24a328 |
|
f26a31e8 |
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil) |
ff24a328 |
hostFile, err := os.Create(cpFullPath) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
defer hostFile.Close()
defer os.RemoveAll(cpTestPathParent)
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
tmpname := filepath.Join(tmpdir, cpTestName)
defer os.RemoveAll(tmpdir)
|
c5b312dc |
path := path.Join("/", "container_path", cpTestName) |
ff24a328 |
|
f26a31e8 |
dockerCmd(c, "cp", containerID+":"+path, tmpdir) |
ff24a328 |
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file) |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ff24a328 |
|
f26a31e8 |
// output matched host file -- symlink path component can escape container rootfs
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents) |
ff24a328 |
|
f26a31e8 |
// output doesn't match the input for symlink path component
c.Assert(string(test), checker.Equals, cpContainerContents) |
ff24a328 |
}
|
5eef0a28 |
// Check that cp with unprivileged user doesn't return any error |
dc944ea7 |
func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
dc944ea7 |
testRequires(c, UnixCli) // uses chmod/su: not available on windows |
492a58f0 |
|
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName) |
5eef0a28 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
5eef0a28 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
5eef0a28 |
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
5eef0a28 |
defer os.RemoveAll(tmpdir)
|
f26a31e8 |
c.Assert(os.Chmod(tmpdir, 0777), checker.IsNil) |
5eef0a28 |
path := cpTestName
|
f26a31e8 |
_, _, 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)) |
5eef0a28 |
} |
ef98fe07 |
|
dc944ea7 |
func (s *DockerSuite) TestCpSpecialFiles(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
dc944ea7 |
testRequires(c, SameHostDaemon) |
8bc330d8 |
outDir, err := ioutil.TempDir("", "cp-test-special-files") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
8bc330d8 |
defer os.RemoveAll(outDir)
|
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo") |
8bc330d8 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
8bc330d8 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
8bc330d8 |
// Copy actual /etc/resolv.conf |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir) |
8bc330d8 |
|
f26a31e8 |
expected, err := readContainerFile(containerID, "resolv.conf") |
8bc330d8 |
actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
|
f26a31e8 |
// Expected copied file to be duplicate of the container resolvconf
c.Assert(bytes.Equal(actual, expected), checker.True) |
8bc330d8 |
// Copy actual /etc/hosts |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/etc/hosts", outDir) |
8bc330d8 |
|
f26a31e8 |
expected, err = readContainerFile(containerID, "hosts") |
8bc330d8 |
actual, err = ioutil.ReadFile(outDir + "/hosts")
|
f26a31e8 |
// Expected copied file to be duplicate of the container hosts
c.Assert(bytes.Equal(actual, expected), checker.True) |
8bc330d8 |
// Copy actual /etc/resolv.conf |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/etc/hostname", outDir) |
8bc330d8 |
|
f26a31e8 |
expected, err = readContainerFile(containerID, "hostname") |
8bc330d8 |
actual, err = ioutil.ReadFile(outDir + "/hostname")
|
f26a31e8 |
// Expected copied file to be duplicate of the container resolvconf
c.Assert(bytes.Equal(actual, expected), checker.True) |
8bc330d8 |
}
|
dc944ea7 |
func (s *DockerSuite) TestCpVolumePath(c *check.C) { |
ea3afdad |
// stat /tmp/cp-test-volumepath851508420/test gets permission denied for the user
testRequires(c, NotUserNamespace) |
f9a3558a |
testRequires(c, DaemonIsLinux) |
dc944ea7 |
testRequires(c, SameHostDaemon) |
b686b65c |
|
ef98fe07 |
tmpDir, err := ioutil.TempDir("", "cp-test-volumepath") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ef98fe07 |
defer os.RemoveAll(tmpDir)
outDir, err := ioutil.TempDir("", "cp-test-volumepath-out") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ef98fe07 |
defer os.RemoveAll(outDir)
_, err = os.Create(tmpDir + "/test") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ef98fe07 |
|
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar") |
ef98fe07 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
ef98fe07 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
ef98fe07 |
// Copy actual volume path |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/foo", outDir) |
621b601b |
|
ef98fe07 |
stat, err := os.Stat(outDir + "/foo") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// expected copied content to be dir
c.Assert(stat.IsDir(), checker.True) |
ef98fe07 |
stat, err = os.Stat(outDir + "/foo/bar") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// Expected file `bar` to be a file
c.Assert(stat.IsDir(), checker.False) |
ef98fe07 |
// Copy file nested in volume |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/foo/bar", outDir) |
621b601b |
|
ef98fe07 |
stat, err = os.Stat(outDir + "/bar") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// Expected file `bar` to be a file
c.Assert(stat.IsDir(), checker.False) |
ef98fe07 |
// Copy Bind-mounted dir |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/baz", outDir) |
ef98fe07 |
stat, err = os.Stat(outDir + "/baz") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// Expected `baz` to be a dir
c.Assert(stat.IsDir(), checker.True) |
ef98fe07 |
// Copy file nested in bind-mounted dir |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/baz/test", outDir) |
ef98fe07 |
fb, err := ioutil.ReadFile(outDir + "/baz/test") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ef98fe07 |
fb2, err := ioutil.ReadFile(tmpDir + "/test") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// Expected copied file to be duplicate of bind-mounted file
c.Assert(bytes.Equal(fb, fb2), checker.True) |
ef98fe07 |
// Copy bind-mounted file |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/test", outDir) |
ef98fe07 |
fb, err = ioutil.ReadFile(outDir + "/test") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
ef98fe07 |
fb2, err = ioutil.ReadFile(tmpDir + "/test") |
f26a31e8 |
c.Assert(err, checker.IsNil)
// Expected copied file to be duplicate of bind-mounted file
c.Assert(bytes.Equal(fb, fb2), checker.True) |
ef98fe07 |
} |
be5bfbe2 |
|
dc944ea7 |
func (s *DockerSuite) TestCpToDot(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test") |
be5bfbe2 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
be5bfbe2 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
be5bfbe2 |
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
be5bfbe2 |
defer os.RemoveAll(tmpdir)
cwd, err := os.Getwd() |
f26a31e8 |
c.Assert(err, checker.IsNil) |
be5bfbe2 |
defer os.Chdir(cwd) |
f26a31e8 |
c.Assert(os.Chdir(tmpdir), checker.IsNil)
dockerCmd(c, "cp", containerID+":/test", ".") |
be5bfbe2 |
content, err := ioutil.ReadFile("./test") |
f26a31e8 |
c.Assert(string(content), checker.Equals, "lololol\n") |
be5bfbe2 |
} |
f3d96e81 |
|
dc944ea7 |
func (s *DockerSuite) TestCpToStdout(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test") |
f3d96e81 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
f3d96e81 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
f3d96e81 |
|
621b601b |
out, _, err := runCommandPipelineWithOutput( |
f26a31e8 |
exec.Command(dockerBinary, "cp", containerID+":/test", "-"), |
f3d96e81 |
exec.Command("tar", "-vtf", "-")) |
621b601b |
|
f26a31e8 |
c.Assert(err, checker.IsNil) |
f3d96e81 |
|
f26a31e8 |
c.Assert(out, checker.Contains, "test")
c.Assert(out, checker.Contains, "-rw") |
f3d96e81 |
} |
50372973 |
|
dc944ea7 |
func (s *DockerSuite) TestCpNameHasColon(c *check.C) { |
25c38339 |
testRequires(c, SameHostDaemon, DaemonIsLinux) |
50372973 |
|
f26a31e8 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t") |
50372973 |
|
f26a31e8 |
containerID := strings.TrimSpace(out) |
50372973 |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
50372973 |
tmpdir, err := ioutil.TempDir("", "docker-integration") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
50372973 |
defer os.RemoveAll(tmpdir) |
f26a31e8 |
dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir) |
50372973 |
content, err := ioutil.ReadFile(tmpdir + "/te:s:t") |
f26a31e8 |
c.Assert(string(content), checker.Equals, "lololol\n") |
50372973 |
} |
04f99a6c |
func (s *DockerSuite) TestCopyAndRestart(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
04f99a6c |
expectedMsg := "hello" |
5c295460 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg) |
f26a31e8 |
containerID := strings.TrimSpace(out) |
04f99a6c |
|
f26a31e8 |
out, _ = dockerCmd(c, "wait", containerID)
// failed to set up container
c.Assert(strings.TrimSpace(out), checker.Equals, "0") |
04f99a6c |
tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
04f99a6c |
defer os.RemoveAll(tmpDir)
|
f26a31e8 |
dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir) |
04f99a6c |
|
f26a31e8 |
out, _ = dockerCmd(c, "start", "-a", containerID) |
04f99a6c |
|
f26a31e8 |
c.Assert(strings.TrimSpace(out), checker.Equals, expectedMsg) |
04f99a6c |
} |
289ee90b |
func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
5c295460 |
dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox") |
289ee90b |
tmpDir, err := ioutil.TempDir("", "test") |
f26a31e8 |
c.Assert(err, checker.IsNil) |
289ee90b |
defer os.RemoveAll(tmpDir) |
5c295460 |
dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir) |
289ee90b |
} |
92600bde |
// test copy with option `-L`: following symbol link
// Check that symlinks to a file behave as expected when copying one from
// a container to host following symbol link
func (s *DockerSuite) TestCpSymlinkFromConToHostFollowSymlink(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+" /dir_link")
if exitCode != 0 {
c.Fatal("failed to create a container", out)
}
cleanedContainerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out)
}
testDir, err := ioutil.TempDir("", "test-cp-symlink-container-to-host-follow-symlink")
if err != nil {
c.Fatal(err)
}
defer os.RemoveAll(testDir)
// This copy command should copy the symlink, not the target, into the
// temporary directory.
dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", testDir)
expectedPath := filepath.Join(testDir, "dir_link")
expected := []byte(cpContainerContents)
actual, err := ioutil.ReadFile(expectedPath)
if !bytes.Equal(actual, expected) {
c.Fatalf("Expected copied file to be duplicate of the container symbol link target")
}
os.Remove(expectedPath)
// now test copy symbol link to an non-existing file in host
expectedPath = filepath.Join(testDir, "somefile_host")
// expectedPath shouldn't exist, if exists, remove it
if _, err := os.Lstat(expectedPath); err == nil {
os.Remove(expectedPath)
}
dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", expectedPath)
actual, err = ioutil.ReadFile(expectedPath)
if !bytes.Equal(actual, expected) {
c.Fatalf("Expected copied file to be duplicate of the container symbol link target")
}
defer os.Remove(expectedPath)
} |