backup: test that missing parent directory is correctly handled

This commit is contained in:
Michael Eischer
2025-06-12 22:37:57 +02:00
parent c8bb7bd312
commit 484cdf12e5

View File

@@ -1870,12 +1870,13 @@ func TestArchiverErrorReporting(t *testing.T) {
} }
var tests = []struct { var tests = []struct {
name string name string
src TestDir targets []string
want TestDir src TestDir
prepare func(t testing.TB) want TestDir
errFn ErrorFunc prepare func(t testing.TB)
mustError bool errFn ErrorFunc
errStr string
}{ }{
{ {
name: "no-error", name: "no-error",
@@ -1888,8 +1889,8 @@ func TestArchiverErrorReporting(t *testing.T) {
src: TestDir{ src: TestDir{
"targetfile": TestFile{Content: "foobar"}, "targetfile": TestFile{Content: "foobar"},
}, },
prepare: chmodUnreadable("targetfile"), prepare: chmodUnreadable("targetfile"),
mustError: true, errStr: "open targetfile: permission denied",
}, },
{ {
name: "file-unreadable-ignore-error", name: "file-unreadable-ignore-error",
@@ -1910,8 +1911,8 @@ func TestArchiverErrorReporting(t *testing.T) {
"targetfile": TestFile{Content: "foobar"}, "targetfile": TestFile{Content: "foobar"},
}, },
}, },
prepare: chmodUnreadable("subdir/targetfile"), prepare: chmodUnreadable("subdir/targetfile"),
mustError: true, errStr: "open subdir/targetfile: permission denied",
}, },
{ {
name: "file-subdir-unreadable-ignore-error", name: "file-subdir-unreadable-ignore-error",
@@ -1929,6 +1930,12 @@ func TestArchiverErrorReporting(t *testing.T) {
prepare: chmodUnreadable("subdir/targetfile"), prepare: chmodUnreadable("subdir/targetfile"),
errFn: ignoreErrorForBasename("targetfile"), errFn: ignoreErrorForBasename("targetfile"),
}, },
{
name: "parent-dir-missing",
targets: []string{"subdir/missing"},
src: TestDir{},
errStr: "stat subdir: no such file or directory",
},
} }
for _, test := range tests { for _, test := range tests {
@@ -1948,14 +1955,18 @@ func TestArchiverErrorReporting(t *testing.T) {
arch := New(repo, fs.Track{FS: fs.Local{}}, Options{}) arch := New(repo, fs.Track{FS: fs.Local{}}, Options{})
arch.Error = test.errFn arch.Error = test.errFn
_, snapshotID, _, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()}) target := test.targets
if test.mustError { if len(target) == 0 {
if err != nil { target = []string{"."}
}
_, snapshotID, _, err := arch.Snapshot(ctx, target, SnapshotOptions{Time: time.Now()})
if test.errStr != "" {
if strings.Contains(err.Error(), test.errStr) {
t.Logf("found expected error (%v), skipping further checks", err) t.Logf("found expected error (%v), skipping further checks", err)
return return
} }
t.Fatalf("expected error not returned by archiver") t.Fatalf("expected error (%v) not returned by archiver, got (%v)", test.errStr, err)
return return
} }