From a01ac5003ff2bbe384ec785a1b862193a0178928 Mon Sep 17 00:00:00 2001 From: deelawn Date: Wed, 24 Jan 2024 11:32:42 -0800 Subject: [PATCH 1/2] add feature for dynamic user address in txtar test --- gno.land/pkg/integration/doc.go | 6 + .../pkg/integration/testdata/adduser.txtar | 19 +-- .../pkg/integration/testing_integration.go | 110 +++++++++++------- 3 files changed, 81 insertions(+), 54 deletions(-) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index fb0b0bdf7a0..c0b4675e0b8 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -21,6 +21,12 @@ // - Creates a new user in the default keybase directory. // - Must be run before `gnoland start`. // +// 4. `withuser`: +// - usage is `withuser gnokey`. +// - username should be registered username, either prepopulated or added 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 diff --git a/gno.land/pkg/integration/testdata/adduser.txtar b/gno.land/pkg/integration/testdata/adduser.txtar index ebd0e4abb43..218a47bbc70 100644 --- a/gno.land/pkg/integration/testdata/adduser.txtar +++ b/gno.land/pkg/integration/testdata/adduser.txtar @@ -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: ' @@ -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 } diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 8974ea75637..478636ebc3c 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -24,7 +24,10 @@ import ( "github.com/rogpeppe/go-internal/testscript" ) -const numTestAccounts int = 4 +const ( + numTestAccounts int = 4 + gnokeyCmd string = "gnokey" +) type tSeqShim struct{ *testing.T } @@ -80,7 +83,49 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // 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")) @@ -207,44 +252,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { 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)) { @@ -261,12 +269,32 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { 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 " + gnokeyCmd) + } + + accountAddress, ok := addedUserAddresses[args[0]] + if !ok { + ts.Fatalf("user %s not added using `adduser`", args[1]) + } + + for i, arg := range args { + if strings.Contains(arg, "USERADDRESS") { + args[i] = strings.Replace(arg, "USERADDRESS", accountAddress, 1) + } + } + + gnokeyFunc(ts, neg, args[2:]) }, }, } From 1dd23d83ecba4dc0eb4457f3fbe607f374187eb7 Mon Sep 17 00:00:00 2001 From: deelawn Date: Wed, 24 Jan 2024 11:36:04 -0800 Subject: [PATCH 2/2] modified doc --- gno.land/pkg/integration/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index c0b4675e0b8..e077d09ecdc 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -23,7 +23,7 @@ // // 4. `withuser`: // - usage is `withuser gnokey`. -// - username should be registered username, either prepopulated or added via the adduser command. +// - 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' ... ` //