integration-cli/docker_cli_save_load_test.go
6db32fde
 package main
 
 import (
e4478cad
 	"encoding/json"
6db32fde
 	"fmt"
cc5fb986
 	"io/ioutil"
6db32fde
 	"os"
 	"os/exec"
cc5fb986
 	"path/filepath"
 	"reflect"
4352da78
 	"regexp"
b37fdc5d
 	"sort"
 	"strings"
e4478cad
 	"time"
dc944ea7
 
4352da78
 	"github.com/docker/distribution/digest"
d5830d66
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
e4ba82d5
 // save a repo using gz compression and try to load it using stdout
dc944ea7
 func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
a268e367
 	name := "test-save-xz-and-load-repo-stdout"
012b67c3
 	dockerCmd(c, "run", "--name", name, "busybox", "true")
e4ba82d5
 
 	repoName := "foobar-save-load-test-xz-gz"
012b67c3
 	out, _ := dockerCmd(c, "commit", name, repoName)
e4ba82d5
 
012b67c3
 	dockerCmd(c, "inspect", repoName)
e4ba82d5
 
b81105ea
 	repoTarball, _, err := runCommandPipelineWithOutput(
 		exec.Command(dockerBinary, "save", repoName),
 		exec.Command("xz", "-c"),
 		exec.Command("gzip", "-c"))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save repo: %v %v", out, err))
e4ba82d5
 	deleteImages(repoName)
 
b81105ea
 	loadCmd := exec.Command(dockerBinary, "load")
 	loadCmd.Stdin = strings.NewReader(repoTarball)
e4ba82d5
 	out, _, err = runCommandWithOutput(loadCmd)
d5830d66
 	c.Assert(err, checker.NotNil, check.Commentf("expected error, but succeeded with no error and output: %v", out))
e4ba82d5
 
693ba98c
 	after, _, err := dockerCmdWithError("inspect", repoName)
d5830d66
 	c.Assert(err, checker.NotNil, check.Commentf("the repo should not exist: %v", after))
e4ba82d5
 }
 
 // save a repo using xz+gz compression and try to load it using stdout
dc944ea7
 func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
a268e367
 	name := "test-save-xz-gz-and-load-repo-stdout"
012b67c3
 	dockerCmd(c, "run", "--name", name, "busybox", "true")
e4ba82d5
 
 	repoName := "foobar-save-load-test-xz-gz"
012b67c3
 	dockerCmd(c, "commit", name, repoName)
e4ba82d5
 
012b67c3
 	dockerCmd(c, "inspect", repoName)
e4ba82d5
 
012b67c3
 	out, _, err := runCommandPipelineWithOutput(
b81105ea
 		exec.Command(dockerBinary, "save", repoName),
 		exec.Command("xz", "-c"),
 		exec.Command("gzip", "-c"))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save repo: %v %v", out, err))
e4ba82d5
 
 	deleteImages(repoName)
 
b81105ea
 	loadCmd := exec.Command(dockerBinary, "load")
 	loadCmd.Stdin = strings.NewReader(out)
e4ba82d5
 	out, _, err = runCommandWithOutput(loadCmd)
d5830d66
 	c.Assert(err, checker.NotNil, check.Commentf("expected error, but succeeded with no error and output: %v", out))
e4ba82d5
 
693ba98c
 	after, _, err := dockerCmdWithError("inspect", repoName)
d5830d66
 	c.Assert(err, checker.NotNil, check.Commentf("the repo should not exist: %v", after))
e4ba82d5
 }
 
dc944ea7
 func (s *DockerSuite) TestSaveSingleTag(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
600f65b2
 	repoName := "foobar-save-single-tag-test"
012b67c3
 	dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
600f65b2
 
012b67c3
 	out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
475c6531
 	cleanedImageID := strings.TrimSpace(out)
600f65b2
 
012b67c3
 	out, _, err := runCommandPipelineWithOutput(
b81105ea
 		exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
 		exec.Command("tar", "t"),
 		exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err))
600f65b2
 }
 
e4478cad
 func (s *DockerSuite) TestSaveCheckTimes(c *check.C) {
25c38339
 	testRequires(c, DaemonIsLinux)
e4478cad
 	repoName := "busybox:latest"
 	out, _ := dockerCmd(c, "inspect", repoName)
 	data := []struct {
 		ID      string
 		Created time.Time
 	}{}
 	err := json.Unmarshal([]byte(out), &data)
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to marshal from %q: err %v", repoName, err))
 	c.Assert(len(data), checker.Not(checker.Equals), 0, check.Commentf("failed to marshal the data from %q", repoName))
e4478cad
 	tarTvTimeFormat := "2006-01-02 15:04"
 	out, _, err = runCommandPipelineWithOutput(
 		exec.Command(dockerBinary, "save", repoName),
 		exec.Command("tar", "tv"),
4352da78
 		exec.Command("grep", "-E", fmt.Sprintf("%s %s", data[0].Created.Format(tarTvTimeFormat), digest.Digest(data[0].ID).Hex())))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err))
