add some fuse tests for readonly ipfs fs
Jeromy committed
Feb 23, 2015 at 20:39 UTC
856c87bdc093b6a6642b262fd4c1321bb94e9c62
1 file changed
+166
fuse/readonly/ipfs_test.go
new
+166
@@ -0,0 +1,166 @@
1
+package readonly
2
+
3
+import (
4
+ "bytes"
5
+ "crypto/rand"
6
+ //context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
+ "io/ioutil"
8
+ "os"
9
+ "testing"
10
+
11
+ fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
12
+
13
+ core "github.com/jbenet/go-ipfs/core"
14
+ importer "github.com/jbenet/go-ipfs/importer"
15
+ chunk "github.com/jbenet/go-ipfs/importer/chunk"
16
+ dag "github.com/jbenet/go-ipfs/merkledag"
17
+ uio "github.com/jbenet/go-ipfs/unixfs/io"
18
+ u "github.com/jbenet/go-ipfs/util"
19
+ ci "github.com/jbenet/go-ipfs/util/testutil/ci"
20
+)
21
+
22
+func maybeSkipFuseTests(t *testing.T) {
23
+ if ci.NoFuse() {
24
+ t.Skip("Skipping FUSE tests")
25
+ }
26
+}
27
+
28
+func randBytes(size int) []byte {
29
+ b := make([]byte, size)
30
+ rand.Read(b)
31
+ return b
32
+}
33
+
34
+func randObj(t *testing.T, nd *core.IpfsNode, size int64) (*dag.Node, []byte) {
35
+ buf := make([]byte, size)
36
+ u.NewTimeSeededRand().Read(buf)
37
+ read := bytes.NewReader(buf)
38
+ obj, err := importer.BuildTrickleDagFromReader(read, nd.DAG, nil, chunk.DefaultSplitter)
39
+ if err != nil {
40
+ t.Fatal(err)
41
+ }
42
+
43
+ return obj, buf
44
+}
45
+
46
+func setupIpfsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.Mount) {
47
+ maybeSkipFuseTests(t)
48
+
49
+ var err error
50
+ if node == nil {
51
+ node, err = core.NewMockNode()
52
+ if err != nil {
53
+ t.Fatal(err)
54
+ }
55
+ }
56
+
57
+ fs := NewFileSystem(node)
58
+ mnt, err := fstest.MountedT(t, fs)
59
+ if err != nil {
60
+ t.Fatal(err)
61
+ }
62
+
63
+ return node, mnt
64
+}
65
+
66
+// Test writing an object and reading it back through fuse
67
+func TestIpfsBasicRead(t *testing.T) {
68
+ if testing.Short() {
69
+ t.SkipNow()
70
+ }
71
+ nd, mnt := setupIpfsTest(t, nil)
72
+ defer mnt.Close()
73
+
74
+ fi, data := randObj(t, nd, 10000)
75
+ k, err := fi.Key()
76
+ if err != nil {
77
+ t.Fatal(err)
78
+ }
79
+
80
+ fname := mnt.Dir + "/" + k.String()
81
+ rbuf, err := ioutil.ReadFile(fname)
82
+ if err != nil {
83
+ t.Fatal(err)
84
+ }
85
+
86
+ if !bytes.Equal(rbuf, data) {
87
+ t.Fatal("Incorrect Read!")
88
+ }
89
+}
90
+
91
+// Test writing a file and reading it back
92
+func TestIpfsBasicDirRead(t *testing.T) {
93
+ if testing.Short() {
94
+ t.SkipNow()
95
+ }
96
+ nd, mnt := setupIpfsTest(t, nil)
97
+ defer mnt.Close()
98
+
99
+ // Make a 'file'
100
+ fi, data := randObj(t, nd, 10000)
101
+ k, err := fi.Key()
102
+ if err != nil {
103
+ t.Fatal(err)
104
+ }
105
+
106
+ // Make a directory and put that file in it
107
+ db := uio.NewDirectory(nd.DAG)
108
+ err = db.AddChild("actual", k)
109
+ if err != nil {
110
+ t.Fatal(err)
111
+ }
112
+
113
+ d1nd := db.GetNode()
114
+ d1ndk, err := nd.DAG.Add(d1nd)
115
+ if err != nil {
116
+ t.Fatal(err)
117
+ }
118
+
119
+ dirname := mnt.Dir + "/" + d1ndk.String()
120
+ fname := dirname + "/actual"
121
+ rbuf, err := ioutil.ReadFile(fname)
122
+ if err != nil {
123
+ t.Fatal(err)
124
+ }
125
+
126
+ dirents, err := ioutil.ReadDir(dirname)
127
+ if err != nil {
128
+ t.Fatal(err)
129
+ }
130
+ if len(dirents) != 1 {
131
+ t.Fatal("Bad directory entry count")
132
+ }
133
+ if dirents[0].Name() != "actual" {
134
+ t.Fatal("Bad directory entry")
135
+ }
136
+
137
+ if !bytes.Equal(rbuf, data) {
138
+ t.Fatal("Incorrect Read!")
139
+ }
140
+}
141
+
142
+// Test to make sure the filesystem reports file sizes correctly
143
+func TestFileSizeReporting(t *testing.T) {
144
+ if testing.Short() {
145
+ t.SkipNow()
146
+ }
147
+ nd, mnt := setupIpfsTest(t, nil)
148
+ defer mnt.Close()
149
+
150
+ fi, data := randObj(t, nd, 10000)
151
+ k, err := fi.Key()
152
+ if err != nil {
153
+ t.Fatal(err)
154
+ }
155
+
156
+ fname := mnt.Dir + "/" + k.String()
157
+
158
+ finfo, err := os.Stat(fname)
159
+ if err != nil {
160
+ t.Fatal(err)
161
+ }
162
+
163
+ if finfo.Size() != int64(len(data)) {
164
+ t.Fatal("Read incorrect size from stat!")
165
+ }
166
+}