Skip to content

Commit

Permalink
pkg/archive: test tar headers are interoperable
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
corhere committed Oct 23, 2023
1 parent 452ca90 commit 6a8a792
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
41 changes: 37 additions & 4 deletions pkg/archive/archive_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package archive // import "github.com/docker/docker/pkg/archive"

import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
"syscall"
"testing"

"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"
)

Expand Down Expand Up @@ -103,11 +108,39 @@ func TestOverlayTarUntar(t *testing.T) {
Compression: Uncompressed,
WhiteoutFormat: OverlayWhiteoutFormat,
}
archive, err := TarWithOptions(src, options)
assert.NilError(t, err)
defer archive.Close()
reader, err := TarWithOptions(src, options)
assert.NilError(t, err)
archive, err := io.ReadAll(reader)
reader.Close()
assert.NilError(t, err)

// 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(archive, dst, options)
err = Untar(bytes.NewReader(archive), dst, options)
assert.NilError(t, err)

checkFileMode(t, filepath.Join(dst, "d1"), 0o700|os.ModeDir)
Expand Down
21 changes: 21 additions & 0 deletions pkg/archive/archive_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6a8a792

Please sign in to comment.