From 452a24d80b806649849d3d83f302fcabe2c9834e Mon Sep 17 00:00:00 2001 From: hacktron95 Date: Tue, 13 Oct 2020 10:47:25 +0800 Subject: [PATCH] add absolute path mode for gzip --- archive/gzip/gzip_test.go | 103 +++++++++++++++++++++++++++++++++----- archive/tar/tar_test.go | 4 -- 2 files changed, 90 insertions(+), 17 deletions(-) diff --git a/archive/gzip/gzip_test.go b/archive/gzip/gzip_test.go index 91e81ed3..6c1e4671 100644 --- a/archive/gzip/gzip_test.go +++ b/archive/gzip/gzip_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "github.com/go-kit/kit/log" @@ -19,12 +20,21 @@ var ( testRoot = "testdata" testRootMounted = "testdata/mounted" testRootExtracted = "testdata/extracted" + testAbsPattern = "testdata_absolute" ) func TestCreate(t *testing.T) { test.Ok(t, os.MkdirAll(testRootMounted, 0755)) test.Ok(t, os.MkdirAll(testRootExtracted, 0755)) - t.Cleanup(func() { os.RemoveAll(testRoot) }) + + testAbs, err := ioutil.TempDir("", testAbsPattern) + test.Ok(t, err) + test.Equals(t, filepath.IsAbs(testAbs), true) + + t.Cleanup(func() { + os.RemoveAll(testRoot) + os.RemoveAll(testAbs) + }) for _, tc := range []struct { name string @@ -44,7 +54,7 @@ func TestCreate(t *testing.T) { name: "non-existing mount paths", tgz: New(log.NewNopLogger(), testRootMounted, true, flate.DefaultCompression), srcs: []string{ - "iamnotexists", + "idonotexist", "metoo", }, written: 0, @@ -53,7 +63,7 @@ func TestCreate(t *testing.T) { { name: "existing mount paths", tgz: New(log.NewNopLogger(), testRootMounted, true, flate.DefaultCompression), - srcs: exampleFileTree(t, "gzip_create"), + srcs: exampleFileTree(t, "gzip_create", testRootMounted), written: 43, // 3 x tmpfile in dir, 1 tmpfile err: nil, }, @@ -71,12 +81,30 @@ func TestCreate(t *testing.T) { written: 43, err: nil, }, + { + name: "absolute mount paths", + tgz: New(log.NewNopLogger(), testRootMounted, true, flate.DefaultCompression), + srcs: exampleFileTree(t, "tar_create", testAbs), + written: 43, + err: nil, + }, } { - tc := tc // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables + tc := tc // NOTICE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables t.Run(tc.name, func(t *testing.T) { t.Parallel() // Setup + var absSrcs []string + var relativeSrcs []string + + for _, src := range tc.srcs { + if strings.HasPrefix(src, "/") { + absSrcs = append(absSrcs, src) + } else { + relativeSrcs = append(relativeSrcs, src) + } + } + dstDir, dstDirClean := test.CreateTempDir(t, "gzip_create_archives", testRootMounted) t.Cleanup(dstDirClean) @@ -91,12 +119,21 @@ func TestCreate(t *testing.T) { return } + // Test + for _, src := range absSrcs { + test.Ok(t, os.RemoveAll(src)) + } + test.Exists(t, archivePath) test.Assert(t, written == tc.written, "case %q: written bytes got %d want %v", tc.name, written, tc.written) _, err = extract(tc.tgz, archivePath, extDir) test.Ok(t, err) - test.EqualDirs(t, extDir, testRootMounted, tc.srcs) + test.EqualDirs(t, extDir, testRootMounted, relativeSrcs) + + for _, src := range absSrcs { + test.Exists(t, src) + } }) } } @@ -104,7 +141,15 @@ func TestCreate(t *testing.T) { func TestExtract(t *testing.T) { test.Ok(t, os.MkdirAll(testRootMounted, 0755)) test.Ok(t, os.MkdirAll(testRootExtracted, 0755)) - t.Cleanup(func() { os.RemoveAll(testRoot) }) + + testAbs, err := ioutil.TempDir("", testAbsPattern) + test.Ok(t, err) + test.Equals(t, filepath.IsAbs(testAbs), true) + + t.Cleanup(func() { + os.RemoveAll(testRoot) + os.RemoveAll(testAbs) + }) // Setup tgz := New(log.NewNopLogger(), testRootMounted, false, flate.DefaultCompression) @@ -112,9 +157,9 @@ func TestExtract(t *testing.T) { arcDir, arcDirClean := test.CreateTempDir(t, "gzip_extract_archive") t.Cleanup(arcDirClean) - files := exampleFileTree(t, "gzip_extract") + files := exampleFileTree(t, "gzip_extract", testRootMounted) archivePath := filepath.Join(arcDir, "test.tar.gz") - _, err := create(tgz, files, archivePath) + _, err = create(tgz, files, archivePath) test.Ok(t, err) nestedFiles := exampleNestedFileTree(t, "gzip_extract_nested") @@ -134,6 +179,11 @@ func TestExtract(t *testing.T) { badArchivePath := filepath.Join(arcDir, "bad_test.tar.gz") test.Ok(t, ioutil.WriteFile(badArchivePath, []byte("hello\ndrone\n"), 0644)) + filesAbs := exampleFileTree(t, ".gzip_extract_absolute", testAbs) + archiveAbsPath := filepath.Join(arcDir, "test_absolute.tar.gz") + _, err = create(tgz, filesAbs, archiveAbsPath) + test.Ok(t, err) + for _, tc := range []struct { name string tgz *Archive @@ -198,22 +248,49 @@ func TestExtract(t *testing.T) { written: 43, err: nil, }, + { + name: "absolute mount paths", + tgz: New(log.NewNopLogger(), testRootMounted, true, flate.DefaultCompression), + archivePath: archiveAbsPath, + srcs: filesAbs, + written: 43, + err: nil, + }, } { tc := tc // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables t.Run(tc.name, func(t *testing.T) { t.Parallel() + // Setup + var absSrcs []string + var relativeSrcs []string + + for _, src := range tc.srcs { + if strings.HasPrefix(src, "/") { + absSrcs = append(absSrcs, src) + } else { + relativeSrcs = append(relativeSrcs, src) + } + } + dstDir, dstDirClean := test.CreateTempDir(t, "gzip_extract_"+tc.name, testRootExtracted) t.Cleanup(dstDirClean) - + // Run + for _, src := range absSrcs { + test.Ok(t, os.RemoveAll(src)) + } written, err := extract(tc.tgz, tc.archivePath, dstDir) if err != nil { test.Expected(t, err, tc.err) return } + // Test test.Assert(t, written == tc.written, "case %q: written bytes got %d want %v", tc.name, written, tc.written) - test.EqualDirs(t, dstDir, testRootMounted, tc.srcs) + for _, src := range absSrcs { + test.Exists(t, src) + } + test.EqualDirs(t, dstDir, testRootMounted, relativeSrcs) }) } } @@ -272,11 +349,11 @@ func extract(a *Archive, src string, dst string) (int64, error) { // Fixtures -func exampleFileTree(t *testing.T, name string) []string { - file, fileClean := test.CreateTempFile(t, name, []byte("hello\ndrone!\n"), testRootMounted) // 13 bytes +func exampleFileTree(t *testing.T, name string, in string) []string { + file, fileClean := test.CreateTempFile(t, name, []byte("hello\ndrone!\n"), in) // 13 bytes t.Cleanup(fileClean) - dir, dirClean := test.CreateTempFilesInDir(t, name, []byte("hello\ngo!\n"), testRootMounted) // 10 bytes + dir, dirClean := test.CreateTempFilesInDir(t, name, []byte("hello\ngo!\n"), in) // 10 bytes t.Cleanup(dirClean) return []string{file, dir} diff --git a/archive/tar/tar_test.go b/archive/tar/tar_test.go index 17f8d0bf..ed1264be 100644 --- a/archive/tar/tar_test.go +++ b/archive/tar/tar_test.go @@ -126,7 +126,6 @@ func TestCreate(t *testing.T) { _, err = extract(tc.ta, archivePath, extDir) test.Ok(t, err) - test.EqualDirs(t, extDir, testRootMounted, relativeSrcs) for _, src := range absSrcs { @@ -299,13 +298,10 @@ func TestExtract(t *testing.T) { // Test test.Assert(t, written == tc.written, "case %q: written bytes got %d want %v", tc.name, written, tc.written) - for _, src := range absSrcs { test.Exists(t, src) } - test.EqualDirs(t, dstDir, testRootMounted, relativeSrcs) - }) } }