test_runner.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import time
  2. import logging
  3. from utils import WrapLogger
  4. from utils import ShellUtils
  5. class TestRunner:
  6. iAmTestRunnerClass = True
  7. def __init__(self, test, target, logger):
  8. self.test = test
  9. self.target = target
  10. self.logger = logger
  11. # Create convenience variables to decouple the
  12. # setup.py scripts from the internals of TFB
  13. self.benchmarker = test.benchmarker
  14. self.database_host = self.benchmarker.database_host
  15. self.dir = test.directory
  16. (out, err) = WrapLogger(logger, logging.INFO), WrapLogger(logger, logging.ERROR)
  17. self.utils = ShellUtils(self.dir, None, None, logger)
  18. def start(self):
  19. raise NotImplementedError()
  20. def stop(self):
  21. raise NotImplementedError()
  22. def sh(self, *args, **kwargs):
  23. self.utils.sh(*args, **kwargs)
  24. def sh_async(self, *args, **kwargs):
  25. return self.utils.sh_async(*args, **kwargs)
  26. def sh_pkill(self, *args, **kwargs):
  27. self.logger.debug("Got %s and %s" % (args, kwargs))
  28. self.utils.sh_pkill(*args, **kwargs)
  29. @staticmethod
  30. def is_parent_of(target_class):
  31. ''' Checks if provided class object is a subclass of TestRunner '''
  32. try:
  33. # issubclass will not work, as setup_module is loaded in different
  34. # global context and therefore has a different copy of the module
  35. # test_runner. A cheap trick is just to check for this attribute
  36. str(target_class.iAmTestRunnerClass)
  37. # target_class seems to be an instance of TestRunner. Before returning,
  38. # ensure they have not created an __init__ method, as they cannot
  39. # call super.__init__(), and therefore the subclass is doomed to error
  40. try:
  41. target_class()
  42. raise Exception("Subclasses of TestRunner should not define __init__")
  43. except TypeError:
  44. # Good, there is no init method defined
  45. return True
  46. except AttributeError:
  47. return False