Skip to content

Commit a19bf48

Browse files
committed
fix: restore former test and fix settings for new ones
1 parent c4c7f32 commit a19bf48

File tree

1 file changed

+83
-15
lines changed

1 file changed

+83
-15
lines changed

test/script_tools.py

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from vsc.install.testing import TestCase
4444
from vsc.utils.nagios import NAGIOS_EXIT_WARNING, NagiosStatusMixin
4545
from vsc.utils.script_tools import (
46-
ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI,
46+
ExtendedSimpleOption, DEFAULT_OPTIONS, NrpeCLI, CLI, OldCLI,
4747
CLIBase, LockMixin, HAMixin, TimestampMixin)
4848

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

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

108+
class MyOldCLI(OldCLI):
109+
TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
110+
TESTFILE = tempfile.mkstemp()[1]
111+
TESTFILE2 = tempfile.mkstemp()[1]
112+
113+
CLI_OPTIONS = {
114+
'magic': ('some magic', None, 'store', 'magicdef'),
115+
'nagios_check_filename': ('bla', None, 'store', TESTFILE),
116+
'locking_filename': ('test', None, 'store', TESTFILE2),
117+
'nagios_user': ('user nagios runs as', 'string', 'store', getpass.getuser()),
118+
}
119+
def do(self, _):
120+
return magic.go()
121+
122+
class TestOldCLI(TestCase):
123+
"""Tests for the CLI base class"""
124+
125+
@mock.patch('vsc.utils.script_tools.ExtendedSimpleOption.prologue')
126+
def test_opts(self, _):
127+
sys.argv = ['abc']
128+
ms = MyOldCLI()
129+
130+
logging.debug("options %s %s %s", ms.options, dir(ms.options), vars(ms.options))
131+
132+
133+
134+
extsimpopts = {
135+
'configfiles': None,
136+
'debug': False,
137+
'disable_locking': False,
138+
'dry_run': False,
139+
'ha': None,
140+
'help': None,
141+
'ignoreconfigfiles': None,
142+
'info': False,
143+
'locking_filename': ms.TESTFILE2,
144+
'nagios_check_filename': ms.TESTFILE,
145+
'nagios_check_interval_threshold': 0,
146+
'nagios_report': False,
147+
'nagios_user': getpass.getuser(),
148+
'nagios_world_readable_check': False,
149+
'quiet': False,
150+
}
151+
152+
myopts = {
153+
'magic': 'magicdef',
154+
'start_timestamp': None,
155+
'timestamp_file': '/var/cache/abc.timestamp',
156+
}
157+
myopts.update(extsimpopts)
158+
self.assertEqual(ms.options.__dict__, myopts)
159+
160+
myopts = {
161+
'magic': 'magicdef',
162+
}
163+
myopts.update(extsimpopts)
164+
ms = MyOldCLI(default_options={})
165+
logging.debug("options wo default sync options %s", ms.options)
166+
self.assertEqual(ms.options.__dict__, myopts)
167+
168+
@mock.patch('vsc.utils.script_tools.lock_or_bork')
169+
@mock.patch('vsc.utils.script_tools.release_or_bork')
170+
def test_exit(self, locklock, releaselock):
171+
172+
cli = MyOldCLI()
173+
174+
fake_exit = mock.MagicMock()
175+
with mock.patch('sys.exit', fake_exit):
176+
cli.warning("be warned")
177+
fake_exit.assert_called_with(1)
108178

109179

110180
class TestNrpeCLI(TestCase):
@@ -178,13 +248,13 @@ def setUp(self):
178248

179249
class MyCLI(CLI):
180250
TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
181-
TESTFILE = tempfile.mkstemp()[1]
182-
TESTFILE2 = tempfile.mkstemp()[1]
251+
LOCKING_TESTFILE = tempfile.mkstemp()[1]
252+
NAGIOS_TESTFILE = tempfile.mkstemp()[1]
183253

184254
CLI_OPTIONS = {
185255
'magic': ('some magic', None, 'store', 'magicdef'),
186-
'nagios_check_filename': ('bla', None, 'store', TESTFILE),
187-
'locking_filename': ('test', None, 'store', TESTFILE2),
256+
'nagios_check_filename': ('bla', None, 'store', NAGIOS_TESTFILE),
257+
'locking_filename': ('test', None, 'store', LOCKING_TESTFILE),
188258
'nagios_user': ('user nagios runs as', 'str', 'store', getpass.getuser()),
189259
}
190260

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

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

@@ -216,8 +284,8 @@ def test_opts(self, _):
216284
'help': None,
217285
'ignoreconfigfiles': None,
218286
'info': False,
219-
'locking_filename': self.ms.TESTFILE2,
220-
'nagios_check_filename': self.ms.TESTFILE,
287+
'locking_filename': self.ms.LOCKING_TESTFILE,
288+
'nagios_check_filename': self.ms.NAGIOS_TESTFILE,
221289
'nagios_check_interval_threshold': 0,
222290
'nagios_report': False,
223291
'nagios_user': getpass.getuser(),
@@ -237,20 +305,20 @@ def test_without_timestamp_mixin(self):
237305

238306
extsimpopts = {
239307
'configfiles': None,
240-
'debug': False,
308+
#'debug': False,
241309
'disable_locking': False,
242310
'dry_run': False,
243311
'ha': None,
244312
'help': None,
245313
'ignoreconfigfiles': None,
246-
'info': False,
247-
'locking_filename': self.some_ms.LOCKING_TESTFILE,
248-
'nagios_check_filename': self.some_ms.NAGIOS_TESTFILE,
314+
#'info': False,
315+
'locking_filename': '/var/lock/setup.lock',
316+
'nagios_check_filename': '/var/cache/setup.nagios.json.gz',
249317
'nagios_check_interval_threshold': 0,
250318
'nagios_report': False,
251-
'nagios_user': getpass.getuser(),
319+
'nagios_user': 'nrpe',
252320
'nagios_world_readable_check': False,
253-
'quiet': False,
321+
#'quiet': False,
254322
}
255323

256324
myopts = {

0 commit comments

Comments
 (0)