diff --git a/lib/vsc/utils/script_tools.py b/lib/vsc/utils/script_tools.py index 13cc701..c7fade5 100644 --- a/lib/vsc/utils/script_tools.py +++ b/lib/vsc/utils/script_tools.py @@ -307,15 +307,15 @@ def __init__(self, name=None): if isinstance(self, HAMixin): argparser = populate_config_parser(argparser, self.__class__.HA_MIXIN_OPTIONS) + if isinstance(self, LogMixin): + argparser = populate_config_parser(argparser, self.__class__.LOG_MIXIN_OPTIONS) + if isinstance(self, TimestampMixin): argparser = populate_config_parser(argparser, self.__class__.TIMESTAMP_MIXIN_OPTIONS) if isinstance(self, LockMixin): argparser = populate_config_parser(argparser, self.__class__.LOCK_MIXIN_OPTIONS) - if isinstance(self, LogMixin): - argparser = populate_config_parser(argparser, self.__class__.LOG_MIXIN_OPTIONS) - if isinstance(self, NagiosStatusMixin): argparser = populate_config_parser(argparser, self.__class__.NAGIOS_MIXIN_OPTIONS) @@ -323,7 +323,6 @@ def __init__(self, name=None): self.options = argparser.parse_args() - def critical(self, msg): if isinstance(self, NagiosStatusMixin): self.nagios_epilogue(NAGIOS_CRITICAL, msg) @@ -361,7 +360,7 @@ def main(self): #errors = [] msg = self.name - if self.options.dry_run: + if msg and self.options.dry_run: msg += " (dry-run)" logging.info("%s started.", msg) @@ -412,6 +411,9 @@ def main(self): if isinstance(self, NagiosStatusMixin): self.nagios_epilogue() + if isinstance(self, LogMixin): + self.log_epilogue() + class FullCLIBase(HAMixin, LockMixin, TimestampMixin, LogMixin, NagiosStatusMixin, CLIBase): """ @@ -438,6 +440,12 @@ def _merge_options(options): return opts +class CLI(FullCLIBase): + + def __init__(self, name=None, default_options=None): # pylint: disable=unused-argument + super().__init__(name) + + @deprecated_class("Base your scripts on the CLIBase class instead") class ExtendedSimpleOption(SimpleOption): """ @@ -564,11 +572,6 @@ def critical_exception_handler(self, tp, value, traceback): self.critical(message) -class CLI(FullCLIBase): - - def __init__(self, name=None, default_options=None): # pylint: disable=unused-argument - super().__init__(name) - @deprecated_class("Base your scripts on the CLIBase class instead") class OldCLI: """ diff --git a/test/script_tools.py b/test/script_tools.py index c930e86..2120908 100644 --- a/test/script_tools.py +++ b/test/script_tools.py @@ -44,9 +44,7 @@ from vsc.utils.nagios import NAGIOS_EXIT_WARNING, NagiosStatusMixin from vsc.utils.script_tools import ( ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI, OldCLI, - CLIBase, LockMixin, HAMixin, TimestampMixin) - -from lib.vsc.utils.script_tools import LogMixin + CLIBase, LockMixin, HAMixin, TimestampMixin, LogMixin) class TestExtendedSimpleOption(TestCase): @@ -120,7 +118,7 @@ def do(self, _): return magic.go() class TestOldCLI(TestCase): - """Tests for the CLI base class""" + """Tests for the OldCLI base class""" @mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue') def test_opts(self, _): @@ -178,7 +176,7 @@ def test_exit(self, locklock, releaselock): class TestNrpeCLI(TestCase): - """Tests for the CLI base class""" + """Tests for the NrpeCLI base class""" def setUp(self): super().setUp() @@ -190,7 +188,7 @@ class MyNrpeCLI(NrpeCLI): 'magic': ('some magic', None, 'store', 'magicdef'), } - def do(self,dryrun): + def do(self, dry_run): return magic.go() self.cli = MyNrpeCLI(name="abc") @@ -263,16 +261,15 @@ def do(self, dry_run): self.ms = MyCLI(name="abc") - class SomeCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase): - CLI_OPTIONS = { - 'magic': ('magicdef', None, 'store', 'magicdef'), - } - - self.some_ms = SomeCLI(name="abc") - @mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue') def test_opts(self, _): + self.assertTrue(isinstance(self.ms, LogMixin)) + self.assertTrue(isinstance(self.ms, HAMixin)) + self.assertTrue(isinstance(self.ms, LockMixin)) + self.assertTrue(isinstance(self.ms, NagiosStatusMixin)) + self.assertTrue(isinstance(self.ms, TimestampMixin)) + logging.debug("options %s %s %s", self.ms.options, dir(self.ms.options), vars(self.ms.options)) extsimpopts = { @@ -301,32 +298,74 @@ def test_opts(self, _): myopts.update(extsimpopts) self.assertEqual(self.ms.options.__dict__, myopts) + @mock.patch('vsc.utils.script_tools.lock_or_bork') + @mock.patch('vsc.utils.script_tools.release_or_bork') + def test_exit(self, locklock, releaselock): + + fake_exit = mock.MagicMock() + with mock.patch('vsc.utils.script_tools.sys.exit', fake_exit): + self.ms.warning("be warned") + fake_exit.assert_called_with(1) + + +class TestBaseNoTimestamp(TestCase): + + def setUp(self): + super().setUp() + + sys.argv = ["abc"] + + class NoTimeStampCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase): + CLI_OPTIONS = { + 'magic': ('magicdef', None, 'store', 'magicdef'), + } + def do(self, dry_run): + return magic.go() + + self.ms = NoTimeStampCLI(name="abc") + + if isinstance(self.ms, LogMixin): + logging.warning("LogMixin is part of this instance") + else: + logging.warning("LogMixin is not part of this instance") + + + if isinstance(self.ms, TimestampMixin): + logging.warning("TimestampMixin is part of this instance") + else: + logging.warning("TimestampMixin is not part of this instance") + def test_without_timestamp_mixin(self): + self.assertTrue(isinstance(self.ms, LogMixin)) + self.assertTrue(isinstance(self.ms, HAMixin)) + self.assertTrue(isinstance(self.ms, LockMixin)) + self.assertTrue(isinstance(self.ms, NagiosStatusMixin)) + extsimpopts = { 'configfiles': None, - #'debug': False, + 'debug': False, 'disable_locking': False, 'dry_run': False, 'ha': None, 'help': None, 'ignoreconfigfiles': None, - #'info': False, + 'info': False, 'locking_filename': '/var/lock/setup.lock', 'nagios_check_filename': '/var/cache/setup.nagios.json.gz', 'nagios_check_interval_threshold': 0, 'nagios_report': False, 'nagios_user': 'nrpe', 'nagios_world_readable_check': False, - #'quiet': False, + 'quiet': False, } myopts = { 'magic': 'magicdef', } myopts.update(extsimpopts) - logging.debug("options wo default sync options %s", self.some_ms.options) - self.assertEqual(self.some_ms.options.__dict__, myopts) + logging.warning("options wo default sync options %s", self.ms.options) + self.assertEqual(self.ms.options.__dict__, myopts) @mock.patch('vsc.utils.script_tools.lock_or_bork') @mock.patch('vsc.utils.script_tools.release_or_bork')