From fb4b43fb97ed54f215220a3aeb2cecde8976e416 Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 28 Feb 2024 15:35:11 +0100 Subject: [PATCH] Cleanup and refactor e2e tests functions a bit --- cmd/zetae2e/list_tests.go | 2 +- cmd/zetae2e/local/admin.go | 9 +- cmd/zetae2e/local/bitcoin.go | 9 +- cmd/zetae2e/local/erc20.go | 9 +- cmd/zetae2e/local/ethereum.go | 9 +- cmd/zetae2e/local/misc.go | 9 +- cmd/zetae2e/local/zeta.go | 15 ++- cmd/zetae2e/run.go | 18 +-- e2e/e2etests/e2etests.go | 232 +++++----------------------------- e2e/runner/runner.go | 79 ++++++++---- 10 files changed, 136 insertions(+), 255 deletions(-) diff --git a/cmd/zetae2e/list_tests.go b/cmd/zetae2e/list_tests.go index cf59dbedcd..1544d7a954 100644 --- a/cmd/zetae2e/list_tests.go +++ b/cmd/zetae2e/list_tests.go @@ -47,6 +47,6 @@ func renderTests(logger *runner.Logger, tests []runner.E2ETest) { formatString := fmt.Sprintf("%%-%ds | %%-%ds | %%s", maxNameLength, maxDescriptionLength) logger.Print(formatString, "Name", "Description", "Arguments") for _, test := range tests { - logger.Print(formatString, test.Name, test.Description, test.ArgumentsDescription) + logger.Print(formatString, test.Name, test.Description, test.ArgsDescription) } } diff --git a/cmd/zetae2e/local/admin.go b/cmd/zetae2e/local/admin.go index 26adb3fa0a..7db44c4453 100644 --- a/cmd/zetae2e/local/admin.go +++ b/cmd/zetae2e/local/admin.go @@ -61,12 +61,17 @@ func adminTestRoutine( adminRunner.WaitForMinedCCTX(txERC20Deposit) // run erc20 advanced test - if err := adminRunner.RunE2ETestsFromNames( + testsToRun, err := adminRunner.GetE2ETestsToRunByName( e2etests.AllE2ETests, e2etests.TestPauseZRC20Name, e2etests.TestUpdateBytecodeName, e2etests.TestDepositEtherLiquidityCapName, - ); err != nil { + ) + if err != nil { + return fmt.Errorf("admin tests failed: %v", err) + } + + if err := adminRunner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("admin tests failed: %v", err) } diff --git a/cmd/zetae2e/local/bitcoin.go b/cmd/zetae2e/local/bitcoin.go index 2cd39f9c53..97aa4a732e 100644 --- a/cmd/zetae2e/local/bitcoin.go +++ b/cmd/zetae2e/local/bitcoin.go @@ -64,12 +64,17 @@ func bitcoinTestRoutine( // run bitcoin test // Note: due to the extensive block generation in Bitcoin localnet, block header test is run first // to make it faster to catch up with the latest block header - if err := bitcoinRunner.RunE2ETestsFromNames( + testsToRun, err := bitcoinRunner.GetE2ETestsToRunByName( e2etests.AllE2ETests, e2etests.TestBitcoinWithdrawName, e2etests.TestZetaWithdrawBTCRevertName, e2etests.TestCrosschainSwapName, - ); err != nil { + ) + if err != nil { + return fmt.Errorf("bitcoin tests failed: %v", err) + } + + if err := bitcoinRunner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("bitcoin tests failed: %v", err) } diff --git a/cmd/zetae2e/local/erc20.go b/cmd/zetae2e/local/erc20.go index c3dbc3f354..84f11f0307 100644 --- a/cmd/zetae2e/local/erc20.go +++ b/cmd/zetae2e/local/erc20.go @@ -60,13 +60,18 @@ func erc20TestRoutine( //erc20Runner.DepositBTC() // run erc20 test - if err := erc20Runner.RunE2ETestsFromNames( + testsToRun, err := erc20Runner.GetE2ETestsToRunByName( e2etests.AllE2ETests, e2etests.TestERC20WithdrawName, e2etests.TestMultipleWithdrawsName, e2etests.TestERC20DepositAndCallRefundName, e2etests.TestZRC20SwapName, - ); err != nil { + ) + if err != nil { + return fmt.Errorf("erc20 tests failed: %v", err) + } + + if err := erc20Runner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("erc20 tests failed: %v", err) } diff --git a/cmd/zetae2e/local/ethereum.go b/cmd/zetae2e/local/ethereum.go index 31e0e33b37..f606511e81 100644 --- a/cmd/zetae2e/local/ethereum.go +++ b/cmd/zetae2e/local/ethereum.go @@ -53,13 +53,18 @@ func ethereumTestRoutine( // run ethereum test // Note: due to the extensive block generation in Ethereum localnet, block header test is run first // to make it faster to catch up with the latest block header - if err := ethereumRunner.RunE2ETestsFromNames( + testsToRun, err := ethereumRunner.GetE2ETestsToRunByName( e2etests.AllE2ETests, e2etests.TestEtherWithdrawName, e2etests.TestContextUpgradeName, e2etests.TestEtherDepositAndCallName, e2etests.TestDepositAndCallRefundName, - ); err != nil { + ) + if err != nil { + return fmt.Errorf("ethereum tests failed: %v", err) + } + + if err := ethereumRunner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("ethereum tests failed: %v", err) } diff --git a/cmd/zetae2e/local/misc.go b/cmd/zetae2e/local/misc.go index 26aeb9f3b4..b3f2b90e4b 100644 --- a/cmd/zetae2e/local/misc.go +++ b/cmd/zetae2e/local/misc.go @@ -56,11 +56,16 @@ func miscTestRoutine( miscRunner.WaitForMinedCCTX(txZetaDeposit) // run misc test - if err := miscRunner.RunE2ETestsFromNames( + testsToRun, err := miscRunner.GetE2ETestsToRunByName( e2etests.AllE2ETests, //e2etests.TestBlockHeadersName, e2etests.TestMyTestName, - ); err != nil { + ) + if err != nil { + return fmt.Errorf("misc tests failed: %v", err) + } + + if err := miscRunner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("misc tests failed: %v", err) } diff --git a/cmd/zetae2e/local/zeta.go b/cmd/zetae2e/local/zeta.go index ac7243f7fb..7b5d625d74 100644 --- a/cmd/zetae2e/local/zeta.go +++ b/cmd/zetae2e/local/zeta.go @@ -57,13 +57,16 @@ func zetaTestRoutine( zetaRunner.WaitForMinedCCTX(txEtherDeposit) // run zeta test - if err := zetaRunner.RunE2ETestsFromNames( + testsToRun, err := zetaRunner.GetE2ETestsToRunByName( e2etests.AllE2ETests, - e2etests.TestZetaWithdrawName, - e2etests.TestMessagePassingName, - e2etests.TestMessagePassingRevertFailName, - e2etests.TestMessagePassingRevertSuccessName, - ); err != nil { + //e2etests.TestBlockHeadersName, + e2etests.TestMyTestName, + ) + if err != nil { + return fmt.Errorf("zeta tests failed: %v", err) + } + + if err := zetaRunner.RunE2ETests(testsToRun); err != nil { return fmt.Errorf("zeta tests failed: %v", err) } diff --git a/cmd/zetae2e/run.go b/cmd/zetae2e/run.go index fe83b4f874..f970f625d1 100644 --- a/cmd/zetae2e/run.go +++ b/cmd/zetae2e/run.go @@ -81,16 +81,16 @@ func runE2ETest(cmd *cobra.Command, args []string) error { return errors.New("invalid EVM address") } - // parse test names and arguments - testList := []runner.E2ETest{} + // parse test names and arguments from cmd args + testArgsMap := make(map[string][]string) for _, arg := range args { parts := strings.SplitN(arg, ":", 2) testName := parts[0] - var testArgs []string + testArgs := []string{} if len(parts) > 1 && parts[1] != "" { testArgs = strings.Split(parts[1], ",") } - testList = append(testList, runner.E2ETest{Name: testName, Args: testArgs}) + testArgsMap[testName] = testArgs } // initialize deployer runner with config @@ -129,10 +129,12 @@ func runE2ETest(cmd *cobra.Command, args []string) error { } //run tests - reports, err := testRunner.RunE2ETestsFromNamesIntoReport( - e2etests.AllE2ETests, - testList, - ) + testsToRun, err := testRunner.GetE2ETestsToRunByNameAndArgs(e2etests.AllE2ETests, testArgsMap) + if err != nil { + cancel() + return err + } + reports, err := testRunner.RunE2ETestsIntoReport(testsToRun) if err != nil { cancel() return err diff --git a/e2e/e2etests/e2etests.go b/e2e/e2etests/e2etests.go index 275353f8de..0f92d155b9 100644 --- a/e2e/e2etests/e2etests.go +++ b/e2e/e2etests/e2etests.go @@ -43,207 +43,33 @@ const ( // AllE2ETests is an ordered list of all e2e tests var AllE2ETests = []runner.E2ETest{ - { - TestContextUpgradeName, - []string{}, - "tests sending ETH on ZEVM and check context data using ContextApp", - "", - TestContextUpgrade, - }, - { - TestDepositAndCallRefundName, - []string{}, - "deposit ZRC20 into ZEVM and call a contract that reverts; should refund", - "", - TestDepositAndCallRefund, - }, - { - TestMultipleERC20DepositName, - []string{}, - "deposit USDT ERC20 into ZEVM in multiple deposits", - "", - TestMultipleERC20Deposit, - }, - { - TestERC20WithdrawName, - []string{}, - "withdraw ERC20 from ZEVM", - "", - TestERC20Withdraw, - }, - { - TestMultipleWithdrawsName, - []string{}, - "withdraw ERC20 from ZEVM in multiple deposits", - "", - TestMultipleWithdraws, - }, - { - TestZetaWithdrawName, - []string{}, - "withdraw ZETA from ZEVM to Ethereum", - "", - TestZetaWithdraw, - }, - { - TestZetaDepositName, - []string{}, - "deposit ZETA from Ethereum to ZEVM", - "", - TestZetaDeposit, - }, - { - TestZetaWithdrawBTCRevertName, - []string{}, - "sending ZETA from ZEVM to Bitcoin with a message that should revert cctxs", - "", - TestZetaWithdrawBTCRevert, - }, - { - TestMessagePassingName, - []string{}, - "goerli->goerli message passing (sending ZETA only)", - "", - TestMessagePassing, - }, - { - TestZRC20SwapName, - []string{}, - "swap ZRC20 USDT for ZRC20 ETH", - "", - TestZRC20Swap, - }, - { - TestBitcoinWithdrawName, - []string{}, - "withdraw BTC from ZEVM", - "", - TestBitcoinWithdraw, - }, - { - TestCrosschainSwapName, - []string{}, - "testing Bitcoin ERC20 cross-chain swap", - "", - TestCrosschainSwap, - }, - { - TestMessagePassingRevertFailName, - []string{}, - "goerli->goerli message passing (revert fail)", - "", - TestMessagePassingRevertFail, - }, - { - TestMessagePassingRevertSuccessName, - []string{}, - "goerli->goerli message passing (revert success)", - "", - TestMessagePassingRevertSuccess, - }, - { - TestPauseZRC20Name, - []string{}, - "pausing ZRC20 on ZetaChain", - "", - TestPauseZRC20, - }, - { - TestERC20DepositAndCallRefundName, - []string{}, - "deposit a non-gas ZRC20 into ZEVM and call a contract that reverts", - "", - TestERC20DepositAndCallRefund, - }, - { - TestUpdateBytecodeName, - []string{}, - "update ZRC20 bytecode swap", - "", - TestUpdateBytecode, - }, - { - TestEtherDepositAndCallName, - []string{}, - "deposit ZRC20 into ZEVM and call a contract", - "", - TestEtherDepositAndCall, - }, - { - TestDepositEtherLiquidityCapName, - []string{}, - "deposit Ethers into ZEVM with a liquidity cap", - "", - TestDepositEtherLiquidityCap, - }, - { - TestMyTestName, - []string{}, - "performing custom test", - "", - TestMyTest, - }, - { - TestERC20DepositName, - []string{}, - "deposit ERC20 into ZEVM", - "", - TestERC20Deposit, - }, - { - TestEtherDepositName, - []string{}, - "deposit Ether into ZEVM", - "amount in wei (default 0.01ETH)", - TestEtherDeposit, - }, - { - TestEtherWithdrawName, - []string{}, - "withdraw Ether from ZEVM", - "", - TestEtherWithdraw, - }, - { - TestBitcoinDepositName, - []string{}, - "deposit Bitcoin into ZEVM", - "", - TestBitcoinDeposit, - }, - { - TestDonationEtherName, - []string{}, - "donate Ether to the TSS", - "", - TestDonationEther, - }, - { - TestStressEtherWithdrawName, - []string{}, - "stress test Ether withdrawal", - "", - TestStressEtherWithdraw, - }, - { - TestStressBTCWithdrawName, - []string{}, - "stress test BTC withdrawal", - "", - TestStressBTCWithdraw, - }, - { - TestStressEtherDepositName, - []string{}, - "stress test Ether deposit", - "", - TestStressEtherDeposit, - }, - { - TestStressBTCDepositName, - []string{}, - "stress test BTC deposit", - "", - TestStressBTCDeposit, - }, + runner.NewE2ETest(TestContextUpgradeName, "tests sending ETH on ZEVM and check context data using ContextApp", "", TestContextUpgrade), + runner.NewE2ETest(TestDepositAndCallRefundName, "deposit ZRC20 into ZEVM and call a contract that reverts; should refund", "", TestDepositAndCallRefund), + runner.NewE2ETest(TestMultipleERC20DepositName, "deposit USDT ERC20 into ZEVM in multiple deposits", "", TestMultipleERC20Deposit), + runner.NewE2ETest(TestERC20WithdrawName, "withdraw ERC20 from ZEVM", "", TestERC20Withdraw), + runner.NewE2ETest(TestMultipleWithdrawsName, "withdraw ERC20 from ZEVM in multiple deposits", "", TestMultipleWithdraws), + runner.NewE2ETest(TestZetaWithdrawName, "withdraw ZETA from ZEVM to Ethereum", "", TestZetaWithdraw), + runner.NewE2ETest(TestZetaDepositName, "deposit ZETA from Ethereum to ZEVM", "", TestZetaDeposit), + runner.NewE2ETest(TestZetaWithdrawBTCRevertName, "sending ZETA from ZEVM to Bitcoin with a message that should revert cctxs", "", TestZetaWithdrawBTCRevert), + runner.NewE2ETest(TestMessagePassingName, "goerli->goerli message passing (sending ZETA only)", "", TestMessagePassing), + runner.NewE2ETest(TestZRC20SwapName, "swap ZRC20 USDT for ZRC20 ETH", "", TestZRC20Swap), + runner.NewE2ETest(TestBitcoinWithdrawName, "withdraw BTC from ZEVM", "", TestBitcoinWithdraw), + runner.NewE2ETest(TestCrosschainSwapName, "testing Bitcoin ERC20 cross-chain swap", "", TestCrosschainSwap), + runner.NewE2ETest(TestMessagePassingRevertFailName, "goerli->goerli message passing (revert fail)", "", TestMessagePassingRevertFail), + runner.NewE2ETest(TestMessagePassingRevertSuccessName, "goerli->goerli message passing (revert success)", "", TestMessagePassingRevertSuccess), + runner.NewE2ETest(TestPauseZRC20Name, "pausing ZRC20 on ZetaChain", "", TestPauseZRC20), + runner.NewE2ETest(TestERC20DepositAndCallRefundName, "deposit a non-gas ZRC20 into ZEVM and call a contract that reverts", "", TestERC20DepositAndCallRefund), + runner.NewE2ETest(TestUpdateBytecodeName, "update ZRC20 bytecode swap", "", TestUpdateBytecode), + runner.NewE2ETest(TestEtherDepositAndCallName, "deposit ZRC20 into ZEVM and call a contract", "", TestEtherDepositAndCall), + runner.NewE2ETest(TestDepositEtherLiquidityCapName, "deposit Ethers into ZEVM with a liquidity cap", "", TestDepositEtherLiquidityCap), + runner.NewE2ETest(TestMyTestName, "performing custom test", "", TestMyTest), + runner.NewE2ETest(TestERC20DepositName, "deposit ERC20 into ZEVM", "", TestERC20Deposit), + runner.NewE2ETest(TestEtherDepositName, "deposit Ether into ZEVM", "", TestEtherDeposit), + runner.NewE2ETest(TestEtherWithdrawName, "withdraw Ether from ZEVM", "", TestEtherWithdraw), + runner.NewE2ETest(TestBitcoinDepositName, "deposit Bitcoin into ZEVM", "", TestBitcoinDeposit), + runner.NewE2ETest(TestDonationEtherName, "donate Ether to the TSS", "", TestDonationEther), + runner.NewE2ETest(TestStressEtherWithdrawName, "stress test Ether withdrawal", "", TestStressEtherWithdraw), + runner.NewE2ETest(TestStressBTCWithdrawName, "stress test BTC withdrawal", "", TestStressBTCWithdraw), + runner.NewE2ETest(TestStressEtherDepositName, "stress test Ether deposit", "", TestStressEtherDeposit), + runner.NewE2ETest(TestStressBTCDepositName, "stress test BTC deposit", "", TestStressBTCDeposit), } diff --git a/e2e/runner/runner.go b/e2e/runner/runner.go index 7dd0f84e6e..32492ef643 100644 --- a/e2e/runner/runner.go +++ b/e2e/runner/runner.go @@ -165,43 +165,69 @@ type E2ETestFunc func(*E2ERunner, []string) // E2ETest represents a E2E test with a name type E2ETest struct { - Name string - Args []string // TODO: should args be here, probably there is a cleaner way - Description string - ArgumentsDescription string - E2ETest E2ETestFunc + Name string + Args []string // TODO: should args be here, probably there is a cleaner way + Description string + ArgsDescription string + E2ETest E2ETestFunc } -// RunE2ETestsFromNames runs a list of E2E tests by name in a list of e2e tests -func (runner *E2ERunner) RunE2ETestsFromNames(e2eTests []E2ETest, e2eTestsToRun ...E2ETest) error { - for _, e2eTestName := range e2eTestsToRun { - e2eTest, ok := findE2ETest(e2eTestName, e2eTests) - if !ok { - return fmt.Errorf("e2e test %s not found", e2eTestName) +// NewE2ETest creates a new instance of E2ETest with specified parameters. +func NewE2ETest(name, description, argsDescription string, e2eTestFunc E2ETestFunc) E2ETest { + return E2ETest{ + Name: name, + Description: description, + E2ETest: e2eTestFunc, + ArgsDescription: argsDescription, + Args: []string{}, + } +} + +func (runner *E2ERunner) GetE2ETestsToRunByName(e2eTests []E2ETest, e2eTestNames ...string) ([]E2ETest, error) { + e2eTestsWithArgs := make(map[string][]string) + for _, testName := range e2eTestNames { + e2eTestsWithArgs[testName] = []string{} // no args specified + } + return runner.GetE2ETestsToRunByNameAndArgs(e2eTests, e2eTestsWithArgs) +} + +func (runner *E2ERunner) GetE2ETestsToRunByNameAndArgs(e2eTests []E2ETest, e2eTestsWithArgs map[string][]string) ([]E2ETest, error) { + testsToRun := []E2ETest{} + for testName, args := range e2eTestsWithArgs { + e2eTest, found := findE2ETestByName(e2eTests, testName) + if !found { + return nil, fmt.Errorf("e2e test %s not found", testName) } + // clone the test to modify its Args without affecting the original + e2eTestToRun := NewE2ETest( + e2eTest.Name, + e2eTest.Description, + e2eTest.ArgsDescription, + e2eTest.E2ETest, + ) + e2eTestToRun.Args = args + testsToRun = append(testsToRun, e2eTestToRun) + } + + return testsToRun, nil +} + +// RunE2ETests runs a list of e2e tests +func (runner *E2ERunner) RunE2ETests(e2eTests []E2ETest) (err error) { + for _, e2eTest := range e2eTests { if err := runner.RunE2ETest(e2eTest, true); err != nil { return err } } - return nil } // RunE2ETestsFromNamesIntoReport runs a list of e2e tests by name in a list of e2e tests and returns a report // The function doesn't return an error, it returns a report with the error -func (runner *E2ERunner) RunE2ETestsFromNamesIntoReport(e2eTests []E2ETest, e2eTestsToRun []E2ETest) (TestReports, error) { - // get all tests so we can return an error if a test is not found - tests := make([]E2ETest, 0, len(e2eTestsToRun)) - for _, e2eTestWithArgs := range e2eTestsToRun { - _, ok := findE2ETest(e2eTestWithArgs, e2eTests) - if !ok { - return nil, fmt.Errorf("e2e test %s not found", e2eTestWithArgs.Name) - } - } - +func (runner *E2ERunner) RunE2ETestsIntoReport(e2eTests []E2ETest) (TestReports, error) { // go through all tests - reports := make(TestReports, 0, len(e2eTestsToRun)) - for _, test := range tests { + reports := make(TestReports, 0, len(e2eTests)) + for _, test := range e2eTests { // get info before test balancesBefore, err := runner.GetAccountBalances(true) if err != nil { @@ -271,10 +297,9 @@ func (runner *E2ERunner) RunE2ETest(e2eTest E2ETest, checkAccounting bool) (err } // findE2ETest finds a e2e test by name -func findE2ETest(e2eTest E2ETest, e2eTests []E2ETest) (E2ETest, bool) { +func findE2ETestByName(e2eTests []E2ETest, e2eTestName string) (E2ETest, bool) { for _, test := range e2eTests { - if test.Name == e2eTest.Name { - test.Args = e2eTest.Args + if test.Name == e2eTestName { return test, true } }