test_shell.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2019-2022 The Limenka developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  import pathlib
   6  
   7  from test_framework.test_framework import LimenkaTestFramework
   8  
   9  
  10  class TestShell:
  11      """Wrapper Class for LimenkaTestFramework.
  12  
  13      The TestShell class extends the LimenkaTestFramework
  14      rpc & daemon process management functionality to external
  15      python environments.
  16  
  17      It is a singleton class, which ensures that users only
  18      start a single TestShell at a time."""
  19  
  20      class __TestShell(LimenkaTestFramework):
  21          def add_options(self, parser):
  22              self.add_wallet_options(parser)
  23  
  24          def set_test_params(self):
  25              pass
  26  
  27          def run_test(self):
  28              pass
  29  
  30          def setup(self, **kwargs):
  31              if self.running:
  32                  print("TestShell is already running!")
  33                  return
  34  
  35              # Num_nodes parameter must be set
  36              # by LimenkaTestFramework child class.
  37              self.num_nodes = 1
  38  
  39              # User parameters override default values.
  40              for key, value in kwargs.items():
  41                  if hasattr(self, key):
  42                      setattr(self, key, value)
  43                  elif hasattr(self.options, key):
  44                      setattr(self.options, key, value)
  45                  else:
  46                      raise KeyError(key + " not a valid parameter key!")
  47  
  48              super().setup()
  49              self.running = True
  50              return self
  51  
  52          def shutdown(self):
  53              if not self.running:
  54                  print("TestShell is not running!")
  55              else:
  56                  super().shutdown()
  57                  self.running = False
  58  
  59          def reset(self):
  60              if self.running:
  61                  print("Shutdown TestShell before resetting!")
  62              else:
  63                  self.num_nodes = None
  64                  dummy_testshell_file = pathlib.Path(__file__).absolute().parent.parent / "testshell_dummy.py"
  65                  super().__init__(dummy_testshell_file)
  66  
  67      instance = None
  68  
  69      def __new__(cls):
  70          # This implementation enforces singleton pattern, and will return the
  71          # previously initialized instance if available
  72          if not TestShell.instance:
  73              # LimenkaTestFramework instances are supposed to be constructed with the path
  74              # of the calling test in order to find shared data like configuration and the
  75              # cache. Since TestShell is meant for interactive use, there is no concrete
  76              # test; passing a dummy name is fine though, as only the containing directory
  77              # is relevant for successful initialization.
  78              dummy_testshell_file = pathlib.Path(__file__).absolute().parent.parent / "testshell_dummy.py"
  79              TestShell.instance = TestShell.__TestShell(dummy_testshell_file)
  80              TestShell.instance.running = False
  81          return TestShell.instance
  82  
  83      def __getattr__(self, name):
  84          return getattr(self.instance, name)
  85  
  86      def __setattr__(self, name, value):
  87          return setattr(self.instance, name, value)
  88