Skip to content

Commit 076ce72

Browse files
committed
test: clean up imports and add assertions
1 parent a19bf48 commit 076ce72

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

lib/vsc/utils/script_tools.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,22 @@ def __init__(self, name=None):
307307
if isinstance(self, HAMixin):
308308
argparser = populate_config_parser(argparser, self.__class__.HA_MIXIN_OPTIONS)
309309

310+
if isinstance(self, LogMixin):
311+
argparser = populate_config_parser(argparser, self.__class__.LOG_MIXIN_OPTIONS)
312+
310313
if isinstance(self, TimestampMixin):
311314
argparser = populate_config_parser(argparser, self.__class__.TIMESTAMP_MIXIN_OPTIONS)
312315

313316
if isinstance(self, LockMixin):
314317
argparser = populate_config_parser(argparser, self.__class__.LOCK_MIXIN_OPTIONS)
315318

316-
if isinstance(self, LogMixin):
317-
argparser = populate_config_parser(argparser, self.__class__.LOG_MIXIN_OPTIONS)
318-
319319
if isinstance(self, NagiosStatusMixin):
320320
argparser = populate_config_parser(argparser, self.__class__.NAGIOS_MIXIN_OPTIONS)
321321

322322
argparser = populate_config_parser(argparser, self.get_options())
323323

324324
self.options = argparser.parse_args()
325325

326-
327326
def critical(self, msg):
328327
if isinstance(self, NagiosStatusMixin):
329328
self.nagios_epilogue(NAGIOS_CRITICAL, msg)
@@ -361,7 +360,7 @@ def main(self):
361360
#errors = []
362361

363362
msg = self.name
364-
if self.options.dry_run:
363+
if msg and self.options.dry_run:
365364
msg += " (dry-run)"
366365
logging.info("%s started.", msg)
367366

@@ -412,6 +411,9 @@ def main(self):
412411
if isinstance(self, NagiosStatusMixin):
413412
self.nagios_epilogue()
414413

414+
if isinstance(self, LogMixin):
415+
self.log_epilogue()
416+
415417

416418
class FullCLIBase(HAMixin, LockMixin, TimestampMixin, LogMixin, NagiosStatusMixin, CLIBase):
417419
"""
@@ -438,6 +440,12 @@ def _merge_options(options):
438440
return opts
439441

440442

443+
class CLI(FullCLIBase):
444+
445+
def __init__(self, name=None, default_options=None): # pylint: disable=unused-argument
446+
super().__init__(name)
447+
448+
441449
@deprecated_class("Base your scripts on the CLIBase class instead")
442450
class ExtendedSimpleOption(SimpleOption):
443451
"""
@@ -564,11 +572,6 @@ def critical_exception_handler(self, tp, value, traceback):
564572
self.critical(message)
565573

566574

