2018-05-04 11:59:21 +02:00
|
|
|
from concurrent import futures
|
2019-08-30 18:59:53 +02:00
|
|
|
from db import SqliteDbProvider, PostgresDbProvider
|
2019-04-10 23:37:15 +02:00
|
|
|
from utils import NodeFactory, BitcoinD, ElementsD
|
2018-05-04 11:59:21 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
import re
|
2018-06-05 15:12:05 +02:00
|
|
|
import shutil
|
2018-08-02 16:20:48 +02:00
|
|
|
import sys
|
2018-05-04 11:59:21 +02:00
|
|
|
import tempfile
|
|
|
|
|
|
|
|
|
2018-06-06 22:40:24 +02:00
|
|
|
with open('config.vars') as configfile:
|
|
|
|
config = dict([(line.rstrip().split('=', 1)) for line in configfile])
|
|
|
|
|
|
|
|
VALGRIND = os.getenv("VALGRIND", config['VALGRIND']) == "1"
|
2019-05-12 21:26:26 +02:00
|
|
|
TEST_NETWORK = os.getenv("TEST_NETWORK", config['TEST_NETWORK'])
|
2018-06-06 22:40:24 +02:00
|
|
|
DEVELOPER = os.getenv("DEVELOPER", config['DEVELOPER']) == "1"
|
2018-05-04 11:59:21 +02:00
|
|
|
TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1"
|
|
|
|
|
2019-01-11 06:51:08 +01:00
|
|
|
|
2018-08-02 16:20:48 +02:00
|
|
|
if TEST_DEBUG:
|
|
|
|
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
|
|
|
|
|
|
|
|
|
2018-05-05 15:30:14 +02:00
|
|
|
# A dict in which we count how often a particular test has run so far. Used to
|
|
|
|
# give each attempt its own numbered directory, and avoid clashes.
|
|
|
|
__attempts = {}
|
|
|
|
|
|
|
|
|
2018-06-05 15:41:18 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def test_base_dir():
|
2019-09-06 13:30:45 +02:00
|
|
|
d = os.getenv("TEST_DIR", "/tmp")
|
|
|
|
|
|
|
|
directory = tempfile.mkdtemp(prefix='ltests-', dir=d)
|
2018-06-05 15:41:18 +02:00
|
|
|
print("Running tests in {}".format(directory))
|
|
|
|
|
|
|
|
yield directory
|
|
|
|
|
|
|
|
if os.listdir(directory) == []:
|
|
|
|
shutil.rmtree(directory)
|
|
|
|
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
@pytest.fixture
|
2018-08-01 16:18:01 +02:00
|
|
|
def directory(request, test_base_dir, test_name):
|
2018-05-05 15:30:14 +02:00
|
|
|
"""Return a per-test specific directory.
|
|
|
|
|
|
|
|
This makes a unique test-directory even if a test is rerun multiple times.
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
"""
|
2018-06-05 15:41:18 +02:00
|
|
|
global __attempts
|
2018-05-05 15:30:14 +02:00
|
|
|
# Auto set value if it isn't in the dict yet
|
|
|
|
__attempts[test_name] = __attempts.get(test_name, 0) + 1
|
2018-06-05 15:41:18 +02:00
|
|
|
directory = os.path.join(test_base_dir, "{}_{}".format(test_name, __attempts[test_name]))
|
2018-09-11 22:31:31 +02:00
|
|
|
request.node.has_errors = False
|
2018-06-05 15:41:18 +02:00
|
|
|
|
|
|
|
yield directory
|
|
|
|
|
2018-08-01 16:18:01 +02:00
|
|
|
# This uses the status set in conftest.pytest_runtest_makereport to
|
2019-04-10 21:47:32 +02:00
|
|
|
# determine whether we succeeded or failed. Outcome can be None if the
|
|
|
|
# failure occurs during the setup phase, hence the use to getattr instead
|
|
|
|
# of accessing it directly.
|
2019-08-28 23:44:44 +02:00
|
|
|
outcome = getattr(request.node, 'rep_call', None).outcome
|
2019-04-10 21:47:32 +02:00
|
|
|
failed = not outcome or request.node.has_errors or outcome != 'passed'
|
|
|
|
|
|
|
|
if not failed:
|
2018-08-01 16:18:01 +02:00
|
|
|
shutil.rmtree(directory)
|
|
|
|
else:
|
|
|
|
logging.debug("Test execution failed, leaving the test directory {} intact.".format(directory))
|
2018-05-04 11:59:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_name(request):
|
|
|
|
yield request.function.__name__
|
|
|
|
|
|
|
|
|
2019-04-10 23:37:15 +02:00
|
|
|
network_daemons = {
|
|
|
|
'regtest': BitcoinD,
|
|
|
|
'liquid-regtest': ElementsD,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
@pytest.fixture
|
2019-09-05 17:15:56 +02:00
|
|
|
def bitcoind(directory, teardown_checks):
|
2019-04-10 23:37:15 +02:00
|
|
|
chaind = network_daemons[config.get('TEST_NETWORK', 'regtest')]
|
|
|
|
bitcoind = chaind(bitcoin_dir=directory)
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
try:
|
|
|
|
bitcoind.start()
|
|
|
|
except Exception:
|
|
|
|
bitcoind.stop()
|
|
|
|
raise
|
|
|
|
|
|
|
|
info = bitcoind.rpc.getnetworkinfo()
|
|
|
|
|
|
|
|
if info['version'] < 160000:
|
|
|
|
bitcoind.rpc.stop()
|
|
|
|
raise ValueError("bitcoind is too old. At least version 16000 (v0.16.0)"
|
|
|
|
" is needed, current version is {}".format(info['version']))
|
|
|
|
|
|
|
|
info = bitcoind.rpc.getblockchaininfo()
|
|
|
|
# Make sure we have some spendable funds
|
|
|
|
if info['blocks'] < 101:
|
|
|
|
bitcoind.generate_block(101 - info['blocks'])
|
|
|
|
elif bitcoind.rpc.getwalletinfo()['balance'] < 1:
|
|
|
|
logging.debug("Insufficient balance, generating 1 block")
|
|
|
|
bitcoind.generate_block(1)
|
|
|
|
|
|
|
|
yield bitcoind
|
|
|
|
|
|
|
|
try:
|
2019-01-25 18:29:54 +01:00
|
|
|
bitcoind.stop()
|
2018-05-04 11:59:21 +02:00
|
|
|
except Exception:
|
|
|
|
bitcoind.proc.kill()
|
|
|
|
bitcoind.proc.wait()
|
|
|
|
|
|
|
|
|
2019-09-05 17:15:56 +02:00
|
|
|
class TeardownErrors(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.errors = []
|
|
|
|
self.node_errors = []
|
|
|
|
|
|
|
|
def add_error(self, msg):
|
|
|
|
self.errors.append(msg)
|
|
|
|
|
|
|
|
def add_node_error(self, node, msg):
|
|
|
|
self.node_errors.append((node.daemon.prefix, msg))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
node_errors = [" - {}: {}".format(*e) for e in self.node_errors]
|
|
|
|
errors = [" - {}".format(e) for e in self.errors]
|
|
|
|
|
|
|
|
errors = ["\nNode errors:"] + node_errors + ["Global errors:"] + errors
|
|
|
|
return "\n".join(errors)
|
|
|
|
|
|
|
|
def has_errors(self):
|
|
|
|
return len(self.errors) > 0 or len(self.node_errors) > 0
|
|
|
|
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
@pytest.fixture
|
2019-09-05 17:15:56 +02:00
|
|
|
def teardown_checks(request):
|
|
|
|
"""A simple fixture to collect errors during teardown.
|
|
|
|
|
|
|
|
We need to collect the errors and raise them as the very last step in the
|
|
|
|
fixture tree, otherwise some fixtures may not be cleaned up
|
|
|
|
correctly. Require this fixture in all other fixtures that need to either
|
|
|
|
cleanup before reporting an error or want to add an error that is to be
|
|
|
|
reported.
|
|
|
|
|
|
|
|
"""
|
|
|
|
errors = TeardownErrors()
|
|
|
|
yield errors
|
|
|
|
|
|
|
|
if errors.has_errors():
|
|
|
|
# Format a nice list of everything that went wrong and raise an exception
|
|
|
|
request.node.has_errors = True
|
|
|
|
raise ValueError(str(errors))
|
|
|
|
|
2019-09-07 06:52:03 +02:00
|
|
|
|
2019-09-05 17:15:56 +02:00
|
|
|
@pytest.fixture
|
2019-08-30 18:59:53 +02:00
|
|
|
def node_factory(request, directory, test_name, bitcoind, executor, db_provider, teardown_checks):
|
2019-09-05 17:15:56 +02:00
|
|
|
nf = NodeFactory(
|
|
|
|
test_name,
|
|
|
|
bitcoind,
|
|
|
|
executor,
|
|
|
|
directory=directory,
|
2019-08-30 18:59:53 +02:00
|
|
|
db_provider=db_provider,
|
2019-09-05 17:15:56 +02:00
|
|
|
)
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
yield nf
|
2019-09-04 19:11:51 +02:00
|
|
|
ok, errs = nf.killall([not n.may_fail for n in nf.nodes])
|
2018-09-11 22:31:31 +02:00
|
|
|
|
2019-09-05 17:15:56 +02:00
|
|
|
for e in errs:
|
|
|
|
teardown_checks.add_error(e)
|
2018-09-11 22:31:31 +02:00
|
|
|
|
2019-09-05 17:59:36 +02:00
|
|
|
def map_node_error(nodes, f, msg):
|
|
|
|
for n in nodes:
|
|
|
|
if n and f(n):
|
|
|
|
teardown_checks.add_node_error(n, msg)
|
|
|
|
|
|
|
|
map_node_error(nf.nodes, printValgrindErrors, "reported valgrind errors")
|
|
|
|
map_node_error(nf.nodes, printCrashLog, "had crash.log files")
|
|
|
|
map_node_error(nf.nodes, lambda n: not n.allow_broken_log and n.daemon.is_in_log(r'\*\*BROKEN\*\*'), "had BROKEN messages")
|
|
|
|
map_node_error(nf.nodes, checkReconnect, "had unexpected reconnections")
|
|
|
|
map_node_error(nf.nodes, checkBadGossip, "had bad gossip messages")
|
|
|
|
map_node_error(nf.nodes, lambda n: n.daemon.is_in_log('Bad reestablish'), "had bad reestablish")
|
|
|
|
map_node_error(nf.nodes, lambda n: n.daemon.is_in_log('bad hsm request'), "had bad hsm requests")
|
2019-10-20 23:34:12 +02:00
|
|
|
map_node_error(nf.nodes, lambda n: n.daemon.is_in_log(r'Accessing a null column'), "Accessing a null column")
|
2019-09-05 17:59:36 +02:00
|
|
|
map_node_error(nf.nodes, checkMemleak, "had memleak messages")
|
2019-09-04 19:10:08 +02:00
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
if not ok:
|
2019-09-05 17:15:56 +02:00
|
|
|
teardown_checks.add_error("At least one lightning exited with unexpected non-zero return code")
|
2018-05-04 11:59:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getValgrindErrors(node):
|
|
|
|
for error_file in os.listdir(node.daemon.lightning_dir):
|
2018-10-04 20:51:49 +02:00
|
|
|
if not re.fullmatch(r"valgrind-errors.\d+", error_file):
|
2018-05-04 11:59:21 +02:00
|
|
|
continue
|
|
|
|
with open(os.path.join(node.daemon.lightning_dir, error_file), 'r') as f:
|
|
|
|
errors = f.read().strip()
|
|
|
|
if errors:
|
|
|
|
return errors, error_file
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
def printValgrindErrors(node):
|
|
|
|
errors, fname = getValgrindErrors(node)
|
|
|
|
if errors:
|
|
|
|
print("-" * 31, "Valgrind errors", "-" * 32)
|
|
|
|
print("Valgrind error file:", fname)
|
|
|
|
print(errors)
|
|
|
|
print("-" * 80)
|
|
|
|
return 1 if errors else 0
|
|
|
|
|
|
|
|
|
|
|
|
def getCrashLog(node):
|
|
|
|
if node.may_fail:
|
|
|
|
return None, None
|
|
|
|
try:
|
|
|
|
crashlog = os.path.join(node.daemon.lightning_dir, 'crash.log')
|
|
|
|
with open(crashlog, 'r') as f:
|
|
|
|
return f.readlines(), crashlog
|
|
|
|
except Exception:
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
def printCrashLog(node):
|
|
|
|
errors, fname = getCrashLog(node)
|
|
|
|
if errors:
|
|
|
|
print("-" * 10, "{} (last 50 lines)".format(fname), "-" * 10)
|
2018-08-06 21:52:38 +02:00
|
|
|
print("".join(errors[-50:]))
|
2018-05-04 11:59:21 +02:00
|
|
|
print("-" * 80)
|
|
|
|
return 1 if errors else 0
|
|
|
|
|
|
|
|
|
|
|
|
def checkReconnect(node):
|
|
|
|
# Without DEVELOPER, we can't suppress reconnection.
|
|
|
|
if node.may_reconnect or not DEVELOPER:
|
|
|
|
return 0
|
|
|
|
if node.daemon.is_in_log('Peer has reconnected'):
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2019-05-28 23:10:30 +02:00
|
|
|
def checkBadGossip(node):
|
2019-09-05 17:59:36 +02:00
|
|
|
if node.allow_bad_gossip:
|
|
|
|
return 0
|
2019-05-28 23:10:30 +02:00
|
|
|
# We can get bad gossip order from inside error msgs.
|
|
|
|
if node.daemon.is_in_log('Bad gossip order from (?!error)'):
|
|
|
|
# This can happen if a node sees a node_announce after a channel
|
|
|
|
# is deleted, however.
|
|
|
|
if node.daemon.is_in_log('Deleting channel'):
|
|
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# Other 'Bad' messages shouldn't happen.
|
|
|
|
if node.daemon.is_in_log(r'gossipd.*Bad (?!gossip order from error)'):
|
2018-05-17 07:09:59 +02:00
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2019-06-30 02:42:44 +02:00
|
|
|
def checkBroken(node):
|
2019-09-05 17:59:36 +02:00
|
|
|
if node.allow_broken_log:
|
|
|
|
return 0
|
2019-06-30 02:42:44 +02:00
|
|
|
# We can get bad gossip order from inside error msgs.
|
|
|
|
if node.daemon.is_in_log(r'\*\*BROKEN\*\*'):
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-08-17 06:14:38 +02:00
|
|
|
def checkBadReestablish(node):
|
|
|
|
if node.daemon.is_in_log('Bad reestablish'):
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-09-20 05:06:42 +02:00
|
|
|
def checkBadHSMRequest(node):
|
|
|
|
if node.daemon.is_in_log('bad hsm request'):
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
def checkMemleak(node):
|
|
|
|
if node.daemon.is_in_log('MEMLEAK:'):
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2019-08-30 18:59:53 +02:00
|
|
|
# Mapping from TEST_DB_PROVIDER env variable to class to be used
|
|
|
|
providers = {
|
|
|
|
'sqlite3': SqliteDbProvider,
|
|
|
|
'postgres': PostgresDbProvider,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def db_provider(test_base_dir):
|
|
|
|
provider = providers[os.getenv('TEST_DB_PROVIDER', 'sqlite3')](test_base_dir)
|
|
|
|
provider.start()
|
|
|
|
yield provider
|
|
|
|
provider.stop()
|
|
|
|
|
|
|
|
|
2018-05-04 11:59:21 +02:00
|
|
|
@pytest.fixture
|
2019-09-05 17:59:36 +02:00
|
|
|
def executor(teardown_checks):
|
2018-05-04 11:59:21 +02:00
|
|
|
ex = futures.ThreadPoolExecutor(max_workers=20)
|
|
|
|
yield ex
|
|
|
|
ex.shutdown(wait=False)
|
2019-05-06 20:50:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def chainparams():
|
|
|
|
chainparams = {
|
|
|
|
'regtest': {
|
|
|
|
"bip173_prefix": "bcrt",
|
|
|
|
"elements": False,
|
|
|
|
"name": "regtest",
|
|
|
|
"p2sh_prefix": '2',
|
|
|
|
"elements": False,
|
2019-06-29 18:54:39 +02:00
|
|
|
"example_addr": "bcrt1qeyyk6sl5pr49ycpqyckvmttus5ttj25pd0zpvg",
|
|
|
|
"feeoutput": False,
|
2019-05-06 20:50:11 +02:00
|
|
|
},
|
|
|
|
'liquid-regtest': {
|
|
|
|
"bip173_prefix": "ert",
|
|
|
|
"elements": True,
|
|
|
|
"name": "liquid-regtest",
|
|
|
|
"p2sh_prefix": 'X',
|
|
|
|
"elements": True,
|
2019-06-29 18:54:39 +02:00
|
|
|
"example_addr": "ert1qq8adjz4u6enf0cjey9j8yt0y490tact9fahkwf",
|
|
|
|
"feeoutput": True,
|
2019-05-06 20:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chainparams[config['TEST_NETWORK']]
|