Skip to content

Commit

Permalink
test(monitoringp): increase code coverage (#736)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Bertrand <lucas.bertrand.22@gmail.com>
  • Loading branch information
Alex Johnson and lumtis authored Apr 22, 2022
1 parent 8a4a18e commit 9bab52f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 13 deletions.
3 changes: 2 additions & 1 deletion testutil/keeper/ibc_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func (c ChannelMock) GetChannel(_ sdk.Context, _, channelID string) (channel cha
}

// GetNextSequenceSend implements ChannelKeeper
// returns true for all cases
func (c ChannelMock) GetNextSequenceSend(_ sdk.Context, _, _ string) (uint64, bool) {
return 0, false
return 0, true
}

// SendPacket implements ChannelKeeper
Expand Down
108 changes: 108 additions & 0 deletions x/monitoringp/keeper/begin_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,111 @@ func TestKeeper_ReportBlockSignatures(t *testing.T) {
})
}
}

func TestKeeper_TransmitSignatures(t *testing.T) {
ctx, tk, _ := monitoringpKeeperWithFooClient(t)
valFoo, valBar, valBaz, valFred, valQux := sample.Validator(t, r),
sample.Validator(t, r),
sample.Validator(t, r),
sample.Validator(t, r),
sample.Validator(t, r)

// initialize staking validator set
tk.StakingKeeper.SetValidator(ctx, valFoo)
tk.StakingKeeper.SetValidator(ctx, valBar)
tk.StakingKeeper.SetValidator(ctx, valBaz)
tk.StakingKeeper.SetValidator(ctx, valFred)
tk.StakingKeeper.SetValidator(ctx, valQux)
err := tk.StakingKeeper.SetValidatorByConsAddr(ctx, valFoo)
require.NoError(t, err)
err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valBar)
require.NoError(t, err)
err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valBaz)
require.NoError(t, err)
err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valFred)
require.NoError(t, err)
err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valQux)
require.NoError(t, err)

