From 78649d45b90576411e9821bc7a86f065e9b79165 Mon Sep 17 00:00:00 2001 From: Nikola Divic Date: Tue, 8 Feb 2022 18:24:45 +0100 Subject: [PATCH] test: cli chain getblock command Unit test for the cli `chain getblock` command. Tests if output is JSON in the expected format. --- cli/chain.go | 7 ++++--- cli/chain_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index e782d2ca9..4d03ac4f7 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -99,6 +99,8 @@ var ChainGetBlock = &cli.Command{ }, }, Action: func(cctx *cli.Context) error { + afmt := NewAppFmt(cctx.App) + api, closer, err := GetFullNodeAPI(cctx) if err != nil { return err @@ -126,7 +128,7 @@ var ChainGetBlock = &cli.Command{ return err } - fmt.Println(string(out)) + afmt.Println(string(out)) return nil } @@ -165,9 +167,8 @@ var ChainGetBlock = &cli.Command{ return err } - fmt.Println(string(out)) + afmt.Println(string(out)) return nil - }, } diff --git a/cli/chain_test.go b/cli/chain_test.go index c8c491e4d..eb98110f0 100644 --- a/cli/chain_test.go +++ b/cli/chain_test.go @@ -3,13 +3,16 @@ package cli import ( "bytes" "context" + "encoding/json" "regexp" "testing" "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api/mocks" + types "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types/mock" "github.com/golang/mock/gomock" + cid "github.com/ipfs/go-cid" "github.com/stretchr/testify/assert" ucli "github.com/urfave/cli/v2" ) @@ -52,3 +55,38 @@ func TestChainHead(t *testing.T) { assert.Regexp(t, regexp.MustCompile(ts.Cids()[0].String()), buf.String()) } + +// TestGetBlock checks if "lotus chain getblock" returns the block information in the expected format +func TestGetBlock(t *testing.T) { + app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainGetBlock)) + defer done() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + block := mock.MkBlock(nil, 0, 0) + blockMsgs := api.BlockMessages{} + + gomock.InOrder( + mockApi.EXPECT().ChainGetBlock(ctx, block.Cid()).Return(block, nil), + mockApi.EXPECT().ChainGetBlockMessages(ctx, block.Cid()).Return(&blockMsgs, nil), + mockApi.EXPECT().ChainGetParentMessages(ctx, block.Cid()).Return([]api.Message{}, nil), + mockApi.EXPECT().ChainGetParentReceipts(ctx, block.Cid()).Return([]*types.MessageReceipt{}, nil), + ) + + err := app.Run([]string{"chain", "getblock", block.Cid().String()}) + assert.NoError(t, err) + + out := struct { + types.BlockHeader + BlsMessages []*types.Message + SecpkMessages []*types.SignedMessage + ParentReceipts []*types.MessageReceipt + ParentMessages []cid.Cid + }{} + + err = json.Unmarshal(buf.Bytes(), &out) + assert.NoError(t, err) + + assert.True(t, block.Cid().Equals(out.Cid())) +}