Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cli: chain category unit tests #8048

Merged
merged 20 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ var ChainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -80,7 +82,7 @@ var ChainHeadCmd = &cli.Command{
}

for _, c := range head.Cids() {
fmt.Println(c)
afmt.Println(c)
}
return nil
},
Expand All @@ -97,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
Expand Down Expand Up @@ -124,7 +128,7 @@ var ChainGetBlock = &cli.Command{
return err
}

fmt.Println(string(out))
afmt.Println(string(out))
return nil
}

Expand Down Expand Up @@ -163,9 +167,8 @@ var ChainGetBlock = &cli.Command{
return err
}

fmt.Println(string(out))
afmt.Println(string(out))
return nil

},
}

Expand Down
92 changes: 92 additions & 0 deletions cli/chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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"
)

// newMockAppWithFullAPI returns a gomock-ed CLI app used for unit tests
// see cli/util/api.go:GetFullNodeAPI for mock API injection
func newMockAppWithFullAPI(t *testing.T, cmd *ucli.Command) (*ucli.App, *mocks.MockFullNode, *bytes.Buffer, func()) {
app := ucli.NewApp()
app.Commands = ucli.Commands{cmd}
app.Setup()

// create and inject the mock API into app Metadata
ctrl := gomock.NewController(t)
mockFullNode := mocks.NewMockFullNode(ctrl)
var fullNode api.FullNode = mockFullNode
app.Metadata["test-full-api"] = fullNode

// this will only work if the implementation uses the app.Writer,
// if it uses fmt.*, it has to be refactored
buf := &bytes.Buffer{}
app.Writer = buf

return app, mockFullNode, buf, ctrl.Finish
}

func TestChainHead(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainHeadCmd))
defer done()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ts := mock.TipSet(mock.MkBlock(nil, 0, 0))
gomock.InOrder(
mockApi.EXPECT().ChainHead(ctx).Return(ts, nil),
)

err := app.Run([]string{"chain", "head"})
assert.NoError(t, err)

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth it but there's maybe some value in passing along specific values from the mocks rather than empty values and then checking that cli output data matches. This could catch a bug that makes a command only return empty valued data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea. Checking for command outputs is a good enough solution, though it might miss bugs similar to the ones you have mentioned.
However, for this PR, I would leave the current solution as it stands, and we can refactor it in the future.

assert.NoError(t, err)

assert.True(t, block.Cid().Equals(out.Cid()))
}
5 changes: 5 additions & 0 deletions cli/util/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ func GetCommonAPI(ctx *cli.Context) (api.CommonNet, jsonrpc.ClientCloser, error)
}

func GetFullNodeAPI(ctx *cli.Context) (v0api.FullNode, jsonrpc.ClientCloser, error) {
// use the mocked API in CLI unit tests, see cli/chain_test.go for mock definition
if mock, ok := ctx.App.Metadata["test-full-api"]; ok {
return &v0api.WrapperV1Full{FullNode: mock.(v1api.FullNode)}, func() {}, nil
}

if tn, ok := ctx.App.Metadata["testnode-full"]; ok {
return &v0api.WrapperV1Full{FullNode: tn.(v1api.FullNode)}, func() {}, nil
}
Expand Down