From 15cf8492389e61dadb516bb3505e82f2566e05f9 Mon Sep 17 00:00:00 2001 From: Alexander Peters Date: Fri, 13 Sep 2024 13:28:02 +0200 Subject: [PATCH] refactor(systemtest): Add cli.RunAndWait for common operations (#21689) (cherry picked from commit 81c917280a1f259c3f9cf6e581d2aed7a027f77e) --- tests/systemtests/account_test.go | 12 ++++-------- tests/systemtests/cli.go | 11 +++++++++++ tests/systemtests/staking_test.go | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/systemtests/account_test.go b/tests/systemtests/account_test.go index 42580862e608..5f3ee7e340ad 100644 --- a/tests/systemtests/account_test.go +++ b/tests/systemtests/account_test.go @@ -33,10 +33,8 @@ func TestAccountCreation(t *testing.T) { rsp := cli.CustomQuery("q", "auth", "account", account1Addr) 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") - txResult, found := cli.AwaitTxCommitted(rsp1) - require.True(t, found) - RequireTxSuccess(t, txResult) + rsp1 := cli.RunAndWait("tx", "bank", "send", account1Addr, account2Addr, "5000stake", "--from="+account1Addr, "--fees=1stake") + RequireTxSuccess(t, rsp1) // query account2 assertNotFound := func(t assert.TestingT, err error, msgAndArgs ...interface{}) (ok bool) { @@ -44,10 +42,8 @@ func TestAccountCreation(t *testing.T) { } _ = cli.WithRunErrorMatcher(assertNotFound).CustomQuery("q", "auth", "account", account2Addr) - 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) + rsp3 := cli.RunAndWait("tx", "bank", "send", account2Addr, account1Addr, "1000stake", "--from="+account2Addr, "--fees=1stake") + RequireTxSuccess(t, rsp3) // query account2 to make sure its created rsp4 := cli.CustomQuery("q", "auth", "account", account2Addr) diff --git a/tests/systemtests/cli.go b/tests/systemtests/cli.go index 871d810ea4a3..fcc6bb518c05 100644 --- a/tests/systemtests/cli.go +++ b/tests/systemtests/cli.go @@ -169,7 +169,18 @@ func (c CLIWrapper) Run(args ...string) string { return rsp } +// RunAndWait runs a cli command and waits for the server result when the TX is executed +// It returns the result of the transaction. +func (c CLIWrapper) RunAndWait(args ...string) string { + rsp := c.Run(args...) + RequireTxSuccess(c.t, rsp) + txResult, found := c.AwaitTxCommitted(rsp) + require.True(c.t, found) + return txResult +} + // AwaitTxCommitted wait for tx committed on chain +// returns the server execution result and true when found within 3 blocks. func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) { c.t.Helper() RequireTxSuccess(c.t, submitResp) diff --git a/tests/systemtests/staking_test.go b/tests/systemtests/staking_test.go index f197bff1a0ac..5f8f911191bc 100644 --- a/tests/systemtests/staking_test.go +++ b/tests/systemtests/staking_test.go @@ -31,7 +31,7 @@ func TestStakeUnstake(t *testing.T) { valAddr := gjson.Get(rsp, "validators.#.operator_address").Array()[0].String() // stake tokens - rsp = cli.Run("tx", "staking", "delegate", valAddr, "10000stake", "--from="+account1Addr, "--fees=1stake") + rsp = cli.RunAndWait("tx", "staking", "delegate", valAddr, "10000stake", "--from="+account1Addr, "--fees=1stake") RequireTxSuccess(t, rsp) t.Log(cli.QueryBalance(account1Addr, "stake")) @@ -42,7 +42,7 @@ func TestStakeUnstake(t *testing.T) { assert.Equal(t, "stake", gjson.Get(rsp, "delegation_response.balance.denom").String(), rsp) // unstake tokens - rsp = cli.Run("tx", "staking", "unbond", valAddr, "5000stake", "--from="+account1Addr, "--fees=1stake") + rsp = cli.RunAndWait("tx", "staking", "unbond", valAddr, "5000stake", "--from="+account1Addr, "--fees=1stake") RequireTxSuccess(t, rsp) rsp = cli.CustomQuery("q", "staking", "delegation", account1Addr, valAddr)