2014-05-10 08:51:45 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-24 09:07:11 -04:00
|
|
|
"bytes"
|
2014-05-10 08:51:45 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-05-20 19:34:48 -04:00
|
|
|
"os/exec"
|
2015-02-18 04:55:08 -05:00
|
|
|
"path"
|
2014-05-10 08:51:45 -04:00
|
|
|
"path/filepath"
|
2015-03-05 11:39:32 -05:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-05-10 08:51:45 -04: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 16:44:29 -04:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "must specify at least one container source") {
|
|
|
|
c.Fatalf("unexpected output: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 08:51:45 -04:00
|
|
|
// Test for #5656
|
|
|
|
// Check that garbage paths don't escape the container's rootfs
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpGarbagePath(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 04:55:08 -05:00
|
|
|
path := path.Join("../../../../../../../../../../../../", cpFullPath)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) == cpHostContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output matched host file -- garbage path can escape container rootfs")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) != cpContainerContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output doesn't match the input for garbage path")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that relative paths are relative to the container's rootfs
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpRelativePath(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 04:55:08 -05: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:]
|
|
|
|
} else {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("path %s was assumed to be an absolute path", cpFullPath)
|
2015-02-18 04:55:08 -05:00
|
|
|
}
|
2014-05-10 08:51:45 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+relPath, tmpdir)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) == cpHostContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output matched host file -- relative path can escape container rootfs")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) != cpContainerContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output doesn't match the input for relative path")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that absolute paths are relative to the container's rootfs
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpAbsolutePath(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
path := cpFullPath
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
|
2014-05-10 08:51:45 -04:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) == cpHostContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output matched host file -- absolute path can escape container rootfs")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) != cpContainerContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output doesn't match the input for absolute path")
|
2014-05-10 08:51:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-05-20 19:34:48 -04:00
|
|
|
|
2014-05-23 08:42:46 -04:00
|
|
|
// Test for #5619
|
|
|
|
// Check that absolute symlinks are still relative to the container's rootfs
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpAbsoluteSymlink(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
tmpname := filepath.Join(tmpdir, "container_path")
|
2014-05-23 08:42:46 -04:00
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 04:55:08 -05:00
|
|
|
path := path.Join("/", "container_path")
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// We should have copied a symlink *NOT* the file itself!
|
|
|
|
linkTarget, err := os.Readlink(tmpname)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
if linkTarget != filepath.FromSlash(cpFullPath) {
|
|
|
|
c.Errorf("symlink target was %q, but expected: %q", linkTarget, cpFullPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that symlinks to a directory behave as expected when copying one from
|
|
|
|
// a container.
|
|
|
|
func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-24 17:12:55 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-from-symlink-to-dir-")
|
2014-05-23 08:42:46 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
2015-07-24 17:12:55 -04:00
|
|
|
defer os.RemoveAll(testDir)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// This copy command should copy the symlink, not the target, into the
|
|
|
|
// temporary directory.
|
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+"/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)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
if linkTarget != filepath.FromSlash(cpTestPathParent) {
|
|
|
|
c.Errorf("symlink target was %q, but expected: %q", linkTarget, cpTestPathParent)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(expectedPath)
|
|
|
|
|
|
|
|
// This copy command should resolve the symlink (note the trailing
|
2015-08-07 18:24:18 -04:00
|
|
|
// separator), copying the target into the temporary directory.
|
2015-07-24 17:12:55 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+"/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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
|
|
|
c.Errorf("should have copied a directory but got %q instead", stat.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that symlinks to a directory behave as expected when copying one to a
|
|
|
|
// container.
|
|
|
|
func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-24 17:12:55 -04:00
|
|
|
testRequires(c, SameHostDaemon) // Requires local volume mount bind.
|
|
|
|
|
|
|
|
testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanedContainerID := 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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
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)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// Now copy that symlink into the test volume in the container.
|
|
|
|
dockerCmd(c, "cp", localLink, cleanedContainerID+":/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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualLinkTarget != linkTarget {
|
|
|
|
c.Errorf("symlink target was %q, but expected: %q", actualLinkTarget, linkTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 18:24:18 -04:00
|
|
|
// separator), copying the target into the test volume directory in the
|
2015-07-24 17:12:55 -04:00
|
|
|
// container.
|
|
|
|
dockerCmd(c, "cp", localLink+"/", cleanedContainerID+":/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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
|
|
|
c.Errorf("should have copied a directory but got %q instead", stat.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
// And this directory should contain the file copied from the host at the
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(fileContents) != cpHostContents {
|
|
|
|
c.Fatalf("file contains %q but expected %q", string(fileContents), cpHostContents)
|
|
|
|
}
|
2014-05-23 08:42:46 -04: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 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostFile, err := os.Create(cpFullPath)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
defer hostFile.Close()
|
|
|
|
defer os.RemoveAll(cpTestPathParent)
|
|
|
|
|
|
|
|
fmt.Fprintf(hostFile, "%s", cpHostContents)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpname := filepath.Join(tmpdir, cpTestName)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2015-02-18 04:55:08 -05:00
|
|
|
path := path.Join("/", "container_path", cpTestName)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":"+path, tmpdir)
|
2014-05-23 08:42:46 -04:00
|
|
|
|
|
|
|
file, _ := os.Open(tmpname)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
test, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) == cpHostContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output matched host file -- symlink path component can escape container rootfs")
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if string(test) != cpContainerContents {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("output doesn't match the input for symlink path component")
|
2014-05-23 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-20 19:34:48 -04:00
|
|
|
// Check that cp with unprivileged user doesn't return any error
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, UnixCli) // uses chmod/su: not available on windows
|
2015-02-20 04:37:27 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-05-20 19:34:48 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-20 19:34:48 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-05-20 19:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-20 19:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
if err = os.Chmod(tmpdir, 0777); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-20 19:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path := cpTestName
|
|
|
|
|
|
|
|
_, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+cleanedContainerID+":"+path+" "+tmpdir))
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("couldn't copy with unprivileged user: %s:%s %s", cleanedContainerID, path, err)
|
2014-05-20 19:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-09-24 09:07:11 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpSpecialFiles(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-03-29 06:58:57 -04:00
|
|
|
|
|
|
|
outDir, err := ioutil.TempDir("", "cp-test-special-files")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-03-29 06:58:57 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy actual /etc/resolv.conf
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/etc/resolv.conf", outDir)
|
2015-03-29 06:58:57 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
expected, err := readContainerFile(cleanedContainerID, "resolv.conf")
|
2015-03-29 06:58:57 -04:00
|
|
|
actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
|
|
|
|
|
|
|
|
if !bytes.Equal(actual, expected) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected copied file to be duplicate of the container resolvconf")
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy actual /etc/hosts
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/etc/hosts", outDir)
|
2015-03-29 06:58:57 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
expected, err = readContainerFile(cleanedContainerID, "hosts")
|
2015-03-29 06:58:57 -04:00
|
|
|
actual, err = ioutil.ReadFile(outDir + "/hosts")
|
|
|
|
|
|
|
|
if !bytes.Equal(actual, expected) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected copied file to be duplicate of the container hosts")
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy actual /etc/resolv.conf
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/etc/hostname", outDir)
|
2015-03-29 06:58:57 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
expected, err = readContainerFile(cleanedContainerID, "hostname")
|
2015-03-29 06:58:57 -04:00
|
|
|
actual, err = ioutil.ReadFile(outDir + "/hostname")
|
|
|
|
|
|
|
|
if !bytes.Equal(actual, expected) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected copied file to be duplicate of the container resolvconf")
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpVolumePath(c *check.C) {
|
2015-09-18 13:41:12 -04:00
|
|
|
// stat /tmp/cp-test-volumepath851508420/test gets permission denied for the user
|
|
|
|
testRequires(c, NotUserNamespace)
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-02-18 23:06:23 -05:00
|
|
|
|
2014-09-24 09:07:11 -04:00
|
|
|
tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
_, err = os.Create(tmpDir + "/test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-09-24 09:07:11 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy actual volume path
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/foo", outDir)
|
2015-04-14 01:16:19 -04:00
|
|
|
|
2014-09-24 09:07:11 -04:00
|
|
|
stat, err := os.Stat(outDir + "/foo")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("expected copied content to be dir")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
stat, err = os.Stat(outDir + "/foo/bar")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected file `bar` to be a file")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy file nested in volume
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/foo/bar", outDir)
|
2015-04-14 01:16:19 -04:00
|
|
|
|
2014-09-24 09:07:11 -04:00
|
|
|
stat, err = os.Stat(outDir + "/bar")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected file `bar` to be a file")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy Bind-mounted dir
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/baz", outDir)
|
2014-09-24 09:07:11 -04:00
|
|
|
stat, err = os.Stat(outDir + "/baz")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected `baz` to be a dir")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy file nested in bind-mounted dir
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/baz/test", outDir)
|
2014-09-24 09:07:11 -04:00
|
|
|
fb, err := ioutil.ReadFile(outDir + "/baz/test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
fb2, err := ioutil.ReadFile(tmpDir + "/test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if !bytes.Equal(fb, fb2) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected copied file to be duplicate of bind-mounted file")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy bind-mounted file
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/test", outDir)
|
2014-09-24 09:07:11 -04:00
|
|
|
fb, err = ioutil.ReadFile(outDir + "/test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
fb2, err = ioutil.ReadFile(tmpDir + "/test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
if !bytes.Equal(fb, fb2) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected copied file to be duplicate of bind-mounted file")
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-27 02:00:13 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpToDot(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-11-27 02:00:13 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
if err := os.Chdir(tmpdir); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/test", ".")
|
2014-11-27 02:00:13 -05:00
|
|
|
content, err := ioutil.ReadFile("./test")
|
|
|
|
if string(content) != "lololol\n" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 11:39:32 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpToStdout(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to create a container:%s\n", out)
|
2015-03-05 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2015-03-05 11:39:32 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to set up container:%s\n", out)
|
2015-03-05 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:16:19 -04:00
|
|
|
out, _, err := runCommandPipelineWithOutput(
|
2015-03-05 11:39:32 -05:00
|
|
|
exec.Command(dockerBinary, "cp", cID+":/test", "-"),
|
|
|
|
exec.Command("tar", "-vtf", "-"))
|
2015-04-14 01:16:19 -04:00
|
|
|
|
2015-03-05 11:39:32 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to run commands: %s", err)
|
2015-03-05 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(out, "test") || !strings.Contains(out, "-rw") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Missing file from tar TOC:\n%s", out)
|
2015-03-05 11:39:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 00:08:05 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
2015-04-10 00:08:05 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
|
2015-04-14 01:16:19 -04:00
|
|
|
if exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to create a container", out)
|
2015-04-10 00:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", cleanedContainerID)
|
2015-04-14 01:16:19 -04:00
|
|
|
if strings.TrimSpace(out) != "0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("failed to set up container", out)
|
2015-04-10 00:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-integration")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 00:08:05 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", cleanedContainerID+":/te:s:t", tmpdir)
|
2015-04-10 00:08:05 -04:00
|
|
|
content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
|
|
|
|
if string(content) != "lololol\n" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
|
2015-04-10 00:08:05 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 17:20:55 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-26 17:20:55 -04:00
|
|
|
expectedMsg := "hello"
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg)
|
2015-05-26 17:20:55 -04:00
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", id)
|
2015-05-26 17:20:55 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
status := strings.TrimSpace(out)
|
2015-05-26 17:20:55 -04:00
|
|
|
if status != "0" {
|
|
|
|
c.Fatalf("container exited with status %s", status)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("unable to make temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-10-13 12:39:05 -04:00
|
|
|
dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", id), tmpDir)
|
2015-05-26 17:20:55 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "start", "-a", id)
|
2015-05-26 17:20:55 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
msg := strings.TrimSpace(out)
|
2015-05-26 17:20:55 -04:00
|
|
|
if msg != expectedMsg {
|
|
|
|
c.Fatalf("expected %q but got %q", expectedMsg, msg)
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 23:15:09 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
|
2015-07-07 23:15:09 -04:00
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "test")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("unable to make temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
|
2015-07-07 23:15:09 -04:00
|
|
|
}
|