From 16c8e2739f76b2241c095a12a11361cc03b8bd04 Mon Sep 17 00:00:00 2001 From: Marie Gauthier Date: Fri, 13 May 2022 16:45:53 +0200 Subject: [PATCH] chore: audit testutil (#11954) ## Description Ref: https://github.com/cosmos/cosmos-sdk/issues/11362 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- testutil/mock/privval.go | 2 +- testutil/mock/privval_test.go | 32 ++++++++++++++++++++++++++++++++ testutil/network/network.go | 4 ++++ testutil/network/util.go | 6 ++++-- 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 testutil/mock/privval_test.go diff --git a/testutil/mock/privval.go b/testutil/mock/privval.go index 6aaceedd2..9c5a745d1 100644 --- a/testutil/mock/privval.go +++ b/testutil/mock/privval.go @@ -14,7 +14,7 @@ import ( var _ tmtypes.PrivValidator = PV{} -// MockPV implements PrivValidator without any safety or persistence. +// PV implements PrivValidator without any safety or persistence. // Only use it for testing. type PV struct { PrivKey cryptotypes.PrivKey diff --git a/testutil/mock/privval_test.go b/testutil/mock/privval_test.go new file mode 100644 index 000000000..a3326dbe4 --- /dev/null +++ b/testutil/mock/privval_test.go @@ -0,0 +1,32 @@ +package mock + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) + +func TestGetPubKey(t *testing.T) { + pv := NewPV() + pb, err := pv.GetPubKey(context.Background()) + require.NoError(t, err) + require.NotNil(t, pb) +} + +func TestSignVote(t *testing.T) { + pv := NewPV() + v := tmproto.Vote{} + err := pv.SignVote(context.Background(), "chain-id", &v) + require.NoError(t, err) + require.NotNil(t, v.Signature) +} + +func TestSignProposal(t *testing.T) { + pv := NewPV() + p := tmproto.Proposal{} + err := pv.SignProposal(context.Background(), "chain-id", &p) + require.NoError(t, err) + require.NotNil(t, p.Signature) +} diff --git a/testutil/network/network.go b/testutil/network/network.go index 995eb5688..2d89c4e69 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -181,18 +181,22 @@ type Logger interface { var _ Logger = (*testing.T)(nil) var _ Logger = (*CLILogger)(nil) +// CLILogger wraps a cobra.Command and provides command logging methods. type CLILogger struct { cmd *cobra.Command } +// Log logs given args. func (s CLILogger) Log(args ...interface{}) { s.cmd.Println(args...) } +// Logf logs given args according to a format specifier. func (s CLILogger) Logf(format string, args ...interface{}) { s.cmd.Printf(format, args...) } +// NewCLILogger creates a new CLILogger. func NewCLILogger(cmd *cobra.Command) CLILogger { return CLILogger{cmd} } diff --git a/testutil/network/util.go b/testutil/network/util.go index 6e5d2865c..79777ed09 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -2,6 +2,7 @@ package network import ( "encoding/json" + "fmt" "io/ioutil" "path/filepath" "time" @@ -16,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/api" servergrpc "github.com/cosmos/cosmos-sdk/server/grpc" srvtypes "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/types/errors" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/genutil" @@ -55,11 +57,11 @@ func startInProcess(cfg Config, val *Validator) error { if val.RPCAddress != "" { node, ok := val.tmNode.(local.NodeService) if !ok { - panic("can't cast service.Service to NodeService") + return fmt.Errorf("failed to cast %T to NodeService", val.tmNode) } val.RPCClient, err = local.New(node) if err != nil { - panic("cant create a local node") + return errors.Wrap(err, "failed to create a local node") } }