Add Send CLI tests

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2021-02-15 13:15:39 +01:00
parent afd5828d74
commit 607d1bc6f7
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
3 changed files with 176 additions and 17 deletions

View File

@ -148,13 +148,12 @@ var sendCmd = &cli.Command{
if err != nil {
if errors.Is(err, ErrSendBalanceTooLow) {
fmt.Printf("%s\n", err.Error())
return fmt.Errorf("--force must be specified for this action to have an effect; you have been warned: %w", err)
}
return xerrors.Errorf("executing send: %w", err)
}
fmt.Printf("%s\n", msgCid)
fmt.Fprintf(cctx.App.Writer, "%s\n", msgCid)
return nil
},
}

128
cli/send_test.go Normal file
View File

@ -0,0 +1,128 @@
package cli
import (
"bytes"
"errors"
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
types "github.com/filecoin-project/lotus/chain/types"
gomock "github.com/golang/mock/gomock"
cid "github.com/ipfs/go-cid"
"github.com/stretchr/testify/assert"
ucli "github.com/urfave/cli/v2"
)
var arbtCid = (&types.Message{
From: mustAddr(address.NewIDAddress(2)),
To: mustAddr(address.NewIDAddress(1)),
Value: types.NewInt(1000),
}).Cid()
func mustAddr(a address.Address, err error) address.Address {
if err != nil {
panic(err)
}
return a
}
func newMockCmd(t *testing.T, cmd *ucli.Command) (*ucli.App, *MockServicesAPI, *bytes.Buffer, func()) {
app := ucli.NewApp()
app.Commands = ucli.Commands{cmd}
app.Setup()
mockCtrl := gomock.NewController(t)
mockSrvcs := NewMockServicesAPI(mockCtrl)
app.Metadata["test-services"] = mockSrvcs
buf := &bytes.Buffer{}
app.Writer = buf
return app, mockSrvcs, buf, mockCtrl.Finish
}
func TestSendCLI(t *testing.T) {
oneFil := abi.TokenAmount(types.MustParseFIL("1"))
t.Run("simple", func(t *testing.T) {
app, mockSrvcs, buf, done := newMockCmd(t, sendCmd)
defer done()
gomock.InOrder(
mockSrvcs.EXPECT().Send(gomock.Any(), SendParams{
To: mustAddr(address.NewIDAddress(1)),
Val: oneFil,
}).Return(arbtCid, nil),
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "t01", "1"})
assert.NoError(t, err)
assert.EqualValues(t, arbtCid.String()+"\n", buf.String())
})
t.Run("ErrSendBalanceTooLow", func(t *testing.T) {
app, mockSrvcs, _, done := newMockCmd(t, sendCmd)
defer done()
gomock.InOrder(
mockSrvcs.EXPECT().Send(gomock.Any(), SendParams{
To: mustAddr(address.NewIDAddress(1)),
Val: oneFil,
}).Return(cid.Undef, ErrSendBalanceTooLow),
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "t01", "1"})
assert.ErrorIs(t, err, ErrSendBalanceTooLow)
})
t.Run("generic-err-is-forwarded", func(t *testing.T) {
app, mockSrvcs, _, done := newMockCmd(t, sendCmd)
defer done()
errMark := errors.New("something")
gomock.InOrder(
mockSrvcs.EXPECT().Send(gomock.Any(), SendParams{
To: mustAddr(address.NewIDAddress(1)),
Val: oneFil,
}).Return(cid.Undef, errMark),
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "t01", "1"})
assert.ErrorIs(t, err, errMark)
})
t.Run("from-specific", func(t *testing.T) {
app, mockSrvcs, buf, done := newMockCmd(t, sendCmd)
defer done()
gomock.InOrder(
mockSrvcs.EXPECT().Send(gomock.Any(), SendParams{
To: mustAddr(address.NewIDAddress(1)),
From: mustAddr(address.NewIDAddress(2)),
Val: oneFil,
}).Return(arbtCid, nil),
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "--from=t02", "t01", "1"})
assert.NoError(t, err)
assert.EqualValues(t, arbtCid.String()+"\n", buf.String())
})
t.Run("nonce-specific", func(t *testing.T) {
app, mockSrvcs, buf, done := newMockCmd(t, sendCmd)
defer done()
zero := uint64(0)
gomock.InOrder(
mockSrvcs.EXPECT().Send(gomock.Any(), SendParams{
To: mustAddr(address.NewIDAddress(1)),
Nonce: &zero,
Val: oneFil,
}).Return(arbtCid, nil),
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "--nonce=0", "t01", "1"})
assert.NoError(t, err)
assert.EqualValues(t, arbtCid.String()+"\n", buf.String())
})
}

