mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-13 12:18:00 +08:00
Also: * Updates go-floodsub to fix a data race. * Updates golang-lru License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package coremock
|
|
|
|
import (
|
|
"context"
|
|
|
|
commands "github.com/ipfs/go-ipfs/commands"
|
|
core "github.com/ipfs/go-ipfs/core"
|
|
"github.com/ipfs/go-ipfs/repo"
|
|
|
|
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
|
|
testutil "gx/ipfs/QmRNhSdqzMcuRxX9A1egBeQ3BhDTguDV5HPwi8wRykkPU8/go-testutil"
|
|
datastore "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore"
|
|
syncds "gx/ipfs/QmSpg1CvpXQQow5ernt1gNBXaXV6yxyNqi7XoeerWfzB5w/go-datastore/sync"
|
|
libp2p "gx/ipfs/QmUEqyXr97aUbNmQADHYNknjwjjdVpJXEt1UZXmSG81EV4/go-libp2p"
|
|
mocknet "gx/ipfs/QmUEqyXr97aUbNmQADHYNknjwjjdVpJXEt1UZXmSG81EV4/go-libp2p/p2p/net/mock"
|
|
config "gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
|
|
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
|
|
host "gx/ipfs/QmeMYW7Nj8jnnEfs9qhm7SxKkoDPUWXu3MsxX6BFwz34tf/go-libp2p-host"
|
|
)
|
|
|
|
// NewMockNode constructs an IpfsNode for use in tests.
|
|
func NewMockNode() (*core.IpfsNode, error) {
|
|
ctx := context.Background()
|
|
|
|
// effectively offline, only peer in its network
|
|
return core.NewNode(ctx, &core.BuildCfg{
|
|
Online: true,
|
|
Host: MockHostOption(mocknet.New(ctx)),
|
|
})
|
|
}
|
|
|
|
func MockHostOption(mn mocknet.Mocknet) core.HostOption {
|
|
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, _ ...libp2p.Option) (host.Host, error) {
|
|
return mn.AddPeerWithPeerstore(id, ps)
|
|
}
|
|
}
|
|
|
|
func MockCmdsCtx() (commands.Context, error) {
|
|
// Generate Identity
|
|
ident, err := testutil.RandIdentity()
|
|
if err != nil {
|
|
return commands.Context{}, err
|
|
}
|
|
p := ident.ID()
|
|
|
|
conf := config.Config{
|
|
Identity: config.Identity{
|
|
PeerID: p.String(),
|
|
},
|
|
}
|
|
|
|
r := &repo.Mock{
|
|
D: syncds.MutexWrap(datastore.NewMapDatastore()),
|
|
C: conf,
|
|
}
|
|
|
|
node, err := core.NewNode(context.Background(), &core.BuildCfg{
|
|
Repo: r,
|
|
})
|
|
if err != nil {
|
|
return commands.Context{}, err
|
|
}
|
|
|
|
return commands.Context{
|
|
Online: true,
|
|
ConfigRoot: "/tmp/.mockipfsconfig",
|
|
LoadConfig: func(path string) (*config.Config, error) {
|
|
return &conf, nil
|
|
},
|
|
ConstructNode: func() (*core.IpfsNode, error) {
|
|
return node, nil
|
|
},
|
|
}, nil
|
|
}
|