@cryptotaxi247 / kubo / commits / 6bf442908

test: check behavior of loading UnixFS sharded directories with missing shards

Adin Schmahmann committed Sep 23, 2021 at 14:12 UTC 6bf442908246783758fb76b653d18359f7fc37f8
1 file changed +79
core/coreapi/test/path_test.go new
+79
@@ -0,0 +1,79 @@
1 +package test
2 +
3 +import (
4 + "context"
5 + "strconv"
6 + "testing"
7 + "time"
8 +
9 + files "github.com/ipfs/go-ipfs-files"
10 + "github.com/ipfs/go-merkledag"
11 + uio "github.com/ipfs/go-unixfs/io"
12 + "github.com/ipfs/interface-go-ipfs-core/options"
13 + "github.com/ipfs/interface-go-ipfs-core/path"
14 + "github.com/ipld/go-ipld-prime"
15 +)
16 +
17 +
18 +func TestPathUnixFSHAMTPartial(t *testing.T) {
19 + ctx, cancel := context.WithCancel(context.Background())
20 + defer cancel()
21 +
22 + // Create a node
23 + apis, err := NodeProvider{}.MakeAPISwarm(ctx, true, 1)
24 + if err != nil {
25 + t.Fatal(err)
26 + }
27 + a := apis[0]
28 +
29 + // Setting this after instantiating the swarm so that it's not clobbered by loading the go-ipfs config
30 + prevVal := uio.UseHAMTSharding
31 + uio.UseHAMTSharding = true
32 + defer func() {
33 + uio.UseHAMTSharding = prevVal
34 + }()
35 +
36 + // Create and add a sharded directory
37 + dir := make(map[string]files.Node)
38 + // Make sure we have at least two levels of sharding
39 + for i := 0; i < uio.DefaultShardWidth + 1; i++ {
40 + dir[strconv.Itoa(i)] = files.NewBytesFile([]byte(strconv.Itoa(i)))
41 + }
42 +
43 + r, err := a.Unixfs().Add(ctx, files.NewMapDirectory(dir), options.Unixfs.Pin(false))
44 + if err != nil {
45 + t.Fatal(err)
46 + }
47 +
48 + // Get the root of the directory
49 + nd, err := a.Dag().Get(ctx, r.Cid())
50 + if err != nil {
51 + t.Fatal(err)
52 + }
53 +
54 + // Make sure the root is a DagPB node (this API might change in the future to account for ADLs)
55 + _ = nd.(ipld.Node)
56 + pbNode := nd.(*merkledag.ProtoNode)
57 +
58 + // Remove one of the sharded directory blocks
59 + if err := a.Block().Rm(ctx, path.IpfsPath(pbNode.Links()[0].Cid)); err != nil {
60 + t.Fatal(err)
61 + }
62 +
63 + // Try and resolve each of the entries in the sharded directory which will result in pathing over the missing block
64 + //
65 + // Note: we could just check a particular path here, but it would require either greater use of the HAMT internals
66 + // or some hard coded values in the test both of which would be a pain to follow.
67 + for k := range dir {
68 + // The node will go out to the (non-existent) network looking for the missing block. Make sure we're erroring
69 + // because we exceeded the timeout on our query
70 + timeoutCtx, timeoutCancel := context.WithTimeout(ctx, time.Second * 1)
71 + _, err := a.ResolveNode(timeoutCtx, path.Join(r, k))
72 + if err != nil {
73 + if timeoutCtx.Err() == nil {
74 + t.Fatal(err)
75 + }
76 + }
77 + timeoutCancel()
78 + }
79 +}