e4478cad
 }
 
dc944ea7
 func (s *DockerSuite) TestSaveImageId(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
600f65b2
 	repoName := "foobar-save-image-id-test"
012b67c3
 	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
600f65b2
 
012b67c3
 	out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
4352da78
 	cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
600f65b2
 
012b67c3
 	out, _ = dockerCmd(c, "images", "-q", repoName)
475c6531
 	cleanedShortImageID := strings.TrimSpace(out)
600f65b2
 
c71a99af
 	// Make sure IDs are not empty
d5830d66
 	c.Assert(cleanedLongImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty."))
 	c.Assert(cleanedShortImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty."))
c71a99af
 
b81105ea
 	saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
 	tarCmd := exec.Command("tar", "t")
012b67c3
 
 	var err error
b81105ea
 	tarCmd.Stdin, err = saveCmd.StdoutPipe()
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("cannot set stdout pipe for tar: %v", err))
b81105ea
 	grepCmd := exec.Command("grep", cleanedLongImageID)
 	grepCmd.Stdin, err = tarCmd.StdoutPipe()
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("cannot set stdout pipe for grep: %v", err))
600f65b2
 
d5830d66
 	c.Assert(tarCmd.Start(), checker.IsNil, check.Commentf("tar failed with error: %v", err))
 	c.Assert(saveCmd.Start(), checker.IsNil, check.Commentf("docker save failed with error: %v", err))
b1cc78b8
 	defer func() {
 		saveCmd.Wait()
 		tarCmd.Wait()
 		dockerCmd(c, "rmi", repoName)
 	}()
b81105ea
 
 	out, _, err = runCommandWithOutput(grepCmd)
 
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save repo with image ID: %s, %v", out, err))
600f65b2
 }
 
6228761f
 // save a repo and try to load it using flags
dc944ea7
 func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
a268e367
 	name := "test-save-and-load-repo-flags"
012b67c3
 	dockerCmd(c, "run", "--name", name, "busybox", "true")
 
6228761f
 	repoName := "foobar-save-load-test"
 
b81105ea
 	deleteImages(repoName)
012b67c3
 	dockerCmd(c, "commit", name, repoName)
6228761f
 
012b67c3
 	before, _ := dockerCmd(c, "inspect", repoName)
6228761f
 
012b67c3
 	out, _, err := runCommandPipelineWithOutput(
b81105ea
 		exec.Command(dockerBinary, "save", repoName),
 		exec.Command(dockerBinary, "load"))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save and load repo: %s, %v", out, err))
6228761f
 
012b67c3
 	after, _ := dockerCmd(c, "inspect", repoName)
d5830d66
 	c.Assert(before, checker.Equals, after, check.Commentf("inspect is not the same after a save / load"))
6db32fde
 }
79501dcd
 
ed231d40
 func (s *DockerSuite) TestSaveWithNoExistImage(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	imgName := "foobar-non-existing-image"
 
 	out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
 	c.Assert(err, checker.NotNil, check.Commentf("save image should fail for non-existing image"))
 	c.Assert(out, checker.Contains, fmt.Sprintf("No such image: %s", imgName))
 }
 
dc944ea7
 func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
79501dcd
 	repoName := "foobar-save-multi-name-test"
 
 	// Make one image
012b67c3
 	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
da3d3b97
 
79501dcd
 	// Make two images
012b67c3
 	dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
79501dcd
 
012b67c3
 	out, _, err := runCommandPipelineWithOutput(
b81105ea
 		exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
 		exec.Command("tar", "xO", "repositories"),
 		exec.Command("grep", "-q", "-E", "(-one|-two)"),
 	)
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save multiple repos: %s, %v", out, err))
79501dcd
 }
cc5fb986
 
dc944ea7
 func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
b37fdc5d
 	makeImage := func(from string, tag string) string {
 		var (
 			out string
 		)
012b67c3
 		out, _ = dockerCmd(c, "run", "-d", from, "true")
475c6531
 		cleanedContainerID := strings.TrimSpace(out)
b37fdc5d
 
012b67c3
 		out, _ = dockerCmd(c, "commit", cleanedContainerID, tag)
475c6531
 		imageID := strings.TrimSpace(out)
b37fdc5d
 		return imageID
 	}
 
 	repoName := "foobar-save-multi-images-test"
 	tagFoo := repoName + ":foo"
 	tagBar := repoName + ":bar"
 
 	idFoo := makeImage("busybox:latest", tagFoo)
 	idBar := makeImage("busybox:latest", tagBar)
 
 	deleteImages(repoName)
 
 	// create the archive
b81105ea
 	out, _, err := runCommandPipelineWithOutput(
4352da78
 		exec.Command(dockerBinary, "save", repoName, "busybox:latest"),
 		exec.Command("tar", "t"))
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save multiple images: %s, %v", out, err))
4352da78
 
 	lines := strings.Split(strings.TrimSpace(out), "\n")
 	var actual []string
 	for _, l := range lines {
 		if regexp.MustCompile("^[a-f0-9]{64}\\.json$").Match([]byte(l)) {
 			actual = append(actual, strings.TrimSuffix(l, ".json"))
 		}
 	}
