forked from vladimirvivien/gowfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsio_test.go
277 lines (224 loc) · 7.54 KB
/
fsio_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
269
270
271
272
273
274
275
276
277
package webhdfs
import "testing"
import "net/url"
import "fmt"
import "log"
import "bytes"
import "io/ioutil"
import "net/http"
import "strings"
import "net/http/httptest"
func Test_Create(t *testing.T) {
// start a new server to handle redirect
server1 := mockServerFor_WriteFile()
servUrl, _ := url.Parse(server1.URL)
server2 := mockServerFor_CreatFile(servUrl)
defer server2.Close()
defer server1.Close()
t.Logf("Test_Create() - Started httptest.Server on %v", server2.URL)
url, _ := url.Parse(server2.URL)
conf := Configuration{Addr: url.Host}
fs, _ := NewFileSystem(conf)
_, err := fs.Create(
bytes.NewBufferString("Hello webhdfs users!"),
Path{Name: "/testing/newfile"},
false,
0,
0,
0700,
0,
)
if err != nil {
t.Fatal(err)
}
}
func Test_Open(t *testing.T) {
server := mockServerFor_OpenAndRead()
defer server.Close()
t.Logf("Started httptest.Server on %v", server.URL)
url, _ := url.Parse(server.URL)
conf := Configuration{Addr: url.Host}
fs, _ := NewFileSystem(conf)
data, err := fs.Open(Path{Name: "/test"}, 0, 512, 2048)
if err != nil {
t.Fatal(err)
}
defer data.Close() // make sure to close.
rcvdData, _ := ioutil.ReadAll(data)
expectedData := []byte("Hello, webhdfs user!")
if string(rcvdData) != string(expectedData) {
t.Errorf("Open() - Expecting binary response [%v], but got [%v]",
string(expectedData), string(rcvdData))
}
}
func Test_Append(t *testing.T) {
// start a new server to handle redirect
server1 := mockServerFor_Append()
servUrl, _ := url.Parse(server1.URL)
server2 := mockServerFor_OpenForAppend(servUrl)
defer server2.Close()
defer server1.Close()
t.Logf("Test_Append() - Started httptest.Server on %v", server2.URL)
url, _ := url.Parse(server2.URL)
conf := Configuration{Addr: url.Host}
fs, _ := NewFileSystem(conf)
ok, err := fs.Append(bytes.NewBufferString("Hello webhdfs users!"),
Path{Name: "/testing/existing.f"}, 4096)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Errorf("URL does not contain expected value, expecting %v", "/testing/newfile")
}
}
func Test_Concat(t *testing.T) {
server := mockServerFor_Concat()
defer server.Close()
t.Logf("Started httptest.Server on %v", server.URL)
url, _ := url.Parse(server.URL)
conf := Configuration{Addr: url.Host}
fs, _ := NewFileSystem(conf)
_, err := fs.Concat(Path{Name: "/testing/concat.f"}, []string{"a/b/c", "e/f/g"})
if err != nil {
t.Fatal(err)
}
}
// ***************************** Mock Servers for Tests **********************//
func mockServerFor_OpenAndRead() *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
log.Fatalf("Expecting Request.Method GET, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_OPEN {
log.Fatal(`Server Missing expected URL parameter: op=` + OP_OPEN)
}
if q.Get("offset") != "0" {
log.Fatalf("Expected param offset to be 0, but was %v", q.Get("offset"))
}
if q.Get("length") != "512" {
log.Fatalf("Expected param offset to be 512, but was %v", q.Get("length"))
}
if q.Get("buffersize") != "2048" {
log.Fatalf("Expected param offset to be 2048, but was %v", q.Get("buffersize"))
}
fmt.Fprintf(rsp, "Hello, webhdfs user!")
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func mockServerFor_CreatFile(redir *url.URL) *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "PUT" {
log.Fatalf("CreateFile() - Expecting Request.Method PUT, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_CREATE {
log.Fatalf("Server Missing expected URL parameter: op=%v", OP_CREATE)
}
if q.Get("blocksize") != "134217728" {
log.Fatalf("Expected param blocksize to be 134217728, but was %v", q.Get("blocksize"))
}
if q.Get("replication") != "3" {
log.Fatalf("Expected param replciation to be 3, but was %v", q.Get("replication"))
}
if q.Get("permission") != "700" {
log.Fatalf("Expected param permission to be 700, but was %v", q.Get("permission"))
}
if q.Get("buffersize") != "4096" {
log.Fatalf("Expected param offset to be 4096, but was %v", q.Get("buffersize"))
}
rsp.Header().Set("Location", redir.Scheme+"://"+redir.Host+req.URL.String())
rsp.WriteHeader(http.StatusSeeOther)
fmt.Fprintf(rsp, "")
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func mockServerFor_WriteFile() *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "PUT" {
log.Fatalf("WriteFile() - Expecting Request.Method PUT, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_CREATE {
log.Fatalf("Server Missing expected URL parameter: op=%v", OP_CREATE)
}
if q.Get("blocksize") != "134217728" {
log.Fatalf("Expected param blocksize to be 134217728, but was %v", q.Get("blocksize"))
}
if q.Get("replication") != "3" {
log.Fatalf("Expected param replication to be 3, but was %v", q.Get("replication"))
}
if q.Get("permission") == "" {
log.Fatalf("Expected param permission, but was empty.")
}
if q.Get("buffersize") != "4096" {
log.Fatalf("Expected param offset to be 4096, but was %v", q.Get("buffersize"))
}
// ensure data maded it
data, _ := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if string(data) != "Hello webhdfs users!" {
log.Fatalf("Expected data not posted to server. Server got %v", string(data))
}
rsp.WriteHeader(http.StatusCreated)
fmt.Fprintf(rsp, "")
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func mockServerFor_OpenForAppend(redir *url.URL) *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Fatalf("OpenForAppend() - Expecting Request.Method POST, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_APPEND {
log.Fatalf("Server Missing expected URL parameter: op=%v", OP_APPEND)
}
if q.Get("buffersize") != "4096" {
log.Fatalf("Expected param offset to be 4096, but was %v", q.Get("buffersize"))
}
rsp.Header().Set("Location", redir.Scheme+"://"+redir.Host+req.URL.String())
rsp.WriteHeader(http.StatusSeeOther)
fmt.Fprintf(rsp, "")
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func mockServerFor_Append() *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Fatalf("Append() - Expecting Request.Method POST, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_APPEND {
log.Fatalf("Server Missing expected URL parameter: op=%v", OP_APPEND)
}
if q.Get("buffersize") != "4096" {
log.Fatalf("Expected param offset to be 4096, but was %v", q.Get("buffersize"))
}
// ensure data maded it
data, _ := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if string(data) != "Hello webhdfs users!" {
log.Fatalf("Expected data not posted to server. Server got %v", string(data))
}
rsp.WriteHeader(http.StatusOK)
fmt.Fprintf(rsp, "")
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func mockServerFor_Concat() *httptest.Server {
handler := func(rsp http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Fatalf("Expecting Request.Method POST, but got %v", req.Method)
}
q := req.URL.Query()
if q.Get("op") != OP_CONCAT {
log.Fatalf("Server Missing expected URL parameter: op=%v", OP_CONCAT)
}
if q.Get("sources") != strings.Join([]string{"a/b/c", "e/f/g"}, ",") {
log.Fatalf("Expected param sources a/b/c, e/f/g, but was %v", q.Get("sources"))
}
fmt.Fprintf(rsp, "")
}
return httptest.NewServer(http.HandlerFunc(handler))
}