Browse Source

pytest: Consolidate access to environment variables

We use `env()` to look up configuration variables and allow them to be
overridden by the environment.
travis-debug
Christian Decker 5 years ago
parent
commit
f6a016c17d
  1. 23
      tests/fixtures.py
  2. 49
      tests/utils.py

23
tests/fixtures.py

@ -1,29 +1,16 @@
from concurrent import futures from concurrent import futures
from db import SqliteDbProvider, PostgresDbProvider from db import SqliteDbProvider, PostgresDbProvider
from utils import NodeFactory, BitcoinD, ElementsD from utils import NodeFactory, BitcoinD, ElementsD, env
from utils import DEVELOPER, TEST_NETWORK # noqa: F401,F403
import logging import logging
import os import os
import pytest import pytest
import re import re
import shutil import shutil
import sys
import tempfile import tempfile
with open('config.vars') as configfile:
config = dict([(line.rstrip().split('=', 1)) for line in configfile])
VALGRIND = os.getenv("VALGRIND", config['VALGRIND']) == "1"
TEST_NETWORK = os.getenv("TEST_NETWORK", config['TEST_NETWORK'])
DEVELOPER = os.getenv("DEVELOPER", config['DEVELOPER']) == "1"
TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1"
if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
# A dict in which we count how often a particular test has run so far. Used to # 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. # give each attempt its own numbered directory, and avoid clashes.
__attempts = {} __attempts = {}
@ -31,7 +18,7 @@ __attempts = {}
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def test_base_dir(): def test_base_dir():
d = os.getenv("TEST_DIR", "/tmp") d = env("TEST_DIR", "/tmp")
directory = tempfile.mkdtemp(prefix='ltests-', dir=d) directory = tempfile.mkdtemp(prefix='ltests-', dir=d)
print("Running tests in {}".format(directory)) print("Running tests in {}".format(directory))
@ -83,7 +70,7 @@ network_daemons = {
@pytest.fixture @pytest.fixture
def bitcoind(directory, teardown_checks): def bitcoind(directory, teardown_checks):
chaind = network_daemons[config.get('TEST_NETWORK', 'regtest')] chaind = network_daemons[env('TEST_NETWORK', 'regtest')]
bitcoind = chaind(bitcoin_dir=directory) bitcoind = chaind(bitcoin_dir=directory)
try: try:
@ -332,4 +319,4 @@ def chainparams():
} }
} }
return chainparams[config['TEST_NETWORK']] return chainparams[env('TEST_NETWORK', 'regtest')]

49
tests/utils.py

@ -19,6 +19,7 @@ import struct
import subprocess import subprocess
import threading import threading
import time import time
import sys
BITCOIND_CONFIG = { BITCOIND_CONFIG = {
"regtest": 1, "regtest": 1,
@ -36,22 +37,42 @@ LIGHTNINGD_CONFIG = OrderedDict({
'disable-dns': None, 'disable-dns': None,
}) })
with open('config.vars') as configfile:
config = dict([(line.rstrip().split('=', 1)) for line in configfile])
DEVELOPER = os.getenv("DEVELOPER", config['DEVELOPER']) == "1" def env(name, default=None):
EXPERIMENTAL_FEATURES = os.getenv("EXPERIMENTAL_FEATURES", config['EXPERIMENTAL_FEATURES']) == "1" """Access to environment variables
# Gossip can be slow without DEVELOPER. Allows access to environment variables, falling back to config.vars (part
if DEVELOPER: of c-lightning's `./configure` output), and finally falling back to a
DEFAULT_TIMEOUT = 60 default value.
else:
DEFAULT_TIMEOUT = 180
TIMEOUT = int(os.getenv("TIMEOUT", str(DEFAULT_TIMEOUT))) """
VALGRIND = os.getenv("VALGRIND", config['VALGRIND']) == "1" fname = 'config.vars'
SLOW_MACHINE = os.getenv("SLOW_MACHINE", "0") == "1" if os.path.exists(fname):
COMPAT = os.getenv("COMPAT", config['COMPAT']) == "1" lines = open(fname, 'r').readlines()
config = dict([(line.rstrip().split('=', 1)) for line in lines])
else:
config = {}
if name in os.environ:
return os.environ[name]
elif name in config:
return config[name]
else:
return default
VALGRIND = env("VALGRIND") == "1"
TEST_NETWORK = env("TEST_NETWORK", 'regtest')
DEVELOPER = env("DEVELOPER", "1") == "1"
TEST_DEBUG = env("TEST_DEBUG", "0") == "1"
SLOW_MACHINE = env("SLOW_MACHINE", "0") == "1"
COMPAT = env("COMPAT", "1") == "1"
EXPERIMENTAL_FEATURES = os.getenv("EXPERIMENTAL_FEATURES", "0") == "1"
TIMEOUT = int(env("TIMEOUT", 180 if SLOW_MACHINE else 60))
if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
def wait_for(success, timeout=TIMEOUT): def wait_for(success, timeout=TIMEOUT):
@ -459,7 +480,7 @@ class LightningD(TailableProc):
'lightning-dir': lightning_dir, 'lightning-dir': lightning_dir,
'addr': '127.0.0.1:{}'.format(port), 'addr': '127.0.0.1:{}'.format(port),
'allow-deprecated-apis': 'false', 'allow-deprecated-apis': 'false',
'network': config.get('TEST_NETWORK', 'regtest'), 'network': env('TEST_NETWORK', 'regtest'),
'ignore-fee-limits': 'false', 'ignore-fee-limits': 'false',
'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'], 'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'],
'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'], 'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'],

Loading…
Cancel
Save