mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 19:07:48 +08:00
Note: This commit is technically broken. However, I need to make a bunch of cmds changes to make this work and I'd rather not bundle both changes into a single commit. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package coremock
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
commands "github.com/ipfs/go-ipfs/commands"
|
|
core "github.com/ipfs/go-ipfs/core"
|
|
"github.com/ipfs/go-ipfs/repo"
|
|
config "github.com/ipfs/go-ipfs/repo/config"
|
|
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
|
|
testutil "gx/ipfs/QmQgLZP9haZheimMHqqAjJh2LhRmNfEoZDfbtkpeMhi9xK/go-testutil"
|
|
|
|
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
|
|
host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
|
|
mocknet "gx/ipfs/QmTzs3Gp2rU3HuNayjBVG7qBgbaKWE8bgtwJ7faRxAe9UP/go-libp2p/p2p/net/mock"
|
|
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
|
|
smux "gx/ipfs/QmY9JXR3FupnYAYJWK9aMr9bCpqWKcToQ1tz8DVGTrHpHw/go-stream-muxer"
|
|
ipnet "gx/ipfs/QmauYrW3kDcfZwUuqjyDCSTyaicL8tvo3a7VkAVgAEes96/go-libp2p-interface-pnet"
|
|
metrics "gx/ipfs/QmbXmeK6KgUAkbyVGRxXknupmWAHnt6ryghT8BFSsEh2sB/go-libp2p-metrics"
|
|
)
|
|
|
|
// 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, bwr metrics.Reporter, fs []*net.IPNet, _ smux.Transport, _ ipnet.Protector, _ *core.ConstructPeerHostOpts) (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: ds2.ThreadSafeCloserMapDatastore(),
|
|
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
|
|
}
|