From 1350f7662774a586c6a785cb8dbea9cd68275ff1 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 22 Jul 2022 08:55:38 +0800 Subject: [PATCH] itest: add a new test entry point to run the temp tests This commit adds a new function, `TestLightningNetworkDaemonTemp`, to run the new tests. --- lntest/itest/list_off_test.go | 9 ++ lntest/itest/list_on_test.go | 9 ++ lntest/itest/lnd_test.go | 36 +----- lntest/itest/temp_lnd_test.go | 199 ++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 34 deletions(-) create mode 100644 lntest/itest/list_off_test.go create mode 100644 lntest/itest/list_on_test.go create mode 100644 lntest/itest/temp_lnd_test.go diff --git a/lntest/itest/list_off_test.go b/lntest/itest/list_off_test.go new file mode 100644 index 000000000..e34443092 --- /dev/null +++ b/lntest/itest/list_off_test.go @@ -0,0 +1,9 @@ +//go:build !rpctest +// +build !rpctest + +package itest + +import "github.com/lightningnetwork/lnd/lntemp" + +// TODO(yy): remove the temp. +var allTestCasesTemp = []*lntemp.TestCase{} diff --git a/lntest/itest/list_on_test.go b/lntest/itest/list_on_test.go new file mode 100644 index 000000000..3637af41e --- /dev/null +++ b/lntest/itest/list_on_test.go @@ -0,0 +1,9 @@ +//go:build rpctest +// +build rpctest + +package itest + +import "github.com/lightningnetwork/lnd/lntemp" + +// TODO(yy): remove the temp. +var allTestCasesTemp = []*lntemp.TestCase{} diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index 79e09f4a5..00b3efc7a 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -13,39 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -const ( - // defaultSplitTranches is the default number of tranches we split the - // test cases into. - defaultSplitTranches uint = 1 - - // defaultRunTranche is the default index of the test cases tranche that - // we run. - defaultRunTranche uint = 0 -) - var ( - // testCasesSplitParts is the number of tranches the test cases should - // be split into. By default this is set to 1, so no splitting happens. - // If this value is increased, then the -runtranche flag must be - // specified as well to indicate which part should be run in the current - // invocation. - testCasesSplitTranches = flag.Uint( - "splittranches", defaultSplitTranches, "split the test cases "+ - "in this many tranches and run the tranche at "+ - "0-based index specified by the -runtranche flag", - ) - - // testCasesRunTranche is the 0-based index of the split test cases - // tranche to run in the current invocation. - testCasesRunTranche = flag.Uint( - "runtranche", defaultRunTranche, "run the tranche of the "+ - "split test cases with the given (0-based) index", - ) - - // dbBackendFlag specifies the backend to use. - dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend "+ - "(bbolt, etcd, postgres)") - // tempTest is a flag used to mark whether we should run the old or the // new test cases. Used here so we can transit smoothly during our new // itest construction. @@ -57,7 +25,7 @@ var ( // getTestCaseSplitTranche returns the sub slice of the test cases that should // be run as the current split tranche as well as the index and slice offset of // the tranche. -func getTestCaseSplitTranche() ([]*testCase, uint, uint) { +func getTestCaseSplitTrancheOld() ([]*testCase, uint, uint) { numTranches := defaultSplitTranches if testCasesSplitTranches != nil { numTranches = *testCasesSplitTranches @@ -102,7 +70,7 @@ func TestLightningNetworkDaemon(t *testing.T) { // Parse testing flags that influence our test execution. logDir := lntest.GetLogDir() require.NoError(t, os.MkdirAll(logDir, 0700)) - testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche() + testCases, trancheIndex, trancheOffset := getTestCaseSplitTrancheOld() lntest.ApplyPortOffset(uint32(trancheIndex) * 1000) // Before we start any node, we need to make sure that any btcd node diff --git a/lntest/itest/temp_lnd_test.go b/lntest/itest/temp_lnd_test.go new file mode 100644 index 000000000..6c19c6e7f --- /dev/null +++ b/lntest/itest/temp_lnd_test.go @@ -0,0 +1,199 @@ +package itest + +import ( + "flag" + "fmt" + "io" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/btcsuite/btcd/integration/rpctest" + "github.com/lightningnetwork/lnd/lntemp" + "github.com/lightningnetwork/lnd/lntest" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/grpclog" +) + +const ( + // defaultSplitTranches is the default number of tranches we split the + // test cases into. + defaultSplitTranches uint = 1 + + // defaultRunTranche is the default index of the test cases tranche that + // we run. + defaultRunTranche uint = 0 +) + +var ( + // testCasesSplitParts is the number of tranches the test cases should + // be split into. By default this is set to 1, so no splitting happens. + // If this value is increased, then the -runtranche flag must be + // specified as well to indicate which part should be run in the current + // invocation. + testCasesSplitTranches = flag.Uint( + "splittranches", defaultSplitTranches, "split the test cases "+ + "in this many tranches and run the tranche at "+ + "0-based index specified by the -runtranche flag", + ) + + // testCasesRunTranche is the 0-based index of the split test cases + // tranche to run in the current invocation. + testCasesRunTranche = flag.Uint( + "runtranche", defaultRunTranche, "run the tranche of the "+ + "split test cases with the given (0-based) index", + ) + + // dbBackendFlag specifies the backend to use. + dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend "+ + "(bbolt, etcd, postgres)") +) + +// TestLightningNetworkDaemonTemp performs a series of integration tests +// amongst a programmatically driven network of lnd nodes. +func TestLightningNetworkDaemonTemp(t *testing.T) { + if !*tempTest { + t.Skip("Running old tests, new tests are skipped") + } + + // If no tests are registered, then we can exit early. + if len(allTestCasesTemp) == 0 { + t.Skip("integration tests not selected with flag 'rpctest'") + } + + // Get the test cases to be run in this tranche. + testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche() + lntest.ApplyPortOffset(uint32(trancheIndex) * 1000) + + // Get the binary path and setup the harness test. + binary := getLndBinary(t) + harnessTest := lntemp.SetupHarness(t, binary, *dbBackendFlag) + defer harnessTest.Stop() + + // Setup standby nodes, Alice and Bob, which will be alive and shared + // among all the test cases. + harnessTest.SetupStandbyNodes() + + // Run the subset of the test cases selected in this tranche. + for idx, testCase := range testCases { + testCase := testCase + name := fmt.Sprintf("tranche%02d/%02d-of-%d/%s/%s", + trancheIndex, trancheOffset+uint(idx)+1, + len(allTestCases), harnessTest.ChainBackendName(), + testCase.Name) + + success := t.Run(name, func(t1 *testing.T) { + // Create a separate harness test for the testcase to + // avoid overwriting the external harness test that is + // tied to the parent test. + ht, cleanup := harnessTest.Subtest(t1) + defer cleanup() + + cleanTestCaseName := strings.ReplaceAll( + testCase.Name, " ", "_", + ) + ht.SetTestName(cleanTestCaseName) + + logLine := fmt.Sprintf( + "STARTING ============ %v ============\n", + testCase.Name, + ) + + ht.Alice.AddToLogf(logLine) + ht.Bob.AddToLogf(logLine) + + // Start every test with the default static fee + // estimate. + ht.SetFeeEstimate(12500) + ht.EnsureConnected(ht.Alice, ht.Bob) + + ht.RunTestCase(testCase) + }) + + // Stop at the first failure. Mimic behavior of original test + // framework. + if !success { + // Log failure time to help relate the lnd logs to the + // failure. + t.Logf("Failure time: %v", time.Now().Format( + "2006-01-02 15:04:05.000", + )) + break + } + } + + _, height := harnessTest.Miner.GetBestBlock() + t.Logf("=========> tests finished for tranche: %v, tested %d "+ + "cases, end height: %d\n", trancheIndex, len(testCases), height) +} + +// getTestCaseSplitTranche returns the sub slice of the test cases that should +// be run as the current split tranche as well as the index and slice offset of +// the tranche. +func getTestCaseSplitTranche() ([]*lntemp.TestCase, uint, uint) { + numTranches := defaultSplitTranches + if testCasesSplitTranches != nil { + numTranches = *testCasesSplitTranches + } + runTranche := defaultRunTranche + if testCasesRunTranche != nil { + runTranche = *testCasesRunTranche + } + + // There's a special flake-hunt mode where we run the same test multiple + // times in parallel. In that case the tranche index is equal to the + // thread ID, but we need to actually run all tests for the regex + // selection to work. + threadID := runTranche + if numTranches == 1 { + runTranche = 0 + } + + numCases := uint(len(allTestCasesTemp)) + testsPerTranche := numCases / numTranches + trancheOffset := runTranche * testsPerTranche + trancheEnd := trancheOffset + testsPerTranche + if trancheEnd > numCases || runTranche == numTranches-1 { + trancheEnd = numCases + } + + return allTestCasesTemp[trancheOffset:trancheEnd], threadID, + trancheOffset +} + +func getLndBinary(t *testing.T) string { + binary := itestLndBinary + lndExec := "" + if lndExecutable != nil && *lndExecutable != "" { + lndExec = *lndExecutable + } + if lndExec == "" && runtime.GOOS == "windows" { + // Windows (even in a bash like environment like git bash as on + // Travis) doesn't seem to like relative paths to exe files... + currentDir, err := os.Getwd() + require.NoError(t, err, "unable to get working directory") + + targetPath := filepath.Join(currentDir, "../../lnd-itest.exe") + binary, err = filepath.Abs(targetPath) + require.NoError(t, err, "unable to get absolute path") + } else if lndExec != "" { + binary = lndExec + } + + return binary +} + +func init() { + // Before we start any node, we need to make sure that any btcd node + // that is started through the RPC harness uses a unique port as well + // to avoid any port collisions. + rpctest.ListenAddressGenerator = lntest.GenerateBtcdListenerAddresses + + // Swap out grpc's default logger with out fake logger which drops the + // statements on the floor. + fakeLogger := grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard) + grpclog.SetLoggerV2(fakeLogger) +}