coreapi: run tests from interface
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@88e58e6b286d55882958c68525d69c4a3dd213b8 This commit was moved from ipfs/boxo@6649a031fab719883b2f507b4191538d61d183bb
Łukasz Magiera committed
Dec 20, 2018 at 20:11 UTC
373e313822941476f1b96535cab7e822254cad50
11 files changed
+233
-117
core/coreiface/tests/api.go
new
+129
@@ -0,0 +1,129 @@
1
+package tests
2
+
3
+import (
4
+ "context"
5
+ "encoding/base64"
6
+ "fmt"
7
+ "testing"
8
+
9
+ "github.com/ipfs/go-ipfs/core"
10
+ "github.com/ipfs/go-ipfs/core/coreapi"
11
+ coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
+ mock "github.com/ipfs/go-ipfs/core/mock"
13
+ "github.com/ipfs/go-ipfs/keystore"
14
+ "github.com/ipfs/go-ipfs/repo"
15
+
16
+ ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
17
+ "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock"
18
+ "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
19
+ pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
20
+ "gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config"
21
+ "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
22
+ syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
23
+)
24
+
25
+
26
+func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
27
+ mn := mocknet.New(ctx)
28
+
29
+ nodes := make([]*core.IpfsNode, n)
30
+ apis := make([]coreiface.CoreAPI, n)
31
+
32
+ for i := 0; i < n; i++ {
33
+ var ident config.Identity
34
+ if fullIdentity {
35
+ sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
36
+ if err != nil {
37
+ return nil, err
38
+ }
39
+
40
+ id, err := peer.IDFromPublicKey(pk)
41
+ if err != nil {
42
+ return nil, err
43
+ }
44
+
45
+ kbytes, err := sk.Bytes()
46
+ if err != nil {
47
+ return nil, err
48
+ }
49
+
50
+ ident = config.Identity{
51
+ PeerID: id.Pretty(),
52
+ PrivKey: base64.StdEncoding.EncodeToString(kbytes),
53
+ }
54
+ } else {
55
+ ident = config.Identity{
56
+ PeerID: testPeerID,
57
+ }
58
+ }
59
+
60
+ c := config.Config{}
61
+ c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
62
+ c.Identity = ident
63
+
64
+ r := &repo.Mock{
65
+ C: c,
66
+ D: syncds.MutexWrap(datastore.NewMapDatastore()),
67
+ K: keystore.NewMemKeystore(),
68
+ }
69
+
70
+ node, err := core.NewNode(ctx, &core.BuildCfg{
71
+ Repo: r,
72
+ Host: mock.MockHostOption(mn),
73
+ Online: fullIdentity,
74
+ ExtraOpts: map[string]bool{
75
+ "pubsub": true,
76
+ },
77
+ })
78
+ if err != nil {
79
+ return nil, err
80
+ }
81
+ nodes[i] = node
82
+ apis[i], err = coreapi.NewCoreAPI(node)
83
+ if err != nil {
84
+ return nil, err
85
+ }
86
+ }
87
+
88
+ err := mn.LinkAll()
89
+ if err != nil {
90
+ return nil, err
91
+ }
92
+
93
+ bsinf := core.BootstrapConfigWithPeers(
94
+ []pstore.PeerInfo{
95
+ nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
96
+ },
97
+ )
98
+
99
+ for _, n := range nodes[1:] {
100
+ if err := n.Bootstrap(bsinf); err != nil {
101
+ return nil, err
102
+ }
103
+ }
104
+
105
+ return apis, nil
106
+}
107
+
108
+func makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
109
+ api, err := makeAPISwarm(ctx, false, 1)
110
+ if err != nil {
111
+ return nil, err
112
+ }
113
+
114
+ return api[0], nil
115
+}
116
+
117
+
118
+func TestApi(t *testing.T) {
119
+ t.Run("Block", TestBlock)
120
+ t.Run("TestDag", TestDag)
121
+ t.Run("TestDht", TestDht)
122
+ t.Run("TestKey", TestKey)
123
+ t.Run("TestName", TestName)
124
+ t.Run("TestObject", TestObject)
125
+ t.Run("TestPath", TestPath)
126
+ t.Run("TestPin", TestPin)
127
+ t.Run("TestPubSub", TestPubSub)
128
+ t.Run("TestUnixfs", TestUnixfs)
129
+}
core/coreiface/tests/block.go
renamed
+10
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -12,6 +12,15 @@ import (
12
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
13
)
14
15
+func TestBlock(t *testing.T) {
16
+ t.Run("TestBlockPut", TestBlockPut)
17
+ t.Run("TestBlockPutFormat", TestBlockPutFormat)
18
+ t.Run("TestBlockPutHash", TestBlockPutHash)
19
+ t.Run("TestBlockGet", TestBlockGet)
20
+ t.Run("TestBlockRm", TestBlockRm)
21
+ t.Run("TestBlockStat", TestBlockStat)
22
+}
23
+
24
func TestBlockPut(t *testing.T) {
25
ctx := context.Background()
26
api, err := makeAPI(ctx)
core/coreiface/tests/dag.go
renamed
+10
-2
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -12,6 +12,14 @@ import (
12
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
13
)
14
15
+func TestDag(t *testing.T) {
16
+ t.Run("TestPut", TestPut)
17
+ t.Run("TestPutWithHash", TestPutWithHash)
18
+ t.Run("TestPath", TestDagPath)
19
+ t.Run("TestTree", TestTree)
20
+ t.Run("TestBatch", TestBatch)
21
+}
22
+
23
var (
24
treeExpected = map[string]struct{}{
25
"a": {},
@@ -56,7 +64,7 @@ func TestPutWithHash(t *testing.T) {
64
}
65
}
66
59
-func TestPath(t *testing.T) {
67
+func TestDagPath(t *testing.T) {
68
ctx := context.Background()
69
api, err := makeAPI(ctx)
70
if err != nil {
core/coreiface/tests/dht.go
renamed
+7
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -8,6 +8,12 @@ import (
8
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
)
10
11
+func TestDht(t *testing.T) {
12
+ t.Run("TestDhtFindPeer", TestDhtFindPeer)
13
+ t.Run("TestDhtFindProviders", TestDhtFindProviders)
14
+ t.Run("TestDhtProvide", TestDhtProvide)
15
+}
16
+
17
func TestDhtFindPeer(t *testing.T) {
18
ctx := context.Background()
19
apis, err := makeAPISwarm(ctx, true, 5)
core/coreiface/tests/key.go
renamed
+18
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -8,6 +8,23 @@ import (
8
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
)
10
11
+func TestKey(t *testing.T) {
12
+ t.Run("TestListSelf", TestListSelf)
13
+ t.Run("TestRenameSelf", TestRenameSelf)
14
+ t.Run("TestRemoveSelf", TestRemoveSelf)
15
+ t.Run("TestGenerateSize", TestGenerateSize)
16
+ t.Run("TestGenerateExisting", TestGenerateExisting)
17
+ t.Run("TestList", TestList)
18
+ t.Run("TestRename", TestRename)
19
+ t.Run("TestRenameToSelf", TestRenameToSelf)
20
+ t.Run("TestRenameToSelfForce", TestRenameToSelfForce)
21
+ t.Run("TestRenameOverwriteNoForce", TestRenameOverwriteNoForce)
22
+ t.Run("TestRenameOverwrite", TestRenameOverwrite)
23
+ t.Run("TestRenameSameNameNoForce", TestRenameSameNameNoForce)
24
+ t.Run("TestRenameSameName", TestRenameSameName)
25
+ t.Run("TestRemove", TestRemove)
26
+}
27
+
28
func TestListSelf(t *testing.T) {
29
ctx := context.Background()
30
api, err := makeAPI(ctx)
core/coreiface/tests/name.go
renamed
+6
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -15,6 +15,11 @@ import (
15
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16
)
17
18
+func TestName(t *testing.T) {
19
+ t.Run("TestPublishResolve", TestPublishResolve)
20
+ t.Run("TestBasicPublishResolveKey", TestBasicPublishResolveKey)
21
+}
22
+
23
var rnd = rand.New(rand.NewSource(0x62796532303137))
24
25
func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
core/coreiface/tests/object.go
renamed
+16
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"bytes"
@@ -12,6 +12,21 @@ import (
12
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
)
14
15
+func TestObject(t *testing.T) {
16
+ t.Run("TestNew", TestNew)
17
+ t.Run("TestObjectPut", TestObjectPut)
18
+ t.Run("TestObjectGet", TestObjectGet)
19
+ t.Run("TestObjectData", TestObjectData)
20
+ t.Run("TestObjectLinks", TestObjectLinks)
21
+ t.Run("TestObjectStat", TestObjectStat)
22
+ t.Run("TestObjectAddLink", TestObjectAddLink)
23
+ t.Run("TestObjectAddLinkCreate", TestObjectAddLinkCreate)
24
+ t.Run("TestObjectRmLink", TestObjectRmLink)
25
+ t.Run("TestObjectAddData", TestObjectAddData)
26
+ t.Run("TestObjectSetData", TestObjectSetData)
27
+ t.Run("TestDiffTest", TestDiffTest)
28
+}
29
+
30
func TestNew(t *testing.T) {
31
ctx := context.Background()
32
api, err := makeAPI(ctx)
core/coreiface/tests/path.go
renamed
+9
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -9,6 +9,14 @@ import (
9
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
)
11
12
+func TestPath(t *testing.T) {
13
+ t.Run("TestMutablePath", TestMutablePath)
14
+ t.Run("TestPathRemainder", TestPathRemainder)
15
+ t.Run("TestEmptyPathRemainder", TestEmptyPathRemainder)
16
+ t.Run("TestInvalidPathRemainder", TestInvalidPathRemainder)
17
+ t.Run("TestPathRoot", TestPathRoot)
18
+}
19
+
20
func TestMutablePath(t *testing.T) {
21
ctx := context.Background()
22
api, err := makeAPI(ctx)
core/coreiface/tests/pin.go
renamed
+7
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -8,6 +8,12 @@ import (
8
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
)
10
11
+func TestPin(t *testing.T) {
12
+ t.Run("TestPinAdd", TestPinAdd)
13
+ t.Run("TestPinSimple", TestPinSimple)
14
+ t.Run("TestPinRecursive", TestPinRecursive)
15
+}
16
+
17
func TestPinAdd(t *testing.T) {
18
ctx := context.Background()
19
api, err := makeAPI(ctx)
core/coreiface/tests/pubsub.go
renamed
+5
-1
@@ -1,4 +1,4 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"context"
@@ -7,6 +7,10 @@ import (
7
"time"
8
)
9
10
+func TestPubSub(t *testing.T) {
11
+ t.Run("TestBasicPubSub", TestBasicPubSub)
12
+}
13
+
14
func TestBasicPubSub(t *testing.T) {
15
ctx, cancel := context.WithCancel(context.Background())
16
defer cancel()
core/coreiface/tests/unixfs.go
renamed
+16
-107
@@ -1,11 +1,8 @@
1
-package tests_test
1
+package tests
2
3
import (
4
"bytes"
5
"context"
6
- "encoding/base64"
7
- "fmt"
8
- "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
6
"io"
7
"io/ioutil"
8
"math"
@@ -15,28 +12,31 @@ import (
12
"sync"
13
"testing"
14
18
- "github.com/ipfs/go-ipfs/core"
19
- "github.com/ipfs/go-ipfs/core/coreapi"
15
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
16
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
22
- mock "github.com/ipfs/go-ipfs/core/mock"
23
- "github.com/ipfs/go-ipfs/keystore"
24
- "github.com/ipfs/go-ipfs/repo"
17
26
- ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
27
- "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock"
18
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
19
cbor "gx/ipfs/QmRoARq3nkUb13HSKZGepCZSWe5GrVPwx7xURJGZ7KWv9V/go-ipld-cbor"
20
"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
30
- "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
31
- pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
21
"gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs"
33
- "gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config"
22
mdag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
23
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
36
- "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
37
- syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
24
)
25
26
+func TestUnixfs(t *testing.T) {
27
+ t.Run("TestAdd", TestAdd)
28
+ t.Run("TestAddPinned", TestAddPinned)
29
+ t.Run("TestAddHashOnly", TestAddHashOnly)
30
+ t.Run("TestGetEmptyFile", TestGetEmptyFile)
31
+ t.Run("TestGetDir", TestGetDir)
32
+ t.Run("TestGetNonUnixfs", TestGetNonUnixfs)
33
+ t.Run("TestLs", TestLs)
34
+ t.Run("TestEntriesExpired", TestEntriesExpired)
35
+ t.Run("TestLsEmptyDir", TestLsEmptyDir)
36
+ t.Run("TestLsNonUnixfs", TestLsNonUnixfs)
37
+ t.Run("TestAddCloses", TestAddCloses)
38
+}
39
+
40
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
41
42
// `echo -n 'hello, world!' | ipfs add`
@@ -46,97 +46,6 @@ var helloStr = "hello, world!"
46
// `echo -n | ipfs add`
47
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
48
49
-func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
50
- mn := mocknet.New(ctx)
51
-
52
- nodes := make([]*core.IpfsNode, n)
53
- apis := make([]coreiface.CoreAPI, n)
54
-
55
- for i := 0; i < n; i++ {
56
- var ident config.Identity
57
- if fullIdentity {
58
- sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
59
- if err != nil {
60
- return nil, err
61
- }
62
-
63
- id, err := peer.IDFromPublicKey(pk)
64
- if err != nil {
65
- return nil, err
66
- }
67
-
68
- kbytes, err := sk.Bytes()
69
- if err != nil {
70
- return nil, err
71
- }
72
-
73
- ident = config.Identity{
74
- PeerID: id.Pretty(),
75
- PrivKey: base64.StdEncoding.EncodeToString(kbytes),
76
- }
77
- } else {
78
- ident = config.Identity{
79
- PeerID: testPeerID,
80
- }
81
- }
82
-
83
- c := config.Config{}
84
- c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
85
- c.Identity = ident
86
-
87
- r := &repo.Mock{
88
- C: c,
89
- D: syncds.MutexWrap(datastore.NewMapDatastore()),
90
- K: keystore.NewMemKeystore(),
91
- }
92
-
93
- node, err := core.NewNode(ctx, &core.BuildCfg{
94
- Repo: r,
95
- Host: mock.MockHostOption(mn),
96
- Online: fullIdentity,
97
- ExtraOpts: map[string]bool{
98
- "pubsub": true,
99
- },
100
- })
101
- if err != nil {
102
- return nil, err
103
- }
104
- nodes[i] = node
105
- apis[i], err = coreapi.NewCoreAPI(node)
106
- if err != nil {
107
- return nil, err
108
- }
109
- }
110
-
111
- err := mn.LinkAll()
112
- if err != nil {
113
- return nil, err
114
- }
115
-
116
- bsinf := core.BootstrapConfigWithPeers(
117
- []pstore.PeerInfo{
118
- nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
119
- },
120
- )
121
-
122
- for _, n := range nodes[1:] {
123
- if err := n.Bootstrap(bsinf); err != nil {
124
- return nil, err
125
- }
126
- }
127
-
128
- return apis, nil
129
-}
130
-
131
-func makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
132
- api, err := makeAPISwarm(ctx, false, 1)
133
- if err != nil {
134
- return nil, err
135
- }
136
-
137
- return api[0], nil
138
-}
139
-
49
func strFile(data string) func() files.Node {
50
return func() files.Node {
51
return files.NewBytesFile([]byte(data))