|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2019 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +from test_framework.test_framework import BitcoinTestFramework |
| 6 | +import argparse |
| 7 | +from os.path import abspath, join |
| 8 | +from os import getpid |
| 9 | + |
| 10 | +"""Wrapper Class for BitcoinTestFramework. |
| 11 | +
|
| 12 | +Provides the BitcoinTestFramework rpc & daemon process management |
| 13 | +functionality to external python projects. |
| 14 | +
|
| 15 | +The TestWrapper class is a singleton class, which ensures that users only ever |
| 16 | +instantiate a single TestWrapper.""" |
| 17 | + |
| 18 | +class TestWrapper: |
| 19 | + |
| 20 | + class __TestWrapper(BitcoinTestFramework): |
| 21 | + |
| 22 | + def set_test_params(self): |
| 23 | + # This can be overriden in setup() parameter. |
| 24 | + self.num_nodes=3 |
| 25 | + |
| 26 | + def run_test(self): |
| 27 | + pass |
| 28 | + |
| 29 | + def setup(self, |
| 30 | + bitcoind=abspath(join(__file__ ,"../../../..") + "/src/bitcoind"), |
| 31 | + bitcoincli=None, |
| 32 | + setup_clean_chain=True, |
| 33 | + num_nodes=3, |
| 34 | + network_thread=None, |
| 35 | + rpc_timeout=60, |
| 36 | + supports_cli=False, |
| 37 | + bind_to_localhost_only=True, |
| 38 | + nocleanup=False, |
| 39 | + noshutdown=False, |
| 40 | + cachedir=abspath(join(__file__ ,"../../../..") + "/test/cache"), |
| 41 | + tmpdir=None, |
| 42 | + loglevel='INFO', |
| 43 | + trace_rpc=False, |
| 44 | + port_seed=getpid(), |
| 45 | + coveragedir=None, |
| 46 | + configfile=abspath(join(__file__ ,"../../../..") + "/test/config.ini"), |
| 47 | + pdbonfailure=False, |
| 48 | + usecli = False, |
| 49 | + perf = False, |
| 50 | + randomseed = None): |
| 51 | + |
| 52 | + |
| 53 | + if self.running: |
| 54 | + print("TestWrapper is already running!") |
| 55 | + return |
| 56 | + |
| 57 | + self.setup_clean_chain = setup_clean_chain |
| 58 | + self.num_nodes = num_nodes |
| 59 | + self.network_thread = network_thread |
| 60 | + self.rpc_timeout = rpc_timeout |
| 61 | + self.supports_cli = supports_cli |
| 62 | + self.bind_to_localhost_only = bind_to_localhost_only |
| 63 | + |
| 64 | + self.options = argparse.Namespace |
| 65 | + self.options.nocleanup = nocleanup |
| 66 | + self.options.noshutdown = noshutdown |
| 67 | + self.options.cachedir = cachedir |
| 68 | + self.options.tmpdir = tmpdir |
| 69 | + self.options.loglevel = loglevel |
| 70 | + self.options.trace_rpc = trace_rpc |
| 71 | + self.options.port_seed = port_seed |
| 72 | + self.options.coveragedir = coveragedir |
| 73 | + self.options.configfile = configfile |
| 74 | + self.options.pdbonfailure = pdbonfailure |
| 75 | + self.options.usecli = usecli |
| 76 | + self.options.perf = perf |
| 77 | + self.options.randomseed = randomseed |
| 78 | + |
| 79 | + self.options.bitcoind = bitcoind |
| 80 | + self.options.bitcoincli = bitcoincli |
| 81 | + |
| 82 | + super().setup() |
| 83 | + self.running = True |
| 84 | + |
| 85 | + def shutdown(self): |
| 86 | + if not self.running: |
| 87 | + print("TestWrapper is not running!") |
| 88 | + else: |
| 89 | + super().shutdown() |
| 90 | + self.running = False |
| 91 | + |
| 92 | + instance = None |
| 93 | + |
| 94 | + def __new__(cls): |
| 95 | + if not TestWrapper.instance: |
| 96 | + TestWrapper.instance = TestWrapper.__TestWrapper() |
| 97 | + TestWrapper.instance.running = False |
| 98 | + return TestWrapper.instance |
| 99 | + |
| 100 | + def __getattr__(self, name): |
| 101 | + return getattr(self.instance, name) |
| 102 | + |
| 103 | + def __setattr__(self, name, value): |
| 104 | + return setattr(self.instance, name, value) |
0 commit comments