2019-06-29 09:19:06 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-07-18 15:52:48 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-29 09:19:06 +00:00
|
|
|
)
|
|
|
|
|
2019-09-23 15:27:30 +00:00
|
|
|
type TestNode struct {
|
|
|
|
api.FullNode
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestStorageNode struct {
|
|
|
|
api.StorageMiner
|
2019-11-30 23:17:50 +00:00
|
|
|
|
2020-04-23 21:12:42 +00:00
|
|
|
MineOne func(context.Context, func(bool)) error
|
2019-09-23 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:50:52 +00:00
|
|
|
var PresealGenesis = -1
|
|
|
|
|
|
|
|
type StorageMiner struct {
|
|
|
|
Full int
|
|
|
|
Preseal int
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:05:43 +00:00
|
|
|
// APIBuilder is a function which is invoked in test suite to provide
|
|
|
|
// test nodes and networks
|
2019-09-23 15:27:30 +00:00
|
|
|
//
|
|
|
|
// storage array defines storage nodes, numbers in the array specify full node
|
|
|
|
// index the storage node 'belongs' to
|
2020-04-23 17:50:52 +00:00
|
|
|
type APIBuilder func(t *testing.T, nFull int, storage []StorageMiner) ([]TestNode, []TestStorageNode)
|
2019-06-29 09:19:06 +00:00
|
|
|
type testSuite struct {
|
2019-07-09 16:27:07 +00:00
|
|
|
makeNodes APIBuilder
|
2019-06-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:05:43 +00:00
|
|
|
// TestApis is the entry point to API test suite
|
2019-07-02 12:40:25 +00:00
|
|
|
func TestApis(t *testing.T, b APIBuilder) {
|
2019-06-29 09:19:06 +00:00
|
|
|
ts := testSuite{
|
2019-07-09 16:27:07 +00:00
|
|
|
makeNodes: b,
|
2019-06-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("version", ts.testVersion)
|
2019-07-09 17:03:36 +00:00
|
|
|
t.Run("id", ts.testID)
|
|
|
|
t.Run("testConnectTwo", ts.testConnectTwo)
|
2019-09-23 15:27:30 +00:00
|
|
|
t.Run("testMining", ts.testMining)
|
2019-06-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:50:52 +00:00
|
|
|
var oneMiner = []StorageMiner{{Full: 0, Preseal: PresealGenesis}}
|
|
|
|
|
2019-06-29 09:19:06 +00:00
|
|
|
func (ts *testSuite) testVersion(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-04-23 17:50:52 +00:00
|
|
|
apis, _ := ts.makeNodes(t, 1, oneMiner)
|
2019-09-23 15:27:30 +00:00
|
|
|
api := apis[0]
|
2019-06-29 09:19:06 +00:00
|
|
|
|
2019-07-09 17:03:36 +00:00
|
|
|
v, err := api.Version(ctx)
|
2019-06-29 09:19:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-17 15:26:16 +00:00
|
|
|
if v.Version != build.BuildVersion {
|
2019-06-29 09:19:06 +00:00
|
|
|
t.Error("Version didn't work properly")
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 17:03:36 +00:00
|
|
|
|
|
|
|
func (ts *testSuite) testID(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-04-23 17:50:52 +00:00
|
|
|
apis, _ := ts.makeNodes(t, 1, oneMiner)
|
2019-09-23 15:27:30 +00:00
|
|
|
api := apis[0]
|
2019-07-09 17:03:36 +00:00
|
|
|
|
|
|
|
id, err := api.ID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-18 15:52:48 +00:00
|
|
|
assert.Regexp(t, "^12", id.Pretty())
|
2019-07-09 17:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testSuite) testConnectTwo(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-04-23 17:50:52 +00:00
|
|
|
apis, _ := ts.makeNodes(t, 2, oneMiner)
|
2019-07-09 17:03:36 +00:00
|
|
|
|
|
|
|
p, err := apis[0].NetPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(p) != 0 {
|
|
|
|
t.Error("Node 0 has a peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = apis[1].NetPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(p) != 0 {
|
|
|
|
t.Error("Node 1 has a peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := apis[1].NetAddrsListen(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := apis[0].NetConnect(ctx, addrs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = apis[0].NetPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(p) != 1 {
|
|
|
|
t.Error("Node 0 doesn't have 1 peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = apis[1].NetPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(p) != 1 {
|
|
|
|
t.Error("Node 0 doesn't have 1 peer")
|
|
|
|
}
|
|
|
|
}
|