github.com/slene/docker@v1.8.0-rc1/integration-cli/docker_cli_cp_utils.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/pkg/archive"
    13  	"github.com/go-check/check"
    14  )
    15  
    16  type fileType uint32
    17  
    18  const (
    19  	ftRegular fileType = iota
    20  	ftDir
    21  	ftSymlink
    22  )
    23  
    24  type fileData struct {
    25  	filetype fileType
    26  	path     string
    27  	contents string
    28  }
    29  
    30  func (fd fileData) creationCommand() string {
    31  	var command string
    32  
    33  	switch fd.filetype {
    34  	case ftRegular:
    35  		// Don't overwrite the file if it already exists!
    36  		command = fmt.Sprintf("if [ ! -f %s ]; then echo %q > %s; fi", fd.path, fd.contents, fd.path)
    37  	case ftDir:
    38  		command = fmt.Sprintf("mkdir -p %s", fd.path)
    39  	case ftSymlink:
    40  		command = fmt.Sprintf("ln -fs %s %s", fd.contents, fd.path)
    41  	}
    42  
    43  	return command
    44  }
    45  
    46  func mkFilesCommand(fds []fileData) string {
    47  	commands := make([]string, len(fds))
    48  
    49  	for i, fd := range fds {
    50  		commands[i] = fd.creationCommand()
    51  	}
    52  
    53  	return strings.Join(commands, " && ")
    54  }
    55  
    56  var defaultFileData = []fileData{
    57  	{ftRegular, "file1", "file1"},
    58  	{ftRegular, "file2", "file2"},
    59  	{ftRegular, "file3", "file3"},
    60  	{ftRegular, "file4", "file4"},
    61  	{ftRegular, "file5", "file5"},
    62  	{ftRegular, "file6", "file6"},
    63  	{ftRegular, "file7", "file7"},
    64  	{ftDir, "dir1", ""},
    65  	{ftRegular, "dir1/file1-1", "file1-1"},
    66  	{ftRegular, "dir1/file1-2", "file1-2"},
    67  	{ftDir, "dir2", ""},
    68  	{ftRegular, "dir2/file2-1", "file2-1"},
    69  	{ftRegular, "dir2/file2-2", "file2-2"},
    70  	{ftDir, "dir3", ""},
    71  	{ftRegular, "dir3/file3-1", "file3-1"},
    72  	{ftRegular, "dir3/file3-2", "file3-2"},
    73  	{ftDir, "dir4", ""},
    74  	{ftRegular, "dir4/file3-1", "file4-1"},
    75  	{ftRegular, "dir4/file3-2", "file4-2"},
    76  	{ftDir, "dir5", ""},
    77  	{ftSymlink, "symlink1", "target1"},
    78  	{ftSymlink, "symlink2", "target2"},
    79  }
    80  
    81  func defaultMkContentCommand() string {
    82  	return mkFilesCommand(defaultFileData)
    83  }
    84  
    85  func makeTestContentInDir(c *check.C, dir string) {
    86  	for _, fd := range defaultFileData {
    87  		path := filepath.Join(dir, filepath.FromSlash(fd.path))
    88  		switch fd.filetype {
    89  		case ftRegular:
    90  			if err := ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)); err != nil {
    91  				c.Fatal(err)
    92  			}
    93  		case ftDir:
    94  			if err := os.Mkdir(path, os.FileMode(0777)); err != nil {
    95  				c.Fatal(err)
    96  			}
    97  		case ftSymlink:
    98  			if err := os.Symlink(fd.contents, path); err != nil {
    99  				c.Fatal(err)
   100  			}
   101  		}
   102  	}
   103  }
   104  
   105  type testContainerOptions struct {
   106  	addContent bool
   107  	readOnly   bool
   108  	volumes    []string
   109  	workDir    string
   110  	command    string
   111  }
   112  
   113  func makeTestContainer(c *check.C, options testContainerOptions) (containerID string) {
   114  	if options.addContent {
   115  		mkContentCmd := defaultMkContentCommand()
   116  		if options.command == "" {
   117  			options.command = mkContentCmd
   118  		} else {
   119  			options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
   120  		}
   121  	}
   122  
   123  	if options.command == "" {
   124  		options.command = "#(nop)"
   125  	}
   126  
   127  	args := []string{"run", "-d"}
   128  
   129  	for _, volume := range options.volumes {
   130  		args = append(args, "-v", volume)
   131  	}
   132  
   133  	if options.workDir != "" {
   134  		args = append(args, "-w", options.workDir)
   135  	}
   136  
   137  	if options.readOnly {
   138  		args = append(args, "--read-only")
   139  	}
   140  
   141  	args = append(args, "busybox", "/bin/sh", "-c", options.command)
   142  
   143  	out, status := dockerCmd(c, args...)
   144  	if status != 0 {
   145  		c.Fatalf("failed to run container, status %d: %s", status, out)
   146  	}
   147  
   148  	containerID = strings.TrimSpace(out)
   149  
   150  	out, status = dockerCmd(c, "wait", containerID)
   151  	if status != 0 {
   152  		c.Fatalf("failed to wait for test container container, status %d: %s", status, out)
   153  	}
   154  
   155  	if exitCode := strings.TrimSpace(out); exitCode != "0" {
   156  		logs, status := dockerCmd(c, "logs", containerID)
   157  		if status != 0 {
   158  			logs = "UNABLE TO GET LOGS"
   159  		}
   160  		c.Fatalf("failed to make test container, exit code (%d): %s", exitCode, logs)
   161  	}
   162  
   163  	return
   164  }
   165  
   166  func makeCatFileCommand(path string) string {
   167  	return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
   168  }
   169  
   170  func cpPath(pathElements ...string) string {
   171  	localizedPathElements := make([]string, len(pathElements))
   172  	for i, path := range pathElements {
   173  		localizedPathElements[i] = filepath.FromSlash(path)
   174  	}
   175  	return strings.Join(localizedPathElements, string(filepath.Separator))
   176  }
   177  
   178  func cpPathTrailingSep(pathElements ...string) string {
   179  	return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
   180  }
   181  
   182  func containerCpPath(containerID string, pathElements ...string) string {
   183  	joined := strings.Join(pathElements, "/")
   184  	return fmt.Sprintf("%s:%s", containerID, joined)
   185  }
   186  
   187  func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
   188  	return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
   189  }
   190  
   191  func runDockerCp(c *check.C, src, dst string) (err error) {
   192  	c.Logf("running `docker cp %s %s`", src, dst)
   193  
   194  	args := []string{"cp", src, dst}
   195  
   196  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
   197  	if err != nil {
   198  		err = fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
   199  	}
   200  
   201  	return
   202  }
   203  
   204  func startContainerGetOutput(c *check.C, cID string) (out string, err error) {
   205  	c.Logf("running `docker start -a %s`", cID)
   206  
   207  	args := []string{"start", "-a", cID}
   208  
   209  	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, args...))
   210  	if err != nil {
   211  		err = fmt.Errorf("error executing `docker start` command: %s: %s", err, out)
   212  	}
   213  
   214  	return
   215  }
   216  
   217  func getTestDir(c *check.C, label string) (tmpDir string) {
   218  	var err error
   219  
   220  	if tmpDir, err = ioutil.TempDir("", label); err != nil {
   221  		c.Fatalf("unable to make temporary directory: %s", err)
   222  	}
   223  
   224  	return
   225  }
   226  
   227  func isCpNotExist(err error) bool {
   228  	return strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "cannot find the file specified")
   229  }
   230  
   231  func isCpDirNotExist(err error) bool {
   232  	return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
   233  }
   234  
   235  func isCpNotDir(err error) bool {
   236  	return strings.Contains(err.Error(), archive.ErrNotDirectory.Error()) || strings.Contains(err.Error(), "filename, directory name, or volume label syntax is incorrect")
   237  }
   238  
   239  func isCpCannotCopyDir(err error) bool {
   240  	return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
   241  }
   242  
   243  func isCpCannotCopyReadOnly(err error) bool {
   244  	return strings.Contains(err.Error(), "marked read-only")
   245  }
   246  
   247  func isCannotOverwriteNonDirWithDir(err error) bool {
   248  	return strings.Contains(err.Error(), "cannot overwrite non-directory")
   249  }
   250  
   251  func fileContentEquals(c *check.C, filename, contents string) (err error) {
   252  	c.Logf("checking that file %q contains %q\n", filename, contents)
   253  
   254  	fileBytes, err := ioutil.ReadFile(filename)
   255  	if err != nil {
   256  		return
   257  	}
   258  
   259  	expectedBytes, err := ioutil.ReadAll(strings.NewReader(contents))
   260  	if err != nil {
   261  		return
   262  	}
   263  
   264  	if !bytes.Equal(fileBytes, expectedBytes) {
   265  		err = fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
   266  	}
   267  
   268  	return
   269  }
   270  
   271  func containerStartOutputEquals(c *check.C, cID, contents string) (err error) {
   272  	c.Logf("checking that container %q start output contains %q\n", cID, contents)
   273  
   274  	out, err := startContainerGetOutput(c, cID)
   275  	if err != nil {
   276  		return err
   277  	}
   278  
   279  	if out != contents {
   280  		err = fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
   281  	}
   282  
   283  	return
   284  }
   285  
   286  func defaultVolumes(tmpDir string) []string {
   287  	if SameHostDaemon.Condition() {
   288  		return []string{
   289  			"/vol1",
   290  			fmt.Sprintf("%s:/vol2", tmpDir),
   291  			fmt.Sprintf("%s:/vol3", filepath.Join(tmpDir, "vol3")),
   292  			fmt.Sprintf("%s:/vol_ro:ro", filepath.Join(tmpDir, "vol_ro")),
   293  		}
   294  	}
   295  
   296  	// Can't bind-mount volumes with separate host daemon.
   297  	return []string{"/vol1", "/vol2", "/vol3", "/vol_ro:/vol_ro:ro"}
   298  }