-
Notifications
You must be signed in to change notification settings - Fork 123
/
fs_error_test.go
268 lines (219 loc) · 5.08 KB
/
fs_error_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package gitbase
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/src-d/go-borges"
"github.com/src-d/go-borges/plain"
fixtures "github.com/src-d/go-git-fixtures"
"github.com/src-d/go-mysql-server/sql"
"github.com/stretchr/testify/require"
billy "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/osfs"
)
func TestFSErrorTables(t *testing.T) {
logrus.SetLevel(logrus.FatalLevel)
tests := []struct {
table string
rows int
}{
{BlobsTableName, 14},
{CommitBlobsTableName, 88},
{CommitFilesTableName, 88},
{CommitTreesTableName, 40},
{CommitsTableName, 9},
{FilesTableName, 82},
{RefCommitsTableName, 64},
{ReferencesTableName, 8},
{RepositoriesTableName, 3},
{TreeEntriesTableName, 45},
}
for _, test := range tests {
t.Run(test.table, func(t *testing.T) {
testTable(t, test.table, test.rows)
})
}
}
// setupErrorRepos creates a pool with three repos. One with read error in
// packfile, another with an index file missing (and ghost packfile) and
// finally a correct repository.
func setupErrorRepos(t *testing.T) (*sql.Context, CleanupFunc) {
require := require.New(t)
t.Helper()
fixture := fixtures.ByTag("worktree").One()
baseFS := fixture.Worktree()
tmpDir, err := ioutil.TempDir("", "gitbase")
require.NoError(err)
rootFS := osfs.New(tmpDir)
lib, pool, err := newMultiPool()
require.NoError(err)
repos := []struct {
name string
t brokenType
}{
{
name: "packfile",
t: brokenPackfile,
},
{
name: "index",
t: brokenIndex,
},
{
name: "ok",
t: brokenNone,
},
}
var fs billy.Filesystem
for _, repo := range repos {
err = rootFS.MkdirAll(repo.name, 0777)
require.NoError(err)
fs, err = rootFS.Chroot(repo.name)
require.NoError(err)
err = recursiveCopy(repo.name, fs, ".git", baseFS)
require.NoError(err)
fs, err = brokenFS(repo.t, fs)
require.NoError(err)
loc, err := plain.NewLocation(borges.LocationID(repo.name), fs, &plain.LocationOptions{
Bare: true,
})
require.NoError(err)
lib.plain.AddLocation(loc)
}
session := NewSession(pool, WithSkipGitErrors(true))
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
cleanup := func() {
t.Helper()
require.NoError(fixtures.Clean())
require.NoError(os.RemoveAll(tmpDir))
}
return ctx, cleanup
}
func brokenFS(
brokenType brokenType,
fs billy.Filesystem,
) (billy.Filesystem, error) {
var brokenFS billy.Filesystem
if brokenType == brokenNone {
brokenFS = fs
} else {
brokenFS = NewBrokenFS(brokenType, fs)
}
return brokenFS, nil
}
func testTable(t *testing.T, tableName string, number int) {
require := require.New(t)
ctx, cleanup := setupErrorRepos(t)
defer cleanup()
table := getTable(t, tableName, ctx)
rows, err := tableToRows(ctx, table)
require.NoError(err)
if len(rows) < number {
t.Errorf("table %s returned %v rows and it should be at least %v",
tableName, len(rows), number)
t.FailNow()
}
}
type brokenType uint64
const (
// no errors
brokenNone brokenType = 0
// packfile has read errors
brokenPackfile brokenType = 1 << iota
// there's no index for one packfile
brokenIndex
packFileGlob = "objects/pack/pack-*.pack"
packBrokenName = "pack-ffffffffffffffffffffffffffffffffffffffff.pack"
)
func NewBrokenFS(b brokenType, fs billy.Filesystem) billy.Filesystem {
return &BrokenFS{
Filesystem: fs,
brokenType: b,
}
}
type BrokenFS struct {
billy.Filesystem
brokenType brokenType
}
func (fs *BrokenFS) Open(filename string) (billy.File, error) {
return fs.OpenFile(filename, os.O_RDONLY, 0)
}
func (fs *BrokenFS) OpenFile(
name string,
flag int,
perm os.FileMode,
) (billy.File, error) {
file, err := fs.Filesystem.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
if fs.brokenType&brokenPackfile == 0 {
return file, err
}
match, err := filepath.Match(packFileGlob, name)
if err != nil {
return nil, err
}
if !match {
return file, nil
}
return &BrokenFile{
File: file,
}, nil
}
func (fs *BrokenFS) ReadDir(path string) ([]os.FileInfo, error) {
files, err := fs.Filesystem.ReadDir(path)
if err != nil {
return nil, err
}
if fs.brokenType&brokenIndex != 0 {
dummyPack := &brokenFileInfo{packBrokenName}
files = append(files, dummyPack)
}
return files, err
}
func (fs *BrokenFS) Stat(path string) (os.FileInfo, error) {
stat, err := fs.Filesystem.Stat(path)
return stat, err
}
type BrokenFile struct {
billy.File
count int
}
func (fs *BrokenFile) Read(p []byte) (int, error) {
_, err := fs.Seek(0, os.SEEK_CUR)
if err != nil {
return 0, err
}
fs.count++
if fs.count == 10 {
return 0, fmt.Errorf("could not read from broken file")
}
return fs.File.Read(p)
}
type brokenFileInfo struct {
name string
}
func (b *brokenFileInfo) Name() string {
return b.name
}
func (b *brokenFileInfo) Size() int64 {
return 1024 * 1024
}
func (b *brokenFileInfo) Mode() os.FileMode {
return 0600
}
func (b *brokenFileInfo) ModTime() time.Time {
return time.Now()
}
func (b *brokenFileInfo) IsDir() bool {
return false
}
func (b *brokenFileInfo) Sys() interface{} {
return nil
}