lotus/api/test/test.go

102 lines
1.8 KiB
Go
Raw Normal View History

package test
import (
"context"
2019-07-09 17:03:36 +00:00
"strings"
"testing"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
)
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-07-09 16:27:07 +00:00
type APIBuilder func(t *testing.T, n int) []api.API
type testSuite struct {
2019-07-09 16:27:07 +00:00
makeNodes APIBuilder
}
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) {
ts := testSuite{
2019-07-09 16:27:07 +00:00
makeNodes: b,
}
t.Run("version", ts.testVersion)
2019-07-09 17:03:36 +00:00
t.Run("id", ts.testID)
t.Run("testConnectTwo", ts.testConnectTwo)
}
func (ts *testSuite) testVersion(t *testing.T) {
ctx := context.Background()
2019-07-09 17:03:36 +00:00
api := ts.makeNodes(t, 1)[0]
2019-07-09 17:03:36 +00:00
v, err := api.Version(ctx)
if err != nil {
t.Fatal(err)
}
if v.Version != build.Version {
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()
api := ts.makeNodes(t, 1)[0]
id, err := api.ID(ctx)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(id.Pretty(), "Qm") {
t.Error("expected identity to be Qm..")
}
}
func (ts *testSuite) testConnectTwo(t *testing.T) {
ctx := context.Background()
apis := ts.makeNodes(t, 2)
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")
}
}