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

feat: add support for dynamic user addresses in txtar tests #1581

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gno.land/pkg/integration/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
// - Creates a new user in the default keybase directory.
// - Must be run before `gnoland start`.
//
// 4. `withuser`:
// - usage is `withuser <username> gnokey`.
// - username should be a username registered via the `adduser“ command.
// - using the placeholder USERADDRESS in the command will be replaced with the user's address.
// - example `withuser test8 gnokey maketx call ... -func Render -args 'USERADDRESS' ... `
//
// Logging:
//
// Gnoland logs aren't forwarded to stdout to avoid overwhelming the tests with too much
Expand Down
19 changes: 6 additions & 13 deletions gno.land/pkg/integration/testdata/adduser.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ adduser test8
## start a new node
gnoland start

## add bar.gno package located in $WORK directory as gno.land/r/foobar/bar
gnokey maketx addpkg -pkgdir $WORK/bar -pkgpath gno.land/r/foobar/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test8
## add bar.gno package located in $WORK directory as gno.land/r/bar
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test8

## execute Render
gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test8 $WORK/script/script.gno
withuser test8 gnokey maketx call -pkgpath gno.land/r/bar -func Render -args 'USERADDRESS' -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test8

## compare render
stdout 'main: --- hello from foo ---'
stdout 'hello from'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '
Expand All @@ -19,16 +19,9 @@ stdout 'GAS USED: '
! adduser test5
stderr '"adduser" error: adduser must be used before starting node'

-- bar/bar.gno --
-- bar.gno --
package bar

func Render(path string) string {
return "hello from foo"
}

-- script/script.gno --
package main
import "gno.land/r/foobar/bar"
func main() {
println("main: ---", bar.Render(""), "---")
return "hello from " + path
}
110 changes: 69 additions & 41 deletions gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
"github.com/rogpeppe/go-internal/testscript"
)

const numTestAccounts int = 4
const (
numTestAccounts int = 4
gnokeyCmd string = "gnokey"
)

type tSeqShim struct{ *testing.T }

Expand Down Expand Up @@ -80,7 +83,49 @@

// Track new user balances added via the `adduser` command. These are added to the genesis
// state when the node is started.
var newUserBalances []gnoland.Balance
var (
newUserBalances []gnoland.Balance
addedUserAddresses = make(map[string]string)
)

gnokeyFunc := func(ts *testscript.TestScript, neg bool, args []string) {
logger := ts.Value("_logger").(log.Logger) // grab logger
sid := ts.Getenv("SID") // grab session id

// Setup IO command
io := commands.NewTestIO()
io.SetOut(commands.WriteNopCloser(ts.Stdout()))
io.SetErr(commands.WriteNopCloser(ts.Stderr()))
cmd := keyscli.NewRootCmd(io, client.DefaultBaseOptions)

io.SetIn(strings.NewReader("\n")) // Inject empty password to stdin.
defaultArgs := []string{
"-home", gnoHomeDir,
"-insecure-password-stdin=true", // There no use to not have this param by default.
}

if n, ok := nodes[sid]; ok {
if raddr := n.Config().RPC.ListenAddress; raddr != "" {
defaultArgs = append(defaultArgs, "-remote", raddr)
}

n.nGnoKeyExec++
headerlog := fmt.Sprintf("%.02d!EXEC_GNOKEY", n.nGnoKeyExec)

// Log the command inside gnoland logger, so we can better scope errors.
logger.Info(headerlog, strings.Join(args, " "))
defer logger.Info(headerlog, "END")
}

// Inject default argument, if duplicate
// arguments, it should be override by the ones
// user provided.
args = append(defaultArgs, args...)

err := cmd.ParseAndRun(context.Background(), args)

tsValidateError(ts, gnokeyCmd, neg, err)
}

updateScripts, _ := strconv.ParseBool(os.Getenv("UPDATE_SCRIPTS"))
persistWorkDir, _ := strconv.ParseBool(os.Getenv("TESTWORK"))
Expand Down Expand Up @@ -207,44 +252,7 @@

tsValidateError(ts, "gnoland "+cmd, neg, err)
},
"gnokey": func(ts *testscript.TestScript, neg bool, args []string) {
logger := ts.Value("_logger").(log.Logger) // grab logger
sid := ts.Getenv("SID") // grab session id

// Setup IO command
io := commands.NewTestIO()
io.SetOut(commands.WriteNopCloser(ts.Stdout()))
io.SetErr(commands.WriteNopCloser(ts.Stderr()))
cmd := keyscli.NewRootCmd(io, client.DefaultBaseOptions)

io.SetIn(strings.NewReader("\n")) // Inject empty password to stdin.
defaultArgs := []string{
"-home", gnoHomeDir,
"-insecure-password-stdin=true", // There no use to not have this param by default.
}

if n, ok := nodes[sid]; ok {
if raddr := n.Config().RPC.ListenAddress; raddr != "" {
defaultArgs = append(defaultArgs, "-remote", raddr)
}

n.nGnoKeyExec++
headerlog := fmt.Sprintf("%.02d!EXEC_GNOKEY", n.nGnoKeyExec)

// Log the command inside gnoland logger, so we can better scope errors.
logger.Info(headerlog, strings.Join(args, " "))
defer logger.Info(headerlog, "END")
}

// Inject default argument, if duplicate
// arguments, it should be override by the ones
// user provided.
args = append(defaultArgs, args...)

err := cmd.ParseAndRun(context.Background(), args)

tsValidateError(ts, "gnokey", neg, err)
},
gnokeyCmd: gnokeyFunc,
// adduser commands must be executed before starting the node; it errors out otherwise.
"adduser": func(ts *testscript.TestScript, neg bool, args []string) {
if nodeIsRunning(nodes, getNodeSID(ts)) {
Expand All @@ -261,12 +269,32 @@
ts.Fatalf("unable to get keybase")
}

balance, err := createAccount(ts, kb, args[0])
username := args[0]
balance, err := createAccount(ts, kb, username)
if err != nil {
ts.Fatalf("error creating account %s: %s", args[0], err)
}

newUserBalances = append(newUserBalances, balance)
addedUserAddresses[username] = balance.Address.String()
},
"withuser": func(ts *testscript.TestScript, neg bool, args []string) {
if len(args) < 2 || args[1] != gnokeyCmd {
ts.Fatalf("usage: withuser <username> " + gnokeyCmd)
}

Check warning on line 284 in gno.land/pkg/integration/testing_integration.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/integration/testing_integration.go#L283-L284

Added lines #L283 - L284 were not covered by tests

accountAddress, ok := addedUserAddresses[args[0]]
if !ok {
ts.Fatalf("user %s not added using `adduser`", args[1])
}

Check warning on line 289 in gno.land/pkg/integration/testing_integration.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/integration/testing_integration.go#L288-L289

Added lines #L288 - L289 were not covered by tests

for i, arg := range args {
if strings.Contains(arg, "USERADDRESS") {
args[i] = strings.Replace(arg, "USERADDRESS", accountAddress, 1)
}
}

gnokeyFunc(ts, neg, args[2:])
},
},
}
Expand Down
Loading