/ test / functional / test_framework / test_shell.py
test_shell.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2019-2022 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 add_options(self, parser):
20              self.add_wallet_options(parser)
21  
22          def set_test_params(self):
23              pass
24  
25          def run_test(self):
26              pass
27  
28          def setup(self, **kwargs):
29              if self.running:
30                  print("TestShell is already running!")
31                  return
32  
33              # Num_nodes parameter must be set
34              # by BitcoinTestFramework child class.
35              self.num_nodes = 1
36  
37              # User parameters override default values.
38              for key, value in kwargs.items():
39                  if hasattr(self, key):
40                      setattr(self, key, value)
41                  elif hasattr(self.options, key):
42                      setattr(self.options, key, value)
43                  else:
44                      raise KeyError(key + " not a valid parameter key!")
45  
46              super().setup()
47              self.running = True
48              return self
49  
50          def shutdown(self):
51              if not self.running:
52                  print("TestShell is not running!")
53              else:
54                  super().shutdown()
55                  self.running = False
56  
57          def reset(self):
58              if self.running:
59                  print("Shutdown TestShell before resetting!")
60              else:
61                  self.num_nodes = None
62                  super().__init__()
63  
64      instance = None
65  
66      def __new__(cls):
67          # This implementation enforces singleton pattern, and will return the
68          # previously initialized instance if available
69          if not TestShell.instance:
70              TestShell.instance = TestShell.__TestShell()
71              TestShell.instance.running = False
72          return TestShell.instance
73  
74      def __getattr__(self, name):
75          return getattr(self.instance, name)
76  
77      def __setattr__(self, name, value):
78          return setattr(self.instance, name, value)