forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilereader_test.go
204 lines (159 loc) · 4.68 KB
/
filereader_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
package hdfs
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"hash/crc32"
"io"
"io/ioutil"
"os"
"testing"
"time"
)
const (
// many grep -b died to bring us this information...
testStr = "Abominable are the tumblers into which he pours his poison."
testStrOff = 48847
testStr2 = "http://www.gutenberg.org"
testStr2Off = 1256988
testStr2NegativeOff = -288
)
func TestFileRead(t *testing.T) {
client := getClient(t)
file, err := client.Open("/_test/foo.txt")
require.Nil(t, err)
bytes, err := ioutil.ReadAll(file)
assert.Nil(t, err)
assert.Equal(t, "bar\n", string(bytes))
info := file.Stat()
assert.False(t, info.IsDir())
assert.Equal(t, 4, info.Size())
assert.Equal(t, time.Now().Year(), info.ModTime().Year())
assert.Equal(t, time.Now().Month(), info.ModTime().Month())
}
func TestReadEmptyFile(t *testing.T) {
client := getClient(t)
touch(t, "/_test/emptyfile")
file, err := client.Open("/_test/emptyfile")
require.Nil(t, err)
bytes, err := ioutil.ReadAll(file)
assert.Nil(t, err)
assert.Equal(t, "", string(bytes))
}
func TestFileBigRead(t *testing.T) {
client := getClient(t)
file, err := client.Open("/_test/mobydick.txt")
require.Nil(t, err)
hash := crc32.NewIEEE()
n, err := io.Copy(hash, file)
assert.Nil(t, err)
assert.Equal(t, n, 1257276)
assert.Equal(t, 0xef026e98, hash.Sum32())
}
func TestFileBigReadN(t *testing.T) {
client := getClient(t)
file, err := client.Open("/_test/mobydick.txt")
require.Nil(t, err)
n, err := io.CopyN(ioutil.Discard, file, 1000000)
assert.Nil(t, err)
assert.Equal(t, n, 1000000)
}
func TestFileReadAt(t *testing.T) {
client := getClient(t)
file, err := client.Open("/_test/mobydick.txt")
require.Nil(t, err)
buf := make([]byte, len(testStr))
off := 0
for off < len(buf) {
n, err := file.ReadAt(buf[off:], int64(testStrOff+off))
assert.Nil(t, err)
assert.True(t, n > 0)
off += n
}
assert.Equal(t, string(buf), testStr)
buf = make([]byte, len(testStr2))
off = 0
for off < len(buf) {
n, err := file.ReadAt(buf[off:], int64(testStr2Off+off))
assert.Nil(t, err)
assert.True(t, n > 0)
off += n
}
assert.Equal(t, testStr2, string(buf))
}
func TestFileSeek(t *testing.T) {
client := getClient(t)
file, err := client.Open("/_test/mobydick.txt")
require.Nil(t, err)
buf := new(bytes.Buffer)
off, err := file.Seek(testStrOff, 0)
assert.Nil(t, err)
assert.Equal(t, testStrOff, off)
n, err := io.CopyN(buf, file, int64(len(testStr)))
assert.Nil(t, err)
assert.Equal(t, len(testStr), n)
assert.Equal(t, testStr, string(buf.Bytes()))
// seek backwards and read it again
off, err = file.Seek(-1*int64(len(testStr)), 1)
assert.Nil(t, err)
assert.Equal(t, testStrOff, off)
buf.Reset()
n, err = io.CopyN(buf, file, int64(len(testStr)))
assert.Nil(t, err)
assert.Equal(t, len(testStr), n)
assert.Equal(t, testStr, string(buf.Bytes()))
// now seek forward to another block and read a string
off, err = file.Seek(testStr2NegativeOff, 2)
assert.Nil(t, err)
assert.Equal(t, testStr2Off, off)
buf.Reset()
n, err = io.CopyN(buf, file, int64(len(testStr2)))
assert.Nil(t, err)
assert.Equal(t, len(testStr2), n)
assert.Equal(t, testStr2, string(buf.Bytes()))
}
func TestFileReadDir(t *testing.T) {
client := getClient(t)
mkdirp(t, "/_test/fulldir3")
mkdirp(t, "/_test/fulldir3/dir")
touch(t, "/_test/fulldir3/1")
touch(t, "/_test/fulldir3/2")
touch(t, "/_test/fulldir3/3")
file, err := client.Open("/_test/fulldir3")
require.Nil(t, err)
res, err := file.Readdir(2)
require.Equal(t, 2, len(res))
assert.Equal(t, "1", res[0].Name())
assert.Equal(t, "2", res[1].Name())
res, err = file.Readdir(5)
require.Equal(t, 2, len(res))
assert.Equal(t, "3", res[0].Name())
assert.Equal(t, "dir", res[1].Name())
res, err = file.Readdir(0)
require.Equal(t, 4, len(res))
assert.Equal(t, "1", res[0].Name())
assert.Equal(t, "2", res[1].Name())
assert.Equal(t, "3", res[2].Name())
assert.Equal(t, "dir", res[3].Name())
}
func TestFileReadDirnames(t *testing.T) {
client := getClient(t)
mkdirp(t, "/_test/fulldir4")
mkdirp(t, "/_test/fulldir4/dir")
touch(t, "/_test/fulldir4/1")
touch(t, "/_test/fulldir4/2")
touch(t, "/_test/fulldir4/3")
file, err := client.Open("/_test/fulldir4")
require.Nil(t, err)
res, err := file.Readdirnames(0)
require.Equal(t, 4, len(res))
assert.Equal(t, []string{"1", "2", "3", "dir"}, res)
}
func TestOpenFileWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
file, err := otherClient.Open("/_test/accessdenied/foo")
assert.Nil(t, file)
assertPathError(t, err, "open", "/_test/accessdenied/foo", os.ErrPermission)
}