@cryptotaxi247 / kubo / commits / 65ffff241

Add tests that PosInfo() is getting set.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Oct 24, 2016 at 16:37 UTC 65ffff241806279547372a9bd4bb40f4af6b8b96
1 file changed +115 -1
core/coreunix/add_test.go
+115 -1
@@ -2,20 +2,26 @@ package coreunix
2
3 import (
4 "bytes"
5 + "context"
6 "io"
7 "io/ioutil"
8 + "math/rand"
9 + "os"
10 "testing"
11 "time"
12
13 + "github.com/ipfs/go-ipfs/blocks"
14 + "github.com/ipfs/go-ipfs/blocks/blockstore"
15 + "github.com/ipfs/go-ipfs/blockservice"
16 "github.com/ipfs/go-ipfs/commands/files"
17 "github.com/ipfs/go-ipfs/core"
18 dag "github.com/ipfs/go-ipfs/merkledag"
19 "github.com/ipfs/go-ipfs/pin/gc"
20 "github.com/ipfs/go-ipfs/repo"
21 "github.com/ipfs/go-ipfs/repo/config"
22 + pi "github.com/ipfs/go-ipfs/thirdparty/posinfo"
23 "github.com/ipfs/go-ipfs/thirdparty/testutil"
24
18 - "context"
25 cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
26 )
27
@@ -162,3 +168,111 @@ func TestAddGCLive(t *testing.T) {
168 t.Fatal(err)
169 }
170 }
171 +
172 +func testAddWPosInfo(t *testing.T, rawLeaves bool) {
173 + r := &repo.Mock{
174 + C: config.Config{
175 + Identity: config.Identity{
176 + PeerID: "Qmfoo", // required by offline node
177 + },
178 + },
179 + D: testutil.ThreadSafeCloserMapDatastore(),
180 + }
181 + node, err := core.NewNode(context.Background(), &core.BuildCfg{Repo: r})
182 + if err != nil {
183 + t.Fatal(err)
184 + }
185 +
186 + bs := &testBlockstore{GCBlockstore: node.Blockstore, expectedPath: "/tmp/foo.txt", t: t}
187 + bserv := blockservice.New(bs, node.Exchange)
188 + dserv := dag.NewDAGService(bserv)
189 + adder, err := NewAdder(context.Background(), node.Pinning, bs, dserv)
190 + if err != nil {
191 + t.Fatal(err)
192 + }
193 + adder.Out = make(chan interface{})
194 + adder.Progress = true
195 + adder.RawLeaves = rawLeaves
196 +
197 + data := make([]byte, 5*1024*1024)
198 + rand.New(rand.NewSource(2)).Read(data) // Rand.Read never returns an error
199 + fileData := ioutil.NopCloser(bytes.NewBuffer(data))
200 + fileInfo := dummyFileInfo{"foo.txt", int64(len(data)), time.Now()}
201 + file := files.NewReaderFile("foo.txt", "/tmp/foo.txt", fileData, &fileInfo)
202 +
203 + go func() {
204 + defer close(adder.Out)
205 + err = adder.AddFile(file)
206 + if err != nil {
207 + t.Fatal(err)
208 + }
209 + }()
210 + for _ = range adder.Out {
211 + }
212 +
213 + if bs.countAtOffsetZero != 2 {
214 + t.Fatal("expected 2 blocks with an offset at zero (one root and one leafh), got", bs.countAtOffsetZero)
215 + }
216 + if bs.countAtOffsetNonZero != 19 {
217 + // note: the exact number will depend on the size and the sharding algo. used
218 + t.Fatal("expected 19 blocks with an offset > 0, got", bs.countAtOffsetNonZero)
219 + }
220 +}
221 +
222 +func TestAddWPosInfo(t *testing.T) {
223 + testAddWPosInfo(t, false)
224 +}
225 +
226 +func TestAddWPosInfoAndRawLeafs(t *testing.T) {
227 + testAddWPosInfo(t, true)
228 +}
229 +
230 +
231 +type testBlockstore struct {
232 + blockstore.GCBlockstore
233 + expectedPath string
234 + t *testing.T
235 + countAtOffsetZero int
236 + countAtOffsetNonZero int
237 +}
238 +
239 +func (bs *testBlockstore) Put(block blocks.Block) error {
240 + bs.CheckForPosInfo(block)
241 + return bs.GCBlockstore.Put(block)
242 +}
243 +
244 +func (bs *testBlockstore) PutMany(blocks []blocks.Block) error {
245 + for _, blk := range blocks {
246 + bs.CheckForPosInfo(blk)
247 + }
248 + return bs.GCBlockstore.PutMany(blocks)
249 +}
250 +
251 +func (bs *testBlockstore) CheckForPosInfo(block blocks.Block) error {
252 + fsn, ok := block.(*pi.FilestoreNode)
253 + if ok {
254 + posInfo := fsn.PosInfo
255 + if posInfo.FullPath != bs.expectedPath {
256 + bs.t.Fatal("PosInfo does not have the expected path")
257 + }
258 + if posInfo.Offset == 0 {
259 + bs.countAtOffsetZero += 1
260 + } else {
261 + bs.countAtOffsetNonZero += 1
262 + }
263 + }
264 + return nil
265 +}
266 +
267 +type dummyFileInfo struct {
268 + name string
269 + size int64
270 + modTime time.Time
271 +}
272 +
273 +func (fi *dummyFileInfo) Name() string { return fi.name }
274 +func (fi *dummyFileInfo) Size() int64 { return fi.size }
275 +func (fi *dummyFileInfo) Mode() os.FileMode { return 0 }
276 +func (fi *dummyFileInfo) ModTime() time.Time { return fi.modTime }
277 +func (fi *dummyFileInfo) IsDir() bool { return false }
278 +func (fi *dummyFileInfo) Sys() interface{} { return nil }