2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-21 18:43:39 -04:00
|
|
|
"archive/tar"
|
2015-10-01 12:07:30 -04:00
|
|
|
"encoding/json"
|
2014-02-25 11:17:48 -05:00
|
|
|
"fmt"
|
2017-08-21 18:43:39 -04:00
|
|
|
"io"
|
2014-09-06 07:49:40 -04:00
|
|
|
"io/ioutil"
|
2014-02-25 11:17:48 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-09-06 07:49:40 -04:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2015-11-18 17:20:54 -05:00
|
|
|
"regexp"
|
2014-11-11 04:18:22 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-10-01 12:07:30 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2019-08-05 10:37:47 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2014-12-08 15:40:27 -05:00
|
|
|
// save a repo using gz compression and try to load it using stdout
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-16 10:59:53 -04:00
|
|
|
name := "test-save-xz-and-load-repo-stdout"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "true")
|
2014-12-08 15:40:27 -05:00
|
|
|
|
|
|
|
repoName := "foobar-save-load-test-xz-gz"
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ := dockerCmd(c, "commit", name, repoName)
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "inspect", repoName)
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
repoTarball, err := RunCommandPipelineWithOutput(
|
2015-02-14 17:25:13 -05:00
|
|
|
exec.Command(dockerBinary, "save", repoName),
|
|
|
|
exec.Command("xz", "-c"),
|
|
|
|
exec.Command("gzip", "-c"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save repo: %v %v", out, err)
|
2014-12-08 15:40:27 -05:00
|
|
|
deleteImages(repoName)
|
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "load"},
|
|
|
|
Stdin: strings.NewReader(repoTarball),
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
after, _, err := dockerCmdWithError("inspect", repoName)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
|
2014-12-08 15:40:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// save a repo using xz+gz compression and try to load it using stdout
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-16 10:59:53 -04:00
|
|
|
name := "test-save-xz-gz-and-load-repo-stdout"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "true")
|
2014-12-08 15:40:27 -05:00
|
|
|
|
|
|
|
repoName := "foobar-save-load-test-xz-gz"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "commit", name, repoName)
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "inspect", repoName)
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 17:25:13 -05:00
|
|
|
exec.Command(dockerBinary, "save", repoName),
|
|
|
|
exec.Command("xz", "-c"),
|
|
|
|
exec.Command("gzip", "-c"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save repo: %v %v", out, err)
|
2014-12-08 15:40:27 -05:00
|
|
|
|
|
|
|
deleteImages(repoName)
|
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "load"},
|
|
|
|
Stdin: strings.NewReader(out),
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-12-08 15:40:27 -05:00
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
after, _, err := dockerCmdWithError("inspect", repoName)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
|
2014-12-08 15:40:27 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveSingleTag(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-07-07 14:58:27 -04:00
|
|
|
repoName := "foobar-save-single-tag-test"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedImageID := strings.TrimSpace(out)
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 17:25:13 -05:00
|
|
|
exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
|
|
|
|
exec.Command("tar", "t"),
|
|
|
|
exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
|
2014-07-07 14:58:27 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveCheckTimes(c *testing.T) {
|
2016-01-08 16:49:43 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-01 12:07:30 -04:00
|
|
|
repoName := "busybox:latest"
|
|
|
|
out, _ := dockerCmd(c, "inspect", repoName)
|
2018-05-19 07:38:54 -04:00
|
|
|
var data []struct {
|
2015-10-01 12:07:30 -04:00
|
|
|
ID string
|
|
|
|
Created time.Time
|
2018-05-19 07:38:54 -04:00
|
|
|
}
|
2015-10-01 12:07:30 -04:00
|
|
|
err := json.Unmarshal([]byte(out), &data)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to marshal from %q: err %v", repoName, err)
|
|
|
|
assert.Assert(c, len(data) != 0, "failed to marshal the data from %q", repoName)
|
2015-10-01 12:07:30 -04:00
|
|
|
tarTvTimeFormat := "2006-01-02 15:04"
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err = RunCommandPipelineWithOutput(
|
2015-10-01 12:07:30 -04:00
|
|
|
exec.Command(dockerBinary, "save", repoName),
|
|
|
|
exec.Command("tar", "tv"),
|
2015-11-18 17:20:54 -05:00
|
|
|
exec.Command("grep", "-E", fmt.Sprintf("%s %s", data[0].Created.Format(tarTvTimeFormat), digest.Digest(data[0].ID).Hex())))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
|
2015-10-01 12:07:30 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveImageId(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-07-07 14:58:27 -04:00
|
|
|
repoName := "foobar-save-image-id-test"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
|
2015-11-18 17:20:54 -05:00
|
|
|
cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ = dockerCmd(c, "images", "-q", repoName)
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedShortImageID := strings.TrimSpace(out)
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2015-08-22 08:06:48 -04:00
|
|
|
// Make sure IDs are not empty
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
|
|
|
|
assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
|
2015-08-22 08:06:48 -04:00
|
|
|
|
2015-02-14 17:25:13 -05:00
|
|
|
saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
|
|
|
|
tarCmd := exec.Command("tar", "t")
|
2015-07-21 14:01:24 -04:00
|
|
|
|
|
|
|
var err error
|
2015-02-14 17:25:13 -05:00
|
|
|
tarCmd.Stdin, err = saveCmd.StdoutPipe()
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err)
|
2015-02-14 17:25:13 -05:00
|
|
|
grepCmd := exec.Command("grep", cleanedLongImageID)
|
|
|
|
grepCmd.Stdin, err = tarCmd.StdoutPipe()
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
|
2014-07-07 14:58:27 -04:00
|
|
|
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
|
|
|
|
assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
|
2015-02-24 13:33:18 -05:00
|
|
|
defer func() {
|
|
|
|
saveCmd.Wait()
|
|
|
|
tarCmd.Wait()
|
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
}()
|
2015-02-14 17:25:13 -05:00
|
|
|
|
|
|
|
out, _, err = runCommandWithOutput(grepCmd)
|
|
|
|
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err)
|
2014-07-07 14:58:27 -04:00
|
|
|
}
|
|
|
|
|
2014-06-16 21:01:38 -04:00
|
|
|
// save a repo and try to load it using flags
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-16 10:59:53 -04:00
|
|
|
name := "test-save-and-load-repo-flags"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "true")
|
|
|
|
|
2014-06-16 21:01:38 -04:00
|
|
|
repoName := "foobar-save-load-test"
|
|
|
|
|
2015-02-14 17:25:13 -05:00
|
|
|
deleteImages(repoName)
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "commit", name, repoName)
|
2014-06-16 21:01:38 -04:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
before, _ := dockerCmd(c, "inspect", repoName)
|
2014-06-16 21:01:38 -04:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 17:25:13 -05:00
|
|
|
exec.Command(dockerBinary, "save", repoName),
|
|
|
|
exec.Command(dockerBinary, "load"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
|
2014-06-16 21:01:38 -04:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
after, _ := dockerCmd(c, "inspect", repoName)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, before, after, "inspect is not the same after a save / load")
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
2014-08-28 21:33:13 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveWithNoExistImage(c *testing.T) {
|
2016-03-04 05:05:34 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
imgName := "foobar-non-existing-image"
|
|
|
|
|
|
|
|
out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "", "save image should fail for non-existing image")
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf("No such image: %s", imgName)))
|
2016-03-04 05:05:34 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveMultipleNames(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-08-28 21:33:13 -04:00
|
|
|
repoName := "foobar-save-multi-name-test"
|
|
|
|
|
|
|
|
// Make one image
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
|
2014-11-17 11:05:49 -05:00
|
|
|
|
2014-08-28 21:33:13 -04:00
|
|
|
// Make two images
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
|
2014-08-28 21:33:13 -04:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 17:25:13 -05:00
|
|
|
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)"),
|
|
|
|
)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err)
|
2014-08-28 21:33:13 -04:00
|
|
|
}
|
2014-09-06 07:49:40 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-11-11 04:18:22 -05:00
|
|
|
makeImage := func(from string, tag string) string {
|
|
|
|
var (
|
|
|
|
out string
|
|
|
|
)
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", from, "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-11-11 04:18:22 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _ = dockerCmd(c, "commit", cleanedContainerID, tag)
|
2015-04-06 09:21:18 -04:00
|
|
|
imageID := strings.TrimSpace(out)
|
2014-11-11 04:18:22 -05:00
|
|
|
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
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-11-18 17:20:54 -05:00
|
|
|
exec.Command(dockerBinary, "save", repoName, "busybox:latest"),
|
|
|
|
exec.Command("tar", "t"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save multiple images: %s, %v", out, err)
|
2015-11-18 17:20:54 -05:00
|
|
|
|
|
|
|
lines := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
var actual []string
|
|
|
|
for _, l := range lines {
|
2019-08-05 10:53:46 -04:00
|
|
|
if regexp.MustCompile(`^[a-f0-9]{64}\.json$`).Match([]byte(l)) {
|
2015-11-18 17:20:54 -05:00
|
|
|
actual = append(actual, strings.TrimSuffix(l, ".json"))
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 04:18:22 -05:00
|
|
|
|
|
|
|
// make the list of expected layers
|
2016-01-28 09:19:25 -05:00
|
|
|
out = inspectField(c, "busybox:latest", "Id")
|
2015-11-18 17:20:54 -05:00
|
|
|
expected := []string{strings.TrimSpace(out), idFoo, idBar}
|
|
|
|
|
|
|
|
// prefixes are not in tar
|
|
|
|
for i := range expected {
|
|
|
|
expected[i] = digest.Digest(expected[i]).Hex()
|
|
|
|
}
|
2014-11-11 04:18:22 -05:00
|
|
|
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(expected)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(actual, expected), "archive does not contains the right layers: got %v, expected %v, output: %q", actual, expected, out)
|
2014-11-11 04:18:22 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 07:49:40 -04:00
|
|
|
// Issue #6722 #5892 ensure directories are included in changes
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveDirectoryPermissions(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-09-06 07:49:40 -04:00
|
|
|
layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
|
2014-09-19 14:08:57 -04:00
|
|
|
layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
|
2014-09-06 07:49:40 -04:00
|
|
|
|
|
|
|
name := "save-directory-permissions"
|
|
|
|
tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "failed to create temporary directory: %s", err)
|
2014-10-14 16:22:06 -04:00
|
|
|
extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
|
|
|
|
os.Mkdir(extractionDirectory, 0777)
|
|
|
|
|
2014-09-06 07:49:40 -04:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox
|
2014-09-06 07:49:40 -04:00
|
|
|
RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
|
2017-01-16 05:30:14 -05:00
|
|
|
RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`))
|
2014-09-06 07:49:40 -04:00
|
|
|
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-24 18:52:06 -05:00
|
|
|
exec.Command(dockerBinary, "save", name),
|
|
|
|
exec.Command("tar", "-xf", "-", "-C", extractionDirectory),
|
2015-10-12 04:24:16 -04:00
|
|
|
)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save and extract image: %s", out)
|
2014-09-06 07:49:40 -04:00
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(extractionDirectory)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to get a listing of the layer directories: %s", err)
|
2014-09-06 07:49:40 -04:00
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, entry := range dirs {
|
2015-02-25 18:53:07 -05:00
|
|
|
var entriesSansDev []string
|
2014-09-06 07:49:40 -04:00
|
|
|
if entry.IsDir() {
|
|
|
|
layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
|
|
|
|
|
|
|
|
f, err := os.Open(layerPath)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to open %s: %s", layerPath, err)
|
|
|
|
|
2016-06-24 23:57:21 -04:00
|
|
|
defer f.Close()
|
2014-09-06 07:49:40 -04:00
|
|
|
|
2017-08-21 18:43:39 -04:00
|
|
|
entries, err := listTar(f)
|
2015-02-01 19:42:35 -05:00
|
|
|
for _, e := range entries {
|
|
|
|
if !strings.Contains(e, "dev/") {
|
|
|
|
entriesSansDev = append(entriesSansDev, e)
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "encountered error while listing tar entries: %s", err)
|
2014-09-06 07:49:40 -04:00
|
|
|
|
2015-02-01 19:42:35 -05:00
|
|
|
if reflect.DeepEqual(entriesSansDev, layerEntries) || reflect.DeepEqual(entriesSansDev, layerEntriesAUFS) {
|
2014-09-06 07:49:40 -04:00
|
|
|
found = true
|
2014-09-19 14:08:57 -04:00
|
|
|
break
|
2014-09-06 07:49:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, found, "failed to find the layer with the right content listing")
|
2014-09-06 07:49:40 -04:00
|
|
|
}
|
2015-11-23 20:20:44 -05:00
|
|
|
|
2017-08-21 18:43:39 -04:00
|
|
|
func listTar(f io.Reader) ([]string, error) {
|
|
|
|
tr := tar.NewReader(f)
|
|
|
|
var entries []string
|
|
|
|
|
|
|
|
for {
|
|
|
|
th, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
entries = append(entries, th.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 20:20:44 -05:00
|
|
|
// 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
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestLoadZeroSizeLayer(c *testing.T) {
|
2018-05-04 17:15:00 -04:00
|
|
|
// this will definitely not work if using remote daemon
|
|
|
|
// very weird test
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2015-11-23 20:20:44 -05:00
|
|
|
|
2018-04-16 04:48:58 -04:00
|
|
|
dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar")
|
2015-11-23 20:20:44 -05:00
|
|
|
}
|
2016-03-21 16:52:36 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveLoadParents(c *testing.T) {
|
2016-03-21 16:52:36 -04:00
|
|
|
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)
|
|
|
|
|
2016-04-07 00:57:32 -04:00
|
|
|
dockerCmd(c, "rm", "-f", cleanedContainerID)
|
2016-03-21 16:52:36 -04:00
|
|
|
return imageID
|
|
|
|
}
|
|
|
|
|
|
|
|
idFoo := makeImage("busybox", "foo")
|
|
|
|
idBar := makeImage(idFoo, "bar")
|
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "save-load-parents")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-21 16:52:36 -04:00
|
|
|
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")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, inspectOut, idFoo)
|
2016-03-21 16:52:36 -04:00
|
|
|
|
|
|
|
inspectOut = inspectField(c, idFoo, "Parent")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, inspectOut, "")
|
2016-03-21 16:52:36 -04:00
|
|
|
}
|
2016-05-25 21:25:12 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestSaveLoadNoTag(c *testing.T) {
|
2016-05-25 21:25:12 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
name := "saveloadnotag"
|
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV foo=bar"))
|
2016-05-25 21:25:12 -04:00
|
|
|
id := inspectField(c, name, "Id")
|
|
|
|
|
|
|
|
// Test to make sure that save w/o name just shows imageID during load
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2016-05-25 21:25:12 -04:00
|
|
|
exec.Command(dockerBinary, "save", id),
|
|
|
|
exec.Command(dockerBinary, "load"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
|
2016-05-25 21:25:12 -04:00
|
|
|
|
|
|
|
// Should not show 'name' but should show the image ID during the load
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "Loaded image: "))
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "Loaded image ID:"))
|
|
|
|
assert.Assert(c, strings.Contains(out, id))
|
2016-05-25 21:25:12 -04:00
|
|
|
// Test to make sure that save by name shows that name during load
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err = RunCommandPipelineWithOutput(
|
2016-05-25 21:25:12 -04:00
|
|
|
exec.Command(dockerBinary, "save", name),
|
|
|
|
exec.Command(dockerBinary, "load"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
|
|
|
|
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "Loaded image: "+name+":latest"))
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "Loaded image ID:"))
|
2016-05-25 21:25:12 -04:00
|
|
|
}
|