Skip to content

Commit f85af3b

Browse files
authored
Fix generation of archives when using exclude_symlink_directories (#298)
* 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)
1 parent 02f627f commit f85af3b

File tree

8 files changed

+494
-46
lines changed

8 files changed

+494
-46
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: BUG FIXES
2+
body: 'data-source/archive_file: Prevent error when generating archive from source
3+
containing symbolically linked directories, and `exclude_symlink_directories`
4+
is set to true'
5+
time: 2024-01-15T10:13:58.177253Z
6+
custom:
7+
Issue: "298"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'resource/archive_file: Prevent error when generating archive from source containing
3+
symbolically linked directories, and `exclude_symlink_directories` is set to true'
4+
time: 2024-01-15T10:15:10.869072Z
5+
custom:
6+
Issue: "298"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'resource/archive_file: Return error when generated archive would be empty'
3+
time: 2024-01-17T10:18:51.941981Z
4+
custom:
5+
Issue: "298"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'data-source/archive_file: Return error when generated archive would be empty'
3+
time: 2024-01-17T10:19:23.907477Z
4+
custom:
5+
Issue: "298"

internal/provider/data_source_archive_file_test.go

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,8 @@ func TestAccArchiveFile_SymlinkFile_Absolute_ExcludeSymlinkDirectories(t *testin
960960
})
961961
}
962962

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

@@ -980,14 +980,14 @@ func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *t
980980
exclude_symlink_directories = true
981981
}
982982
`, filepath.ToSlash("test-fixtures/test-symlink-dir"), filepath.ToSlash(f)),
983-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
983+
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
984984
},
985985
},
986986
})
987987
}
988988

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

@@ -1011,7 +1011,7 @@ func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *t
10111011
exclude_symlink_directories = true
10121012
}
10131013
`, filepath.ToSlash(symlinkDirWithRegularFilesAbs), filepath.ToSlash(f)),
1014-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
1014+
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
10151015
},
10161016
},
10171017
})
@@ -1181,8 +1181,8 @@ func TestAccArchiveFile_SymlinkDirectoryWithSymlinkFile_Absolute_ExcludeSymlinkD
11811181
})
11821182
}
11831183

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

@@ -1197,18 +1197,17 @@ func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDir
11971197
type = "zip"
11981198
source_dir = "%s"
11991199
output_path = "%s"
1200-
output_file_mode = "0666"
12011200
exclude_symlink_directories = true
12021201
}
12031202
`, filepath.ToSlash("test-fixtures/test-dir-with-symlink-dir"), filepath.ToSlash(f)),
1204-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
1203+
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
12051204
},
12061205
},
12071206
})
12081207
}
12091208

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

@@ -1228,23 +1227,24 @@ func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSym
12281227
type = "zip"
12291228
source_dir = "%s"
12301229
output_path = "%s"
1231-
output_file_mode = "0666"
12321230
exclude_symlink_directories = true
12331231
}
12341232
`, filepath.ToSlash(symlinkDirInRegularDirAbs), filepath.ToSlash(f)),
1235-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
1233+
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
12361234
},
12371235
},
12381236
})
12391237
}
12401238

1241-
// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that an error is
1242-
// generated when trying to a directory which contains a symlink to a directory.
1239+
// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that
1240+
// symlinked directories are excluded.
12431241
func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T) {
12441242
td := t.TempDir()
12451243

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

1246+
var fileSize string
1247+
12481248
r.ParallelTest(t, r.TestCase{
12491249
ProtoV5ProviderFactories: protoV5ProviderFactories(),
12501250
Steps: []r.TestStep{
@@ -1258,14 +1258,32 @@ func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T
12581258
exclude_symlink_directories = true
12591259
}
12601260
`, filepath.ToSlash("test-fixtures"), filepath.ToSlash(f)),
1261-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
1261+
Check: r.ComposeTestCheckFunc(
1262+
testAccArchiveFileSize(f, &fileSize),
1263+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
1264+
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
1265+
ensureContents(t, value, map[string][]byte{
1266+
"test-dir/test-dir1/file1.txt": []byte("This is file 1"),
1267+
"test-dir/test-dir1/file2.txt": []byte("This is file 2"),
1268+
"test-dir/test-dir1/file3.txt": []byte("This is file 3"),
1269+
"test-dir/test-dir2/file1.txt": []byte("This is file 1"),
1270+
"test-dir/test-dir2/file2.txt": []byte("This is file 2"),
1271+
"test-dir/test-dir2/file3.txt": []byte("This is file 3"),
1272+
"test-dir/test-file.txt": []byte("This is test content"),
1273+
"test-dir-with-symlink-file/test-file.txt": []byte("This is test content"),
1274+
"test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"),
1275+
})
1276+
ensureFileMode(t, value, "0666")
1277+
return nil
1278+
}),
1279+
),
12621280
},
12631281
},
12641282
})
12651283
}
12661284

1267-
// TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories verifies that an error is
1268-
// generated when trying to a directory which contains a symlink to a directory.
1285+
// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that
1286+
// symlinked directories are excluded.
12691287
func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T) {
12701288
td := t.TempDir()
12711289

@@ -1276,6 +1294,8 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T
12761294
t.Fatal(err)
12771295
}
12781296

1297+
var fileSize string
1298+
12791299
r.ParallelTest(t, r.TestCase{
12801300
ProtoV5ProviderFactories: protoV5ProviderFactories(),
12811301
Steps: []r.TestStep{
@@ -1289,7 +1309,25 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T
12891309
exclude_symlink_directories = true
12901310
}
12911311
`, filepath.ToSlash(multipleDirsAndFilesAbs), filepath.ToSlash(f)),
1292-
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`),
1312+
Check: r.ComposeTestCheckFunc(
1313+
testAccArchiveFileSize(f, &fileSize),
1314+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
1315+
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
1316+
ensureContents(t, value, map[string][]byte{
1317+
"test-dir/test-dir1/file1.txt": []byte("This is file 1"),
1318+
"test-dir/test-dir1/file2.txt": []byte("This is file 2"),
1319+
"test-dir/test-dir1/file3.txt": []byte("This is file 3"),
1320+
"test-dir/test-dir2/file1.txt": []byte("This is file 1"),
1321+
"test-dir/test-dir2/file2.txt": []byte("This is file 2"),
1322+
"test-dir/test-dir2/file3.txt": []byte("This is file 3"),
1323+
"test-dir/test-file.txt": []byte("This is test content"),
1324+
"test-dir-with-symlink-file/test-file.txt": []byte("This is test content"),
1325+
"test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"),
1326+
})
1327+
ensureFileMode(t, value, "0666")
1328+
return nil
1329+
}),
1330+
),
12931331
},
12941332
},
12951333
})

0 commit comments

Comments
 (0)