Skip to content

Commit

Permalink
Revisit CLI and system tests (#1627)
Browse files Browse the repository at this point in the history
* Restructure CLI; fix system test

* Review feedback
  • Loading branch information
alpe authored Sep 15, 2023
1 parent e15f052 commit c5a9537
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 120 deletions.
17 changes: 16 additions & 1 deletion tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
"fmt"
"io"
"os/exec"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -196,6 +197,10 @@ func (c WasmdCli) CustomQuery(args ...string) string {

// execute shell command
func (c WasmdCli) run(args []string) (output string, ok bool) {
return c.runWithInput(args, nil)
}

func (c WasmdCli) runWithInput(args []string, input io.Reader) (output string, ok bool) {
if c.Debug {
c.t.Logf("+++ running `%s %s`", c.execBinary, strings.Join(args, " "))
}
Expand All @@ -207,6 +212,7 @@ func (c WasmdCli) run(args []string) (output string, ok bool) {
}()
cmd := exec.Command(locateExecutable("wasmd"), args...) //nolint:gosec
cmd.Dir = workDir
cmd.Stdin = input
return cmd.CombinedOutput()
}()
ok = c.assertErrorFn(c.t, gotErr, string(gotOut))
Expand Down Expand Up @@ -256,13 +262,22 @@ func (c WasmdCli) WasmExecute(contractAddr, msg, from string, args ...string) st

// AddKey add key to default keyring. Returns address
func (c WasmdCli) 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)
return addr
}

// AddKeyFromSeed recovers the key from given seed and add it to default keyring. Returns address
func (c WasmdCli) AddKeyFromSeed(name, mnemoic string) string {
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)
return addr
}

// GetKeyAddr returns address
func (c WasmdCli) GetKeyAddr(name string) string {
cmd := c.withKeyringFlags("keys", "show", name, "-a")
Expand Down
20 changes: 10 additions & 10 deletions tests/system/permissioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
)

func TestGrantStoreCodePermissionedChain(t *testing.T) {
sut.ResetChain(t)
cli := NewWasmdCLI(t, sut, verbose)
// set params to restrict chain
const chainAuthorityAddress = "wasm1pvuujjdk0xt043ga0j9nrfh5u8pzj4rpplyqkm"
sut.ModifyGenesisJSON(t, SetCodeUploadPermission(t, "AnyOfAddresses", chainAuthorityAddress))

chainAuthorizedAccount := cli.AddKey("chain_authorized_account")
recoveredAddress := cli.AddKeyFromSeed("chain_authority", "aisle ship absurd wedding arch admit fringe foam cluster tide trim aisle salad shiver tackle palm glance wrist valley hamster couch crystal frozen chronic")
require.Equal(t, chainAuthorityAddress, recoveredAddress)
devAccount := cli.AddKey("dev_account")

//set params
sut.ModifyGenesisJSON(t, SetCodeUploadPermission(t, "AnyOfAddresses", chainAuthorizedAccount))

sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", chainAuthorizedAccount, "100000000stake"},
[]string{"genesis", "add-genesis-account", chainAuthorityAddress, "100000000stake"},
)
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", devAccount, "100000000stake"},
Expand All @@ -37,18 +37,18 @@ func TestGrantStoreCodePermissionedChain(t *testing.T) {
require.Equal(t, 1, len(addrRes))

require.Equal(t, permission, "AnyOfAddresses")
require.Equal(t, chainAuthorizedAccount, addrRes[0].Str)
require.Equal(t, chainAuthorityAddress, addrRes[0].Str)

// chain_authorized_account grant upload permission to dev_account
rsp = cli.CustomCommand("tx", "wasm", "grant", devAccount, "store-code", "*:*", "--from="+chainAuthorizedAccount)
// chain_authority grant upload permission to dev_account
rsp = cli.CustomCommand("tx", "wasm", "grant", "store-code", devAccount, "*:*", "--from="+chainAuthorityAddress)
RequireTxSuccess(t, rsp)

// dev_account store code fails as the address is not in the code-upload accept-list
rsp = cli.CustomCommand("tx", "wasm", "store", "./testdata/hackatom.wasm.gzip", "--from="+devAccount, "--gas=1500000", "--fees=2stake")
RequireTxFailure(t, rsp)

// create tx should work for addresses in the accept-list
args := cli.withTXFlags("tx", "wasm", "store", "./testdata/hackatom.wasm.gzip", "--from="+chainAuthorizedAccount, "--generate-only")
args := cli.withTXFlags("tx", "wasm", "store", "./testdata/hackatom.wasm.gzip", "--from="+chainAuthorityAddress, "--generate-only")
tx, ok := cli.run(args)
require.True(t, ok)

Expand Down
1 change: 1 addition & 0 deletions tests/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ type GenesisMutator func([]byte) []byte
// return state
// }
func (s *SystemUnderTest) ModifyGenesisJSON(t *testing.T, mutators ...GenesisMutator) {
s.ResetChain(t)
s.modifyGenesisJSON(t, mutators...)
}

Expand Down
Loading

0 comments on commit c5a9537

Please sign in to comment.