fix(systemtests): Fix account creation test to wait for TX committed (#21149)

This commit is contained in:
Alexander Peters 2024-08-02 08:40:46 +02:00 committed by GitHub
parent fb9efdf875
commit 23fac2f1b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
@ -21,6 +22,7 @@ func TestAccountCreation(t *testing.T) {
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
account2Addr := cli.AddKey("account2")
require.NotEqual(t, account1Addr, account2Addr)
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "10000000stake"},
)
@ -32,15 +34,20 @@ func TestAccountCreation(t *testing.T) {
assert.Equal(t, account1Addr, gjson.Get(rsp, "account.value.address").String(), rsp)
rsp1 := cli.Run("tx", "bank", "send", account1Addr, account2Addr, "5000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp1)
txResult, found := cli.AwaitTxCommitted(rsp1)
require.True(t, found)
RequireTxSuccess(t, txResult)
// query account2
assertNotFound := func(t assert.TestingT, err error, msgAndArgs ...interface{}) (ok bool) {
return strings.Contains(err.Error(), "not found: key not found")
}
_ = cli.WithRunErrorMatcher(assertNotFound).CustomQuery("q", "auth", "account", account2Addr)
rsp2 := cli.WithRunErrorsIgnored().CustomQuery("q", "auth", "account", account2Addr)
assert.True(t, strings.Contains(rsp2, "not found: key not found"))
rsp3 := cli.Run("tx", "bank", "send", account2Addr, account1Addr, "1000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp3)
rsp3 := cli.Run("tx", "bank", "send", account2Addr, account1Addr, "1000stake", "--from="+account2Addr, "--fees=1stake")
txResult, found = cli.AwaitTxCommitted(rsp3)
require.True(t, found)
RequireTxSuccess(t, txResult)
// query account2 to make sure its created
rsp4 := cli.CustomQuery("q", "auth", "account", account2Addr)

View File

@ -1,6 +1,8 @@
package systemtests
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
@ -150,6 +152,7 @@ func (c CLIWrapper) WithAssertTXUncommitted() CLIWrapper {
// Run main entry for executing cli commands.
// When configured, method blocks until tx is committed.
func (c CLIWrapper) Run(args ...string) string {
c.t.Helper()
if c.fees != "" && !slices.ContainsFunc(args, func(s string) bool {
return strings.HasPrefix(s, "--fees")
}) {
@ -160,14 +163,15 @@ func (c CLIWrapper) Run(args ...string) string {
if !ok {
return execOutput
}
rsp, committed := c.awaitTxCommitted(execOutput, DefaultWaitTime)
rsp, committed := c.AwaitTxCommitted(execOutput, DefaultWaitTime)
c.t.Logf("tx committed: %v", committed)
require.Equal(c.t, c.expTXCommitted, committed, "expected tx committed: %v", c.expTXCommitted)
return rsp
}
// wait for tx committed on chain
func (c CLIWrapper) awaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
// AwaitTxCommitted wait for tx committed on chain
func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
c.t.Helper()
RequireTxSuccess(c.t, submitResp)
txHash := gjson.Get(submitResp, "txhash")
require.True(c.t, txHash.Exists())
@ -201,10 +205,12 @@ func (c CLIWrapper) CustomQuery(args ...string) string {
// execute shell command
func (c CLIWrapper) run(args []string) (output string, ok bool) {
c.t.Helper()
return c.runWithInput(args, nil)
}
func (c CLIWrapper) runWithInput(args []string, input io.Reader) (output string, ok bool) {
c.t.Helper()
if c.Debug {
c.t.Logf("+++ running `%s %s`", c.execBinary, strings.Join(args, " "))
}
@ -219,10 +225,26 @@ func (c CLIWrapper) runWithInput(args []string, input io.Reader) (output string,
cmd.Stdin = input
return cmd.CombinedOutput()
}()
gotOut = filterProtoNoise(gotOut)
ok = c.assertErrorFn(c.t, gotErr, string(gotOut))
return strings.TrimSpace(string(gotOut)), ok
}
func filterProtoNoise(in []byte) []byte {
// temporary hack to get rid of all the noise on the stderr
var out bytes.Buffer
scanner := bufio.NewScanner(bytes.NewReader(in))
for scanner.Scan() {
if !strings.Contains(scanner.Text(), " proto: duplicate proto type registered") {
_, _ = out.Write(scanner.Bytes())
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
return out.Bytes()
}
func (c CLIWrapper) withQueryFlags(args ...string) []string {
args = append(args, "--output", "json")
return c.withChainFlags(args...)
@ -392,10 +414,10 @@ func RequireTxFailure(t *testing.T, got string, containsMsgs ...string) {
func parseResultCode(t *testing.T, got string) (int64, string) {
t.Helper()
code := gjson.Get(got, "code")
require.True(t, code.Exists(), "got response: %s", got)
require.True(t, code.Exists(), "got response: %q", got)
details := got
if log := gjson.Get(got, "raw_log"); log.Exists() {
if log := gjson.Get(got, "raw_log"); log.Exists() && len(log.String()) != 0 {
details = log.String()
}
return code.Int(), details