2019-05-15 09:02:53 +02:00
|
|
|
package itest
|
2016-08-30 07:07:54 +02:00
|
|
|
|
|
|
|
import (
|
2020-11-04 11:03:29 +01:00
|
|
|
"flag"
|
2016-08-30 07:07:54 +02:00
|
|
|
"fmt"
|
2022-11-14 09:55:08 +01:00
|
|
|
"io"
|
2016-11-22 04:16:44 +01:00
|
|
|
"os"
|
2022-11-14 09:55:08 +01:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2017-01-08 05:25:45 +01:00
|
|
|
"strings"
|
2016-10-24 04:00:09 +02:00
|
|
|
"testing"
|
2016-09-14 04:00:47 +02:00
|
|
|
"time"
|
2016-10-24 04:00:09 +02:00
|
|
|
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
2022-11-14 09:55:08 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lntemp"
|
2018-06-15 21:33:49 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
2020-07-07 10:32:13 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-14 09:55:08 +01:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2016-08-30 07:07:54 +02:00
|
|
|
)
|
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
const (
|
|
|
|
// defaultSplitTranches is the default number of tranches we split the
|
|
|
|
// test cases into.
|
|
|
|
defaultSplitTranches uint = 1
|
2020-11-04 11:03:29 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// defaultRunTranche is the default index of the test cases tranche that
|
|
|
|
// we run.
|
|
|
|
defaultRunTranche uint = 0
|
|
|
|
)
|
2020-11-04 11:03:29 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
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",
|
|
|
|
)
|
2020-11-07 13:23:30 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// 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",
|
|
|
|
)
|
2020-11-04 11:03:29 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// dbBackendFlag specifies the backend to use.
|
|
|
|
dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend "+
|
|
|
|
"(bbolt, etcd, postgres)")
|
|
|
|
)
|
2020-11-04 11:03:29 +01:00
|
|
|
|
2016-08-30 07:07:54 +02:00
|
|
|
// TestLightningNetworkDaemon performs a series of integration tests amongst a
|
2016-09-15 20:59:51 +02:00
|
|
|
// programmatically driven network of lnd nodes.
|
2016-08-30 07:07:54 +02:00
|
|
|
func TestLightningNetworkDaemon(t *testing.T) {
|
2020-10-26 14:21:05 +01:00
|
|
|
// If no tests are registered, then we can exit early.
|
2020-11-04 11:03:29 +01:00
|
|
|
if len(allTestCases) == 0 {
|
2020-01-10 15:27:48 +01:00
|
|
|
t.Skip("integration tests not selected with flag 'rpctest'")
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// Get the test cases to be run in this tranche.
|
|
|
|
testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche()
|
2020-11-04 11:03:29 +01:00
|
|
|
lntest.ApplyPortOffset(uint32(trancheIndex) * 1000)
|
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// Create a simple fee service.
|
|
|
|
feeService := lntemp.NewFeeService(t)
|
2016-08-30 07:07:54 +02:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// Get the binary path and setup the harness test.
|
|
|
|
binary := getLndBinary(t)
|
|
|
|
harnessTest := lntemp.SetupHarness(
|
|
|
|
t, binary, *dbBackendFlag, feeService,
|
2019-07-11 13:51:22 +02:00
|
|
|
)
|
2022-11-14 09:55:08 +01:00
|
|
|
defer harnessTest.Stop()
|
2017-01-05 22:17:49 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// Setup standby nodes, Alice and Bob, which will be alive and shared
|
|
|
|
// among all the test cases.
|
|
|
|
harnessTest.SetupStandbyNodes()
|
2016-08-30 07:07:54 +02:00
|
|
|
|
2020-11-04 11:03:29 +01:00
|
|
|
// Run the subset of the test cases selected in this tranche.
|
|
|
|
for idx, testCase := range testCases {
|
|
|
|
testCase := testCase
|
2021-07-13 18:12:31 +02:00
|
|
|
name := fmt.Sprintf("tranche%02d/%02d-of-%d/%s/%s",
|
|
|
|
trancheIndex, trancheOffset+uint(idx)+1,
|
2022-11-14 09:55:08 +01:00
|
|
|
len(allTestCases), harnessTest.ChainBackendName(),
|
|
|
|
testCase.Name)
|
2018-06-15 21:35:12 +02:00
|
|
|
|
2020-10-29 15:37:17 +01:00
|
|
|
success := t.Run(name, func(t1 *testing.T) {
|
2022-11-14 09:55:08 +01:00
|
|
|
// Create a separate harness test for the testcase to
|
|
|
|
// avoid overwriting the external harness test that is
|
|
|
|
// tied to the parent test.
|
|
|
|
ht := harnessTest.Subtest(t1)
|
2018-06-15 21:35:12 +02:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
// TODO(yy): split log files.
|
|
|
|
cleanTestCaseName := strings.ReplaceAll(
|
|
|
|
testCase.Name, " ", "_",
|
2020-10-29 15:37:17 +01:00
|
|
|
)
|
2022-11-14 09:55:08 +01:00
|
|
|
ht.SetTestName(cleanTestCaseName)
|
2020-10-29 15:35:02 +01:00
|
|
|
|
2020-10-29 15:37:17 +01:00
|
|
|
logLine := fmt.Sprintf(
|
|
|
|
"STARTING ============ %v ============\n",
|
2022-11-14 09:55:08 +01:00
|
|
|
testCase.Name,
|
2020-10-29 15:37:17 +01:00
|
|
|
)
|
2018-01-09 12:59:32 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
ht.Alice.AddToLogf(logLine)
|
|
|
|
ht.Bob.AddToLogf(logLine)
|
2020-10-29 15:37:17 +01:00
|
|
|
|
2022-11-14 09:55:08 +01:00
|
|
|
ht.EnsureConnected(ht.Alice, ht.Bob)
|
2020-10-29 15:37:17 +01:00
|
|
|
|
2019-04-15 16:19:47 +02:00
|
|
|
ht.RunTestCase(testCase)
|
2017-06-19 15:53:52 +02:00
|
|
|
})
|
2017-06-19 16:39:07 +02:00
|
|
|
|
2017-06-19 15:53:52 +02:00
|
|
|
// Stop at the first failure. Mimic behavior of original test
|
2017-06-19 16:39:07 +02:00
|
|
|
// framework.
|
2017-06-19 15:53:52 +02:00
|
|
|
if !success {
|
2020-04-06 21:32:26 +02:00
|
|
|
// Log failure time to help relate the lnd logs to the
|
|
|
|
// failure.
|
2020-11-04 11:03:29 +01:00
|
|
|
t.Logf("Failure time: %v", time.Now().Format(
|
|
|
|
"2006-01-02 15:04:05.000",
|
|
|
|
))
|
2017-06-19 15:53:52 +02:00
|
|
|
break
|
|
|
|
}
|
2016-08-30 07:07:54 +02:00
|
|
|
}
|
2022-11-14 09:55:08 +01:00
|
|
|
|
|
|
|
_, 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(allTestCases))
|
|
|
|
testsPerTranche := numCases / numTranches
|
|
|
|
trancheOffset := runTranche * testsPerTranche
|
|
|
|
trancheEnd := trancheOffset + testsPerTranche
|
|
|
|
if trancheEnd > numCases || runTranche == numTranches-1 {
|
|
|
|
trancheEnd = numCases
|
|
|
|
}
|
|
|
|
|
|
|
|
return allTestCases[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)
|
2016-08-30 07:07:54 +02:00
|
|
|
}
|