Skip to content

Commit

Permalink
Fix generation of archives when using exclude_symlink_directories (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jan 15, 2024
1 parent 7fc1b43 commit 1a5dc66
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 43 deletions.
98 changes: 78 additions & 20 deletions internal/provider/data_source_archive_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,8 @@ func TestAccArchiveFile_SymlinkFile_Absolute_ExcludeSymlinkDirectories(t *testin
})
}

// TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an error is generated when
// trying to use a symlink to a directory.
// TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an empty archive
// is generated when trying to archive a directory which only contains a symlink to a directory.
func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

Expand All @@ -980,14 +980,19 @@ func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *t
exclude_symlink_directories = true
}
`, filepath.ToSlash("test-fixtures/test-symlink-dir"), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
},
},
})
}

// TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an error is generated when
// trying to use a symlink to a directory.
// TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an empty archive
// is generated when trying to archive a directory which only contains a symlink to a directory.
func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

Expand All @@ -1011,7 +1016,12 @@ func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *t
exclude_symlink_directories = true
}
`, filepath.ToSlash(symlinkDirWithRegularFilesAbs), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
},
},
})
Expand Down Expand Up @@ -1181,8 +1191,8 @@ func TestAccArchiveFile_SymlinkDirectoryWithSymlinkFile_Absolute_ExcludeSymlinkD
})
}

// TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an error is
// generated when trying to a directory which contains a symlink to a directory.
// TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an empty archive
// is generated when trying to archive a directory which only contains a symlink to a directory.
func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

Expand All @@ -1197,18 +1207,22 @@ func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDir
type = "zip"
source_dir = "%s"
output_path = "%s"
output_file_mode = "0666"
exclude_symlink_directories = true
}
`, filepath.ToSlash("test-fixtures/test-dir-with-symlink-dir"), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
},
},
})
}

// TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an error is
// generated when trying to a directory which contains a symlink to a directory.
// TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an empty archive
// is generated when trying to archive a directory which only contains a symlink to a directory.
func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

Expand All @@ -1228,23 +1242,29 @@ func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSym
type = "zip"
source_dir = "%s"
output_path = "%s"
output_file_mode = "0666"
exclude_symlink_directories = true
}
`, filepath.ToSlash(symlinkDirInRegularDirAbs), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
},
},
})
}

// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that an error is
// generated when trying to a directory which contains a symlink to a directory.
// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that
// symlinked directories are excluded.
func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

f := filepath.Join(td, "zip_file_acc_test.zip")

var fileSize string

r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []r.TestStep{
Expand All @@ -1258,14 +1278,32 @@ func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T
exclude_symlink_directories = true
}
`, filepath.ToSlash("test-fixtures"), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
testAccArchiveFileSize(f, &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{
"test-dir/test-dir1/file1.txt": []byte("This is file 1"),
"test-dir/test-dir1/file2.txt": []byte("This is file 2"),
"test-dir/test-dir1/file3.txt": []byte("This is file 3"),
"test-dir/test-dir2/file1.txt": []byte("This is file 1"),
"test-dir/test-dir2/file2.txt": []byte("This is file 2"),
"test-dir/test-dir2/file3.txt": []byte("This is file 3"),
"test-dir/test-file.txt": []byte("This is test content"),
"test-dir-with-symlink-file/test-file.txt": []byte("This is test content"),
"test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"),
})
ensureFileMode(t, value, "0666")
return nil
}),
),
},
},
})
}

// TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories verifies that an error is
// generated when trying to a directory which contains a symlink to a directory.
// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that
// symlinked directories are excluded.
func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T) {
td := t.TempDir()

Expand All @@ -1276,6 +1314,8 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T
t.Fatal(err)
}

var fileSize string

r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []r.TestStep{
Expand All @@ -1289,7 +1329,25 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T
exclude_symlink_directories = true
}
`, filepath.ToSlash(multipleDirsAndFilesAbs), filepath.ToSlash(f)),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
Check: r.ComposeTestCheckFunc(
testAccArchiveFileSize(f, &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{
"test-dir/test-dir1/file1.txt": []byte("This is file 1"),
"test-dir/test-dir1/file2.txt": []byte("This is file 2"),
"test-dir/test-dir1/file3.txt": []byte("This is file 3"),
"test-dir/test-dir2/file1.txt": []byte("This is file 1"),
"test-dir/test-dir2/file2.txt": []byte("This is file 2"),
"test-dir/test-dir2/file3.txt": []byte("This is file 3"),
"test-dir/test-file.txt": []byte("This is test content"),
"test-dir-with-symlink-file/test-file.txt": []byte("This is test content"),
"test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"),
})
ensureFileMode(t, value, "0666")
return nil
}),
),
},
},
})
Expand Down
26 changes: 14 additions & 12 deletions internal/provider/zip_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,25 @@ func (a *ZipArchiver) createWalkFunc(basePath string, indirname string, opts Arc
}

if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if !opts.ExcludeSymlinkDirectories {
realPath, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}
realPath, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}

realInfo, err := os.Stat(realPath)
if err != nil {
return err
}
realInfo, err := os.Stat(realPath)
if err != nil {
return err
}

if realInfo.IsDir() {
if realInfo.IsDir() {
if !opts.ExcludeSymlinkDirectories {
return filepath.Walk(realPath, a.createWalkFunc(archivePath, realPath, opts))
} else {
return filepath.SkipDir
}

info = realInfo
}

info = realInfo
}

fh, err := zip.FileInfoHeader(info)
Expand Down
15 changes: 4 additions & 11 deletions internal/provider/zip_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -218,11 +217,8 @@ func TestZipArchiver_Dir_ExcludeSymlinkDirectories(t *testing.T) {
ExcludeSymlinkDirectories: true,
})

regex := regexp.MustCompile(`error reading file for archival: read test-fixtures(\/|\\)test-dir-with-symlink-dir(\/|\\)test-symlink-dir: `)
found := regex.Match([]byte(err.Error()))

if !found {
t.Fatalf("expedted error to match %q, got: %s", regex.String(), err.Error())
if err != nil {
t.Errorf("expected no error: %s", err)
}
}

Expand Down Expand Up @@ -270,11 +266,8 @@ func TestZipArchiver_Dir_Exclude_ExcludeSymlinkDirectories(t *testing.T) {
ExcludeSymlinkDirectories: true,
})

regex := regexp.MustCompile(`error reading file for archival: read test-fixtures(\/|\\)test-dir-with-symlink-dir(\/|\\)test-symlink-dir: `)
found := regex.Match([]byte(err.Error()))

if !found {
t.Fatalf("expedted error to match %q, got: %s", regex.String(), err.Error())
if err != nil {
t.Errorf("expected no error: %s", err)
}
}

Expand Down

0 comments on commit 1a5dc66

Please sign in to comment.