forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dir_test.go
164 lines (150 loc) · 6.53 KB
/
Dir_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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
import (
"bazil.org/fuse"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
// Testing whether attributes are cached
func TestAttributeCaching(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
InitLogger(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().Stat("/testDir").Return(Attrs{Name: "testDir", Mode: os.ModeDir | 0757}, nil)
dir, err := root.(*Dir).Lookup(nil, "testDir")
assert.Nil(t, err)
// Second call to Lookup(), shouldn't re-issue Stat() on backend
dir1, err1 := root.(*Dir).Lookup(nil, "testDir")
assert.Nil(t, err1)
assert.Equal(t, dir, dir1) // must return the same entry w/o doing Stat on the backend
// Retrieving attributes from cache
var attr fuse.Attr
assert.Nil(t, dir.Attr(nil, &attr))
assert.Equal(t, os.ModeDir|0757, attr.Mode)
mockClock.NotifyTimeElapsed(2 * time.Second)
assert.Nil(t, dir.Attr(nil, &attr))
assert.Equal(t, os.ModeDir|0757, attr.Mode)
// Lookup should be stil done from cache
dir1, err1 = root.(*Dir).Lookup(nil, "testDir")
assert.Nil(t, err1)
// After 30+31=61 seconds, attempt to query attributes should re-issue a Stat() request to the backend
// this time returing different attributes (555 instead of 757)
hdfsAccessor.EXPECT().Stat("/testDir").Return(Attrs{Name: "testDir", Mode: os.ModeDir | 0555}, nil)
mockClock.NotifyTimeElapsed(4 * time.Second)
assert.Nil(t, dir.Attr(nil, &attr))
assert.Equal(t, os.ModeDir|0555, attr.Mode)
dir1, err1 = root.(*Dir).Lookup(nil, "testDir")
assert.Nil(t, err1)
assert.Equal(t, dir, dir1)
}
// Testing whether '-allowedPrefixes' path filtering works for ReadDir
func TestReadDirWithFiltering(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"foo", "bar"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().ReadDir("/").Return([]Attrs{
{Name: "quz", Mode: os.ModeDir},
{Name: "foo", Mode: os.ModeDir},
{Name: "bar", Mode: os.ModeDir},
{Name: "foobar", Mode: os.ModeDir},
{Name: "baz", Mode: os.ModeDir},
}, nil)
dirents, err := root.(*Dir).ReadDirAll(nil)
assert.Nil(t, err)
assert.Equal(t, 2, len(dirents))
assert.Equal(t, "foo", dirents[0].Name)
assert.Equal(t, "bar", dirents[1].Name)
}
// Testing processing of .zip files if '-expandZips' isn't activated
func TestReadDirWithZipExpansionDisabled(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().ReadDir("/").Return([]Attrs{
{Name: "foo.zipx"},
{Name: "dir.zip", Mode: os.ModeDir},
{Name: "bar.zip"},
}, nil)
dirents, err := root.(*Dir).ReadDirAll(nil)
assert.Nil(t, err)
assert.Equal(t, 3, len(dirents))
assert.Equal(t, "foo.zipx", dirents[0].Name)
assert.Equal(t, "dir.zip", dirents[1].Name)
assert.Equal(t, "bar.zip", dirents[2].Name)
}
// Testing processing of .zip files if '-expandZips' is activated
func TestReadDirWithZipExpansionEnabled(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, true, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().ReadDir("/").Return([]Attrs{
{Name: "foo.zipx"},
{Name: "dir.zip", Mode: os.ModeDir},
{Name: "bar.zip"},
}, nil)
dirents, err := root.(*Dir).ReadDirAll(nil)
assert.Nil(t, err)
assert.Equal(t, 4, len(dirents))
assert.Equal(t, "foo.zipx", dirents[0].Name)
assert.Equal(t, "dir.zip", dirents[1].Name)
assert.Equal(t, "bar.zip", dirents[2].Name)
assert.Equal(t, fuse.DT_File, dirents[2].Type)
assert.Equal(t, "bar.zip@", dirents[3].Name)
assert.Equal(t, fuse.DT_Dir, dirents[3].Type)
}
// Testing whether '-allowedPrefixes' path filtering works for Lookup
func TestLookupWithFiltering(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"foo", "bar"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().Stat("/foo").Return(Attrs{Name: "foo", Mode: os.ModeDir}, nil)
_, err := root.(*Dir).Lookup(nil, "foo")
assert.Nil(t, err)
_, err = root.(*Dir).Lookup(nil, "qux")
assert.Equal(t, fuse.ENOENT, err) // Not found error, since it is not in the allowed prefixes
}
// Testing Mkdir
func TestMkdir(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"foo", "bar"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().Mkdir("/foo", os.FileMode(0757)|os.ModeDir).Return(nil)
node, err := root.(*Dir).Mkdir(nil, &fuse.MkdirRequest{Name: "foo", Mode: os.FileMode(0757) | os.ModeDir})
assert.Nil(t, err)
assert.Equal(t, "foo", node.(*Dir).Attrs.Name)
}
// Testing Chmod and Chown
func TestSetattr(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"foo", "bar"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
root, _ := fs.Root()
hdfsAccessor.EXPECT().Mkdir("/foo", os.FileMode(0757)|os.ModeDir).Return(nil)
node, _ := root.(*Dir).Mkdir(nil, &fuse.MkdirRequest{Name: "foo", Mode: os.FileMode(0757) | os.ModeDir})
hdfsAccessor.EXPECT().Chmod("/foo", os.FileMode(0777)).Return(nil)
err := node.(*Dir).Setattr(nil, &fuse.SetattrRequest{Mode: os.FileMode(0777), Valid: fuse.SetattrMode}, &fuse.SetattrResponse{})
assert.Nil(t, err)
assert.Equal(t, os.FileMode(0777), node.(*Dir).Attrs.Mode)
hdfsAccessor.EXPECT().Chown("/foo", "root", "root").Return(nil)
err = node.(*Dir).Setattr(nil, &fuse.SetattrRequest{Uid: 0, Valid: fuse.SetattrUid}, &fuse.SetattrResponse{})
assert.Nil(t, err)
assert.Equal(t, uint32(0), node.(*Dir).Attrs.Uid)
}