kubo/core/coreapi/interface/tests/api.go
Łukasz Magiera 5a019ba713 coreapi: run tests from interface
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
2019-01-03 22:17:14 +01:00

130 lines
2.9 KiB
Go

package tests
import (
"context"
"encoding/base64"
"fmt"
"testing"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/keystore"
"github.com/ipfs/go-ipfs/repo"
ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
"gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock"
"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
"gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config"
"gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
)
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]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
if fullIdentity {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
return nil, err
}
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, err
}
kbytes, err := sk.Bytes()
if err != nil {
return nil, err
}
ident = config.Identity{
PeerID: id.Pretty(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
}
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
c.Identity = ident
r := &repo.Mock{
C: c,
D: syncds.MutexWrap(datastore.NewMapDatastore()),
K: keystore.NewMemKeystore(),
}
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Host: mock.MockHostOption(mn),
Online: fullIdentity,
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, err
}
nodes[i] = node
apis[i], err = coreapi.NewCoreAPI(node)
if err != nil {
return nil, err
}
}
err := mn.LinkAll()
if err != nil {
return 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, err
}
}
return apis, nil
}
func makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := makeAPISwarm(ctx, false, 1)
if err != nil {
return nil, err
}
return api[0], nil
}
func TestApi(t *testing.T) {
t.Run("Block", TestBlock)
t.Run("TestDag", TestDag)
t.Run("TestDht", TestDht)
t.Run("TestKey", TestKey)
t.Run("TestName", TestName)
t.Run("TestObject", TestObject)
t.Run("TestPath", TestPath)
t.Run("TestPin", TestPin)
t.Run("TestPubSub", TestPubSub)
t.Run("TestUnixfs", TestUnixfs)
}