Wire up node API to tests

This commit is contained in:
Łukasz Magiera 2019-07-09 18:27:07 +02:00
parent b843dcb590
commit ba846e9bfb
4 changed files with 51 additions and 4 deletions

View File

@ -10,15 +10,15 @@ import (
// APIBuilder is a function which is invoked in test suite to provide
// test nodes and networks
type APIBuilder func() api.API
type APIBuilder func(t *testing.T, n int) []api.API
type testSuite struct {
makeNode APIBuilder
makeNodes APIBuilder
}
// TestApis is the entry point to API test suite
func TestApis(t *testing.T, b APIBuilder) {
ts := testSuite{
makeNode: b,
makeNodes: b,
}
t.Run("version", ts.testVersion)
@ -26,7 +26,7 @@ func TestApis(t *testing.T, b APIBuilder) {
func (ts *testSuite) testVersion(t *testing.T) {
ctx := context.Background()
fc := ts.makeNode()
fc := ts.makeNodes(t, 1)[0]
v, err := fc.Version(ctx)
if err != nil {

View File

@ -2,6 +2,7 @@ package api
import (
"encoding/json"
ma "github.com/multiformats/go-multiaddr"
)

View File

@ -3,6 +3,7 @@ package node
import (
"context"
"errors"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"reflect"
"time"
@ -236,6 +237,17 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
// In-memory / testing
func MockHost(mn mocknet.Mocknet) Option {
return Options(
applyIf(func(s *settings) bool { return !s.online },
Error(errors.New("MockHost must be specified after Online")),
),
Override(new(lp2p.RawHost), lp2p.MockHost),
Override(new(mocknet.Mocknet), mn),
)
}
func randomIdentity() Option {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {

34
node/node_test.go Normal file
View File

@ -0,0 +1,34 @@
package node
import (
"context"
"github.com/filecoin-project/go-lotus/api/test"
"testing"
"github.com/filecoin-project/go-lotus/api"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func builder(t *testing.T, n int) []api.API {
ctx := context.Background()
mn := mocknet.New(ctx)
out := make([]api.API, n)
for i := 0; i < n; i++ {
var err error
out[i], err = New(ctx,
Online(),
MockHost(mn),
)
if err != nil {
t.Fatal(err)
}
}
return out
}
func TestAPI(t *testing.T) {
test.TestApis(t, builder)
}