Skip to content
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

testutils refactors and cleanup #467

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/consumer-democracy/proposals_whitelisting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

ibctesting "github.com/cosmos/ibc-go/v3/testing"
appConsumer "github.com/cosmos/interchain-security/app/consumer-democracy"
simapp "github.com/cosmos/interchain-security/testutil/simapp"
ibctestingutils "github.com/cosmos/interchain-security/testutil/ibc_testing_utils"
"github.com/stretchr/testify/require"
)

func TestDemocracyGovernanceWhitelistingKeys(t *testing.T) {
chain := ibctesting.NewTestChain(t, simapp.NewBasicCoordinator(t), simapp.SetupTestingAppConsumerDemocracy, "test")
chain := ibctesting.NewTestChain(t, ibctesting.NewCoordinator(t, 0),
ibctestingutils.DemocracyConsumerAppIniter, "test")
paramKeeper := chain.App.(*appConsumer.App).ParamsKeeper
for paramKey := range appConsumer.WhitelistedParams {
ss, ok := paramKeeper.GetSubspace(paramKey.Subspace)
Expand Down
6 changes: 3 additions & 3 deletions tests/difference/core/driver/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
appConsumer "github.com/cosmos/interchain-security/app/consumer"
appProvider "github.com/cosmos/interchain-security/app/provider"

simibc "github.com/cosmos/interchain-security/testutil/simibc"
ibctestingutils "github.com/cosmos/interchain-security/testutil/ibc_testing_utils"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
Expand All @@ -31,7 +31,7 @@ type CoreSuite struct {
traces Traces

// simulate a relayed path
simibc simibc.RelayedPath
simibc ibctestingutils.RelayedPath

// keep around validators for easy access
valAddresses []sdk.ValAddress
Expand Down Expand Up @@ -464,5 +464,5 @@ func (s *CoreSuite) SetupTest() {
s.valAddresses = valAddresses
s.offsetHeight = offsetHeight
s.offsetTimeUnix = offsetTimeUnix
s.simibc = simibc.MakeRelayedPath(s.Suite.T(), path)
s.simibc = ibctestingutils.MakeRelayedPath(s.Suite.T(), path)
}
24 changes: 11 additions & 13 deletions tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/interchain-security/x/ccv/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -23,7 +22,7 @@ import (

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
simapp "github.com/cosmos/interchain-security/testutil/simapp"
ibctestingutils "github.com/cosmos/interchain-security/testutil/ibc_testing_utils"

cryptoEd25519 "crypto/ed25519"

Expand All @@ -39,7 +38,6 @@ import (
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
appConsumer "github.com/cosmos/interchain-security/app/consumer"
appProvider "github.com/cosmos/interchain-security/app/provider"
simibc "github.com/cosmos/interchain-security/testutil/simibc"
consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
Expand All @@ -50,7 +48,7 @@ import (

type Builder struct {
suite *suite.Suite
link simibc.OrderedLink
link ibctestingutils.OrderedLink
path *ibctesting.Path
coordinator *ibctesting.Coordinator
clientHeaders map[string][]*ibctmtypes.Header
Expand Down Expand Up @@ -337,14 +335,14 @@ func (b *Builder) createValidators() (*tmtypes.ValidatorSet, map[string]tmtypes.

func (b *Builder) createChains() {

coordinator := simapp.NewBasicCoordinator(b.suite.T())
coordinator := ibctesting.NewCoordinator(b.suite.T(), 0)

// Create validators
validators, signers, addresses := b.createValidators()
// Create provider
coordinator.Chains[ibctesting.GetChainID(0)] = b.newChain(coordinator, simapp.SetupTestingappProvider, ibctesting.GetChainID(0), validators, signers)
coordinator.Chains[ibctesting.GetChainID(0)] = b.newChain(coordinator, ibctestingutils.ProviderAppIniter, ibctesting.GetChainID(0), validators, signers)
// Create consumer, using the same validators.
coordinator.Chains[ibctesting.GetChainID(1)] = b.newChain(coordinator, simapp.SetupTestingAppConsumer, ibctesting.GetChainID(1), validators, signers)
coordinator.Chains[ibctesting.GetChainID(1)] = b.newChain(coordinator, ibctestingutils.ConsumerAppIniter, ibctesting.GetChainID(1), validators, signers)

b.coordinator = coordinator
b.valAddresses = addresses
Expand Down Expand Up @@ -486,7 +484,7 @@ func (b *Builder) createConsumerGenesis(tmConfig *ibctesting.TendermintConfig) *
}

func (b *Builder) createLink() {
b.link = simibc.MakeOrderedLink()
b.link = ibctestingutils.MakeOrderedLink()
// init utility data structures
b.mustBeginBlock = map[string]bool{P: true, C: true}
b.clientHeaders = map[string][]*ibctmtypes.Header{}
Expand Down Expand Up @@ -539,7 +537,7 @@ func (b *Builder) sendEmptyVSCPacketToFinishHandshake() {

timeout := uint64(b.chain(P).CurrentHeader.Time.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

pd := types.NewValidatorSetChangePacketData(
pd := ccv.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{},
vscID,
nil,
Expand Down Expand Up @@ -569,7 +567,7 @@ func (b *Builder) sendEmptyVSCPacketToFinishHandshake() {

b.updateClient(b.chainID(C))

ack, err := simibc.TryRecvPacket(b.endpoint(P), b.endpoint(C), packet)
ack, err := ibctestingutils.TryRecvPacket(b.endpoint(P), b.endpoint(C), packet)

b.link.AddAck(b.chainID(C), ack, packet)

Expand Down Expand Up @@ -601,7 +599,7 @@ func (b *Builder) beginBlock(chainID string) {

func (b *Builder) updateClient(chainID string) {
for _, header := range b.clientHeaders[b.otherID(chainID)] {
err := simibc.UpdateReceiverClient(b.endpointFromID(b.otherID(chainID)), b.endpointFromID(chainID), header)
err := ibctestingutils.UpdateReceiverClient(b.endpointFromID(b.otherID(chainID)), b.endpointFromID(chainID), header)
if err != nil {
b.coordinator.Fatal("updateClient")
}
Expand All @@ -614,7 +612,7 @@ func (b *Builder) deliver(chainID string) {
for _, p := range packets {
receiver := b.endpointFromID(chainID)
sender := receiver.Counterparty
ack, err := simibc.TryRecvPacket(sender, receiver, p.Packet)
ack, err := ibctestingutils.TryRecvPacket(sender, receiver, p.Packet)
if err != nil {
b.coordinator.Fatal("deliver")
}
Expand All @@ -624,7 +622,7 @@ func (b *Builder) deliver(chainID string) {

func (b *Builder) deliverAcks(chainID string) {
for _, ack := range b.link.ConsumeAcks(b.otherID(chainID), 999999) {
err := simibc.TryRecvAck(b.endpointFromID(b.otherID(chainID)), b.endpointFromID(chainID), ack.Packet, ack.Ack)
err := ibctestingutils.TryRecvAck(b.endpointFromID(b.otherID(chainID)), b.endpointFromID(chainID), ack.Packet, ack.Ack)
if err != nil {
b.coordinator.Fatal("deliverAcks")
}
Expand Down
19 changes: 14 additions & 5 deletions tests/e2e/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
appProvider "github.com/cosmos/interchain-security/app/provider"
"github.com/cosmos/interchain-security/tests/e2e"
e2etestutil "github.com/cosmos/interchain-security/testutil/e2e"
"github.com/cosmos/interchain-security/testutil/simapp"
ibctestingutils "github.com/cosmos/interchain-security/testutil/ibc_testing_utils"
"github.com/stretchr/testify/suite"
)

Expand All @@ -30,10 +30,13 @@ func TestCCVTestSuite(t *testing.T) {
e2etestutil.ProviderApp,
e2etestutil.ConsumerApp,
) {
coordinator, provider := ibctestingutils.NewCoordinatorWithProvider(t)
consumers := ibctestingutils.AddConsumersToCoordinator(coordinator, t, 1, ibctestingutils.ConsumerAppIniter)
consumer := consumers[0]
// Here we pass the concrete types that must implement the necessary interfaces
// to be ran with e2e tests.
coord, prov, cons := simapp.NewProviderConsumerCoordinator(t)
return coord, prov, cons, prov.App.(*appProvider.App), cons.App.(*appConsumer.App)
// TODO: Allow e2e tests to accept multiple consumers
return coordinator, provider, consumers[0], provider.App.(*appProvider.App), consumer.App.(*appConsumer.App)
},
)
suite.Run(t, ccvSuite)
Expand All @@ -49,10 +52,16 @@ func TestConsumerDemocracyTestSuite(t *testing.T) {
*ibctesting.TestChain,
e2etestutil.DemocConsumerApp,
) {
coordinator := ibctesting.NewCoordinator(t, 0)
chainID := ibctesting.GetChainID(2)

democracyConsumer := ibctesting.NewTestChain(t, coordinator,
ibctestingutils.DemocracyConsumerAppIniter, chainID)

coordinator.Chains[chainID] = democracyConsumer
// Here we pass the concrete types that must implement the necessary interfaces
// to be ran with e2e tests.
coord, _, cons := simapp.NewProviderConsumerDemocracyCoordinator(t)
return coord, cons, cons.App.(*appConsumerDemocracy.App)
return coordinator, democracyConsumer, democracyConsumer.App.(*appConsumerDemocracy.App)
},
)
suite.Run(t, democSuite)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simibc
package ibc_testing_utils

import channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simibc
package ibc_testing_utils

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simibc
package ibc_testing_utils

import (
"time"
Expand Down
74 changes: 74 additions & 0 deletions testutil/ibc_testing_utils/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ibc_testing_utils

// Contains util functions for using the IBC testing package with CCV.

import (
"encoding/json"
"testing"

"github.com/cosmos/cosmos-sdk/simapp"

ibctesting "github.com/cosmos/ibc-go/v3/testing"

"github.com/tendermint/spm/cosmoscmd"
"github.com/tendermint/tendermint/libs/log"
tmdb "github.com/tendermint/tm-db"

appConsumer "github.com/cosmos/interchain-security/app/consumer"
appConsumerDemocracy "github.com/cosmos/interchain-security/app/consumer-democracy"
appProvider "github.com/cosmos/interchain-security/app/provider"
)

var (
provChainID = ibctesting.GetChainID(1)
)

// ProviderAppIniter implements ibctesting.AppIniter for a provider app
func ProviderAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) {
encoding := cosmoscmd.MakeEncodingConfig(appProvider.ModuleBasics)
testApp := appProvider.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, map[int64]bool{},
simapp.DefaultNodeHome, 5, encoding, simapp.EmptyAppOptions{}).(ibctesting.TestingApp)
return testApp, appProvider.NewDefaultGenesisState(encoding.Marshaler)
}

// ConsumerAppIniter implements ibctesting.AppIniter for a consumer app
func ConsumerAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) {
encoding := cosmoscmd.MakeEncodingConfig(appConsumer.ModuleBasics)
testApp := appConsumer.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, map[int64]bool{},
simapp.DefaultNodeHome, 5, encoding, simapp.EmptyAppOptions{}).(ibctesting.TestingApp)
return testApp, appConsumer.NewDefaultGenesisState(encoding.Marshaler)
}

// DemocracyConsumerAppIniter implements ibctesting.AppIniter for a democracy consumer app
func DemocracyConsumerAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) {
encoding := cosmoscmd.MakeEncodingConfig(appConsumerDemocracy.ModuleBasics)
testApp := appConsumerDemocracy.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, map[int64]bool{},
simapp.DefaultNodeHome, 5, encoding, simapp.EmptyAppOptions{}).(ibctesting.TestingApp)
return testApp, appConsumerDemocracy.NewDefaultGenesisState(encoding.Marshaler)
}

// NewCoordinatorWithProvider initializes an IBC testing Coordinator with a properly setup provider
func NewCoordinatorWithProvider(t *testing.T) (*ibctesting.Coordinator, *ibctesting.TestChain) {
coordinator := ibctesting.NewCoordinator(t, 0)
provider := ibctesting.NewTestChain(t, coordinator, ProviderAppIniter, provChainID)
coordinator.Chains[provChainID] = provider
return coordinator, provider
}

func AddConsumersToCoordinator(coordinator *ibctesting.Coordinator,
t *testing.T, numConsumers int, appIniter ibctesting.AppIniter) (consumers []*ibctesting.TestChain) {

providerChain := coordinator.GetChain(provChainID)

// Instantiate specified number of consumers, add them to the coordinator and returned slice
for i := 0; i < numConsumers; i++ {
chainID := ibctesting.GetChainID(i + 2)
testChain := ibctesting.NewTestChainWithValSet(t, coordinator,
appIniter, chainID, providerChain.Vals, providerChain.Signers)

coordinator.Chains[chainID] = testChain
consumers = append(consumers, testChain)
}

return consumers
}
Comment on lines +58 to +74
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this split of creating the app and adding the consumers separately 👍

13 changes: 0 additions & 13 deletions testutil/sample/sample.go

This file was deleted.

79 changes: 0 additions & 79 deletions testutil/simapp/simapp.go

This file was deleted.