Skip to content

Commit

Permalink
fix: restore former test and fix settings for new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
itkovian committed Jan 15, 2025
1 parent c4c7f32 commit a19bf48
Showing 1 changed file with 83 additions and 15 deletions.
98 changes: 83 additions & 15 deletions test/script_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from vsc.install.testing import TestCase
from vsc.utils.nagios import NAGIOS_EXIT_WARNING, NagiosStatusMixin
from vsc.utils.script_tools import (
ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI,
ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI, OldCLI,
CLIBase, LockMixin, HAMixin, TimestampMixin)

from lib.vsc.utils.script_tools import LogMixin
Expand Down Expand Up @@ -105,6 +105,76 @@ def test_threshold_custom_setting(self, mock_proceed, _, mock_lockfile):

magic = mock.MagicMock(name='magic')

class MyOldCLI(OldCLI):
TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
TESTFILE = tempfile.mkstemp()[1]
TESTFILE2 = tempfile.mkstemp()[1]

CLI_OPTIONS = {
'magic': ('some magic', None, 'store', 'magicdef'),
'nagios_check_filename': ('bla', None, 'store', TESTFILE),
'locking_filename': ('test', None, 'store', TESTFILE2),
'nagios_user': ('user nagios runs as', 'string', 'store', getpass.getuser()),
}
def do(self, _):
return magic.go()

class TestOldCLI(TestCase):
"""Tests for the CLI base class"""

@mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue')
def test_opts(self, _):
sys.argv = ['abc']
ms = MyOldCLI()

logging.debug("options %s %s %s", ms.options, dir(ms.options), vars(ms.options))



extsimpopts = {
'configfiles': None,
'debug': False,
'disable_locking': False,
'dry_run': False,
'ha': None,
'help': None,
'ignoreconfigfiles': None,
'info': False,
'locking_filename': ms.TESTFILE2,
'nagios_check_filename': ms.TESTFILE,
'nagios_check_interval_threshold': 0,
'nagios_report': False,
'nagios_user': getpass.getuser(),
'nagios_world_readable_check': False,
'quiet': False,
}

myopts = {
'magic': 'magicdef',
'start_timestamp': None,
'timestamp_file': '/var/cache/abc.timestamp',
}
myopts.update(extsimpopts)
self.assertEqual(ms.options.__dict__, myopts)

myopts = {
'magic': 'magicdef',
}
myopts.update(extsimpopts)
ms = MyOldCLI(default_options={})
logging.debug("options wo default sync options %s", ms.options)
self.assertEqual(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):

cli = MyOldCLI()

fake_exit = mock.MagicMock()
with mock.patch('sys.exit', fake_exit):
cli.warning("be warned")
fake_exit.assert_called_with(1)


class TestNrpeCLI(TestCase):
Expand Down Expand Up @@ -178,13 +248,13 @@ def setUp(self):

class MyCLI(CLI):
TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
TESTFILE = tempfile.mkstemp()[1]
TESTFILE2 = tempfile.mkstemp()[1]
LOCKING_TESTFILE = tempfile.mkstemp()[1]
NAGIOS_TESTFILE = tempfile.mkstemp()[1]

CLI_OPTIONS = {
'magic': ('some magic', None, 'store', 'magicdef'),
'nagios_check_filename': ('bla', None, 'store', TESTFILE),
'locking_filename': ('test', None, 'store', TESTFILE2),
'nagios_check_filename': ('bla', None, 'store', NAGIOS_TESTFILE),
'locking_filename': ('test', None, 'store', LOCKING_TESTFILE),
'nagios_user': ('user nagios runs as', 'str', 'store', getpass.getuser()),
}

Expand All @@ -197,8 +267,6 @@ class SomeCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase):
CLI_OPTIONS = {
'magic': ('magicdef', None, 'store', 'magicdef'),
}
LOCKING_TESTFILE = tempfile.mkstemp()[1]
NAGIOS_TESTFILE = tempfile.mkstemp()[1]

self.some_ms = SomeCLI(name="abc")

Expand All @@ -216,8 +284,8 @@ def test_opts(self, _):
'help': None,
'ignoreconfigfiles': None,
'info': False,
'locking_filename': self.ms.TESTFILE2,
'nagios_check_filename': self.ms.TESTFILE,
'locking_filename': self.ms.LOCKING_TESTFILE,
'nagios_check_filename': self.ms.NAGIOS_TESTFILE,
'nagios_check_interval_threshold': 0,
'nagios_report': False,
'nagios_user': getpass.getuser(),
Expand All @@ -237,20 +305,20 @@ def test_without_timestamp_mixin(self):

extsimpopts = {
'configfiles': None,
'debug': False,
#'debug': False,
'disable_locking': False,
'dry_run': False,
'ha': None,
'help': None,
'ignoreconfigfiles': None,
'info': False,
'locking_filename': self.some_ms.LOCKING_TESTFILE,
'nagios_check_filename': self.some_ms.NAGIOS_TESTFILE,
#'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': getpass.getuser(),
'nagios_user': 'nrpe',
'nagios_world_readable_check': False,
'quiet': False,
#'quiet': False,
}

myopts = {
Expand Down

0 comments on commit a19bf48

Please sign in to comment.