From f6a016c17d4f94919565405942690e6cc6667f33 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 27 Oct 2019 15:26:46 +0100 Subject: [PATCH] pytest: Consolidate access to environment variables We use `env()` to look up configuration variables and allow them to be overridden by the environment. --- tests/fixtures.py | 23 +++++----------------- tests/utils.py | 49 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 2122c75b4..e9acfeb35 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,29 +1,16 @@ from concurrent import futures 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 os import pytest import re import shutil -import sys 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 # give each attempt its own numbered directory, and avoid clashes. __attempts = {} @@ -31,7 +18,7 @@ __attempts = {} @pytest.fixture(scope="session") def test_base_dir(): - d = os.getenv("TEST_DIR", "/tmp") + d = env("TEST_DIR", "/tmp") directory = tempfile.mkdtemp(prefix='ltests-', dir=d) print("Running tests in {}".format(directory)) @@ -83,7 +70,7 @@ network_daemons = { @pytest.fixture 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) try: @@ -332,4 +319,4 @@ def chainparams(): } } - return chainparams[config['TEST_NETWORK']] + return chainparams[env('TEST_NETWORK', 'regtest')] diff --git a/tests/utils.py b/tests/utils.py index 35f17734d..e10b9662e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -19,6 +19,7 @@ import struct import subprocess import threading import time +import sys BITCOIND_CONFIG = { "regtest": 1, @@ -36,22 +37,42 @@ LIGHTNINGD_CONFIG = OrderedDict({ '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" -EXPERIMENTAL_FEATURES = os.getenv("EXPERIMENTAL_FEATURES", config['EXPERIMENTAL_FEATURES']) == "1" +def env(name, default=None): + """Access to environment variables -# Gossip can be slow without DEVELOPER. -if DEVELOPER: - DEFAULT_TIMEOUT = 60 -else: - DEFAULT_TIMEOUT = 180 + Allows access to environment variables, falling back to config.vars (part + of c-lightning's `./configure` output), and finally falling back to a + default value. -TIMEOUT = int(os.getenv("TIMEOUT", str(DEFAULT_TIMEOUT))) -VALGRIND = os.getenv("VALGRIND", config['VALGRIND']) == "1" -SLOW_MACHINE = os.getenv("SLOW_MACHINE", "0") == "1" -COMPAT = os.getenv("COMPAT", config['COMPAT']) == "1" + """ + fname = 'config.vars' + if os.path.exists(fname): + 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): @@ -459,7 +480,7 @@ class LightningD(TailableProc): 'lightning-dir': lightning_dir, 'addr': '127.0.0.1:{}'.format(port), 'allow-deprecated-apis': 'false', - 'network': config.get('TEST_NETWORK', 'regtest'), + 'network': env('TEST_NETWORK', 'regtest'), 'ignore-fee-limits': 'false', 'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'], 'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'],