@@ -14,6 +14,7 @@ import (
14
14
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
15
15
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
16
16
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
17
+
17
18
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
18
19
key "github.com/ipfs/go-ipfs/blocks/key"
19
20
bserv "github.com/ipfs/go-ipfs/blockservice"
@@ -112,6 +113,20 @@ func assertDirAtPath(root *Directory, path string, children []string) error {
112
113
return nil
113
114
}
114
115
116
+ func compStrArrs (a , b []string ) bool {
117
+ if len (a ) != len (b ) {
118
+ return false
119
+ }
120
+
121
+ for i := 0 ; i < len (a ); i ++ {
122
+ if a [i ] != b [i ] {
123
+ return false
124
+ }
125
+ }
126
+
127
+ return true
128
+ }
129
+
115
130
func assertFileAtPath (ds dag.DAGService , root * Directory , exp * dag.Node , path string ) error {
116
131
parts := strings .Split (path , "/" )
117
132
cur := root
@@ -188,7 +203,8 @@ func setupFsAndRoot(ctx context.Context, t *testing.T, rootname string) (*dagser
188
203
}
189
204
190
205
func TestBasic (t * testing.T ) {
191
- ctx := context .TODO ()
206
+ ctx , cancel := context .WithCancel (context .Background ())
207
+ defer cancel ()
192
208
dsp , _ , rt := setupFsAndRoot (ctx , t , "test" )
193
209
194
210
rootdir := rt .GetValue ().(* Directory )
@@ -217,7 +233,8 @@ func TestBasic(t *testing.T) {
217
233
}
218
234
219
235
func TestMkdir (t * testing.T ) {
220
- ctx := context .TODO ()
236
+ ctx , cancel := context .WithCancel (context .Background ())
237
+ defer cancel ()
221
238
_ , _ , rt := setupFsAndRoot (ctx , t , "test" )
222
239
223
240
rootdir := rt .GetValue ().(* Directory )
@@ -247,16 +264,108 @@ func TestMkdir(t *testing.T) {
247
264
}
248
265
}
249
266
250
- func compStrArrs (a , b []string ) bool {
251
- if len (a ) != len (b ) {
252
- return false
267
+ func TestFilesystemMethods (t * testing.T ) {
268
+ ctx , cancel := context .WithCancel (context .Background ())
269
+ defer cancel ()
270
+ dsp := getDagservAndPinner (t )
271
+
272
+ fs , err := NewFilesystem (ctx , dsp .ds , dsp .mp )
273
+ if err != nil {
274
+ t .Fatal (err )
253
275
}
254
276
255
- for i := 0 ; i < len (a ); i ++ {
256
- if a [i ] != b [i ] {
257
- return false
277
+ empty := & dag.Node {Data : ft .FolderPBData ()}
278
+ pf := func (_ context.Context , _ key.Key ) error { return nil }
279
+
280
+ roots := []string {"a" , "b" , "c" , "d" , "e" }
281
+ sort .Strings (roots )
282
+ for _ , r := range roots {
283
+ _ , err := fs .NewRoot (r , empty , pf )
284
+ if err != nil {
285
+ t .Fatal (err )
258
286
}
259
287
}
260
288
261
- return true
289
+ // make sure they all show up in the listing
290
+ var rootstrs []string
291
+ for _ , r := range fs .ListRoots () {
292
+ rootstrs = append (rootstrs , r .Name )
293
+ }
294
+
295
+ sort .Strings (rootstrs )
296
+
297
+ if ! compStrArrs (rootstrs , roots ) {
298
+ t .Fatal ("didnt get expected roots in fs" )
299
+ }
300
+
301
+ // make sure we can 'get' each of them
302
+ for _ , r := range roots {
303
+ _ , err := fs .GetRoot (r )
304
+ if err != nil {
305
+ t .Fatal (err )
306
+ }
307
+ }
308
+
309
+ // make sure we *cant* get ones that dont exist
310
+ _ , err = fs .GetRoot ("NOTREAL" )
311
+ if err != ErrNotExist {
312
+ t .Fatal ("expected ErrNotExist, got: " , err )
313
+ }
314
+
315
+ // make sure we cant make a root that already exists
316
+ _ , err = fs .NewRoot ("a" , empty , pf )
317
+ if err == nil {
318
+ t .Fatal ("expected create already existing root to fail" )
319
+ }
320
+
321
+ // closing a root works
322
+ final , err := fs .CloseRoot ("b" )
323
+ if err != nil {
324
+ t .Fatal (err )
325
+ }
326
+
327
+ ek , _ := empty .Key ()
328
+ if final != ek {
329
+ t .Fatal ("final key was not as expected" )
330
+ }
331
+
332
+ // closing filesystem succeeds
333
+ err = fs .Close ()
334
+ if err != nil {
335
+ t .Fatal (err )
336
+ }
337
+ }
338
+
339
+ func TestMfsFile (t * testing.T ) {
340
+ ctx , cancel := context .WithCancel (context .Background ())
341
+ defer cancel ()
342
+ dsp , _ , rt := setupFsAndRoot (ctx , t , "test" )
343
+
344
+ rootdir := rt .GetValue ().(* Directory )
345
+
346
+ nd := getRandFile (t , dsp .ds , 1000 )
347
+
348
+ err := rootdir .AddChild ("file" , nd )
349
+ if err != nil {
350
+ t .Fatal (err )
351
+ }
352
+
353
+ fsn , err := rootdir .Child ("file" )
354
+ if err != nil {
355
+ t .Fatal (err )
356
+ }
357
+
358
+ fi := fsn .(* File )
359
+
360
+ b := []byte ("THIS IS A TEST" )
361
+ n , err := fi .Write (b )
362
+ if err != nil {
363
+ t .Fatal (err )
364
+ }
365
+
366
+ if n != len (b ) {
367
+ t .Fatal ("didnt write correct number of bytes" )
368
+ }
369
+
370
+ // TODO: test more file stuff
262
371
}
0 commit comments