test: cli chain getblock command

Unit test for the cli `chain getblock` command.
Tests if output is JSON  in the expected format.
This commit is contained in:
Nikola Divic 2022-02-08 18:24:45 +01:00
parent 35918cbd7e
commit 78649d45b9
2 changed files with 42 additions and 3 deletions

View File

@ -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
},
}

View File

@ -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()))
}