浏览代码

test(pkg/archive): add TestImpliedDirectoryPermissions

Co-authored-by: Cory Snider <csnider@mirantis.com>
Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
(cherry picked from commit 5dff494b87ca140626f2b722f78fd729db33fd22)
Bjorn Neergaard 2 年之前
父节点
当前提交
f6ebfaea19
共有 1 个文件被更改,包括 49 次插入1 次删除
  1. 49 1
      pkg/archive/archive_test.go

+ 49 - 1
pkg/archive/archive_test.go

@@ -7,6 +7,7 @@ import (
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"io/fs"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"path/filepath"
 	"path/filepath"
@@ -1216,7 +1217,7 @@ func TestTempArchiveCloseMultipleTimes(t *testing.T) {
 	}
 	}
 }
 }
 
 
-// TestXGlobalNoParent is a regression test to check parent directories are not crated for PAX headers
+// TestXGlobalNoParent is a regression test to check parent directories are not created for PAX headers
 func TestXGlobalNoParent(t *testing.T) {
 func TestXGlobalNoParent(t *testing.T) {
 	buf := &bytes.Buffer{}
 	buf := &bytes.Buffer{}
 	w := tar.NewWriter(buf)
 	w := tar.NewWriter(buf)
@@ -1236,6 +1237,53 @@ func TestXGlobalNoParent(t *testing.T) {
 	assert.Check(t, errors.Is(err, os.ErrNotExist))
 	assert.Check(t, errors.Is(err, os.ErrNotExist))
 }
 }
 
 
+// TestImpliedDirectoryPermissions ensures that directories implied by paths in the tar file, but without their own
+// header entries are created recursively with the default mode (permissions) stored in ImpliedDirectoryMode. This test
+// also verifies that the permissions of explicit directories are respected.
+func TestImpliedDirectoryPermissions(t *testing.T) {
+	skip.If(t, runtime.GOOS == "windows", "skipping test that requires Unix permissions")
+
+	buf := &bytes.Buffer{}
+	headers := []tar.Header{{
+		Name: "deeply/nested/and/implied",
+	}, {
+		Name: "explicit/",
+		Mode: 0644,
+	}, {
+		Name: "explicit/permissions/",
+		Mode: 0600,
+	}, {
+		Name: "explicit/permissions/specified",
+		Mode: 0400,
+	}}
+
+	w := tar.NewWriter(buf)
+	for _, header := range headers {
+		err := w.WriteHeader(&header)
+		assert.NilError(t, err)
+	}
+
+	tmpDir := t.TempDir()
+
+	err := Untar(buf, tmpDir, nil)
+	assert.NilError(t, err)
+
+	assertMode := func(path string, expected uint32) {
+		t.Helper()
+		stat, err := os.Lstat(filepath.Join(tmpDir, path))
+		assert.Check(t, err)
+		assert.Check(t, is.Equal(stat.Mode().Perm(), fs.FileMode(expected)))
+	}
+
+	assertMode("deeply", ImpliedDirectoryMode)
+	assertMode("deeply/nested", ImpliedDirectoryMode)
+	assertMode("deeply/nested/and", ImpliedDirectoryMode)
+
+	assertMode("explicit", 0644)
+	assertMode("explicit/permissions", 0600)
+	assertMode("explicit/permissions/specified", 0400)
+}
+
 func TestReplaceFileTarWrapper(t *testing.T) {
 func TestReplaceFileTarWrapper(t *testing.T) {
 	filesInArchive := 20
 	filesInArchive := 20
 	testcases := []struct {
 	testcases := []struct {