Files
wasmd/tests/system/main_test.go
Alexander Peters a4548ba175 Start system tests (#1410)
* Start system tests

* Go mod tidy

* Add system tests (#1411)

* Add tests

* Add test-system to CI

* Fix path

* Remove store artifact steps

* Add small fixes

* Add stake/unstake tests

* Add unsafe-reset-all extention + system test

* Replace ustake with stake

* Add more tests + fix bug in cli

* Fix comments and add multi contract system test

* Updates and fixes to system tests (#1449)

* Updates

* Minor cleanup

* Make tests pass

---------

Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>

* Fix Makefile to return exit code for system tests (#1450)

* Abort on error results

---------

Co-authored-by: pinosu <95283998+pinosu@users.noreply.github.com>
2023-06-15 16:55:33 +02:00

139 lines
3.3 KiB
Go

//go:build system_test
package system
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"github.com/cometbft/cometbft/libs/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/stretchr/testify/require"
"github.com/CosmWasm/wasmd/app"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
)
var (
sut *SystemUnderTest
verbose bool
)
func init() {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(app.Bech32PrefixAccAddr, app.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(app.Bech32PrefixValAddr, app.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(app.Bech32PrefixConsAddr, app.Bech32PrefixConsPub)
}
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")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.Parse()
// fail fast on most common setup issue
requireEnoughFileHandlers(*nodesCount + 1) // +1 as tests may start another node
dir, err := os.Getwd()
if err != nil {
panic(err)
}
workDir = dir
if verbose {
println("Work dir: ", workDir)
}
defaultWaitTime = *waitTime
sut = NewSystemUnderTest(verbose, *nodesCount, *blockTime)
if *rebuild {
sut.BuildNewBinary()
}
// setup chain and keyring
sut.SetupChain()
// run tests
exitCode := m.Run()
// postprocess
sut.StopChain()
if verbose || exitCode != 0 {
sut.PrintBuffer()
printResultFlag(exitCode == 0)
}
os.Exit(exitCode)
}
// requireEnoughFileHandlers uses `ulimit`
func requireEnoughFileHandlers(nodesCount int) error {
ulimit, err := exec.LookPath("ulimit")
if err != nil || ulimit == "" { // skip when not available
return nil
}
cmd := exec.Command(ulimit, "-n")
cmd.Dir = workDir
out, err := cmd.CombinedOutput()
if err != nil {
panic(fmt.Sprintf("unexpected error :%#+v, output: %s", err, string(out)))
}
fileDescrCount, err := strconv.Atoi(strings.Trim(string(out), " \t\n"))
if err != nil {
panic(fmt.Sprintf("unexpected error :%#+v, output: %s", err, string(out)))
}
expFH := nodesCount * 260 // random number that worked on my box
if fileDescrCount < expFH {
panic(fmt.Sprintf("Fail fast. Insufficient setup. Run 'ulimit -n %d'", expFH))
}
return err
}
const (
successFlag = `
___ _ _ ___ ___ ___ ___ ___
/ __| | | |/ __/ __/ _ \/ __/ __|
\__ \ |_| | (_| (_| __/\__ \__ \
|___/\__,_|\___\___\___||___/___/`
failureFlag = `
__ _ _ _
/ _| (_) | | |
| |_ __ _ _| | ___ __| |
| _/ _| | | |/ _ \/ _| |
| || (_| | | | __/ (_| |
|_| \__,_|_|_|\___|\__,_|`
)
func printResultFlag(ok bool) {
if ok {
fmt.Println(successFlag)
} else {
fmt.Println(failureFlag)
}
}
func randomBech32Addr() string {
src := rand.Bytes(address.Len)
return sdk.AccAddress(src).String()
}
// ContractBech32Address build a wasmd bech32 contract address
func ContractBech32Address(codeID, instanceID uint64) string {
return wasmkeeper.BuildContractAddressClassic(codeID, instanceID).String()
}
func toJson(t *testing.T, o interface{}) string {
bz, err := json.Marshal(o)
require.NoError(t, err)
return string(bz)
}