Commit 88e58e6b authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: run tests from interface

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent a479105a
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)
}
package tests_test package tests
import ( import (
"context" "context"
...@@ -12,6 +12,15 @@ import ( ...@@ -12,6 +12,15 @@ import (
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash" mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
) )
func TestBlock(t *testing.T) {
t.Run("TestBlockPut", TestBlockPut)
t.Run("TestBlockPutFormat", TestBlockPutFormat)
t.Run("TestBlockPutHash", TestBlockPutHash)
t.Run("TestBlockGet", TestBlockGet)
t.Run("TestBlockRm", TestBlockRm)
t.Run("TestBlockStat", TestBlockStat)
}
func TestBlockPut(t *testing.T) { func TestBlockPut(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -12,6 +12,14 @@ import ( ...@@ -12,6 +12,14 @@ import (
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash" mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
) )
func TestDag(t *testing.T) {
t.Run("TestPut", TestPut)
t.Run("TestPutWithHash", TestPutWithHash)
t.Run("TestPath", TestDagPath)
t.Run("TestTree", TestTree)
t.Run("TestBatch", TestBatch)
}
var ( var (
treeExpected = map[string]struct{}{ treeExpected = map[string]struct{}{
"a": {}, "a": {},
...@@ -56,7 +64,7 @@ func TestPutWithHash(t *testing.T) { ...@@ -56,7 +64,7 @@ func TestPutWithHash(t *testing.T) {
} }
} }
func TestPath(t *testing.T) { func TestDagPath(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
if err != nil { if err != nil {
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -8,6 +8,12 @@ import ( ...@@ -8,6 +8,12 @@ import (
"github.com/ipfs/go-ipfs/core/coreapi/interface/options" "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestDht(t *testing.T) {
t.Run("TestDhtFindPeer", TestDhtFindPeer)
t.Run("TestDhtFindProviders", TestDhtFindProviders)
t.Run("TestDhtProvide", TestDhtProvide)
}
func TestDhtFindPeer(t *testing.T) { func TestDhtFindPeer(t *testing.T) {
ctx := context.Background() ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5) apis, err := makeAPISwarm(ctx, true, 5)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -8,6 +8,23 @@ import ( ...@@ -8,6 +8,23 @@ import (
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestKey(t *testing.T) {
t.Run("TestListSelf", TestListSelf)
t.Run("TestRenameSelf", TestRenameSelf)
t.Run("TestRemoveSelf", TestRemoveSelf)
t.Run("TestGenerateSize", TestGenerateSize)
t.Run("TestGenerateExisting", TestGenerateExisting)
t.Run("TestList", TestList)
t.Run("TestRename", TestRename)
t.Run("TestRenameToSelf", TestRenameToSelf)
t.Run("TestRenameToSelfForce", TestRenameToSelfForce)
t.Run("TestRenameOverwriteNoForce", TestRenameOverwriteNoForce)
t.Run("TestRenameOverwrite", TestRenameOverwrite)
t.Run("TestRenameSameNameNoForce", TestRenameSameNameNoForce)
t.Run("TestRenameSameName", TestRenameSameName)
t.Run("TestRemove", TestRemove)
}
func TestListSelf(t *testing.T) { func TestListSelf(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -15,6 +15,11 @@ import ( ...@@ -15,6 +15,11 @@ import (
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestName(t *testing.T) {
t.Run("TestPublishResolve", TestPublishResolve)
t.Run("TestBasicPublishResolveKey", TestBasicPublishResolveKey)
}
var rnd = rand.New(rand.NewSource(0x62796532303137)) var rnd = rand.New(rand.NewSource(0x62796532303137))
func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) { func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
......
package tests_test package tests
import ( import (
"bytes" "bytes"
...@@ -12,6 +12,21 @@ import ( ...@@ -12,6 +12,21 @@ import (
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestObject(t *testing.T) {
t.Run("TestNew", TestNew)
t.Run("TestObjectPut", TestObjectPut)
t.Run("TestObjectGet", TestObjectGet)
t.Run("TestObjectData", TestObjectData)
t.Run("TestObjectLinks", TestObjectLinks)
t.Run("TestObjectStat", TestObjectStat)
t.Run("TestObjectAddLink", TestObjectAddLink)
t.Run("TestObjectAddLinkCreate", TestObjectAddLinkCreate)
t.Run("TestObjectRmLink", TestObjectRmLink)
t.Run("TestObjectAddData", TestObjectAddData)
t.Run("TestObjectSetData", TestObjectSetData)
t.Run("TestDiffTest", TestDiffTest)
}
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -9,6 +9,14 @@ import ( ...@@ -9,6 +9,14 @@ import (
"github.com/ipfs/go-ipfs/core/coreapi/interface/options" "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestPath(t *testing.T) {
t.Run("TestMutablePath", TestMutablePath)
t.Run("TestPathRemainder", TestPathRemainder)
t.Run("TestEmptyPathRemainder", TestEmptyPathRemainder)
t.Run("TestInvalidPathRemainder", TestInvalidPathRemainder)
t.Run("TestPathRoot", TestPathRoot)
}
func TestMutablePath(t *testing.T) { func TestMutablePath(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -8,6 +8,12 @@ import ( ...@@ -8,6 +8,12 @@ import (
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
) )
func TestPin(t *testing.T) {
t.Run("TestPinAdd", TestPinAdd)
t.Run("TestPinSimple", TestPinSimple)
t.Run("TestPinRecursive", TestPinRecursive)
}
func TestPinAdd(t *testing.T) { func TestPinAdd(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api, err := makeAPI(ctx) api, err := makeAPI(ctx)
......
package tests_test package tests
import ( import (
"context" "context"
...@@ -7,6 +7,10 @@ import ( ...@@ -7,6 +7,10 @@ import (
"time" "time"
) )
func TestPubSub(t *testing.T) {
t.Run("TestBasicPubSub", TestBasicPubSub)
}
func TestBasicPubSub(t *testing.T) { func TestBasicPubSub(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
......
package tests_test package tests
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/base64"
"fmt"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"io" "io"
"io/ioutil" "io/ioutil"
"math" "math"
...@@ -15,28 +12,31 @@ import ( ...@@ -15,28 +12,31 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options" "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
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/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock"
cbor "gx/ipfs/QmRoARq3nkUb13HSKZGepCZSWe5GrVPwx7xURJGZ7KWv9V/go-ipld-cbor" cbor "gx/ipfs/QmRoARq3nkUb13HSKZGepCZSWe5GrVPwx7xURJGZ7KWv9V/go-ipld-cbor"
"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
"gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs" "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs"
"gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config"
mdag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag" mdag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash" mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
"gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
) )
func TestUnixfs(t *testing.T) {
t.Run("TestAdd", TestAdd)
t.Run("TestAddPinned", TestAddPinned)
t.Run("TestAddHashOnly", TestAddHashOnly)
t.Run("TestGetEmptyFile", TestGetEmptyFile)
t.Run("TestGetDir", TestGetDir)
t.Run("TestGetNonUnixfs", TestGetNonUnixfs)
t.Run("TestLs", TestLs)
t.Run("TestEntriesExpired", TestEntriesExpired)
t.Run("TestLsEmptyDir", TestLsEmptyDir)
t.Run("TestLsNonUnixfs", TestLsNonUnixfs)
t.Run("TestAddCloses", TestAddCloses)
}
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe" const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
// `echo -n 'hello, world!' | ipfs add` // `echo -n 'hello, world!' | ipfs add`
...@@ -46,97 +46,6 @@ var helloStr = "hello, world!" ...@@ -46,97 +46,6 @@ var helloStr = "hello, world!"
// `echo -n | ipfs add` // `echo -n | ipfs add`
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
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 strFile(data string) func() files.Node { func strFile(data string) func() files.Node {
return func() files.Node { return func() files.Node {
return files.NewBytesFile([]byte(data)) return files.NewBytesFile([]byte(data))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment