From 9e09c76cbf711adfa4abe8a4abf09790d6f12ed8 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 7 Nov 2023 13:11:44 +0100 Subject: [PATCH] cmd/devp2p: pass test parameters as flags instead of args CLIs with multiple positional parameters are kind of hard to use. --- cmd/devp2p/rlpxcmd.go | 53 +++++++++++++++++++++++++++++++++---------- cmd/devp2p/runtest.go | 15 ++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/cmd/devp2p/rlpxcmd.go b/cmd/devp2p/rlpxcmd.go index a6db9b97f9..6f6d02d927 100644 --- a/cmd/devp2p/rlpxcmd.go +++ b/cmd/devp2p/rlpxcmd.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/rlpx" "github.com/ethereum/go-ethereum/rlp" "github.com/urfave/cli/v2" @@ -46,22 +47,28 @@ var ( } rlpxEthTestCommand = &cli.Command{ Name: "eth-test", - Usage: "Runs tests against a node", - ArgsUsage: " ", + Usage: "Runs eth protocol tests against a node", + ArgsUsage: "", Action: rlpxEthTest, Flags: []cli.Flag{ testPatternFlag, testTAPFlag, + testChainDirFlag, + testNodeJWTFlag, + testNodeEngineFlag, }, } rlpxSnapTestCommand = &cli.Command{ Name: "snap-test", - Usage: "Runs tests against a node", - ArgsUsage: " ", + Usage: "Runs snap protocol tests against a node", + ArgsUsage: "", Action: rlpxSnapTest, Flags: []cli.Flag{ testPatternFlag, testTAPFlag, + testChainDirFlag, + testNodeJWTFlag, + testNodeEngineFlag, }, } ) @@ -103,10 +110,8 @@ func rlpxPing(ctx *cli.Context) error { // rlpxEthTest runs the eth protocol test suite. func rlpxEthTest(ctx *cli.Context) error { - if ctx.NArg() < 3 { - exit("missing path to chain.rlp as command-line argument") - } - suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2), ctx.Args().Get(3)) + p := cliTestParams(ctx) + suite, err := ethtest.NewSuite(p.node, p.chainDir, p.engineAPI, p.jwt) if err != nil { exit(err) } @@ -115,12 +120,36 @@ func rlpxEthTest(ctx *cli.Context) error { // rlpxSnapTest runs the snap protocol test suite. func rlpxSnapTest(ctx *cli.Context) error { - if ctx.NArg() < 3 { - exit("missing path to chain.rlp as command-line argument") - } - suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2), ctx.Args().Get(3)) + p := cliTestParams(ctx) + suite, err := ethtest.NewSuite(p.node, p.chainDir, p.engineAPI, p.jwt) if err != nil { exit(err) } return runTests(ctx, suite.SnapTests()) } + +type testParams struct { + node *enode.Node + engineAPI string + jwt string + chainDir string +} + +func cliTestParams(ctx *cli.Context) *testParams { + p := testParams{ + node: getNodeArg(ctx), + engineAPI: ctx.String(testNodeEngineFlag.Name), + jwt: ctx.String(testNodeJWTFlag.Name), + chainDir: ctx.String(testChainDirFlag.Name), + } + if p.engineAPI == "" { + exit(fmt.Errorf("missing -%s", testNodeEngineFlag.Name)) + } + if p.jwt == "" { + exit(fmt.Errorf("missing -%s", testNodeJWTFlag.Name)) + } + if p.chainDir == "" { + exit(fmt.Errorf("missing -%s", testChainDirFlag.Name)) + } + return &p +} diff --git a/cmd/devp2p/runtest.go b/cmd/devp2p/runtest.go index 76af53ee4d..6c370a5833 100644 --- a/cmd/devp2p/runtest.go +++ b/cmd/devp2p/runtest.go @@ -34,6 +34,21 @@ var ( Name: "tap", Usage: "Output TAP", } + + // for eth/snap tests + testChainDirFlag = &cli.StringFlag{ + Name: "chain", + Usage: "Test chain directory", + } + testNodeJWTFlag = &cli.StringFlag{ + Name: "jwt", + Usage: "JWT for talking to the engine API of the test node", + } + testNodeEngineFlag = &cli.StringFlag{ + Name: "engineapi", + Usage: "Engine API endpoint of the test node", + } + // These two are specific to the discovery tests. testListen1Flag = &cli.StringFlag{ Name: "listen1",