mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 20:07:45 +08:00
I'd like to sneak this into the release so we can turn on strict verification ASAP. 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"
|
|
|
|
config "gx/ipfs/QmPEpj17FDRpc7K1aArKZp3RsHtzRMKykeK9GVgn4WQGPR/go-ipfs-config"
|
|
peer "gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
|
|
pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore"
|
|
libp2p "gx/ipfs/QmUDTcnDp2WssbmiDLC6aYurUeyt7QeRakHUQMxA2mZ5iB/go-libp2p"
|
|
mocknet "gx/ipfs/QmUDTcnDp2WssbmiDLC6aYurUeyt7QeRakHUQMxA2mZ5iB/go-libp2p/p2p/net/mock"
|
|
testutil "gx/ipfs/Qma6ESRQTf1ZLPgzpCwDTqQJefPnU6uLvMjP18vK8EWp8L/go-testutil"
|
|
datastore "gx/ipfs/QmaRb5yNXKonhbkpNxNawoydk4N6es6b4fPj19sjEKsh5D/go-datastore"
|
|
syncds "gx/ipfs/QmaRb5yNXKonhbkpNxNawoydk4N6es6b4fPj19sjEKsh5D/go-datastore/sync"
|
|
host "gx/ipfs/QmdJfsSbKSZnMkfZ1kpopiyB9i3Hd6cp8VKWZmtWPa7Moc/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
|
|
}
|