|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 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 | + |
| 6 | +from test_framework.test_framework import BitcoinTestFramework |
| 7 | + |
| 8 | +class TestShell: |
| 9 | + """Wrapper Class for BitcoinTestFramework. |
| 10 | +
|
| 11 | + The TestShell class extends the BitcoinTestFramework |
| 12 | + rpc & daemon process management functionality to external |
| 13 | + python environments. |
| 14 | +
|
| 15 | + It is a singleton class, which ensures that users only |
| 16 | + start a single TestShell at a time.""" |
| 17 | + |
| 18 | + class __TestShell(BitcoinTestFramework): |
| 19 | + def set_test_params(self): |
| 20 | + pass |
| 21 | + |
| 22 | + def run_test(self): |
| 23 | + pass |
| 24 | + |
| 25 | + def setup(self, **kwargs): |
| 26 | + if self.running: |
| 27 | + print("TestShell is already running!") |
| 28 | + return |
| 29 | + |
| 30 | + # Num_nodes parameter must be set |
| 31 | + # by BitcoinTestFramework child class. |
| 32 | + self.num_nodes = kwargs.get('num_nodes', 1) |
| 33 | + kwargs.pop('num_nodes', None) |
| 34 | + |
| 35 | + # User parameters override default values. |
| 36 | + for key, value in kwargs.items(): |
| 37 | + if hasattr(self, key): |
| 38 | + setattr(self, key, value) |
| 39 | + elif hasattr(self.options, key): |
| 40 | + setattr(self.options, key, value) |
| 41 | + else: |
| 42 | + raise KeyError(key + " not a valid parameter key!") |
| 43 | + |
| 44 | + super().setup() |
| 45 | + self.running = True |
| 46 | + |
| 47 | + def shutdown(self): |
| 48 | + if not self.running: |
| 49 | + print("TestShell is not running!") |
| 50 | + else: |
| 51 | + super().shutdown() |
| 52 | + self.running = False |
| 53 | + |
| 54 | + def reset(self): |
| 55 | + if self.running: |
| 56 | + print("Shutdown TestWrapper before resetting!") |
| 57 | + else: |
| 58 | + self.num_nodes = None |
| 59 | + super().__init__() |
| 60 | + |
| 61 | + instance = None |
| 62 | + |
| 63 | + def __new__(cls): |
| 64 | + # This implementation enforces singleton pattern, and will return the |
| 65 | + # previously initialized instance if available |
| 66 | + if not TestShell.instance: |
| 67 | + TestShell.instance = TestShell.__TestShell() |
| 68 | + TestShell.instance.running = False |
| 69 | + return TestShell.instance |
| 70 | + |
| 71 | + def __getattr__(self, name): |
| 72 | + return getattr(self.instance, name) |
| 73 | + |
| 74 | + def __setattr__(self, name, value): |
| 75 | + return setattr(self.instance, name, value) |
0 commit comments