tests := []struct {
name string
monitoringInfoExist bool
inputMonitoringInfo types.MonitoringInfo
lastBlockHeight int64
currentBlockHeight int64
channelIDExist bool
channelID types.ConnectionChannelID
expectedMonitoringInfoFound bool
expectedMonitoringInfo types.MonitoringInfo
wantErr bool
}{
{
name: "currentBlockHeight < lastBlockHeight returns nil",
monitoringInfoExist: false,
lastBlockHeight: 11,
currentBlockHeight: 10,
channelIDExist: false,
expectedMonitoringInfoFound: false,
},
{
name: "lastBlockHeight no channel ID set returns nil",
monitoringInfoExist: false,
lastBlockHeight: 10,
currentBlockHeight: 11,
channelIDExist: false,
expectedMonitoringInfoFound: false,
},
{
name: "no monitoring info found",
monitoringInfoExist: false,
lastBlockHeight: 10,
currentBlockHeight: 11,
channelIDExist: true,
channelID: types.ConnectionChannelID{ChannelID: "channelID"},
expectedMonitoringInfoFound: false,
},
{
name: "monitoring info found with channel not found",
monitoringInfoExist: true,
inputMonitoringInfo: tc.MonitoringInfo(1,
tc.SignatureCount(t,
valFoo.OperatorAddress,
"1")),
lastBlockHeight: 10,
currentBlockHeight: 11,
channelIDExist: true,
channelID: types.ConnectionChannelID{ChannelID: "channelID"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set keeper values
params := tk.MonitoringProviderKeeper.GetParams(ctx)
params.LastBlockHeight = tt.lastBlockHeight
tk.MonitoringProviderKeeper.SetParams(ctx, params)
if tt.monitoringInfoExist {
tk.MonitoringProviderKeeper.SetMonitoringInfo(ctx, tt.inputMonitoringInfo)
} else {
tk.MonitoringProviderKeeper.RemoveMonitoringInfo(ctx)
}

if tt.channelIDExist {
tk.MonitoringProviderKeeper.SetConnectionChannelID(ctx, tt.channelID)
}

// report
err := tk.MonitoringProviderKeeper.TransmitSignatures(ctx, tt.currentBlockHeight)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)

// check saved values
monitoringInfo, found := tk.MonitoringProviderKeeper.GetMonitoringInfo(ctx)
require.EqualValues(t, tt.expectedMonitoringInfoFound, found)
require.EqualValues(t, tt.expectedMonitoringInfo, monitoringInfo)
})
}
}
18 changes: 14 additions & 4 deletions x/monitoringp/keeper/grpc_connection_channel_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ import (
func TestConnectionChannelIDQuery(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t)
wctx := sdk.WrapSDKContext(ctx)
item := createTestConnectionChannelID(ctx, tk.MonitoringProviderKeeper)
for _, tc := range []struct {
desc string
setItem bool
request *types.QueryGetConnectionChannelIDRequest
response *types.QueryGetConnectionChannelIDResponse
err error
}{
{
desc: "Valid Request",
request: &types.QueryGetConnectionChannelIDRequest{},
response: &types.QueryGetConnectionChannelIDResponse{ConnectionChannelID: item},
desc: "object does not exist",
setItem: false,
request: &types.QueryGetConnectionChannelIDRequest{},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "object exists",
setItem: true,
request: &types.QueryGetConnectionChannelIDRequest{},
},
{
desc: "Invalid Request",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
if tc.setItem {
item := createTestConnectionChannelID(ctx, tk.MonitoringProviderKeeper)
tc.response = &types.QueryGetConnectionChannelIDResponse{ConnectionChannelID: item}
}
response, err := tk.MonitoringProviderKeeper.ConnectionChannelID(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
Expand Down
18 changes: 14 additions & 4 deletions x/monitoringp/keeper/grpc_consumer_client_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ import (
func TestConsumerClientIDQuery(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t)
wctx := sdk.WrapSDKContext(ctx)
item := createTestConsumerClientID(ctx, tk.MonitoringProviderKeeper)
for _, tc := range []struct {
desc string
setItem bool
request *types.QueryGetConsumerClientIDRequest
response *types.QueryGetConsumerClientIDResponse
err error
}{
{
desc: "Valid Request",
request: &types.QueryGetConsumerClientIDRequest{},
response: &types.QueryGetConsumerClientIDResponse{ConsumerClientID: item},
desc: "object does not exist",
setItem: false,
request: &types.QueryGetConsumerClientIDRequest{},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "object exists",
setItem: true,
request: &types.QueryGetConsumerClientIDRequest{},
},
{
desc: "Invalid Request",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
if tc.setItem {
item := createTestConsumerClientID(ctx, tk.MonitoringProviderKeeper)
tc.response = &types.QueryGetConsumerClientIDResponse{ConsumerClientID: item}
}
response, err := tk.MonitoringProviderKeeper.ConsumerClientID(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
Expand Down
18 changes: 14 additions & 4 deletions x/monitoringp/keeper/grpc_monitoring_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ import (
func TestMonitoringInfoQuery(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t)
wctx := sdk.WrapSDKContext(ctx)
item := createTestMonitoringInfo(ctx, tk.MonitoringProviderKeeper)
for _, tc := range []struct {
desc string
setItem bool
request *types.QueryGetMonitoringInfoRequest
response *types.QueryGetMonitoringInfoResponse
err error
}{
{
desc: "Valid Request",
request: &types.QueryGetMonitoringInfoRequest{},
response: &types.QueryGetMonitoringInfoResponse{MonitoringInfo: item},
desc: "object does not exist",
setItem: false,
request: &types.QueryGetMonitoringInfoRequest{},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "object exists",
setItem: true,
request: &types.QueryGetMonitoringInfoRequest{},
},
{
desc: "Invalid Request",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
if tc.setItem {
item := createTestMonitoringInfo(ctx, tk.MonitoringProviderKeeper)
tc.response = &types.QueryGetMonitoringInfoResponse{MonitoringInfo: item}
}
response, err := tk.MonitoringProviderKeeper.MonitoringInfo(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
Expand Down
9 changes: 9 additions & 0 deletions x/monitoringp/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func TestKeeper_VerifyClientIDFromChannelID(t *testing.T) {
require.NoError(t, err)
})

t.Run("should fail if channelClientID is not equal to consumerClientID", func(t *testing.T) {
ctx, tk, _ := monitoringpKeeperWithFooClient(t)
tk.MonitoringProviderKeeper.SetConsumerClientID(ctx, types.ConsumerClientID{
ClientID: "notequal",
})
err := tk.MonitoringProviderKeeper.VerifyClientIDFromChannelID(ctx, "foo")
require.ErrorIs(t, err, types.ErrInvalidClient)
})

t.Run("should fail if channel doesn't exist", func(t *testing.T) {
ctx, tk, _ := monitoringpKeeperWithFooClient(t)
tk.MonitoringProviderKeeper.SetConsumerClientID(ctx, types.ConsumerClientID{
Expand Down

0 comments on commit 9bab52f

Please sign in to comment.