Skip to content

Commit

Permalink
Fix generation of archives when using exclude_symlink_directories (#298)
Browse files Browse the repository at this point in the history
* Fix generation of archives when using exclude_symlink_directories (#296)

* Adding change log entries (#296)

* Return error when generating an archive would result in an empty archive (#296)

* Adding equivalent test coverage for archive_file resource (#296)
  • Loading branch information
bendbennett authored Jan 23, 2024
1 parent 02f627f commit f85af3b
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/BUG FIXES-20240115-101358.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: BUG FIXES
body: 'data-source/archive_file: Prevent error when generating archive from source
containing symbolically linked directories, and `exclude_symlink_directories`
is set to true'
time: 2024-01-15T10:13:58.177253Z
custom:
Issue: "298"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240115-101510.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource/archive_file: Prevent error when generating archive from source containing
symbolically linked directories, and `exclude_symlink_directories` is set to true'
time: 2024-01-15T10:15:10.869072Z
custom:
Issue: "298"
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240117-101851.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'resource/archive_file: Return error when generated archive would be empty'
time: 2024-01-17T10:18:51.941981Z
custom:
Issue: "298"
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240117-101923.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'data-source/archive_file: Return error when generated archive would be empty'
time: 2024-01-17T10:19:23.907477Z
custom:
Issue: "298"
78 changes: 58 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,14 @@ 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`),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
}

// 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 +1011,7 @@ 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`),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
Expand Down Expand Up @@ -1181,8 +1181,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 +1197,17 @@ 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`),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
}

// 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 +1227,24 @@ 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`),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
}

// 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 +1258,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 +1294,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 +1309,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
Loading

0 comments on commit f85af3b

Please sign in to comment.