43
43
from vsc .install .testing import TestCase
44
44
from vsc .utils .nagios import NAGIOS_EXIT_WARNING , NagiosStatusMixin
45
45
from vsc .utils .script_tools import (
46
- ExtendedSimpleOption , DEFAULT_OPTIONS , NrpeCLI , CLI ,
46
+ ExtendedSimpleOption , DEFAULT_OPTIONS , NrpeCLI , CLI , OldCLI ,
47
47
CLIBase , LockMixin , HAMixin , TimestampMixin )
48
48
49
49
from lib .vsc .utils .script_tools import LogMixin
@@ -105,6 +105,76 @@ def test_threshold_custom_setting(self, mock_proceed, _, mock_lockfile):
105
105
106
106
magic = mock .MagicMock (name = 'magic' )
107
107
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 )
108
178
109
179
110
180
class TestNrpeCLI (TestCase ):
@@ -178,13 +248,13 @@ def setUp(self):
178
248
179
249
class MyCLI (CLI ):
180
250
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 ]
183
253
184
254
CLI_OPTIONS = {
185
255
'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 ),
188
258
'nagios_user' : ('user nagios runs as' , 'str' , 'store' , getpass .getuser ()),
189
259
}
190
260
@@ -197,8 +267,6 @@ class SomeCLI(HAMixin, LockMixin, LogMixin, NagiosStatusMixin, CLIBase):
197
267
CLI_OPTIONS = {
198
268
'magic' : ('magicdef' , None , 'store' , 'magicdef' ),
199
269
}
200
- LOCKING_TESTFILE = tempfile .mkstemp ()[1 ]
201
- NAGIOS_TESTFILE = tempfile .mkstemp ()[1 ]
202
270
203
271
self .some_ms = SomeCLI (name = "abc" )
204
272
@@ -216,8 +284,8 @@ def test_opts(self, _):
216
284
'help' : None ,
217
285
'ignoreconfigfiles' : None ,
218
286
'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 ,
221
289
'nagios_check_interval_threshold' : 0 ,
222
290
'nagios_report' : False ,
223
291
'nagios_user' : getpass .getuser (),
@@ -237,20 +305,20 @@ def test_without_timestamp_mixin(self):
237
305
238
306
extsimpopts = {
239
307
'configfiles' : None ,
240
- 'debug' : False ,
308
+ # 'debug': False,
241
309
'disable_locking' : False ,
242
310
'dry_run' : False ,
243
311
'ha' : None ,
244
312
'help' : None ,
245
313
'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' ,
249
317
'nagios_check_interval_threshold' : 0 ,
250
318
'nagios_report' : False ,
251
- 'nagios_user' : getpass . getuser () ,
319
+ 'nagios_user' : 'nrpe' ,
252
320
'nagios_world_readable_check' : False ,
253
- 'quiet' : False ,
321
+ # 'quiet': False,
254
322
}
255
323
256
324
myopts = {
0 commit comments