2014-05-10 12:51:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-24 13:07:11 +00:00
|
|
|
"bytes"
|
2014-05-10 12:51:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-05-20 23:34:48 +00:00
|
|
|
"os/exec"
|
2015-02-18 09:55:08 +00:00
|
|
|
"path"
|
2014-05-10 12:51:45 +00:00
|
|
|
"path/filepath"
|
2015-03-05 16:39:32 +00:00
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-05-10 12:51:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cpTestPathParent = "/some"
|
|
|
|
cpTestPath = "/some/path"
|
|
|
|
cpTestName = "test"
|
|
|
|
cpFullPath = "/some/path/test"
|
|
|
|
|
|
|
|
cpContainerContents = "holla, i am the container"
|
|
|
|
cpHostContents = "hello, i am the host"
|
|
|
|
)
|
|
|
|
|
2015-05-14 20:44:29 +00:00
|
|
|
// Ensure that an all-local path case returns an error.
|
|
|
|
func (s *DockerSuite) TestCpLocalOnly(c *check.C) {
|
2016-11-14 13:37:08 +00:00
|
|
|
err := runDockerCp(c, "foo", "bar", nil)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.NotNil)
|
2015-05-14 20:44:29 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err.Error(), checker.Contains, "must specify at least one container source")
|
2015-05-14 20:44:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 12:51:45 +00:00
|
|
|
// Test for #5656
|
|
|
|
// Check that garbage paths don't escape the container's rootfs
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpGarbagePath(c *check.C) {
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 09:55:08 +00:00
|
|
|
path := path.Join("../../../../../../../../../../../../", cpFullPath)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output matched host file -- garbage path can escape container rootfs
|
|
|
|
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output doesn't match the input for garbage path
|
|
|
|
c.Assert(string(test), checker.Equals, cpContainerContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that relative paths are relative to the container's rootfs
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpRelativePath(c *check.C) {
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 09:55:08 +00:00
|
|
|
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:]
|
|
|
|
}
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(path.IsAbs(cpFullPath), checker.True, check.Commentf("path %s was assumed to be an absolute path", cpFullPath))
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+relPath, tmpdir)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output matched host file -- relative path can escape container rootfs
|
|
|
|
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output doesn't match the input for relative path
|
|
|
|
c.Assert(string(test), checker.Equals, cpContainerContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that absolute paths are relative to the container's rootfs
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpAbsolutePath(c *check.C) {
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
path := cpFullPath
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output matched host file -- absolute path can escape container rootfs
|
|
|
|
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output doesn't match the input for absolute path
|
|
|
|
c.Assert(string(test), checker.Equals, cpContainerContents)
|
2014-05-10 12:51:45 +00:00
|
|
|
}
|
2014-05-20 23:34:48 +00:00
|
|
|
|
2014-05-23 12:42:46 +00:00
|
|
|
// Test for #5619
|
|
|
|
// Check that absolute symlinks are still relative to the container's rootfs
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpAbsoluteSymlink(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-07-24 21:12:55 +00:00
|
|
|
tmpname := filepath.Join(tmpdir, "container_path")
|
2014-05-23 12:42:46 +00:00
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 09:55:08 +00:00
|
|
|
path := path.Join("/", "container_path")
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-07-24 21:12:55 +00:00
|
|
|
// We should have copied a symlink *NOT* the file itself!
|
|
|
|
linkTarget, err := os.Readlink(tmpname)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpFullPath))
|
2015-07-24 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that symlinks to a directory behave as expected when copying one from
|
|
|
|
// a container.
|
|
|
|
func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link")
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
testDir, err := ioutil.TempDir("", "test-cp-from-symlink-to-dir-")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
defer os.RemoveAll(testDir)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-07-24 21:12:55 +00:00
|
|
|
// This copy command should copy the symlink, not the target, into the
|
|
|
|
// temporary directory.
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+"/dir_link", testDir)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
expectedPath := filepath.Join(testDir, "dir_link")
|
|
|
|
linkTarget, err := os.Readlink(expectedPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpTestPathParent))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
os.Remove(expectedPath)
|
|
|
|
|
|
|
|
// This copy command should resolve the symlink (note the trailing
|
2015-08-07 22:24:18 +00:00
|
|
|
// separator), copying the target into the temporary directory.
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+"/dir_link/", testDir)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// It *should not* have copied the directory using the target's name, but
|
|
|
|
// used the given name instead.
|
|
|
|
unexpectedPath := filepath.Join(testDir, cpTestPathParent)
|
2015-10-23 01:19:33 +00:00
|
|
|
stat, err := os.Lstat(unexpectedPath)
|
|
|
|
if err == nil {
|
|
|
|
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
|
2015-07-24 21:12:55 +00:00
|
|
|
}
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf(out))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// It *should* have copied the directory using the asked name "dir_link".
|
2015-10-23 01:19:33 +00:00
|
|
|
stat, err = os.Lstat(expectedPath)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
|
2015-07-24 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that symlinks to a directory behave as expected when copying one to a
|
|
|
|
// container.
|
|
|
|
func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-24 21:12:55 +00:00
|
|
|
testRequires(c, SameHostDaemon) // Requires local volume mount bind.
|
|
|
|
|
|
|
|
testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
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.
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox")
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2016-10-29 07:03:26 +00:00
|
|
|
// Create a temp directory to hold a test file nested in a directory.
|
2015-07-24 21:12:55 +00:00
|
|
|
testDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
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)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)), checker.IsNil)
|
|
|
|
c.Assert(ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)), checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// Now create another temp directory to hold a symlink to the
|
|
|
|
// "/testDir/some" directory.
|
|
|
|
linkDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
defer os.RemoveAll(linkDir)
|
|
|
|
|
|
|
|
// Then symlink "/linkDir/dir_link" to "/testdir/some".
|
|
|
|
linkTarget := filepath.Join(testDir, cpTestPathParent)
|
|
|
|
localLink := filepath.Join(linkDir, "dir_link")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.Symlink(linkTarget, localLink), checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-07-24 21:12:55 +00:00
|
|
|
// Now copy that symlink into the test volume in the container.
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", localLink, containerID+":/testVol")
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// This copy command should have copied the symlink *not* the target.
|
|
|
|
expectedPath := filepath.Join(testVol, "dir_link")
|
|
|
|
actualLinkTarget, err := os.Readlink(expectedPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("unable to read symlink at %q", expectedPath))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(actualLinkTarget, checker.Equals, linkTarget)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// Good, now remove that copied link for the next test.
|
|
|
|
os.Remove(expectedPath)
|
|
|
|
|
|
|
|
// This copy command should resolve the symlink (note the trailing
|
2015-08-07 22:24:18 +00:00
|
|
|
// separator), copying the target into the test volume directory in the
|
2015-07-24 21:12:55 +00:00
|
|
|
// container.
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", localLink+"/", containerID+":/testVol")
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// It *should not* have copied the directory using the target's name, but
|
|
|
|
// used the given name instead.
|
|
|
|
unexpectedPath := filepath.Join(testVol, cpTestPathParent)
|
2015-10-23 01:19:33 +00:00
|
|
|
stat, err := os.Lstat(unexpectedPath)
|
|
|
|
if err == nil {
|
|
|
|
out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
|
2015-07-24 21:12:55 +00:00
|
|
|
}
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf(out))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// It *should* have copied the directory using the asked name "dir_link".
|
2015-10-23 01:19:33 +00:00
|
|
|
stat, err = os.Lstat(expectedPath)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
|
2015-07-24 21:12:55 +00:00
|
|
|
|
|
|
|
// 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)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-24 21:12:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(string(fileContents), checker.Equals, cpHostContents)
|
2014-05-23 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test for #5619
|
|
|
|
// Check that symlinks which are part of the resource path are still relative to the container's rootfs
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 09:55:08 +00:00
|
|
|
path := path.Join("/", "container_path", cpTestName)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":"+path, tmpdir)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output matched host file -- symlink path component can escape container rootfs
|
|
|
|
c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
|
2014-05-23 12:42:46 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// output doesn't match the input for symlink path component
|
|
|
|
c.Assert(string(test), checker.Equals, cpContainerContents)
|
2014-05-23 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-20 23:34:48 +00:00
|
|
|
// Check that cp with unprivileged user doesn't return any error
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, UnixCli) // uses chmod/su: not available on windows
|
2015-02-20 09:37:27 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
|
2014-05-20 23:34:48 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-05-20 23:34:48 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-05-20 23:34:48 +00:00
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-05-20 23:34:48 +00:00
|
|
|
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.Chmod(tmpdir, 0777), checker.IsNil)
|
2014-05-20 23:34:48 +00:00
|
|
|
|
2016-08-03 16:20:46 +00:00
|
|
|
result := icmd.RunCommand("su", "unprivilegeduser", "-c",
|
|
|
|
fmt.Sprintf("%s cp %s:%s %s", dockerBinary, containerID, cpTestName, tmpdir))
|
|
|
|
result.Assert(c, icmd.Expected{})
|
2014-05-20 23:34:48 +00:00
|
|
|
}
|
2014-09-24 13:07:11 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpSpecialFiles(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
|
|
|
outDir, err := ioutil.TempDir("", "cp-test-special-files")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-03-29 10:58:57 +00:00
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2015-03-29 10:58:57 +00:00
|
|
|
|
|
|
|
// Copy actual /etc/resolv.conf
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
expected := readContainerFile(c, containerID, "resolv.conf")
|
2015-03-29 10:58:57 +00:00
|
|
|
actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// Expected copied file to be duplicate of the container resolvconf
|
|
|
|
c.Assert(bytes.Equal(actual, expected), checker.True)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
|
|
|
// Copy actual /etc/hosts
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
expected = readContainerFile(c, containerID, "hosts")
|
2015-03-29 10:58:57 +00:00
|
|
|
actual, err = ioutil.ReadFile(outDir + "/hosts")
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// Expected copied file to be duplicate of the container hosts
|
|
|
|
c.Assert(bytes.Equal(actual, expected), checker.True)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
|
|
|
// Copy actual /etc/resolv.conf
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
expected = readContainerFile(c, containerID, "hostname")
|
2015-03-29 10:58:57 +00:00
|
|
|
actual, err = ioutil.ReadFile(outDir + "/hostname")
|
2017-01-18 02:08:31 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-03-29 10:58:57 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
// Expected copied file to be duplicate of the container resolvconf
|
|
|
|
c.Assert(bytes.Equal(actual, expected), checker.True)
|
2015-03-29 10:58:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpVolumePath(c *check.C) {
|
2015-09-18 17:41:12 +00:00
|
|
|
// stat /tmp/cp-test-volumepath851508420/test gets permission denied for the user
|
|
|
|
testRequires(c, NotUserNamespace)
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-02-19 04:06:23 +00:00
|
|
|
|
2014-09-24 13:07:11 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 13:07:11 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 13:07:11 +00:00
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
_, err = os.Create(tmpDir + "/test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
|
2014-09-24 13:07:11 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-09-24 13:07:11 +00:00
|
|
|
|
|
|
|
// Copy actual volume path
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/foo", outDir)
|
2015-04-14 05:16:19 +00:00
|
|
|
|
2014-09-24 13:07:11 +00:00
|
|
|
stat, err := os.Stat(outDir + "/foo")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// expected copied content to be dir
|
|
|
|
c.Assert(stat.IsDir(), checker.True)
|
2014-09-24 13:07:11 +00:00
|
|
|
stat, err = os.Stat(outDir + "/foo/bar")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// Expected file `bar` to be a file
|
|
|
|
c.Assert(stat.IsDir(), checker.False)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
|
|
|
// Copy file nested in volume
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/foo/bar", outDir)
|
2015-04-14 05:16:19 +00:00
|
|
|
|
2014-09-24 13:07:11 +00:00
|
|
|
stat, err = os.Stat(outDir + "/bar")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// Expected file `bar` to be a file
|
|
|
|
c.Assert(stat.IsDir(), checker.False)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
|
|
|
// Copy Bind-mounted dir
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/baz", outDir)
|
2014-09-24 13:07:11 +00:00
|
|
|
stat, err = os.Stat(outDir + "/baz")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// Expected `baz` to be a dir
|
|
|
|
c.Assert(stat.IsDir(), checker.True)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
|
|
|
// Copy file nested in bind-mounted dir
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/baz/test", outDir)
|
2014-09-24 13:07:11 +00:00
|
|
|
fb, err := ioutil.ReadFile(outDir + "/baz/test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 13:07:11 +00:00
|
|
|
fb2, err := ioutil.ReadFile(tmpDir + "/test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// Expected copied file to be duplicate of bind-mounted file
|
|
|
|
c.Assert(bytes.Equal(fb, fb2), checker.True)
|
2014-09-24 13:07:11 +00:00
|
|
|
|
|
|
|
// Copy bind-mounted file
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/test", outDir)
|
2014-09-24 13:07:11 +00:00
|
|
|
fb, err = ioutil.ReadFile(outDir + "/test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 13:07:11 +00:00
|
|
|
fb2, err = ioutil.ReadFile(tmpDir + "/test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
// Expected copied file to be duplicate of bind-mounted file
|
|
|
|
c.Assert(bytes.Equal(fb, fb2), checker.True)
|
2014-09-24 13:07:11 +00:00
|
|
|
}
|
2014-11-27 07:00:13 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpToDot(c *check.C) {
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
|
2014-11-27 07:00:13 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2014-11-27 07:00:13 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2014-11-27 07:00:13 +00:00
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-11-27 07:00:13 +00:00
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
cwd, err := os.Getwd()
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-11-27 07:00:13 +00:00
|
|
|
defer os.Chdir(cwd)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(os.Chdir(tmpdir), checker.IsNil)
|
|
|
|
dockerCmd(c, "cp", containerID+":/test", ".")
|
2014-11-27 07:00:13 +00:00
|
|
|
content, err := ioutil.ReadFile("./test")
|
2017-01-18 02:08:31 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(string(content), checker.Equals, "lololol\n")
|
2014-11-27 07:00:13 +00:00
|
|
|
}
|
2015-03-05 16:39:32 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpToStdout(c *check.C) {
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
|
2015-03-05 16:39:32 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-05 16:39:32 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2015-03-05 16:39:32 +00:00
|
|
|
|
2016-12-30 17:23:00 +00:00
|
|
|
out, _, err := testutil.RunCommandPipelineWithOutput(
|
2015-10-23 01:19:33 +00:00
|
|
|
exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
|
2015-03-05 16:39:32 +00:00
|
|
|
exec.Command("tar", "-vtf", "-"))
|
2015-04-14 05:16:19 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-03-05 16:39:32 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(out, checker.Contains, "test")
|
|
|
|
c.Assert(out, checker.Contains, "-rw")
|
2015-03-05 16:39:32 +00:00
|
|
|
}
|
2015-04-10 04:08:05 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
|
2016-01-08 21:49:43 +00:00
|
|
|
testRequires(c, SameHostDaemon, DaemonIsLinux)
|
2015-04-10 04:08:05 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
|
2015-04-10 04:08:05 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-04-10 04:08:05 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2015-04-10 04:08:05 +00:00
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 04:08:05 +00:00
|
|
|
defer os.RemoveAll(tmpdir)
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
|
2015-04-10 04:08:05 +00:00
|
|
|
content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
|
2017-01-18 02:08:31 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(string(content), checker.Equals, "lololol\n")
|
2015-04-10 04:08:05 +00:00
|
|
|
}
|
2015-05-26 21:20:55 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-26 21:20:55 +00:00
|
|
|
expectedMsg := "hello"
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg)
|
2015-10-23 01:19:33 +00:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-05-26 21:20:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
// failed to set up container
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
|
2015-05-26 21:20:55 +00:00
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-05-26 21:20:55 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir)
|
2015-05-26 21:20:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
out, _ = dockerCmd(c, "start", "-a", containerID)
|
2015-05-26 21:20:55 +00:00
|
|
|
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, expectedMsg)
|
2015-05-26 21:20:55 +00:00
|
|
|
}
|
2015-07-08 03:15:09 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
|
2015-07-08 03:15:09 +00:00
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "test")
|
2015-10-23 01:19:33 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-08 03:15:09 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
|
2015-07-08 03:15:09 +00:00
|
|
|
}
|
2015-10-01 07:56:39 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2016-05-08 01:36:10 +00:00
|
|
|
// now test copy symbol link to a non-existing file in host
|
2015-10-01 07:56:39 +00:00
|
|
|
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)
|
2017-01-18 02:08:31 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-01 07:56:39 +00:00
|
|
|
|
|
|
|
if !bytes.Equal(actual, expected) {
|
|
|
|
c.Fatalf("Expected copied file to be duplicate of the container symbol link target")
|
|
|
|
}
|
|
|
|
defer os.Remove(expectedPath)
|
|
|
|
}
|