coreapi: test using mock swarm
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Mar 10, 2018 at 19:17 UTC
d2e6a61d1e5dad4698d8dd71106cb5c0fad32872
4 files changed
+99
-48
core/coreapi/dht.go
+18
-13
@@ -7,16 +7,16 @@ import (
7
8
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
- dag "github.com/ipfs/go-ipfs/merkledag"
11
-
12
- routing "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing"
13
- notif "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing/notifications"
14
- ipdht "gx/ipfs/QmVSep2WwKcXxMonPASsAJ3nZVjfVMKgMcaSigxKnUWpJv/go-libp2p-kad-dht"
15
- ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
16
- pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
17
- peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
18
- cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
19
- ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
10
+
11
+ dag "gx/ipfs/QmNr4E8z9bGTztvHJktp7uQaMdx9p3r9Asrq6eYk7iCh4a/go-merkledag"
12
+ peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
13
+ ipdht "gx/ipfs/QmRNxiPpZf3skMAtmDJpgHuW9uj1ukqV1zjANj9d6bmHfE/go-libp2p-kad-dht"
14
+ ipld "gx/ipfs/QmX5CsuHyVZeTLxgRSYkgLSDQKb9UjE8xnhQzCEJWWWFsC/go-ipld-format"
15
+ routing "gx/ipfs/QmY9JUvS8kbgao3XbPh6WAV3ChE2nxGKhcGTHiwMC4gmcU/go-libp2p-routing"
16
+ notif "gx/ipfs/QmY9JUvS8kbgao3XbPh6WAV3ChE2nxGKhcGTHiwMC4gmcU/go-libp2p-routing/notifications"
17
+ ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
18
+ cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
19
+ pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
20
)
21
22
var ErrNotDHT = errors.New("routing service is not a DHT")
@@ -93,12 +93,12 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
93
return nil, ErrNotDHT
94
}
95
96
- p, err = api.ResolvePath(ctx, p)
96
+ rp, err := api.ResolvePath(ctx, p)
97
if err != nil {
98
return nil, err
99
}
100
101
- c := p.Cid()
101
+ c := rp.Cid()
102
103
numProviders := settings.NumProviders
104
if numProviders < 1 {
@@ -162,7 +162,12 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
162
return errors.New("cannot provide, no connected peers")
163
}
164
165
- c := path.Cid()
165
+ rp, err := api.ResolvePath(ctx, path)
166
+ if err != nil {
167
+ return err
168
+ }
169
+
170
+ c := rp.Cid()
171
172
has, err := api.node.Blockstore.Has(c)
173
if err != nil {
core/coreapi/interface/dht.go
+2
-2
@@ -5,8 +5,8 @@ import (
5
6
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
7
8
- ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
9
- peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
8
+ peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
9
+ ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
10
)
11
12
// DhtAPI specifies the interface to the DHT
core/coreapi/name_test.go
+8
-4
@@ -21,11 +21,13 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path,
21
22
func TestBasicPublishResolve(t *testing.T) {
23
ctx := context.Background()
24
- n, api, err := makeAPIIdent(ctx, true)
24
+ nds, apis, err := makeAPISwarm(ctx, true, 2)
25
if err != nil {
26
t.Fatal(err)
27
return
28
}
29
+ n := nds[0]
30
+ api := apis[0]
31
32
p, err := addTestObject(ctx, api)
33
if err != nil {
@@ -60,11 +62,12 @@ func TestBasicPublishResolve(t *testing.T) {
62
63
func TestBasicPublishResolveKey(t *testing.T) {
64
ctx := context.Background()
63
- _, api, err := makeAPIIdent(ctx, true)
65
+ _, apis, err := makeAPISwarm(ctx, true, 2)
66
if err != nil {
67
t.Fatal(err)
68
return
69
}
70
+ api := apis[0]
71
72
k, err := api.Key().Generate(ctx, "foo")
73
if err != nil {
@@ -107,12 +110,13 @@ func TestBasicPublishResolveTimeout(t *testing.T) {
110
t.Skip("ValidTime doesn't appear to work at this time resolution")
111
112
ctx := context.Background()
110
- n, api, err := makeAPIIdent(ctx, true)
113
+ nds, apis, err := makeAPISwarm(ctx, true, 2)
114
if err != nil {
115
t.Fatal(err)
116
return
117
}
115
-
118
+ n := nds[0]
119
+ api := apis[0]
120
p, err := addTestObject(ctx, api)
121
if err != nil {
122
t.Fatal(err)
core/coreapi/unixfs_test.go
+71
-29
@@ -4,6 +4,7 @@ import (
4
"bytes"
5
"context"
6
"encoding/base64"
7
+ "fmt"
8
"io"
9
"math"
10
"strings"
@@ -14,6 +15,7 @@ import (
15
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
16
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
17
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
18
+ mock "github.com/ipfs/go-ipfs/core/mock"
19
keystore "github.com/ipfs/go-ipfs/keystore"
20
repo "github.com/ipfs/go-ipfs/repo"
21
@@ -22,8 +24,10 @@ import (
24
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
25
datastore "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore"
26
syncds "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore/sync"
27
+ mocknet "gx/ipfs/QmUEqyXr97aUbNmQADHYNknjwjjdVpJXEt1UZXmSG81EV4/go-libp2p/p2p/net/mock"
28
unixfs "gx/ipfs/QmWAfTyD6KEBm7bzqNRBPvqKrZCDtn5PGbs9V1DKfnVK59/go-unixfs"
29
config "gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
30
+ pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
31
cbor "gx/ipfs/QmepvyyduWnXHm1G7ybmGbJfQQHTAo36DjP2nvF7H7ZXjE/go-ipld-cbor"
32
)
33
@@ -36,51 +40,89 @@ var helloStr = "hello, world!"
40
// `echo -n | ipfs add`
41
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
42
39
-func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
40
- var ident config.Identity
41
- if fullIdentity {
42
- sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
43
- if err != nil {
44
- return nil, nil, err
43
+func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
44
+ mn := mocknet.New(ctx)
45
+
46
+ nodes := make([]*core.IpfsNode, n)
47
+ apis := make([]coreiface.CoreAPI, n)
48
+
49
+ for i := 0; i < n; i++ {
50
+ var ident config.Identity
51
+ if fullIdentity {
52
+ sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
53
+ if err != nil {
54
+ return nil, nil, err
55
+ }
56
+
57
+ id, err := peer.IDFromPublicKey(pk)
58
+ if err != nil {
59
+ return nil, nil, err
60
+ }
61
+
62
+ kbytes, err := sk.Bytes()
63
+ if err != nil {
64
+ return nil, nil, err
65
+ }
66
+
67
+ ident = config.Identity{
68
+ PeerID: id.Pretty(),
69
+ PrivKey: base64.StdEncoding.EncodeToString(kbytes),
70
+ }
71
+ } else {
72
+ ident = config.Identity{
73
+ PeerID: testPeerID,
74
+ }
75
}
76
47
- id, err := peer.IDFromPublicKey(pk)
48
- if err != nil {
49
- return nil, nil, err
77
+ c := config.Config{}
78
+ c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
79
+ c.Identity = ident
80
+
81
+ r := &repo.Mock{
82
+ C: c,
83
+ D: syncds.MutexWrap(datastore.NewMapDatastore()),
84
+ K: keystore.NewMemKeystore(),
85
}
86
52
- kbytes, err := sk.Bytes()
87
+ node, err := core.NewNode(ctx, &core.BuildCfg{
88
+ Repo: r,
89
+ Host: mock.MockHostOption(mn),
90
+ Online: fullIdentity,
91
+ })
92
if err != nil {
93
return nil, nil, err
94
}
95
+ nodes[i] = node
96
+ apis[i] = coreapi.NewCoreAPI(node)
97
+ }
98
57
- ident = config.Identity{
58
- PeerID: id.Pretty(),
59
- PrivKey: base64.StdEncoding.EncodeToString(kbytes),
60
- }
61
- } else {
62
- ident = config.Identity{
63
- PeerID: testPeerID,
64
- }
99
+ err := mn.LinkAll()
100
+ if err != nil {
101
+ return nil, nil, err
102
}
103
67
- r := &repo.Mock{
68
- C: config.Config{
69
- Identity: ident,
104
+ bsinf := core.BootstrapConfigWithPeers(
105
+ []pstore.PeerInfo{
106
+ nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
107
},
71
- D: syncds.MutexWrap(datastore.NewMapDatastore()),
72
- K: keystore.NewMemKeystore(),
108
+ )
109
+
110
+ for _, n := range nodes[1:] {
111
+ if err := n.Bootstrap(bsinf); err != nil {
112
+ return nil, nil, err
113
+ }
114
}
74
- node, err := core.NewNode(ctx, &core.BuildCfg{Repo: r})
115
+
116
+ return nodes, apis, nil
117
+}
118
+
119
+func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
120
+ nd, api, err := makeAPISwarm(ctx, false, 1)
121
if err != nil {
122
return nil, nil, err
123
}
78
- api := coreapi.NewCoreAPI(node)
79
- return node, api, nil
80
-}
124
82
-func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
83
- return makeAPIIdent(ctx, false)
125
+ return nd[0], api[0], nil
126
}
127
128
func TestAdd(t *testing.T) {