Skip to content

Commit

Permalink
Merge pull request #1503 from CosmWasm/system_test_reusable
Browse files Browse the repository at this point in the history
Make system tests extendable for other app binaries
  • Loading branch information
alpe authored Jul 14, 2023
2 parents 788cdbb + d7df231 commit 661fd4b
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 94 deletions.
91 changes: 63 additions & 28 deletions tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,52 @@ type WasmdCli struct {
assertErrorFn RunErrorAssert
awaitNextBlock awaitNextBlock
expTXCommitted bool
execBinary string
}

// NewWasmdCLI constructor
func NewWasmdCLI(t *testing.T, sut *SystemUnderTest, verbose bool) *WasmdCli {
return NewWasmdCLIx(t, sut.rpcAddr, sut.chainID, sut.AwaitNextBlock, filepath.Join(workDir, sut.outputDir), "1"+sdk.DefaultBondDenom, verbose)
return NewWasmdCLIx(
t,
sut.execBinary,
sut.rpcAddr,
sut.chainID,
sut.AwaitNextBlock,
filepath.Join(workDir, sut.outputDir),
"1"+sdk.DefaultBondDenom,
verbose,
assert.NoError,
true,
)
}

// NewWasmdCLIx extended constructor
func NewWasmdCLIx(
t *testing.T,
execBinary string,
nodeAddress string,
chainID string,
awaiter awaitNextBlock,
homeDir string,
fees string,
debug bool,
assertErrorFn RunErrorAssert,
expTXCommitted bool,
) *WasmdCli {
if strings.TrimSpace(execBinary) == "" {
panic("executable binary name must not be empty")
}
return &WasmdCli{
t: t,
execBinary: execBinary,
nodeAddress: nodeAddress,
chainID: chainID,
homeDir: homeDir,
Debug: debug,
assertErrorFn: assert.NoError,
awaitNextBlock: awaiter,
fees: fees,
expTXCommitted: true,
assertErrorFn: assertErrorFn,
expTXCommitted: expTXCommitted,
}
}

Expand All @@ -79,31 +98,48 @@ func (c WasmdCli) WithRunErrorsIgnored() WasmdCli {

// WithRunErrorMatcher assert function to ensure run command error value
func (c WasmdCli) WithRunErrorMatcher(f RunErrorAssert) WasmdCli {
return WasmdCli{
t: c.t,
nodeAddress: c.nodeAddress,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
assertErrorFn: f,
awaitNextBlock: c.awaitNextBlock,
fees: c.fees,
expTXCommitted: c.expTXCommitted,
}
return *NewWasmdCLIx(
c.t,
c.execBinary,
c.nodeAddress,
c.chainID,
c.awaitNextBlock,
c.homeDir,
c.fees,
c.Debug,
f,
c.expTXCommitted,
)
}

func (c WasmdCli) WithNodeAddress(addr string) WasmdCli {
return WasmdCli{
t: c.t,
nodeAddress: addr,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
assertErrorFn: c.assertErrorFn,
awaitNextBlock: c.awaitNextBlock,
fees: c.fees,
expTXCommitted: c.expTXCommitted,
}
func (c WasmdCli) WithNodeAddress(nodeAddr string) WasmdCli {
return *NewWasmdCLIx(
c.t,
c.execBinary,
nodeAddr,
c.chainID,
c.awaitNextBlock,
c.homeDir,
c.fees,
c.Debug,
c.assertErrorFn,
c.expTXCommitted,
)
}

func (c WasmdCli) WithAssertTXUncommitted() WasmdCli {
return *NewWasmdCLIx(
c.t,
c.execBinary,
c.nodeAddress,
c.chainID,
c.awaitNextBlock,
c.homeDir,
c.fees,
c.Debug,
c.assertErrorFn,
false,
)
}

// CustomCommand main entry for executing wasmd cli commands.
Expand Down Expand Up @@ -160,9 +196,8 @@ func (c WasmdCli) CustomQuery(args ...string) string {

// execute shell command
func (c WasmdCli) run(args []string) (output string, ok bool) {
// todo assert error???
if c.Debug {
c.t.Logf("+++ running `wasmd %s`", strings.Join(args, " "))
c.t.Logf("+++ running `%s %s`", c.execBinary, strings.Join(args, " "))
}
gotOut, gotErr := func() (out []byte, err error) {
defer func() {
Expand Down
17 changes: 3 additions & 14 deletions tests/system/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,7 @@ func TestVestingAccounts(t *testing.T) {
assert.Equal(t, myStartTimestamp, accounts[0].Get("start_time").Int())

// check accounts have some balances
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100000000))), getGenesisBalance([]byte(raw), vest1Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100000001))), getGenesisBalance([]byte(raw), vest2Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(200000002))), getGenesisBalance([]byte(raw), vest3Addr))
}