View File

@ -16,6 +16,39 @@ import (
"github.com/stretchr/testify/assert"
)
type makerKeyType struct{}
var markerKey = makerKeyType{}
type contextMatcher struct {
marker *int
}
// Matches returns whether x is a match.
func (cm contextMatcher) Matches(x interface{}) bool {
ctx, ok := x.(context.Context)
if !ok {
return false
}
maybeMarker, ok := ctx.Value(markerKey).(*int)
if !ok {
return false
}
return cm.marker == maybeMarker
}
func (cm contextMatcher) String() string {
return fmt.Sprintf("Context with Value(%v/%T, %p)", markerKey, markerKey, cm.marker)
}
func ContextWithMarker(ctx context.Context) (context.Context, gomock.Matcher) {
marker := new(int)
outCtx := context.WithValue(ctx, markerKey, marker)
return outCtx, contextMatcher{marker: marker}
}
func setupMockSrvcs(t *testing.T) (*ServicesImpl, *mocks.MockFullNode) {
mockCtrl := gomock.NewController(t)
@ -114,8 +147,7 @@ func TestSendService(t *testing.T) {
Val: types.NewInt(balance - 100),
}
ctx, done := context.WithCancel(context.Background())
defer done()
ctx, ctxM := ContextWithMarker(context.Background())
t.Run("happy", func(t *testing.T) {
params := params
@ -123,8 +155,8 @@ func TestSendService(t *testing.T) {
defer srvcs.Close() //nolint:errcheck
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
mockApi.EXPECT().WalletBalance(ctxM, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctxM, MessageMatcher(params), nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
@ -137,7 +169,7 @@ func TestSendService(t *testing.T) {
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close() //nolint:errcheck
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance-200), nil),
mockApi.EXPECT().WalletBalance(ctxM, a1).Return(types.NewInt(balance-200), nil),
// no MpoolPushMessage
)
@ -153,8 +185,8 @@ func TestSendService(t *testing.T) {
defer srvcs.Close() //nolint:errcheck
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance-200), nil).AnyTimes(),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
mockApi.EXPECT().WalletBalance(ctxM, a1).Return(types.NewInt(balance-200), nil).AnyTimes(),
mockApi.EXPECT().MpoolPushMessage(ctxM, MessageMatcher(params), nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
@ -172,9 +204,9 @@ func TestSendService(t *testing.T) {
defer srvcs.Close() //nolint:errcheck
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(a1, nil),
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, mm, nil).DoAndReturn(sign),
mockApi.EXPECT().WalletDefaultAddress(ctxM).Return(a1, nil),
mockApi.EXPECT().WalletBalance(ctxM, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctxM, mm, nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
@ -194,13 +226,13 @@ func TestSendService(t *testing.T) {
var sm *types.SignedMessage
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().WalletSignMessage(ctx, a1, mm).DoAndReturn(
mockApi.EXPECT().WalletBalance(ctxM, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().WalletSignMessage(ctxM, a1, mm).DoAndReturn(
func(_ context.Context, _ address.Address, msg *types.Message) (*types.SignedMessage, error) {
sm = fakeSign(msg)
// now we expect MpoolPush with that SignedMessage
mockApi.EXPECT().MpoolPush(ctx, sm).Return(sm.Cid(), nil)
mockApi.EXPECT().MpoolPush(ctxM, sm).Return(sm.Cid(), nil)
return sm, nil
}),
)
@ -223,8 +255,8 @@ func TestSendService(t *testing.T) {
defer srvcs.Close() //nolint:errcheck
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
mockApi.EXPECT().WalletBalance(ctxM, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctxM, MessageMatcher(params), nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)