pkg/archive: test tar headers are interoperable

The existing pkg/archive unit tests are primarily round-trip tests which
assert that pkg/archive produces tarballs which pkg/archive can unpack.
While these tests are effective at catching regressions in archiving or
unarchiving, they have a blind spot for regressions in compatibility
with the rest of the ecosystem. For example, a typo in the capabilities
extended attribute constant would result in subtly broken image layer
tarballs, but the existing tests would not catch the bug if both the
archiving and unarchiving implementations have the same typo.

Extend the test for archiving an overlay filesystem layer to assert that
the overlayfs style whiteouts (extended attributes and device files) are
transformed into AUFS-style whiteouts (magic file names).

Extend the test for archiving files with extended attributes to assert
that the extended attribute is encoded into the file's tar header in the
standard, interoperable format compatible with the rest of the
ecosystem.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-10-23 15:59:33 -04:00
parent 452ca90fe5
commit 6a8a792019
2 changed files with 57 additions and 3 deletions

View file

@ -1,6 +1,9 @@
package archive // import "github.com/docker/docker/pkg/archive"
import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
"syscall"
@ -8,8 +11,10 @@ import (
"github.com/containerd/containerd/pkg/userns"
"github.com/docker/docker/pkg/system"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
)
@ -103,11 +108,39 @@ func TestOverlayTarUntar(t *testing.T) {
Compression: Uncompressed,
WhiteoutFormat: OverlayWhiteoutFormat,
}
archive, err := TarWithOptions(src, options)
reader, err := TarWithOptions(src, options)
assert.NilError(t, err)
archive, err := io.ReadAll(reader)
reader.Close()
assert.NilError(t, err)
defer archive.Close()
err = Untar(archive, dst, options)
// The archive should encode opaque directories and file whiteouts
// in AUFS format.
entries := make(map[string]struct{})
rdr := tar.NewReader(bytes.NewReader(archive))
for {
h, err := rdr.Next()
if err == io.EOF {
break
}
assert.NilError(t, err)
assert.Check(t, is.Equal(h.Devmajor, int64(0)), "unexpected device file in archive")
assert.Check(t, is.DeepEqual(h.PAXRecords, map[string]string(nil), cmpopts.EquateEmpty()))
entries[h.Name] = struct{}{}
}
assert.DeepEqual(t, entries, map[string]struct{}{
"d1/": {},
"d1/" + WhiteoutOpaqueDir: {},
"d1/f1": {},
"d2/": {},
"d2/" + WhiteoutOpaqueDir: {},
"d2/f1": {},
"d3/": {},
"d3/" + WhiteoutPrefix + "f1": {},
})
err = Untar(bytes.NewReader(archive), dst, options)
assert.NilError(t, err)
checkFileMode(t, filepath.Join(dst, "d1"), 0o700|os.ModeDir)

View file

@ -254,6 +254,27 @@ func TestTarUntarWithXattr(t *testing.T) {
out, err := exec.Command("setcap", "cap_block_suspend+ep", filepath.Join(origin, "2")).CombinedOutput()
assert.NilError(t, err, string(out))
tarball, err := Tar(origin, Uncompressed)
assert.NilError(t, err)
defer tarball.Close()
rdr := tar.NewReader(tarball)
for {
h, err := rdr.Next()
if err == io.EOF {
break
}
assert.NilError(t, err)
capability, hasxattr := h.PAXRecords["SCHILY.xattr.security.capability"]
switch h.Name {
case "2":
if assert.Check(t, hasxattr, "tar entry %q should have the 'security.capability' xattr", h.Name) {
assert.Check(t, len(capability) > 0, "tar entry %q has a blank 'security.capability' xattr value")
}
default:
assert.Check(t, !hasxattr, "tar entry %q should not have the 'security.capability' xattr", h.Name)
}
}
for _, c := range []Compression{
Uncompressed,
Gzip,