-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
811bc62
test: cli test setup & test chain head
TheDivic 35918cb
Merge branch 'master' of https://github.com/filecoin-project/lotus in…
TheDivic 78649d4
test: cli chain getblock command
TheDivic 1cd590a
test: chain read-obj
TheDivic c0f47e5
test: chain delete-obj cli command
TheDivic a923d7c
test: chain stat-obj cli command
TheDivic e797ec1
test: chain getmessage cli command
TheDivic ae49729
test: chain sethead cli command
TheDivic 4e37131
test: chain inspect-usage cli command
TheDivic b3f7db7
test: chain list (love) cli command
TheDivic 6bc2ee2
test: chain get cli command
TheDivic b536dfa
test: chain bisect cli command
TheDivic c0f89e5
test: chain export cli command
TheDivic e5ac866
test: chain gas-price cli command
TheDivic 107eb76
cleanup: small cleanup before final push
TheDivic e6c8c9a
doc: add stm annotation to cli chain tests
TheDivic 6feae19
Fix PR comments.
87c63e2
Add stm file annotation
675012f
Fix matching for TestInspectUsage
580fa86
Change annotation to #cli
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
assert.NoError(t, err) | ||
|
||
assert.True(t, block.Cid().Equals(out.Cid())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.