-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
124 lines (98 loc) · 2.38 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"errors"
"io/fs"
"os"
"syscall"
"testing"
)
// Tests for errors that are not feasible to cover by the end to end tests.
// For example, db.Read() calls are usually preceded by a db.Has(), so we
// can't easily simulate Read seeing an "xattr not found" in an end-to-end
// test.
func TestDBReadError(t *testing.T) {
f, err := os.Open("/dev/null")
if err != nil {
t.Fatal(err)
}
defer f.Close()
db := XattrDB{}
_, err = db.Read(f)
if !errors.Is(err, syscall.ENODATA) {
t.Fatalf("expected ENODATA, got %v", err)
}
}
var testErr = errors.New("test error")
type fakeDirEntry struct{}
func (f fakeDirEntry) Name() string {
return "fake"
}
func (f fakeDirEntry) IsDir() bool {
return false
}
func (f fakeDirEntry) Type() fs.FileMode {
return fs.FileMode(0777)
}
func (f fakeDirEntry) Info() (os.FileInfo, error) {
return nil, testErr
}
func TestOpenAndInfoError(t *testing.T) {
ok, _, _, err := openAndInfo("fake", fakeDirEntry{}, nil, 0)
if !ok || err != testErr {
t.Fatalf("expected ok, testErr, got %v, %v", ok, err)
}
}
type fakeDB struct {
hasAttr bool
hasErr error
readChecksum ChecksumV1
readErr error
writeErr error
}
func (db fakeDB) Has(f *os.File) (bool, error) {
return db.hasAttr, db.hasErr
}
func (db fakeDB) Read(f *os.File) (ChecksumV1, error) {
return db.readChecksum, db.readErr
}
func (db fakeDB) Write(f *os.File, c ChecksumV1) error {
return db.writeErr
}
func (db fakeDB) Close() error {
return nil
}
func TestWalkingFunctionsHandleDBErrors(t *testing.T) {
// Test how the various walking functions (generate, update, verify)
// handle errors from the database.
cases := []struct {
fn walkFn
db fakeDB
expected error
}{
{generate, fakeDB{}, nil},
{generate, fakeDB{hasErr: testErr}, testErr},
{generate, fakeDB{writeErr: testErr}, testErr},
{verify, fakeDB{}, nil},
{verify, fakeDB{hasErr: testErr}, testErr},
{update, fakeDB{}, nil},
{update, fakeDB{hasAttr: true}, nil},
{update, fakeDB{hasErr: testErr}, testErr},
{update, fakeDB{hasAttr: true, readErr: testErr}, testErr},
}
p := NewProgress(false)
for _, c := range cases {
f, err := os.Open("/dev/null")
if err != nil {
t.Fatal(err)
}
info, err := f.Stat()
if err != nil {
t.Fatal(err)
}
options.db = c.db
err = c.fn(f, info, p)
if err != c.expected {
t.Fatalf("expected %v, got %v", c.expected, err)
}
}
}