提交 d2e6a61d 作者: Łukasz Magiera 提交者: Steven Allen

coreapi: test using mock swarm

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 36168542
...@@ -7,16 +7,16 @@ import ( ...@@ -7,16 +7,16 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
dag "github.com/ipfs/go-ipfs/merkledag"
dag "gx/ipfs/QmNr4E8z9bGTztvHJktp7uQaMdx9p3r9Asrq6eYk7iCh4a/go-merkledag"
routing "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing" peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
notif "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing/notifications" ipdht "gx/ipfs/QmRNxiPpZf3skMAtmDJpgHuW9uj1ukqV1zjANj9d6bmHfE/go-libp2p-kad-dht"
ipdht "gx/ipfs/QmVSep2WwKcXxMonPASsAJ3nZVjfVMKgMcaSigxKnUWpJv/go-libp2p-kad-dht" ipld "gx/ipfs/QmX5CsuHyVZeTLxgRSYkgLSDQKb9UjE8xnhQzCEJWWWFsC/go-ipld-format"
ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr" routing "gx/ipfs/QmY9JUvS8kbgao3XbPh6WAV3ChE2nxGKhcGTHiwMC4gmcU/go-libp2p-routing"
pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore" notif "gx/ipfs/QmY9JUvS8kbgao3XbPh6WAV3ChE2nxGKhcGTHiwMC4gmcU/go-libp2p-routing/notifications"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer" ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
) )
var ErrNotDHT = errors.New("routing service is not a DHT") 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,12 +93,12 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
return nil, ErrNotDHT return nil, ErrNotDHT
} }
p, err = api.ResolvePath(ctx, p) rp, err := api.ResolvePath(ctx, p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := p.Cid() c := rp.Cid()
numProviders := settings.NumProviders numProviders := settings.NumProviders
if numProviders < 1 { if numProviders < 1 {
...@@ -162,7 +162,12 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao ...@@ -162,7 +162,12 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
return errors.New("cannot provide, no connected peers") return errors.New("cannot provide, no connected peers")
} }
c := path.Cid() rp, err := api.ResolvePath(ctx, path)
if err != nil {
return err
}
c := rp.Cid()
has, err := api.node.Blockstore.Has(c) has, err := api.node.Blockstore.Has(c)
if err != nil { if err != nil {
......
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr" peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer" ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
) )
// DhtAPI specifies the interface to the DHT // DhtAPI specifies the interface to the DHT
......
...@@ -21,11 +21,13 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, ...@@ -21,11 +21,13 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path,
func TestBasicPublishResolve(t *testing.T) { func TestBasicPublishResolve(t *testing.T) {
ctx := context.Background() ctx := context.Background()
n, api, err := makeAPIIdent(ctx, true) nds, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
return return
} }
n := nds[0]
api := apis[0]
p, err := addTestObject(ctx, api) p, err := addTestObject(ctx, api)
if err != nil { if err != nil {
...@@ -60,11 +62,12 @@ func TestBasicPublishResolve(t *testing.T) { ...@@ -60,11 +62,12 @@ func TestBasicPublishResolve(t *testing.T) {
func TestBasicPublishResolveKey(t *testing.T) { func TestBasicPublishResolveKey(t *testing.T) {
ctx := context.Background() ctx := context.Background()
_, api, err := makeAPIIdent(ctx, true) _, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
return return
} }
api := apis[0]
k, err := api.Key().Generate(ctx, "foo") k, err := api.Key().Generate(ctx, "foo")
if err != nil { if err != nil {
...@@ -107,12 +110,13 @@ func TestBasicPublishResolveTimeout(t *testing.T) { ...@@ -107,12 +110,13 @@ func TestBasicPublishResolveTimeout(t *testing.T) {
t.Skip("ValidTime doesn't appear to work at this time resolution") t.Skip("ValidTime doesn't appear to work at this time resolution")
ctx := context.Background() ctx := context.Background()
n, api, err := makeAPIIdent(ctx, true) nds, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
return return
} }
n := nds[0]
api := apis[0]
p, err := addTestObject(ctx, api) p, err := addTestObject(ctx, api)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/base64" "encoding/base64"
"fmt"
"io" "io"
"math" "math"
"strings" "strings"
...@@ -14,6 +15,7 @@ import ( ...@@ -14,6 +15,7 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
coreunix "github.com/ipfs/go-ipfs/core/coreunix" coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
keystore "github.com/ipfs/go-ipfs/keystore" keystore "github.com/ipfs/go-ipfs/keystore"
repo "github.com/ipfs/go-ipfs/repo" repo "github.com/ipfs/go-ipfs/repo"
...@@ -22,8 +24,10 @@ import ( ...@@ -22,8 +24,10 @@ import (
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer" peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
datastore "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore" datastore "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore"
syncds "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore/sync" syncds "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore/sync"
mocknet "gx/ipfs/QmUEqyXr97aUbNmQADHYNknjwjjdVpJXEt1UZXmSG81EV4/go-libp2p/p2p/net/mock"
unixfs "gx/ipfs/QmWAfTyD6KEBm7bzqNRBPvqKrZCDtn5PGbs9V1DKfnVK59/go-unixfs" unixfs "gx/ipfs/QmWAfTyD6KEBm7bzqNRBPvqKrZCDtn5PGbs9V1DKfnVK59/go-unixfs"
config "gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config" config "gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
cbor "gx/ipfs/QmepvyyduWnXHm1G7ybmGbJfQQHTAo36DjP2nvF7H7ZXjE/go-ipld-cbor" cbor "gx/ipfs/QmepvyyduWnXHm1G7ybmGbJfQQHTAo36DjP2nvF7H7ZXjE/go-ipld-cbor"
) )
...@@ -36,7 +40,13 @@ var helloStr = "hello, world!" ...@@ -36,7 +40,13 @@ var helloStr = "hello, world!"
// `echo -n | ipfs add` // `echo -n | ipfs add`
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) { func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
mn := mocknet.New(ctx)
nodes := make([]*core.IpfsNode, n)
apis := make([]coreiface.CoreAPI, n)
for i := 0; i < n; i++ {
var ident config.Identity var ident config.Identity
if fullIdentity { if fullIdentity {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
...@@ -64,23 +74,55 @@ func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, corei ...@@ -64,23 +74,55 @@ func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, corei
} }
} }
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
c.Identity = ident
r := &repo.Mock{ r := &repo.Mock{
C: config.Config{ C: c,
Identity: ident,
},
D: syncds.MutexWrap(datastore.NewMapDatastore()), D: syncds.MutexWrap(datastore.NewMapDatastore()),
K: keystore.NewMemKeystore(), K: keystore.NewMemKeystore(),
} }
node, err := core.NewNode(ctx, &core.BuildCfg{Repo: r})
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Host: mock.MockHostOption(mn),
Online: fullIdentity,
})
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
api := coreapi.NewCoreAPI(node) nodes[i] = node
return node, api, nil apis[i] = coreapi.NewCoreAPI(node)
}
err := mn.LinkAll()
if err != nil {
return nil, nil, err
}
bsinf := core.BootstrapConfigWithPeers(
[]pstore.PeerInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
)
for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
return nil, nil, err
}
}
return nodes, apis, nil
} }
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) { func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
return makeAPIIdent(ctx, false) nd, api, err := makeAPISwarm(ctx, false, 1)
if err != nil {
return nil, nil, err
}
return nd[0], api[0], nil
} }
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论