github.com/slava-ustovytski/docker@v1.8.2-rc1/integration-cli/docker_cli_save_load_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"reflect"
    10  	"sort"
    11  	"strings"
    12  
    13  	"github.com/go-check/check"
    14  )
    15  
    16  // save a repo using gz compression and try to load it using stdout
    17  func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
    18  	name := "test-save-xz-and-load-repo-stdout"
    19  	dockerCmd(c, "run", "--name", name, "busybox", "true")
    20  
    21  	repoName := "foobar-save-load-test-xz-gz"
    22  	out, _ := dockerCmd(c, "commit", name, repoName)
    23  
    24  	dockerCmd(c, "inspect", repoName)
    25  
    26  	repoTarball, _, err := runCommandPipelineWithOutput(
    27  		exec.Command(dockerBinary, "save", repoName),
    28  		exec.Command("xz", "-c"),
    29  		exec.Command("gzip", "-c"))
    30  	if err != nil {
    31  		c.Fatalf("failed to save repo: %v %v", out, err)
    32  	}
    33  	deleteImages(repoName)
    34  
    35  	loadCmd := exec.Command(dockerBinary, "load")
    36  	loadCmd.Stdin = strings.NewReader(repoTarball)
    37  	out, _, err = runCommandWithOutput(loadCmd)
    38  	if err == nil {
    39  		c.Fatalf("expected error, but succeeded with no error and output: %v", out)
    40  	}
    41  
    42  	after, _, err := dockerCmdWithError(c, "inspect", repoName)
    43  	if err == nil {
    44  		c.Fatalf("the repo should not exist: %v", after)
    45  	}
    46  }
    47  
    48  // save a repo using xz+gz compression and try to load it using stdout
    49  func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
    50  	name := "test-save-xz-gz-and-load-repo-stdout"
    51  	dockerCmd(c, "run", "--name", name, "busybox", "true")
    52  
    53  	repoName := "foobar-save-load-test-xz-gz"
    54  	dockerCmd(c, "commit", name, repoName)
    55  
    56  	dockerCmd(c, "inspect", repoName)
    57  
    58  	out, _, err := runCommandPipelineWithOutput(
    59  		exec.Command(dockerBinary, "save", repoName),
    60  		exec.Command("xz", "-c"),
    61  		exec.Command("gzip", "-c"))
    62  	if err != nil {
    63  		c.Fatalf("failed to save repo: %v %v", out, err)
    64  	}
    65  
    66  	deleteImages(repoName)
    67  
    68  	loadCmd := exec.Command(dockerBinary, "load")
    69  	loadCmd.Stdin = strings.NewReader(out)
    70  	out, _, err = runCommandWithOutput(loadCmd)
    71  	if err == nil {
    72  		c.Fatalf("expected error, but succeeded with no error and output: %v", out)
    73  	}
    74  
    75  	after, _, err := dockerCmdWithError(c, "inspect", repoName)
    76  	if err == nil {
    77  		c.Fatalf("the repo should not exist: %v", after)
    78  	}
    79  }
    80  
    81  func (s *DockerSuite) TestSaveSingleTag(c *check.C) {
    82  	repoName := "foobar-save-single-tag-test"
    83  	dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
    84  
    85  	out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
    86  	cleanedImageID := strings.TrimSpace(out)
    87  
    88  	out, _, err := runCommandPipelineWithOutput(
    89  		exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
    90  		exec.Command("tar", "t"),
    91  		exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
    92  	if err != nil {
    93  		c.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
    94  	}
    95  }
    96  
    97  func (s *DockerSuite) TestSaveImageId(c *check.C) {
    98  	repoName := "foobar-save-image-id-test"
    99  	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
   100  
   101  	out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
   102  	cleanedLongImageID := strings.TrimSpace(out)
   103  
   104  	out, _ = dockerCmd(c, "images", "-q", repoName)
   105  	cleanedShortImageID := strings.TrimSpace(out)
   106  
   107  	saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
   108  	tarCmd := exec.Command("tar", "t")
   109  
   110  	var err error
   111  	tarCmd.Stdin, err = saveCmd.StdoutPipe()
   112  	if err != nil {
   113  		c.Fatalf("cannot set stdout pipe for tar: %v", err)
   114  	}
   115  	grepCmd := exec.Command("grep", cleanedLongImageID)
   116  	grepCmd.Stdin, err = tarCmd.StdoutPipe()
   117  	if err != nil {
   118  		c.Fatalf("cannot set stdout pipe for grep: %v", err)
   119  	}
   120  
   121  	if err = tarCmd.Start(); err != nil {
   122  		c.Fatalf("tar failed with error: %v", err)
   123  	}
   124  	if err = saveCmd.Start(); err != nil {
   125  		c.Fatalf("docker save failed with error: %v", err)
   126  	}
   127  	defer saveCmd.Wait()
   128  	defer tarCmd.Wait()
   129  
   130  	out, _, err = runCommandWithOutput(grepCmd)
   131  
   132  	if err != nil {
   133  		c.Fatalf("failed to save repo with image ID: %s, %v", out, err)
   134  	}
   135  }
   136  
   137  // save a repo and try to load it using flags
   138  func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *check.C) {
   139  	name := "test-save-and-load-repo-flags"
   140  	dockerCmd(c, "run", "--name", name, "busybox", "true")
   141  
   142  	repoName := "foobar-save-load-test"
   143  
   144  	deleteImages(repoName)
   145  	dockerCmd(c, "commit", name, repoName)
   146  
   147  	before, _ := dockerCmd(c, "inspect", repoName)
   148  
   149  	out, _, err := runCommandPipelineWithOutput(
   150  		exec.Command(dockerBinary, "save", repoName),
   151  		exec.Command(dockerBinary, "load"))
   152  	if err != nil {
   153  		c.Fatalf("failed to save and load repo: %s, %v", out, err)
   154  	}
   155  
   156  	after, _ := dockerCmd(c, "inspect", repoName)
   157  	if before != after {
   158  		c.Fatalf("inspect is not the same after a save / load")
   159  	}
   160  }
   161  
   162  func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
   163  	repoName := "foobar-save-multi-name-test"
   164  
   165  	// Make one image
   166  	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
   167  
   168  	// Make two images
   169  	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
   170  
   171  	out, _, err := runCommandPipelineWithOutput(
   172  		exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
   173  		exec.Command("tar", "xO", "repositories"),
   174  		exec.Command("grep", "-q", "-E", "(-one|-two)"),
   175  	)
   176  	if err != nil {
   177  		c.Fatalf("failed to save multiple repos: %s, %v", out, err)
   178  	}
   179  }
   180  
   181  func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
   182  
   183  	makeImage := func(from string, tag string) string {
   184  		var (
   185  			out string
   186  		)
   187  		out, _ = dockerCmd(c, "run", "-d", from, "true")
   188  		cleanedContainerID := strings.TrimSpace(out)
   189  
   190  		out, _ = dockerCmd(c, "commit", cleanedContainerID, tag)
   191  		imageID := strings.TrimSpace(out)
   192  		return imageID
   193  	}
   194  
   195  	repoName := "foobar-save-multi-images-test"
   196  	tagFoo := repoName + ":foo"
   197  	tagBar := repoName + ":bar"
   198  
   199  	idFoo := makeImage("busybox:latest", tagFoo)
   200  	idBar := makeImage("busybox:latest", tagBar)
   201  
   202  	deleteImages(repoName)
   203  
   204  	// create the archive
   205  	out, _, err := runCommandPipelineWithOutput(
   206  		exec.Command(dockerBinary, "save", repoName),
   207  		exec.Command("tar", "t"),
   208  		exec.Command("grep", "VERSION"),
   209  		exec.Command("cut", "-d", "/", "-f1"))
   210  	if err != nil {
   211  		c.Fatalf("failed to save multiple images: %s, %v", out, err)
   212  	}
   213  	actual := strings.Split(strings.TrimSpace(out), "\n")
   214  
   215  	// make the list of expected layers
   216  	out, _ = dockerCmd(c, "history", "-q", "--no-trunc", "busybox:latest")
   217  	expected := append(strings.Split(strings.TrimSpace(out), "\n"), idFoo, idBar)
   218  
   219  	sort.Strings(actual)
   220  	sort.Strings(expected)
   221  	if !reflect.DeepEqual(expected, actual) {
   222  		c.Fatalf("archive does not contains the right layers: got %v, expected %v", actual, expected)
   223  	}
   224  }
   225  
   226  // Issue #6722 #5892 ensure directories are included in changes
   227  func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) {
   228  	layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
   229  	layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
   230  
   231  	name := "save-directory-permissions"
   232  	tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
   233  	if err != nil {
   234  		c.Errorf("failed to create temporary directory: %s", err)
   235  	}
   236  	extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
   237  	os.Mkdir(extractionDirectory, 0777)
   238  
   239  	defer os.RemoveAll(tmpDir)
   240  	_, err = buildImage(name,
   241  		`FROM busybox
   242  	RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
   243  	RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
   244  		true)
   245  	if err != nil {
   246  		c.Fatal(err)
   247  	}
   248  
   249  	if out, _, err := runCommandPipelineWithOutput(
   250  		exec.Command(dockerBinary, "save", name),
   251  		exec.Command("tar", "-xf", "-", "-C", extractionDirectory),
   252  	); err != nil {
   253  		c.Errorf("failed to save and extract image: %s", out)
   254  	}
   255  
   256  	dirs, err := ioutil.ReadDir(extractionDirectory)
   257  	if err != nil {
   258  		c.Errorf("failed to get a listing of the layer directories: %s", err)
   259  	}
   260  
   261  	found := false
   262  	for _, entry := range dirs {
   263  		var entriesSansDev []string
   264  		if entry.IsDir() {
   265  			layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
   266  
   267  			f, err := os.Open(layerPath)
   268  			if err != nil {
   269  				c.Fatalf("failed to open %s: %s", layerPath, err)
   270  			}
   271  
   272  			entries, err := listTar(f)
   273  			for _, e := range entries {
   274  				if !strings.Contains(e, "dev/") {
   275  					entriesSansDev = append(entriesSansDev, e)
   276  				}
   277  			}
   278  			if err != nil {
   279  				c.Fatalf("encountered error while listing tar entries: %s", err)
   280  			}
   281  
   282  			if reflect.DeepEqual(entriesSansDev, layerEntries) || reflect.DeepEqual(entriesSansDev, layerEntriesAUFS) {
   283  				found = true
   284  				break
   285  			}
   286  		}
   287  	}
   288  
   289  	if !found {
   290  		c.Fatalf("failed to find the layer with the right content listing")
   291  	}
   292  
   293  }