func getGenesisBalance(raw []byte, addr string) sdk.Coins {
var r []sdk.Coin
balances := gjson.GetBytes(raw, fmt.Sprintf(`app_state.bank.balances.#[address==%q]#.coins`, addr)).Array()
for _, coins := range balances {
for _, coin := range coins.Array() {
r = append(r, sdk.NewCoin(coin.Get("denom").String(), sdk.NewInt(coin.Get("amount").Int())))
}
}
return r
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100000000))), GetGenesisBalance([]byte(raw), vest1Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100000001))), GetGenesisBalance([]byte(raw), vest2Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(200000002))), GetGenesisBalance([]byte(raw), vest3Addr))
}
6 changes: 4 additions & 2 deletions tests/system/fraud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func TestRecursiveMsgsExternalTrigger(t *testing.T) {
fees = calcMinFeeRequired(t, gas)
}
for _, n := range sut.AllNodes(t) {
clix := cli.WithRunErrorMatcher(spec.expErrMatcher).WithNodeAddress(n.RPCAddr())
clix.expTXCommitted = false
clix := cli.
WithRunErrorMatcher(spec.expErrMatcher).
WithNodeAddress(n.RPCAddr()).
WithAssertTXUncommitted()
clix.WasmExecute(contractAddr, execMsg, defaultSrcAddr, "--gas="+gas, "--broadcast-mode=sync", "--fees="+fees)
}
sut.AwaitNextBlock(t)
Expand Down
33 changes: 33 additions & 0 deletions tests/system/genesis_io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package system

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// SetConsensusMaxGas max gas that can be consumed in a block
func SetConsensusMaxGas(t *testing.T, max int) GenesisMutator {
return func(genesis []byte) []byte {
t.Helper()
state, err := sjson.SetRawBytes(genesis, "consensus_params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, max)))
require.NoError(t, err)
return state
}
}

// GetGenesisBalance return the balance amount for an address from the given genesis json
func GetGenesisBalance(rawGenesis []byte, addr string) sdk.Coins {
var r []sdk.Coin
balances := gjson.GetBytes(rawGenesis, fmt.Sprintf(`app_state.bank.balances.#[address==%q]#.coins`, addr)).Array()
for _, coins := range balances {
for _, coin := range coins.Array() {
r = append(r, sdk.NewCoin(coin.Get("denom").String(), sdk.NewInt(coin.Get("amount").Int())))
}
}
return r
}
19 changes: 0 additions & 19 deletions tests/system/genesis_mutators.go

This file was deleted.

27 changes: 15 additions & 12 deletions tests/system/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,13 @@ var (
verbose bool
)

func init() {
InitSDKConfig("wasm")
}

func InitSDKConfig(bech32Prefix string) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(bech32Prefix, bech32Prefix+sdk.PrefixPublic)
config.SetBech32PrefixForValidator(bech32Prefix+sdk.PrefixValidator+sdk.PrefixOperator, bech32Prefix+sdk.PrefixValidator+sdk.PrefixOperator+sdk.PrefixPublic)
config.SetBech32PrefixForConsensusNode(bech32Prefix+sdk.PrefixValidator+sdk.PrefixConsensus, bech32Prefix+sdk.PrefixValidator+sdk.PrefixConsensus+sdk.PrefixPublic)
}

func TestMain(m *testing.M) {
rebuild := flag.Bool("rebuild", false, "rebuild artifacts")
waitTime := flag.Duration("wait-time", defaultWaitTime, "time to wait for chain events")
nodesCount := flag.Int("nodes-count", 4, "number of nodes in the cluster")
blockTime := flag.Duration("block-time", 1000*time.Millisecond, "block creation time")
execBinary := flag.String("binary", "wasmd", "executable binary for server/ client side")
bech32Prefix := flag.String("bech32", "wasm", "bech32 prefix to be used with addresses")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.Parse()

Expand All @@ -53,8 +44,13 @@ func TestMain(m *testing.M) {
if verbose {
println("Work dir: ", workDir)
}
initSDKConfig(*bech32Prefix)

defaultWaitTime = *waitTime
sut = NewSystemUnderTest(verbose, *nodesCount, *blockTime)
if *execBinary == "" {
panic("executable binary name must not be empty")
}
sut = NewSystemUnderTest(*execBinary, verbose, *nodesCount, *blockTime)
if *rebuild {
sut.BuildNewBinary()
}
Expand Down Expand Up @@ -98,6 +94,13 @@ func requireEnoughFileHandlers(nodesCount int) {
return
}

func initSDKConfig(bech32Prefix string) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(bech32Prefix, bech32Prefix+sdk.PrefixPublic)
config.SetBech32PrefixForValidator(bech32Prefix+sdk.PrefixValidator+sdk.PrefixOperator, bech32Prefix+sdk.PrefixValidator+sdk.PrefixOperator+sdk.PrefixPublic)
config.SetBech32PrefixForConsensusNode(bech32Prefix+sdk.PrefixValidator+sdk.PrefixConsensus, bech32Prefix+sdk.PrefixValidator+sdk.PrefixConsensus+sdk.PrefixPublic)
}

const (
successFlag = `
___ _ _ ___ ___ ___ ___ ___
Expand Down
Loading

0 comments on commit 661fd4b

Please sign in to comment.