From af0c35aabf9c897e11d04936474fca8166f31337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Mon, 13 Feb 2023 17:21:26 +0100 Subject: [PATCH] imp(testing): use testing.TB instead of testing.T (#3138) --- CHANGELOG.md | 1 + modules/core/ante/ante_test.go | 2 +- testing/app.go | 2 +- testing/chain.go | 51 +++++++++++++++++----------------- testing/endpoint.go | 40 +++++++++++++------------- testing/simapp/test_helpers.go | 14 +++++----- testing/utils.go | 6 ++-- 7 files changed, 58 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13401dee974..6a8a1fb333d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (tests) [\#3138](https://github.com/cosmos/ibc-go/pull/3138) Support benchmarks and fuzz tests through `testing.TB`. * (core) [\#3082](https://github.com/cosmos/ibc-go/pull/3082) Add `HasConnection` and `HasChannel` methods. * (tests) [\#2926](https://github.com/cosmos/ibc-go/pull/2926) Lint tests * (apps/transfer) [\#2643](https://github.com/cosmos/ibc-go/pull/2643) Add amount, denom, and memo to transfer event emission. diff --git a/modules/core/ante/ante_test.go b/modules/core/ante/ante_test.go index aca0fefd9a7..20ed350a053 100644 --- a/modules/core/ante/ante_test.go +++ b/modules/core/ante/ante_test.go @@ -168,7 +168,7 @@ func (suite *AnteTestSuite) createUpdateClientMessage() sdk.Msg { endpoint.ClientID, header, endpoint.Chain.SenderAccount.GetAddress().String(), ) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) return msg } diff --git a/testing/app.go b/testing/app.go index f0fa35462f3..fea0ed503b4 100644 --- a/testing/app.go +++ b/testing/app.go @@ -61,7 +61,7 @@ func SetupTestingApp() (TestingApp, map[string]json.RawMessage) { // that also act as delegators. For simplicity, each validator is bonded with a delegation // of one consensus engine unit (10^6) in the default token of the simapp from first genesis // account. A Nop logger is set in SimApp. -func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction math.Int, balances ...banktypes.Balance) TestingApp { +func SetupWithGenesisValSet(t testing.TB, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction math.Int, balances ...banktypes.Balance) TestingApp { app, genesisState := DefaultTestingAppInit() // set genesis accounts diff --git a/testing/chain.go b/testing/chain.go index 709d4416f99..e1449029811 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -48,7 +48,7 @@ type SenderAccount struct { // is used for delivering transactions through the application state. // NOTE: the actual application uses an empty chain-id for ease of testing. type TestChain struct { - *testing.T + testing.TB Coordinator *Coordinator App TestingApp @@ -91,7 +91,7 @@ type TestChain struct { // // CONTRACT: Validator array must be provided in the order expected by Tendermint. // i.e. sorted first by power and then lexicographically by address. -func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, valSet *tmtypes.ValidatorSet, signers map[string]tmtypes.PrivValidator) *TestChain { +func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, valSet *tmtypes.ValidatorSet, signers map[string]tmtypes.PrivValidator) *TestChain { genAccs := []authtypes.GenesisAccount{} genBals := []banktypes.Balance{} senderAccs := []SenderAccount{} @@ -101,7 +101,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, va senderPrivKey := secp256k1.GenPrivKey() acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) amount, ok := sdk.NewIntFromString("10000000000000000000") - require.True(t, ok) + require.True(tb, ok) // add sender account balance := banktypes.Balance{ @@ -120,7 +120,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, va senderAccs = append(senderAccs, senderAcc) } - app := SetupWithGenesisValSet(t, valSet, genAccs, chainID, sdk.DefaultPowerReduction, genBals...) + app := SetupWithGenesisValSet(tb, valSet, genAccs, chainID, sdk.DefaultPowerReduction, genBals...) // create current header and call begin block header := tmproto.Header{ @@ -133,7 +133,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, va // create an account to send transactions from chain := &TestChain{ - T: t, + TB: tb, Coordinator: coord, ChainID: chainID, App: app, @@ -190,7 +190,7 @@ func (chain *TestChain) GetContext() sdk.Context { // their own SimApp. func (chain *TestChain) GetSimApp() *simapp.SimApp { app, ok := chain.App.(*simapp.SimApp) - require.True(chain.T, ok) + require.True(chain.TB, ok) return app } @@ -219,10 +219,10 @@ func (chain *TestChain) QueryProofForStore(storeKey string, key []byte, height i }) merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) proof, err := chain.App.AppCodec().Marshal(&merkleProof) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) revision := clienttypes.ParseChainID(chain.ChainID) @@ -243,10 +243,10 @@ func (chain *TestChain) QueryUpgradeProof(key []byte, height uint64) ([]byte, cl }) merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) proof, err := chain.App.AppCodec().Marshal(&merkleProof) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) revision := clienttypes.ParseChainID(chain.ChainID) @@ -286,7 +286,7 @@ func (chain *TestChain) NextBlock() { // val set changes returned from previous block get applied to the next validators // of this block. See tendermint spec for details. chain.Vals = chain.NextVals - chain.NextVals = ApplyValSetChanges(chain.T, chain.Vals, res.ValidatorUpdates) + chain.NextVals = ApplyValSetChanges(chain.TB, chain.Vals, res.ValidatorUpdates) // increment the current header chain.CurrentHeader = tmproto.Header{ @@ -318,15 +318,14 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { chain.Coordinator.UpdateTimeForChain(chain) _, r, err := simapp.SignAndDeliver( - chain.T, + chain.TB, chain.TxConfig, chain.App.GetBaseApp(), - chain.GetContext().BlockHeader(), msgs, chain.ChainID, []uint64{chain.SenderAccount.GetAccountNumber()}, []uint64{chain.SenderAccount.GetSequence()}, - true, true, chain.SenderPrivKey, + true, chain.SenderPrivKey, ) if err != nil { return nil, err @@ -350,7 +349,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { // expected to exist otherwise testing will fail. func (chain *TestChain) GetClientState(clientID string) exported.ClientState { clientState, found := chain.App.GetIBCKeeper().ClientKeeper.GetClientState(chain.GetContext(), clientID) - require.True(chain.T, found) + require.True(chain.TB, found) return clientState } @@ -382,7 +381,7 @@ func (chain *TestChain) GetValsAtHeight(height int64) (*tmtypes.ValidatorSet, bo // acknowledgement does not exist then testing will fail. func (chain *TestChain) GetAcknowledgement(packet exported.PacketI) []byte { ack, found := chain.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(chain.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - require.True(chain.T, found) + require.True(chain.TB, found) return ack } @@ -457,7 +456,7 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64, valSet *tmproto.ValidatorSet trustedVals *tmproto.ValidatorSet ) - require.NotNil(chain.T, tmValSet) + require.NotNil(chain.TB, tmValSet) vsetHash := tmValSet.Hash() nextValHash := nextVals.Hash() @@ -492,7 +491,7 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64, } commit, err := tmtypes.MakeCommit(blockID, blockHeight, 1, voteSet, signerArr, timestamp) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) signedHeader := &tmproto.SignedHeader{ Header: tmHeader.ToProto(), @@ -501,12 +500,12 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64, if tmValSet != nil { //nolint:staticcheck valSet, err = tmValSet.ToProto() - require.NoError(chain.T, err) + require.NoError(chain.TB, err) } if tmTrustedVals != nil { trustedVals, err = tmTrustedVals.ToProto() - require.NoError(chain.T, err) + require.NoError(chain.TB, err) } // The trusted fields may be nil. They may be filled before relaying messages to a client. @@ -540,11 +539,11 @@ func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.Scope if !ok { // create capability using the IBC capability keeper cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), host.PortPath(portID)) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) // claim capability using the scopedKeeper err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID)) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) } chain.NextBlock() @@ -554,7 +553,7 @@ func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.Scope // exist, otherwise testing will fail. func (chain *TestChain) GetPortCapability(portID string) *capabilitytypes.Capability { cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID)) - require.True(chain.T, ok) + require.True(chain.TB, ok) return cap } @@ -568,9 +567,9 @@ func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.Sc _, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), capName) if !ok { cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), capName) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, capName) - require.NoError(chain.T, err) + require.NoError(chain.TB, err) } chain.NextBlock() @@ -580,7 +579,7 @@ func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.Sc // The capability must exist, otherwise testing will fail. func (chain *TestChain) GetChannelCapability(portID, channelID string) *capabilitytypes.Capability { cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.ChannelCapabilityPath(portID, channelID)) - require.True(chain.T, ok) + require.True(chain.TB, ok) return cap } diff --git a/testing/endpoint.go b/testing/endpoint.go index 4c3fce34936..b30c1534fc7 100644 --- a/testing/endpoint.go +++ b/testing/endpoint.go @@ -89,7 +89,7 @@ func (endpoint *Endpoint) CreateClient() (err error) { switch endpoint.ClientConfig.GetClientType() { case exported.Tendermint: tmConfig, ok := endpoint.ClientConfig.(*TendermintConfig) - require.True(endpoint.Chain.T, ok) + require.True(endpoint.Chain.TB, ok) height := endpoint.Counterparty.Chain.LastHeader.GetHeight().(clienttypes.Height) clientState = ibctm.NewClientState( @@ -98,7 +98,7 @@ func (endpoint *Endpoint) CreateClient() (err error) { consensusState = endpoint.Counterparty.Chain.LastHeader.ConsensusState() case exported.Solomachine: // TODO - // solo := NewSolomachine(endpoint.Chain.T, endpoint.Chain.Codec, clientID, "", 1) + // solo := NewSolomachine(endpoint.Chain.TB, endpoint.Chain.Codec, clientID, "", 1) // clientState = solo.ClientState() // consensusState = solo.ConsensusState() @@ -113,7 +113,7 @@ func (endpoint *Endpoint) CreateClient() (err error) { msg, err := clienttypes.NewMsgCreateClient( clientState, consensusState, endpoint.Chain.SenderAccount.GetAddress().String(), ) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) res, err := endpoint.Chain.SendMsgs(msg) if err != nil { @@ -121,7 +121,7 @@ func (endpoint *Endpoint) CreateClient() (err error) { } endpoint.ClientID, err = ParseClientIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) return nil } @@ -149,7 +149,7 @@ func (endpoint *Endpoint) UpdateClient() (err error) { endpoint.ClientID, header, endpoint.Chain.SenderAccount.GetAddress().String(), ) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) return endpoint.Chain.sendMsgs(msg) } @@ -221,7 +221,7 @@ func (endpoint *Endpoint) ConnOpenInit() error { } endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) return nil } @@ -229,7 +229,7 @@ func (endpoint *Endpoint) ConnOpenInit() error { // ConnOpenTry will construct and execute a MsgConnectionOpenTry on the associated endpoint. func (endpoint *Endpoint) ConnOpenTry() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) counterpartyClient, proofClient, proofConsensus, consensusHeight, proofInit, proofHeight := endpoint.QueryConnectionHandshakeProof() @@ -247,7 +247,7 @@ func (endpoint *Endpoint) ConnOpenTry() error { if endpoint.ConnectionID == "" { endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) } return nil @@ -256,7 +256,7 @@ func (endpoint *Endpoint) ConnOpenTry() error { // ConnOpenAck will construct and execute a MsgConnectionOpenAck on the associated endpoint. func (endpoint *Endpoint) ConnOpenAck() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) counterpartyClient, proofClient, proofConsensus, consensusHeight, proofTry, proofHeight := endpoint.QueryConnectionHandshakeProof() @@ -273,7 +273,7 @@ func (endpoint *Endpoint) ConnOpenAck() error { // ConnOpenConfirm will construct and execute a MsgConnectionOpenConfirm on the associated endpoint. func (endpoint *Endpoint) ConnOpenConfirm() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) connectionKey := host.ConnectionKey(endpoint.Counterparty.ConnectionID) proof, height := endpoint.Counterparty.Chain.QueryProof(connectionKey) @@ -329,7 +329,7 @@ func (endpoint *Endpoint) ChanOpenInit() error { } endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) // update version to selected app version // NOTE: this update must be performed after SendMsgs() @@ -341,7 +341,7 @@ func (endpoint *Endpoint) ChanOpenInit() error { // ChanOpenTry will construct and execute a MsgChannelOpenTry on the associated endpoint. func (endpoint *Endpoint) ChanOpenTry() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) @@ -360,7 +360,7 @@ func (endpoint *Endpoint) ChanOpenTry() error { if endpoint.ChannelID == "" { endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) } // update version to selected app version @@ -373,7 +373,7 @@ func (endpoint *Endpoint) ChanOpenTry() error { // ChanOpenAck will construct and execute a MsgChannelOpenAck on the associated endpoint. func (endpoint *Endpoint) ChanOpenAck() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) @@ -397,7 +397,7 @@ func (endpoint *Endpoint) ChanOpenAck() error { // ChanOpenConfirm will construct and execute a MsgChannelOpenConfirm on the associated endpoint. func (endpoint *Endpoint) ChanOpenConfirm() error { err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) + require.NoError(endpoint.Chain.TB, err) channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) @@ -526,7 +526,7 @@ func (endpoint *Endpoint) TimeoutPacket(packet channeltypes.Packet) error { proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey) nextSeqRecv, found := endpoint.Counterparty.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(endpoint.Counterparty.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) + require.True(endpoint.Chain.TB, found) timeoutMsg := channeltypes.NewMsgTimeout( packet, nextSeqRecv, @@ -556,7 +556,7 @@ func (endpoint *Endpoint) TimeoutOnClose(packet channeltypes.Packet) error { proofClosed, _ := endpoint.Counterparty.QueryProof(channelKey) nextSeqRecv, found := endpoint.Counterparty.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(endpoint.Counterparty.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) + require.True(endpoint.Chain.TB, found) timeoutOnCloseMsg := channeltypes.NewMsgTimeoutOnClose( packet, nextSeqRecv, @@ -593,7 +593,7 @@ func (endpoint *Endpoint) SetClientState(clientState exported.ClientState) { // The consensus state is expected to exist otherwise testing will fail. func (endpoint *Endpoint) GetConsensusState(height exported.Height) exported.ConsensusState { consensusState, found := endpoint.Chain.GetConsensusState(endpoint.ClientID, height) - require.True(endpoint.Chain.T, found) + require.True(endpoint.Chain.TB, found) return consensusState } @@ -607,7 +607,7 @@ func (endpoint *Endpoint) SetConsensusState(consensusState exported.ConsensusSta // connection is expected to exist otherwise testing will fail. func (endpoint *Endpoint) GetConnection() connectiontypes.ConnectionEnd { connection, found := endpoint.Chain.App.GetIBCKeeper().ConnectionKeeper.GetConnection(endpoint.Chain.GetContext(), endpoint.ConnectionID) - require.True(endpoint.Chain.T, found) + require.True(endpoint.Chain.TB, found) return connection } @@ -621,7 +621,7 @@ func (endpoint *Endpoint) SetConnection(connection connectiontypes.ConnectionEnd // is expected to exist otherwise testing will fail. func (endpoint *Endpoint) GetChannel() channeltypes.Channel { channel, found := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) + require.True(endpoint.Chain.TB, found) return channel } diff --git a/testing/simapp/test_helpers.go b/testing/simapp/test_helpers.go index d2d27aa66e0..3a6ff204a87 100644 --- a/testing/simapp/test_helpers.go +++ b/testing/simapp/test_helpers.go @@ -209,8 +209,8 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba // // CONTRACT: BeginBlock must be called before this function. func SignAndDeliver( - t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, - chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, + tb testing.TB, txCfg client.TxConfig, app *bam.BaseApp, msgs []sdk.Msg, + chainID string, accNums, accSeqs []uint64, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { tx, err := simtestutil.GenSignedMockTx( rand.New(rand.NewSource(time.Now().UnixNano())), @@ -223,17 +223,17 @@ func SignAndDeliver( accSeqs, priv..., ) - require.NoError(t, err) + require.NoError(tb, err) // Simulate a sending a transaction gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx) if expPass { - require.NoError(t, err) - require.NotNil(t, res) + require.NoError(tb, err) + require.NotNil(tb, res) } else { - require.Error(t, err) - require.Nil(t, res) + require.Error(tb, err) + require.Nil(tb, res) } return gInfo, res, err diff --git a/testing/utils.go b/testing/utils.go index f9f64bf72bd..b0d810b4cad 100644 --- a/testing/utils.go +++ b/testing/utils.go @@ -10,14 +10,14 @@ import ( // ApplyValSetChanges takes in tmtypes.ValidatorSet and []abci.ValidatorUpdate and will return a new tmtypes.ValidatorSet which has the // provided validator updates applied to the provided validator set. -func ApplyValSetChanges(t *testing.T, valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) *tmtypes.ValidatorSet { +func ApplyValSetChanges(tb testing.TB, valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) *tmtypes.ValidatorSet { updates, err := tmtypes.PB2TM.ValidatorUpdates(valUpdates) - require.NoError(t, err) + require.NoError(tb, err) // must copy since validator set will mutate with UpdateWithChangeSet newVals := valSet.Copy() err = newVals.UpdateWithChangeSet(updates) - require.NoError(t, err) + require.NoError(tb, err) return newVals }