This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
/
fake_file_system.go
674 lines (521 loc) · 13.7 KB
/
fake_file_system.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package fakes
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"sync"
gouuid "github.com/nu7hatch/gouuid"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FakeFileType string
const (
FakeFileTypeFile FakeFileType = "file"
FakeFileTypeSymlink FakeFileType = "symlink"
FakeFileTypeDir FakeFileType = "dir"
)
type FakeFileSystem struct {
files map[string]*FakeFileStats
filesLock sync.Mutex
HomeDirUsername string
HomeDirHomePath string
ExpandPathPath string
ExpandPathExpanded string
ExpandPathErr error
openFiles map[string]*FakeFile
OpenFileErr error
ReadFileError error
readFileErrorByPath map[string]error
WriteFileError error
WriteFileErrors map[string]error
SymlinkError error
MkdirAllError error
mkdirAllErrorByPath map[string]error
ChownErr error
ChmodErr error
CopyFileError error
CopyDirError error
RenameError error
RenameOldPaths []string
RenameNewPaths []string
RemoveAllError error
removeAllErrorByPath map[string]error
ReadLinkError error
TempFileError error
ReturnTempFile boshsys.File
TempDirDir string
TempDirDirs []string
TempDirError error
GlobErr error
globsMap map[string][][]string
WalkErr error
}
type FakeFileStats struct {
FileType FakeFileType
FileMode os.FileMode
Username string
SymlinkTarget string
Content []byte
}
func (stats FakeFileStats) StringContents() string {
return string(stats.Content)
}
type FakeFileInfo struct {
os.FileInfo
file FakeFile
}
func (fi FakeFileInfo) Size() int64 {
return int64(len(fi.file.Contents))
}
func (fi FakeFileInfo) IsDir() bool {
return fi.file.Stats.FileType == FakeFileTypeDir
}
type FakeFile struct {
path string
fs *FakeFileSystem
Stats *FakeFileStats
WriteErr error
Contents []byte
ReadErr error
ReadAtErr error
readIndex int64
CloseErr error
StatErr error
}
func NewFakeFile(path string, fs *FakeFileSystem) *FakeFile {
return &FakeFile{
path: path,
fs: fs,
}
}
func (f *FakeFile) Name() string {
return f.path
}
func (f *FakeFile) Write(contents []byte) (int, error) {
if f.WriteErr != nil {
return 0, f.WriteErr
}
f.fs.filesLock.Lock()
defer f.fs.filesLock.Unlock()
stats := f.fs.getOrCreateFile(f.path)
stats.Content = contents
f.Contents = contents
return len(contents), nil
}
func (f *FakeFile) Read(b []byte) (int, error) {
if f.readIndex >= int64(len(f.Contents)) {
return 0, io.EOF
}
copy(b, f.Contents)
f.readIndex = int64(len(f.Contents))
return len(f.Contents), f.ReadErr
}
func (f *FakeFile) ReadAt(b []byte, offset int64) (int, error) {
copy(b, f.Contents[offset:])
return len(f.Contents[offset:]), f.ReadAtErr
}
func (f *FakeFile) Close() error {
return f.CloseErr
}
func (f FakeFile) Stat() (os.FileInfo, error) {
return FakeFileInfo{file: f}, f.StatErr
}
func NewFakeFileSystem() *FakeFileSystem {
return &FakeFileSystem{
files: map[string]*FakeFileStats{},
openFiles: map[string]*FakeFile{},
globsMap: map[string][][]string{},
readFileErrorByPath: map[string]error{},
removeAllErrorByPath: map[string]error{},
mkdirAllErrorByPath: map[string]error{},
WriteFileErrors: map[string]error{},
}
}
func (fs *FakeFileSystem) GetFileTestStat(path string) *FakeFileStats {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
path = filepath.Join(path)
return fs.files[path]
}
func (fs *FakeFileSystem) HomeDir(username string) (string, error) {
fs.HomeDirUsername = username
return fs.HomeDirHomePath, nil
}
func (fs *FakeFileSystem) ExpandPath(path string) (string, error) {
fs.ExpandPathPath = path
if fs.ExpandPathExpanded == "" {
return fs.ExpandPathPath, fs.ExpandPathErr
}
return fs.ExpandPathExpanded, fs.ExpandPathErr
}
func (fs *FakeFileSystem) RegisterMkdirAllError(path string, err error) {
path = filepath.Join(path)
if _, ok := fs.mkdirAllErrorByPath[path]; ok {
panic(fmt.Sprintf("MkdirAll error is already set for path: %s", path))
}
fs.mkdirAllErrorByPath[path] = err
}
func (fs *FakeFileSystem) MkdirAll(path string, perm os.FileMode) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.MkdirAllError != nil {
return fs.MkdirAllError
}
path = filepath.Join(path)
if fs.mkdirAllErrorByPath[path] != nil {
return fs.mkdirAllErrorByPath[path]
}
stats := fs.getOrCreateFile(path)
stats.FileMode = perm
stats.FileType = FakeFileTypeDir
return nil
}
func (fs *FakeFileSystem) RegisterOpenFile(path string, file *FakeFile) {
path = filepath.Join(path)
fs.openFiles[path] = file
}
func (fs *FakeFileSystem) OpenFile(path string, flag int, perm os.FileMode) (boshsys.File, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.OpenFileErr != nil {
return nil, fs.OpenFileErr
}
path = filepath.Join(path)
// Make sure to record a reference for FileExist, etc. to work
stats := fs.getOrCreateFile(path)
stats.FileMode = perm
stats.FileType = FakeFileTypeFile
if fs.openFiles[path] != nil {
return fs.openFiles[path], nil
}
file := &FakeFile{
path: path,
fs: fs,
}
return file, nil
}
func (fs *FakeFileSystem) Chown(path, username string) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
// check early to avoid requiring file presence
if fs.ChownErr != nil {
return fs.ChownErr
}
path = filepath.Join(path)
stats := fs.files[path]
if stats == nil {
return fmt.Errorf("Path does not exist: %s", path)
}
stats.Username = username
return nil
}
func (fs *FakeFileSystem) Chmod(path string, perm os.FileMode) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
// check early to avoid requiring file presence
if fs.ChmodErr != nil {
return fs.ChmodErr
}
path = filepath.Join(path)
stats := fs.files[path]
if stats == nil {
return fmt.Errorf("Path does not exist: %s", path)
}
stats.FileMode = perm
return nil
}
func (fs *FakeFileSystem) WriteFileString(path, content string) (err error) {
return fs.WriteFile(path, []byte(content))
}
func (fs *FakeFileSystem) WriteFile(path string, content []byte) (err error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if error := fs.WriteFileError; error != nil {
return error
}
if error := fs.WriteFileErrors[path]; error != nil {
return error
}
path = filepath.Join(path)
parent := filepath.Dir(path)
if parent != "." {
fs.writeDir(parent)
}
stats := fs.getOrCreateFile(path)
stats.FileType = FakeFileTypeFile
stats.Content = content
return nil
}
func (fs *FakeFileSystem) writeDir(path string) (err error) {
parent := filepath.Dir(path)
if parent != "." && parent != "/" {
fs.writeDir(parent)
}
stats := fs.getOrCreateFile(path)
stats.FileType = FakeFileTypeDir
return nil
}
func (fs *FakeFileSystem) ConvergeFileContents(path string, content []byte) (bool, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.WriteFileError != nil {
return false, fs.WriteFileError
}
if error := fs.WriteFileErrors[path]; error != nil {
return false, error
}
stats := fs.getOrCreateFile(path)
stats.FileType = FakeFileTypeFile
if bytes.Compare(stats.Content, content) != 0 {
stats.Content = content
return true, nil
}
return false, nil
}
func (fs *FakeFileSystem) ReadFileString(path string) (string, error) {
bytes, err := fs.ReadFile(path)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (fs *FakeFileSystem) RegisterReadFileError(path string, err error) {
if _, ok := fs.readFileErrorByPath[path]; ok {
panic(fmt.Sprintf("ReadFile error is already set for path: %s", path))
}
fs.readFileErrorByPath[path] = err
}
func (fs *FakeFileSystem) ReadFile(path string) ([]byte, error) {
stats := fs.GetFileTestStat(path)
if stats != nil {
if fs.ReadFileError != nil {
return nil, fs.ReadFileError
}
if fs.readFileErrorByPath[path] != nil {
return nil, fs.readFileErrorByPath[path]
}
return stats.Content, nil
}
return nil, bosherr.Errorf("File not found: '%s'", path)
}
func (fs *FakeFileSystem) FileExists(path string) bool {
return fs.GetFileTestStat(path) != nil
}
func (fs *FakeFileSystem) Rename(oldPath, newPath string) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.RenameError != nil {
return fs.RenameError
}
oldPath = filepath.Join(oldPath)
newPath = filepath.Join(newPath)
if fs.files[filepath.Dir(newPath)] == nil {
return errors.New("Parent directory does not exist")
}
stats := fs.files[oldPath]
if stats == nil {
return errors.New("Old path did not exist")
}
fs.RenameOldPaths = append(fs.RenameOldPaths, oldPath)
fs.RenameNewPaths = append(fs.RenameNewPaths, newPath)
newStats := fs.getOrCreateFile(newPath)
newStats.Content = stats.Content
newStats.FileMode = stats.FileMode
newStats.FileType = stats.FileType
// Ignore error from RemoveAll
fs.removeAll(oldPath)
return nil
}
func (fs *FakeFileSystem) Symlink(oldPath, newPath string) (err error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.SymlinkError == nil {
stats := fs.getOrCreateFile(newPath)
stats.FileType = FakeFileTypeSymlink
stats.SymlinkTarget = oldPath
return
}
err = fs.SymlinkError
return
}
func (fs *FakeFileSystem) ReadLink(symlinkPath string) (string, error) {
if fs.ReadLinkError != nil {
return "", fs.ReadLinkError
}
symlinkPath = filepath.Join(symlinkPath)
stat := fs.GetFileTestStat(symlinkPath)
if stat != nil {
return stat.SymlinkTarget, nil
}
return "", os.ErrNotExist
}
func (fs *FakeFileSystem) CopyFile(srcPath, dstPath string) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.CopyFileError != nil {
return fs.CopyFileError
}
srcPath = filepath.Join(srcPath)
dstPath = filepath.Join(dstPath)
fs.files[dstPath] = fs.files[srcPath]
return nil
}
func (fs *FakeFileSystem) CopyDir(srcPath, dstPath string) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.CopyDirError != nil {
return fs.CopyDirError
}
srcPath = filepath.Join(srcPath) + string(os.PathSeparator)
dstPath = filepath.Join(dstPath)
for filePath, fileStats := range fs.files {
if strings.HasPrefix(filePath, srcPath) {
dstPath := filepath.Join(dstPath, filePath[len(srcPath)-1:])
fs.files[dstPath] = fileStats
}
}
return nil
}
func (fs *FakeFileSystem) TempFile(prefix string) (file boshsys.File, err error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.TempFileError != nil {
return nil, fs.TempFileError
}
if fs.ReturnTempFile != nil {
file = fs.ReturnTempFile
} else {
file, err = os.Open("/dev/null")
if err != nil {
err = bosherr.WrapError(err, "Opening /dev/null")
return
}
}
// Make sure to record a reference for FileExist, etc. to work
stats := fs.getOrCreateFile(file.Name())
stats.FileType = FakeFileTypeFile
return
}
func (fs *FakeFileSystem) TempDir(prefix string) (string, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.TempDirError != nil {
return "", fs.TempDirError
}
var path string
if len(fs.TempDirDir) > 0 {
path = fs.TempDirDir
} else if fs.TempDirDirs != nil {
if len(fs.TempDirDirs) == 0 {
return "", errors.New("Failed to create new temp dir: TempDirDirs is empty")
}
path = fs.TempDirDirs[0]
fs.TempDirDirs = fs.TempDirDirs[1:]
} else {
uuid, err := gouuid.NewV4()
if err != nil {
return "", err
}
path = uuid.String()
}
// Make sure to record a reference for FileExist, etc. to work
stats := fs.getOrCreateFile(path)
stats.FileType = FakeFileTypeDir
return path, nil
}
func (fs *FakeFileSystem) RegisterRemoveAllError(path string, err error) {
if _, ok := fs.removeAllErrorByPath[path]; ok {
panic(fmt.Sprintf("RemoveAll error is already set for path: %s", path))
}
fs.removeAllErrorByPath[path] = err
}
func (fs *FakeFileSystem) RemoveAll(path string) error {
if path == "" {
panic("RemoveAll requires path")
}
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
if fs.RemoveAllError != nil {
return fs.RemoveAllError
}
path = filepath.Join(path)
if fs.removeAllErrorByPath[path] != nil {
return fs.removeAllErrorByPath[path]
}
return fs.removeAll(path)
}
func (fs *FakeFileSystem) removeAll(path string) error {
path = filepath.Join(path)
fileInfo := fs.files[path]
if fileInfo != nil {
delete(fs.files, path)
if fileInfo.FileType != FakeFileTypeDir {
return nil
}
}
// path must be a dir
path = path + string(os.PathSeparator)
filesToRemove := []string{}
for name := range fs.files {
if strings.HasPrefix(name, path) {
filesToRemove = append(filesToRemove, name)
}
}
for _, name := range filesToRemove {
delete(fs.files, name)
}
return nil
}
func (fs *FakeFileSystem) Glob(pattern string) (matches []string, err error) {
remainingMatches, found := fs.globsMap[pattern]
if found {
matches = remainingMatches[0]
if len(remainingMatches) > 1 {
fs.globsMap[pattern] = remainingMatches[1:]
}
} else {
matches = []string{}
}
return matches, fs.GlobErr
}
func (fs *FakeFileSystem) Walk(root string, walkFunc filepath.WalkFunc) error {
if fs.WalkErr != nil {
return walkFunc("", nil, fs.WalkErr)
}
var paths []string
for path := range fs.files {
paths = append(paths, path)
}
sort.Strings(paths)
root = filepath.Join(root) + string(os.PathSeparator)
for _, path := range paths {
fileStats := fs.files[path]
if strings.HasPrefix(path, root) {
fakeFile := NewFakeFile(path, fs)
fakeFile.Stats = fileStats
fileInfo, _ := fakeFile.Stat()
err := walkFunc(path, fileInfo, nil)
if err != nil {
return err
}
}
}
return nil
}
func (fs *FakeFileSystem) SetGlob(pattern string, matches ...[]string) {
fs.globsMap[pattern] = matches
}
func (fs *FakeFileSystem) getOrCreateFile(path string) *FakeFileStats {
path = filepath.Join(path)
stats := fs.files[path]
if stats == nil {
stats = new(FakeFileStats)
fs.files[path] = stats
}
return stats
}