6db32fde |
package main
import ( |
6ffbe3f6 |
"archive/tar" |
e4478cad |
"encoding/json" |
6db32fde |
"fmt" |
6ffbe3f6 |
"io" |
cc5fb986 |
"io/ioutil" |
6db32fde |
"os"
"os/exec" |
cc5fb986 |
"path/filepath"
"reflect" |
4352da78 |
"regexp" |
b37fdc5d |
"sort"
"strings" |
59e55dcd |
"testing" |
e4478cad |
"time" |
dc944ea7 |
|
50c4475d |
"github.com/docker/docker/integration-cli/cli/build" |
07ff4f1d |
digest "github.com/opencontainers/go-digest" |
9f0b3f56 |
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/icmd" |
6db32fde |
)
|
e4ba82d5 |
// save a repo using gz compression and try to load it using stdout |
1d92789b |
func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) { |
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 |
|
e885af2a |
repoTarball, err := RunCommandPipelineWithOutput( |
b81105ea |
exec.Command(dockerBinary, "save", repoName),
exec.Command("xz", "-c"),
exec.Command("gzip", "-c")) |
6345208b |
assert.NilError(c, err, "failed to save repo: %v %v", out, err) |
e4ba82d5 |
deleteImages(repoName)
|
ecbb0e62 |
icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "load"},
Stdin: strings.NewReader(repoTarball),
}).Assert(c, icmd.Expected{
ExitCode: 1,
}) |
e4ba82d5 |
|
693ba98c |
after, _, err := dockerCmdWithError("inspect", repoName) |
6345208b |
assert.ErrorContains(c, err, "", "the repo should not exist: %v", after) |
e4ba82d5 |
}
// save a repo using xz+gz compression and try to load it using stdout |
1d92789b |
func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) { |
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 |
|
e885af2a |
out, err := RunCommandPipelineWithOutput( |
b81105ea |
exec.Command(dockerBinary, "save", repoName),
exec.Command("xz", "-c"),
exec.Command("gzip", "-c")) |
6345208b |
assert.NilError(c, err, "failed to save repo: %v %v", out, err) |
e4ba82d5 |
deleteImages(repoName)
|
ecbb0e62 |
icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "load"},
Stdin: strings.NewReader(out),
}).Assert(c, icmd.Expected{
ExitCode: 1,
}) |
e4ba82d5 |
|
693ba98c |
after, _, err := dockerCmdWithError("inspect", repoName) |
6345208b |
assert.ErrorContains(c, err, "", "the repo should not exist: %v", after) |
e4ba82d5 |
}
|
1d92789b |
func (s *DockerSuite) TestSaveSingleTag(c *testing.T) { |
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 |
|
e885af2a |
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))) |
6345208b |
assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err) |
600f65b2 |
}
|
1d92789b |
func (s *DockerSuite) TestSaveCheckTimes(c *testing.T) { |
25c38339 |
testRequires(c, DaemonIsLinux) |
e4478cad |
repoName := "busybox:latest"
out, _ := dockerCmd(c, "inspect", repoName) |
f23c00d8 |
var data []struct { |
e4478cad |
ID string
Created time.Time |
f23c00d8 |
} |
e4478cad |
err := json.Unmarshal([]byte(out), &data) |
6345208b |
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) |
e4478cad |
tarTvTimeFormat := "2006-01-02 15:04" |
e885af2a |
out, err = RunCommandPipelineWithOutput( |
e4478cad |
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()))) |
6345208b |
assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err) |
e4478cad |
}
|
1d92789b |
func (s *DockerSuite) TestSaveImageId(c *testing.T) { |
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 |
6135eec3 |
assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
assert.Assert(c, cleanedShortImageID != "", "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() |
0fabf3e4 |
assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err) |
b81105ea |
grepCmd := exec.Command("grep", cleanedLongImageID)
grepCmd.Stdin, err = tarCmd.StdoutPipe() |
0fabf3e4 |
assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err) |
600f65b2 |
|
0fabf3e4 |
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) |
b1cc78b8 |
defer func() {
saveCmd.Wait()
tarCmd.Wait()
dockerCmd(c, "rmi", repoName)
}() |
b81105ea |
out, _, err = runCommandWithOutput(grepCmd)
|
0fabf3e4 |
assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err) |
600f65b2 |
}
|
6228761f |
// save a repo and try to load it using flags |
1d92789b |
func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *testing.T) { |
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 |
|
e885af2a |
out, err := RunCommandPipelineWithOutput( |
b81105ea |
exec.Command(dockerBinary, "save", repoName),
exec.Command(dockerBinary, "load")) |
6345208b |
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err) |
6228761f |
|
012b67c3 |
after, _ := dockerCmd(c, "inspect", repoName) |
6345208b |
assert.Equal(c, before, after, "inspect is not the same after a save / load") |
6db32fde |
} |
79501dcd |
|
1d92789b |
func (s *DockerSuite) TestSaveWithNoExistImage(c *testing.T) { |
ed231d40 |
testRequires(c, DaemonIsLinux)
imgName := "foobar-non-existing-image"
out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName) |
6345208b |
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))) |
ed231d40 |
}
|
1d92789b |
func (s *DockerSuite) TestSaveMultipleNames(c *testing.T) { |
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 |
|
e885af2a |
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)"),
) |
6345208b |
assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err) |
79501dcd |
} |
cc5fb986 |
|
1d92789b |
func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *testing.T) { |
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 |
e885af2a |
out, err := RunCommandPipelineWithOutput( |
4352da78 |
exec.Command(dockerBinary, "save", repoName, "busybox:latest"),
exec.Command("tar", "t")) |
6345208b |
assert.NilError(c, err, "failed to save multiple images: %s, %v", out, err) |
4352da78 |
lines := strings.Split(strings.TrimSpace(out), "\n")
var actual []string
for _, l := range lines { |
a0d58b22 |
if regexp.MustCompile(`^[a-f0-9]{64}\.json$`).Match([]byte(l)) { |
4352da78 |
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) |
6345208b |
assert.Assert(c, is.DeepEqual(actual, expected), "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 |
1d92789b |
func (s *DockerSuite) TestSaveDirectoryPermissions(c *testing.T) { |
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") |
0fabf3e4 |
assert.Assert(c, err == nil, "failed to create temporary directory: %s", err) |
0c874240 |
extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
os.Mkdir(extractionDirectory, 0777)
|
cc5fb986 |
defer os.RemoveAll(tmpDir) |
50c4475d |
buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox |
cc5fb986 |
RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a |
c10f6ef4 |
RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`)) |
cc5fb986 |
|
e885af2a |
out, err := RunCommandPipelineWithOutput( |
c3e28351 |
exec.Command(dockerBinary, "save", name),
exec.Command("tar", "-xf", "-", "-C", extractionDirectory), |
d5830d66 |
) |
6345208b |
assert.NilError(c, err, "failed to save and extract image: %s", out) |
cc5fb986 |
dirs, err := ioutil.ReadDir(extractionDirectory) |
6345208b |
assert.NilError(c, err, "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) |
6345208b |
assert.NilError(c, err, "failed to open %s: %s", layerPath, err)
|
0ead6244 |
defer f.Close() |
cc5fb986 |
|
6ffbe3f6 |
entries, err := listTar(f) |
ea0fd0e8 |
for _, e := range entries {
if !strings.Contains(e, "dev/") {
entriesSansDev = append(entriesSansDev, e)
}
} |
6345208b |
assert.NilError(c, err, "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 |
}
}
}
|
6345208b |
assert.Assert(c, found, "failed to find the layer with the right content listing") |
cc5fb986 |
} |
7bb9fc41 |
|
6ffbe3f6 |
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)
}
}
|
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 |
1d92789b |
func (s *DockerSuite) TestLoadZeroSizeLayer(c *testing.T) { |
e4408318 |
// this will definitely not work if using remote daemon
// very weird test |
43b15e92 |
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) |
7bb9fc41 |
|
5f56503f |
dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar") |
7bb9fc41 |
} |
faeff511 |
|
1d92789b |
func (s *DockerSuite) TestSaveLoadParents(c *testing.T) { |
faeff511 |
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)
|
0c0198a5 |
dockerCmd(c, "rm", "-f", cleanedContainerID) |
faeff511 |
return imageID
}
idFoo := makeImage("busybox", "foo")
idBar := makeImage(idFoo, "bar")
tmpDir, err := ioutil.TempDir("", "save-load-parents") |
6345208b |
assert.NilError(c, err) |
faeff511 |
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") |
6345208b |
assert.Equal(c, inspectOut, idFoo) |
faeff511 |
inspectOut = inspectField(c, idFoo, "Parent") |
6345208b |
assert.Equal(c, inspectOut, "") |
faeff511 |
} |
6986a322 |
|
1d92789b |
func (s *DockerSuite) TestSaveLoadNoTag(c *testing.T) { |
6986a322 |
testRequires(c, DaemonIsLinux)
name := "saveloadnotag"
|
50c4475d |
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV foo=bar")) |
6986a322 |
id := inspectField(c, name, "Id")
// Test to make sure that save w/o name just shows imageID during load |
e885af2a |
out, err := RunCommandPipelineWithOutput( |
6986a322 |
exec.Command(dockerBinary, "save", id),
exec.Command(dockerBinary, "load")) |
6345208b |
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err) |
6986a322 |
// Should not show 'name' but should show the image ID during the load |
4e2e486b |
assert.Assert(c, !strings.Contains(out, "Loaded image: ")) |
98f2638f |
assert.Assert(c, strings.Contains(out, "Loaded image ID:"))
assert.Assert(c, strings.Contains(out, id)) |
6986a322 |
// Test to make sure that save by name shows that name during load |
e885af2a |
out, err = RunCommandPipelineWithOutput( |
6986a322 |
exec.Command(dockerBinary, "save", name),
exec.Command(dockerBinary, "load")) |
6345208b |
assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
|
98f2638f |
assert.Assert(c, strings.Contains(out, "Loaded image: "+name+":latest")) |
4e2e486b |
assert.Assert(c, !strings.Contains(out, "Loaded image ID:")) |
6986a322 |
} |