b37fdc5d
 
 	// make the list of expected layers
62a856e9
 	out = inspectField(c, "busybox:latest", "Id")
4352da78
 	expected := []string{strings.TrimSpace(out), idFoo, idBar}
 
 	// prefixes are not in tar
 	for i := range expected {
 		expected[i] = digest.Digest(expected[i]).Hex()
 	}
b37fdc5d
 
 	sort.Strings(actual)
 	sort.Strings(expected)
4352da78
 	c.Assert(actual, checker.DeepEquals, expected, check.Commentf("archive does not contains the right layers: got %v, expected %v, output: %q", actual, expected, out))
b37fdc5d
 }
 
cc5fb986
 // Issue #6722 #5892 ensure directories are included in changes
dc944ea7
 func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
cc5fb986
 	layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
1a0347ff
 	layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
cc5fb986
 
 	name := "save-directory-permissions"
 	tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary directory: %s", err))
0c874240
 	extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
 	os.Mkdir(extractionDirectory, 0777)
 
cc5fb986
 	defer os.RemoveAll(tmpDir)
 	_, err = buildImage(name,
 		`FROM busybox
 	RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
 	RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
 		true)
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("%v", err))
cc5fb986
 
d5830d66
 	out, _, err := runCommandPipelineWithOutput(
c3e28351
 		exec.Command(dockerBinary, "save", name),
 		exec.Command("tar", "-xf", "-", "-C", extractionDirectory),
d5830d66
 	)
 	c.Assert(err, checker.IsNil, check.Commentf("failed to save and extract image: %s", out))
cc5fb986
 
 	dirs, err := ioutil.ReadDir(extractionDirectory)
d5830d66
 	c.Assert(err, checker.IsNil, check.Commentf("failed to get a listing of the layer directories: %s", err))
cc5fb986
 
 	found := false
 	for _, entry := range dirs {
8be8e414
 		var entriesSansDev []string
cc5fb986
 		if entry.IsDir() {
 			layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
 
 			f, err := os.Open(layerPath)
d5830d66
 			c.Assert(err, checker.IsNil, check.Commentf("failed to open %s: %s", layerPath, err))
cc5fb986
 
6b3c9281
 			entries, err := listTar(f)
ea0fd0e8
 			for _, e := range entries {
 				if !strings.Contains(e, "dev/") {
 					entriesSansDev = append(entriesSansDev, e)
 				}
 			}
d5830d66
 			c.Assert(err, checker.IsNil, check.Commentf("encountered error while listing tar entries: %s", err))
cc5fb986
 
ea0fd0e8
 			if reflect.DeepEqual(entriesSansDev, layerEntries) || reflect.DeepEqual(entriesSansDev, layerEntriesAUFS) {
cc5fb986
 				found = true
1a0347ff
 				break
cc5fb986
 			}
 		}
 	}
 
d5830d66
 	c.Assert(found, checker.Equals, true, check.Commentf("failed to find the layer with the right content listing"))
cc5fb986
 
 }
7bb9fc41
 
 // Test loading a weird image where one of the layers is of zero size.
 // The layer.tar file is actually zero bytes, no padding or anything else.
 // See issue: 18170
 func (s *DockerSuite) TestLoadZeroSizeLayer(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	dockerCmd(c, "load", "-i", "fixtures/load/emptyLayer.tar")
 }
faeff511
 
 func (s *DockerSuite) TestSaveLoadParents(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	makeImage := func(from string, addfile string) string {
 		var (
 			out string
 		)
 		out, _ = dockerCmd(c, "run", "-d", from, "touch", addfile)
 		cleanedContainerID := strings.TrimSpace(out)
 
 		out, _ = dockerCmd(c, "commit", cleanedContainerID)
 		imageID := strings.TrimSpace(out)
 
 		dockerCmd(c, "rm", cleanedContainerID)
 		return imageID
 	}
 
 	idFoo := makeImage("busybox", "foo")
 	idBar := makeImage(idFoo, "bar")
 
 	tmpDir, err := ioutil.TempDir("", "save-load-parents")
 	c.Assert(err, checker.IsNil)
 	defer os.RemoveAll(tmpDir)
 
 	c.Log("tmpdir", tmpDir)
 
 	outfile := filepath.Join(tmpDir, "out.tar")
 
 	dockerCmd(c, "save", "-o", outfile, idBar, idFoo)
 	dockerCmd(c, "rmi", idBar)
 	dockerCmd(c, "load", "-i", outfile)
 
 	inspectOut := inspectField(c, idBar, "Parent")
 	c.Assert(inspectOut, checker.Equals, idFoo)
 
 	inspectOut = inspectField(c, idFoo, "Parent")
 	c.Assert(inspectOut, checker.Equals, "")
 }