3030"""
3131
3232import logging
33+ import os
3334import random
3435import sys
3536import tempfile
4546 ExtendedSimpleOption , DEFAULT_OPTIONS , NrpeCLI , CLI ,
4647 CLIBase , LockMixin , HAMixin , TimestampMixin )
4748
49+ from lib .vsc .utils .script_tools import LogMixin
50+
4851
4952class TestExtendedSimpleOption (TestCase ):
5053 """
@@ -102,38 +105,30 @@ def test_threshold_custom_setting(self, mock_proceed, _, mock_lockfile):
102105
103106magic = mock .MagicMock (name = 'magic' )
104107
105- class MyNrpeCLI (NrpeCLI ):
106- TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
107- CLI_OPTIONS = {
108- 'magic' : ('some magic' , None , 'store' , 'magicdef' ),
109- }
110- def do (self ,dryrun ):
111- return magic .go ()
112-
113-
114- class MyCLI (CLI ):
115- TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
116- TESTFILE = tempfile .mkstemp ()[1 ]
117- TESTFILE2 = tempfile .mkstemp ()[1 ]
118-
119- CLI_OPTIONS = {
120- 'magic' : ('some magic' , None , 'store' , 'magicdef' ),
121- 'nagios_check_filename' : ('bla' , None , 'store' , TESTFILE ),
122- 'locking_filename' : ('test' , None , 'store' , TESTFILE2 ),
123- 'nagios_user' : ('user nagios runs as' , 'str' , 'store' , getpass .getuser ()),
124- }
125- def do (self , _ ):
126- return magic .go ()
108+
127109
128110class TestNrpeCLI (TestCase ):
129111 """Tests for the CLI base class"""
130112
113+ def setUp (self ):
114+ super ().setUp ()
115+
116+ sys .argv = ["abc" ]
117+ class MyNrpeCLI (NrpeCLI ):
118+ TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
119+ CLI_OPTIONS = {
120+ 'magic' : ('some magic' , None , 'store' , 'magicdef' ),
121+ }
122+
123+ def do (self ,dryrun ):
124+ return magic .go ()
125+
126+ self .cli = MyNrpeCLI (name = "abc" )
127+
131128 @mock .patch ('vsc.utils.script_tools.ExtendedSimpleOption.prologue' )
132129 def test_opts (self , _ ):
133- sys .argv = ['abc' ]
134- ms = MyNrpeCLI ()
135130
136- logging .debug ("options %s %s %s" , ms . options , dir (ms . options ), vars (ms .options ))
131+ logging .debug ("options %s %s %s" , self . cli . options , dir (self . cli . options ), vars (self . cli .options ))
137132
138133 extsimpopts = {
139134 'configfiles' : None ,
@@ -156,44 +151,61 @@ def test_opts(self, _):
156151 myopts = {
157152 'magic' : 'magicdef' ,
158153 'start_timestamp' : None ,
159- 'timestamp_file' : '/var/cache/abc .timestamp' ,
154+ 'timestamp_file' : '/var/cache/setup .timestamp' ,
160155 }
161156 myopts .update (extsimpopts )
162- self .assertEqual (ms .options .__dict__ , myopts )
163-
164- myopts = {
165- 'magic' : 'magicdef' ,
166- }
167- myopts .update (extsimpopts )
168- ms = MyNrpeCLI (default_options = {})
169- logging .debug ("options wo default sync options %s" , ms .options )
170- self .assertEqual (ms .options .__dict__ , myopts )
157+ self .assertEqual (self .cli .options .__dict__ , myopts )
171158
172159 @mock .patch ('vsc.utils.script_tools.ExtendedSimpleOption.prologue' )
173160 def test_exit (self , _ ):
174161
175162 self .original_argv = sys .argv
176163 sys .argv = ["somecli" ]
177164
178- cli = MyNrpeCLI ()
179-
180165 fake_exit = mock .MagicMock ()
181166 with mock .patch ('vsc.utils.nagios._real_exit' , fake_exit ):
182- cli .warning ("be warned" )
167+ self . cli .warning ("be warned" )
183168 fake_exit .assert_called_with ("be warned" , NAGIOS_EXIT_WARNING )
184169
185170
186171class TestCLI (TestCase ):
187172 """Tests for the CLI base class"""
188173
189- @mock .patch ('vsc.utils.script_tools.ExtendedSimpleOption.prologue' )
190- def test_opts (self , _ ):
191- sys .argv = ['abc' ]
192- ms = MyCLI (name = "MyCLI" )
174+ def setUp (self ):
175+ super ().setUp ()
176+
177+ sys .argv = ["abc" ]
178+
179+ class MyCLI (CLI ):
180+ TIMESTAMP_MANDATORY = False # mainly for testing, you really should need this in production
181+ TESTFILE = tempfile .mkstemp ()[1 ]
182+ TESTFILE2 = tempfile .mkstemp ()[1 ]
183+
184+ CLI_OPTIONS = {
185+ 'magic' : ('some magic' , None , 'store' , 'magicdef' ),
186+ 'nagios_check_filename' : ('bla' , None , 'store' , TESTFILE ),
187+ 'locking_filename' : ('test' , None , 'store' , TESTFILE2 ),
188+ 'nagios_user' : ('user nagios runs as' , 'str' , 'store' , getpass .getuser ()),
189+ }
193190
194- logging .debug ("options %s %s %s" , ms .options , dir (ms .options ), vars (ms .options ))
191+ def do (self , dry_run ):
192+ return magic .go ()
195193
194+ self .ms = MyCLI (name = "abc" )
196195
196+ class SomeCLI (HAMixin , LockMixin , LogMixin , NagiosStatusMixin , CLIBase ):
197+ CLI_OPTIONS = {
198+ 'magic' : ('magicdef' , None , 'store' , 'magicdef' ),
199+ }
200+ LOCKING_TESTFILE = tempfile .mkstemp ()[1 ]
201+ NAGIOS_TESTFILE = tempfile .mkstemp ()[1 ]
202+
203+ self .some_ms = SomeCLI (name = "abc" )
204+
205+ @mock .patch ('vsc.utils.script_tools.ExtendedSimpleOption.prologue' )
206+ def test_opts (self , _ ):
207+
208+ logging .debug ("options %s %s %s" , self .ms .options , dir (self .ms .options ), vars (self .ms .options ))
197209
198210 extsimpopts = {
199211 'configfiles' : None ,
@@ -204,8 +216,8 @@ def test_opts(self, _):
204216 'help' : None ,
205217 'ignoreconfigfiles' : None ,
206218 'info' : False ,
207- 'locking_filename' : ms .TESTFILE2 ,
208- 'nagios_check_filename' : ms .TESTFILE ,
219+ 'locking_filename' : self . ms .TESTFILE2 ,
220+ 'nagios_check_filename' : self . ms .TESTFILE ,
209221 'nagios_check_interval_threshold' : 0 ,
210222 'nagios_report' : False ,
211223 'nagios_user' : getpass .getuser (),
@@ -216,32 +228,45 @@ def test_opts(self, _):
216228 myopts = {
217229 'magic' : 'magicdef' ,
218230 'start_timestamp' : None ,
219- 'timestamp_file' : '/var/cache/abc .timestamp' ,
231+ 'timestamp_file' : '/var/cache/setup .timestamp' ,
220232 }
221233 myopts .update (extsimpopts )
222- self .assertEqual (ms .options .__dict__ , myopts )
234+ self .assertEqual (self .ms .options .__dict__ , myopts )
235+
236+ def test_without_timestamp_mixin (self ):
237+
238+ extsimpopts = {
239+ 'configfiles' : None ,
240+ 'debug' : False ,
241+ 'disable_locking' : False ,
242+ 'dry_run' : False ,
243+ 'ha' : None ,
244+ 'help' : None ,
245+ 'ignoreconfigfiles' : None ,
246+ 'info' : False ,
247+ 'locking_filename' : self .some_ms .LOCKING_TESTFILE ,
248+ 'nagios_check_filename' : self .some_ms .NAGIOS_TESTFILE ,
249+ 'nagios_check_interval_threshold' : 0 ,
250+ 'nagios_report' : False ,
251+ 'nagios_user' : getpass .getuser (),
252+ 'nagios_world_readable_check' : False ,
253+ 'quiet' : False ,
254+ }
223255
224256 myopts = {
225257 'magic' : 'magicdef' ,
226258 }
227259 myopts .update (extsimpopts )
228- ms = MyCLI (name = "mycli" , default_options = {})
229- logging .debug ("options wo default sync options %s" , ms .options )
230- self .assertEqual (ms .options .__dict__ , myopts )
260+ logging .debug ("options wo default sync options %s" , self .some_ms .options )
261+ self .assertEqual (self .some_ms .options .__dict__ , myopts )
231262
232263 @mock .patch ('vsc.utils.script_tools.lock_or_bork' )
233264 @mock .patch ('vsc.utils.script_tools.release_or_bork' )
234265 def test_exit (self , locklock , releaselock ):
235- original_argv = sys .argv
236- sys .argv = ["mycli" ]
237-
238- cli = MyCLI (
239- name = "MyCLI" ,
240- )
241266
242267 fake_exit = mock .MagicMock ()
243268 with mock .patch ('vsc.utils.script_tools.sys.exit' , fake_exit ):
244- cli .warning ("be warned" )
269+ self . ms .warning ("be warned" )
245270 fake_exit .assert_called_with (1 )
246271
247272
0 commit comments