/
add.go
120 lines (105 loc) · 3.05 KB
/
add.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
package test
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
"github.com/bittorrent/go-btfs/core"
"github.com/bittorrent/go-btfs/core/coreapi"
"github.com/bittorrent/go-btfs/core/coreunix"
"github.com/bittorrent/go-btfs/repo"
chunker "github.com/bittorrent/go-btfs-chunker"
config "github.com/bittorrent/go-btfs-config"
files "github.com/bittorrent/go-btfs-files"
ftutil "github.com/bittorrent/go-unixfs/util"
coreiface "github.com/bittorrent/interface-go-btfs-core"
"github.com/bittorrent/interface-go-btfs-core/path"
cid "github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
)
const (
testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
TestRsDataSize = 10
TestRsParitySize = 20
)
// HelpTestMockRepo creates the bare minimum mock repo and returns node
func HelpTestMockRepo(t *testing.T, cfg *config.Config) *core.IpfsNode {
mc := config.Config{}
if cfg != nil {
mc = *cfg
}
mc.Identity = config.Identity{
PeerID: testPeerID, // required by offline node
}
r := &repo.Mock{
C: mc,
D: syncds.MutexWrap(datastore.NewMapDatastore()),
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{Repo: r})
if err != nil {
t.Fatal(err)
}
return node
}
// HelpTestAddWithReedSolomomonMetadata is both a helper to testing this feature
// and also a helper to add a reed solomon file for other features.
// It returns a mock node, api, and added hash (cid).
func HelpTestAddWithReedSolomonMetadata(t *testing.T) (*core.IpfsNode, coreiface.CoreAPI, cid.Cid, []byte) {
ctx := context.Background()
node := HelpTestMockRepo(t, nil)
out := make(chan interface{})
adder, err := coreunix.NewAdder(ctx, node.Pinning, node.Blockstore, node.DAG)
if err != nil {
t.Fatal(err)
}
adder.Out = out
// Set to default reed solomon for metadata
dsize, psize, csize := TestRsDataSize, TestRsParitySize, 262144
adder.Chunker = fmt.Sprintf("reed-solomon-%d-%d-%d", dsize, psize, csize)
fb := []byte("testfileA")
fsize := len(fb)
rfa := files.NewBytesFile(fb)
go func() {
defer close(out)
_, err := adder.AddAllAndPin(ctx, rfa)
if err != nil {
t.Fatal(err)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
var addedHash cid.Cid
select {
case o := <-out:
addedHash = o.(*coreiface.AddEvent).Path.Cid()
case <-ctx.Done():
t.Fatal("add timed out")
}
api, err := coreapi.NewCoreAPI(node)
if err != nil {
t.Fatal(err)
}
// Extract and check metadata
b, err := coreunix.GetMetaData(ctx, api, path.IpfsPath(addedHash))
if err != nil {
t.Fatal(err)
}
var rsMeta chunker.RsMetaMap
b1 := ftutil.GetMetadataElement(b)
err = json.Unmarshal(b1, &rsMeta)
if err != nil {
t.Fatal(err)
}
if rsMeta.NumData != uint64(dsize) {
t.Fatal("reed solomon metadata num data does not match")
}
if rsMeta.NumParity != uint64(psize) {
t.Fatal("reed solomon metadata num parity does not match")
}
if rsMeta.FileSize != uint64(fsize) {
t.Fatal("reed solomon metadata file size does not match")
}
return node, api, addedHash, fb
}