567-
class CLI(FullCLIBase):
568-
569-
def __init__(self, name=None, default_options=None): # pylint: disable=unused-argument
570-
super().__init__(name)
571-
572575
@deprecated_class("Base your scripts on the CLIBase class instead")
573576
class OldCLI:
574577
"""

test/script_tools.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
from vsc.utils.nagios import NAGIOS_EXIT_WARNING, NagiosStatusMixin
4545
from vsc.utils.script_tools import (
4646
ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI, OldCLI,
47-
CLIBase, LockMixin, HAMixin, TimestampMixin)
48-
49-
from lib.vsc.utils.script_tools import LogMixin
47+
CLIBase, LockMixin, HAMixin, TimestampMixin, LogMixin)
5048

5149

5250
class TestExtendedSimpleOption(TestCase):
@@ -120,7 +118,7 @@ def do(self, _):
120118
return magic.go()
121119

122120
class TestOldCLI(TestCase):
123-
"""Tests for the CLI base class"""
121+
"""Tests for the OldCLI base class"""
124122

125123
@mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue')
126124
def test_opts(self, _):
@@ -178,7 +176,7 @@ def test_exit(self, locklock, releaselock):
178176

179177

180178
class TestNrpeCLI(TestCase):
181-
"""Tests for the CLI base class"""
179+
"""Tests for the NrpeCLI base class"""
182180

183181
def setUp(self):
184182
super().setUp()
@@ -190,7 +188,7 @@ class MyNrpeCLI(NrpeCLI):
190188
'magic': ('some magic', None, 'store', 'magicdef'),
191189
}
192190

193-
def do(self,dryrun):
191+
def do(self, dry_run):
194192
return magic.go()
195193

196194
self.cli = MyNrpeCLI(name="abc")
@@ -263,16 +261,15 @@ def do(self, dry_run):
263261

264262
self.ms = MyCLI(name="abc")
265263

266-
class SomeCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase):
267-
CLI_OPTIONS = {
268-
'magic': ('magicdef', None, 'store', 'magicdef'),
269-
}
270-
271-
self.some_ms = SomeCLI(name="abc")
272-
273264
@mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue')
274265
def test_opts(self, _):
275266

267+
self.assertTrue(isinstance(self.ms, LogMixin))
268+
self.assertTrue(isinstance(self.ms, HAMixin))
269+
self.assertTrue(isinstance(self.ms, LockMixin))
270+
self.assertTrue(isinstance(self.ms, NagiosStatusMixin))
271+
self.assertTrue(isinstance(self.ms, TimestampMixin))
272+
276273
logging.debug("options %s %s %s", self.ms.options, dir(self.ms.options), vars(self.ms.options))
277274

278275
extsimpopts = {
@@ -301,32 +298,74 @@ def test_opts(self, _):
301298
myopts.update(extsimpopts)
302299
self.assertEqual(self.ms.options.__dict__, myopts)
303300

301+
@mock.patch('vsc.utils.script_tools.lock_or_bork')
302+
@mock.patch('vsc.utils.script_tools.release_or_bork')
303+
def test_exit(self, locklock, releaselock):
304+
305+
fake_exit = mock.MagicMock()
306+
with mock.patch('vsc.utils.script_tools.sys.exit', fake_exit):
307+
self.ms.warning("be warned")
308+
fake_exit.assert_called_with(1)
309+
310+
311+
class TestBaseNoTimestamp(TestCase):
312+
313+
def setUp(self):
314+
super().setUp()
315+
316+
sys.argv = ["abc"]
317+
318+
class NoTimeStampCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase):
319+
CLI_OPTIONS = {
320+
'magic': ('magicdef', None, 'store', 'magicdef'),
321+
}
322+
def do(self, dry_run):
323+
return magic.go()
324+
325+
self.ms = NoTimeStampCLI(name="abc")
326+
327+
if isinstance(self.ms, LogMixin):
328+
logging.warning("LogMixin is part of this instance")
329+
else:
330+
logging.warning("LogMixin is not part of this instance")
331+
332+
333+
if isinstance(self.ms, TimestampMixin):
334+
logging.warning("TimestampMixin is part of this instance")
335+
else:
336+
logging.warning("TimestampMixin is not part of this instance")
337+
304338
def test_without_timestamp_mixin(self):
305339

340+
self.assertTrue(isinstance(self.ms, LogMixin))
341+
self.assertTrue(isinstance(self.ms, HAMixin))
342+
self.assertTrue(isinstance(self.ms, LockMixin))
343+
self.assertTrue(isinstance(self.ms, NagiosStatusMixin))
344+
306345
extsimpopts = {
307346
'configfiles': None,
308-
#'debug': False,
347+
'debug': False,
309348
'disable_locking': False,
310349
'dry_run': False,
311350
'ha': None,
312351
'help': None,
313352
'ignoreconfigfiles': None,
314-
#'info': False,
353+
'info': False,
315354
'locking_filename': '/var/lock/setup.lock',
316355
'nagios_check_filename': '/var/cache/setup.nagios.json.gz',
317356
'nagios_check_interval_threshold': 0,
318357
'nagios_report': False,
319358
'nagios_user': 'nrpe',
320359
'nagios_world_readable_check': False,
321-
#'quiet': False,
360+
'quiet': False,
322361
}
323362

324363
myopts = {
325364
'magic': 'magicdef',
326365
}
327366
myopts.update(extsimpopts)
328-
logging.debug("options wo default sync options %s", self.some_ms.options)
329-
self.assertEqual(self.some_ms.options.__dict__, myopts)
367+
logging.warning("options wo default sync options %s", self.ms.options)
368+
self.assertEqual(self.ms.options.__dict__, myopts)
330369

331370
@mock.patch('vsc.utils.script_tools.lock_or_bork')
332371
@mock.patch('vsc.utils.script_tools.release_or_bork')

0 commit comments

Comments
 (0)