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

refactor(systemtests): Extract system test framework #22578

Merged
merged 11 commits into from
Nov 26, 2024
Merged
93 changes: 51 additions & 42 deletions tests/systemtests/cli.go → systemtests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,30 @@ func (c CLIWrapper) WithRunErrorsIgnored() CLIWrapper {

// WithRunErrorMatcher assert function to ensure run command error value
func (c CLIWrapper) WithRunErrorMatcher(f RunErrorAssert) CLIWrapper {
return *NewCLIWrapperX(
c.t,
c.execBinary,
c.nodeAddress,
c.chainID,
c.awaitNextBlock,
c.nodesCount,
c.homeDir,
c.fees,
c.Debug,
f,
c.expTXCommitted,
)
return c.clone(func(r *CLIWrapper) {
r.assertErrorFn = f
})
}

func (c CLIWrapper) WithNodeAddress(nodeAddr string) CLIWrapper {
return *NewCLIWrapperX(
c.t,
c.execBinary,
nodeAddr,
c.chainID,
c.awaitNextBlock,
c.nodesCount,
c.homeDir,
c.fees,
c.Debug,
c.assertErrorFn,
c.expTXCommitted,
)
return c.clone(func(r *CLIWrapper) {
r.nodeAddress = nodeAddr
})
}

func (c CLIWrapper) WithAssertTXUncommitted() CLIWrapper {
return *NewCLIWrapperX(
return c.clone(func(r *CLIWrapper) {
r.expTXCommitted = false
})
}
func (c CLIWrapper) WithChainID(newChainID string) CLIWrapper {
return c.clone(func(r *CLIWrapper) {
r.chainID = newChainID
})
}

func (c CLIWrapper) clone(mutator ...func(r *CLIWrapper)) CLIWrapper {
r := NewCLIWrapperX(
c.t,
c.execBinary,
c.nodeAddress,
Expand All @@ -143,8 +134,12 @@ func (c CLIWrapper) WithAssertTXUncommitted() CLIWrapper {
c.fees,
c.Debug,
c.assertErrorFn,
false,
c.expTXCommitted,
)
for _, m := range mutator {
m(r)
}
return *r
}

// Run main entry for executing cli commands.
Expand All @@ -156,7 +151,7 @@ func (c CLIWrapper) Run(args ...string) string {
}) {
args = append(args, "--fees="+c.fees) // add default fee
}
args = c.withTXFlags(args...)
args = c.WithTXFlags(args...)
execOutput, ok := c.run(args)
if !ok {
return execOutput
Expand Down Expand Up @@ -207,14 +202,14 @@ func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration

// Keys wasmd keys CLI command
func (c CLIWrapper) Keys(args ...string) string {
args = c.withKeyringFlags(args...)
args = c.WithKeyringFlags(args...)
out, _ := c.run(args)
return out
}

// CustomQuery main entrypoint for wasmd CLI queries
func (c CLIWrapper) CustomQuery(args ...string) string {
args = c.withQueryFlags(args...)
args = c.WithQueryFlags(args...)
out, _ := c.run(args)
return out
}
Expand Down Expand Up @@ -254,23 +249,32 @@ func (c CLIWrapper) runWithInput(args []string, input io.Reader) (output string,
return strings.TrimSpace(string(gotOut)), ok
}

func (c CLIWrapper) withQueryFlags(args ...string) []string {
// WithQueryFlags append the test default query flags to the given args
func (c CLIWrapper) WithQueryFlags(args ...string) []string {
args = append(args, "--output", "json")
return c.withChainFlags(args...)
return c.WithTargetNodeFlags(args...)
}

func (c CLIWrapper) withTXFlags(args ...string) []string {
// WithTXFlags append the test default TX flags to the given args.
// This includes
// - broadcast-mode: sync
// - output: json
// - chain-id
// - keyring flags
// - target-node
func (c CLIWrapper) WithTXFlags(args ...string) []string {
args = append(args,
"--broadcast-mode", "sync",
"--output", "json",
"--yes",
"--chain-id", c.chainID,
)
args = c.withKeyringFlags(args...)
return c.withChainFlags(args...)
args = c.WithKeyringFlags(args...)
return c.WithTargetNodeFlags(args...)
}

func (c CLIWrapper) withKeyringFlags(args ...string) []string {
// WithKeyringFlags append the test default keyring flags to the given args
func (c CLIWrapper) WithKeyringFlags(args ...string) []string {
r := append(args,
"--home", c.homeDir,
"--keyring-backend", "test",
Expand All @@ -283,7 +287,8 @@ func (c CLIWrapper) withKeyringFlags(args ...string) []string {
return append(r, "--output", "json")
}

func (c CLIWrapper) withChainFlags(args ...string) []string {
// WithTargetNodeFlags append the test default target node address flags to the given args
func (c CLIWrapper) WithTargetNodeFlags(args ...string) []string {
return append(args,
"--node", c.nodeAddress,
)
Expand All @@ -297,7 +302,7 @@ func (c CLIWrapper) WasmExecute(contractAddr, msg, from string, args ...string)

// AddKey add key to default keyring. Returns address
func (c CLIWrapper) AddKey(name string) string {
cmd := c.withKeyringFlags("keys", "add", name, "--no-backup")
cmd := c.WithKeyringFlags("keys", "add", name, "--no-backup")
out, _ := c.run(cmd)
addr := gjson.Get(out, "address").String()
require.NotEmpty(c.t, addr, "got %q", out)
Expand All @@ -306,7 +311,7 @@ func (c CLIWrapper) AddKey(name string) string {

// AddKeyFromSeed recovers the key from given seed and add it to default keyring. Returns address
func (c CLIWrapper) AddKeyFromSeed(name, mnemoic string) string {
cmd := c.withKeyringFlags("keys", "add", name, "--recover")
cmd := c.WithKeyringFlags("keys", "add", name, "--recover")
out, _ := c.runWithInput(cmd, strings.NewReader(mnemoic))
addr := gjson.Get(out, "address").String()
require.NotEmpty(c.t, addr, "got %q", out)
Expand All @@ -315,7 +320,7 @@ func (c CLIWrapper) AddKeyFromSeed(name, mnemoic string) string {

// GetKeyAddr returns Acc address
func (c CLIWrapper) GetKeyAddr(name string) string {
cmd := c.withKeyringFlags("keys", "show", name, "-a")
cmd := c.WithKeyringFlags("keys", "show", name, "-a")
out, _ := c.run(cmd)
addr := strings.Trim(out, "\n")
require.NotEmpty(c.t, addr, "got %q", out)
Expand All @@ -324,7 +329,7 @@ func (c CLIWrapper) GetKeyAddr(name string) string {

// GetKeyAddrPrefix returns key address with Beach32 prefix encoding for a key (acc|val|cons)
func (c CLIWrapper) GetKeyAddrPrefix(name, prefix string) string {
cmd := c.withKeyringFlags("keys", "show", name, "-a", "--bech="+prefix)
cmd := c.WithKeyringFlags("keys", "show", name, "-a", "--bech="+prefix)
out, _ := c.run(cmd)
addr := strings.Trim(out, "\n")
require.NotEmpty(c.t, addr, "got %q", out)
Expand Down Expand Up @@ -413,6 +418,10 @@ func (c CLIWrapper) SubmitAndVoteGovProposal(proposalJson string, args ...string
return ourProposalID
}

func (c CLIWrapper) ChainID() string {
return c.chainID
}

// Version returns the current version of the client binary
func (c CLIWrapper) Version() string {
v, ok := c.run([]string{"version"})
Expand Down
File renamed without changes.
Loading
Loading