From ba846e9bfbe6542acc28771ea42b1de908ce5a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 9 Jul 2019 18:27:07 +0200 Subject: [PATCH] Wire up node API to tests --- api/test/test.go | 8 ++++---- api/types.go | 1 + node/builder.go | 12 ++++++++++++ node/node_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 node/node_test.go diff --git a/api/test/test.go b/api/test/test.go index ecb298cf5..e60156bda 100644 --- a/api/test/test.go +++ b/api/test/test.go @@ -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 { diff --git a/api/types.go b/api/types.go index 3faf6f109..449a3ae79 100644 --- a/api/types.go +++ b/api/types.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + ma "github.com/multiformats/go-multiaddr" ) diff --git a/node/builder.go b/node/builder.go index 8b05795b2..d74a936df 100644 --- a/node/builder.go +++ b/node/builder.go @@ -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 { diff --git a/node/node_test.go b/node/node_test.go new file mode 100644 index 000000000..2becf65a1 --- /dev/null +++ b/node/node_test.go @@ -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) +}