Skip to content

Commit

Permalink
Merge pull request #1098 from peterbe/bug843967-crontabber-nagios-fla…
Browse files Browse the repository at this point in the history
…g-always-returns-0

bug 843967 - crontabber --nagios flag always returns 0
  • Loading branch information
lonnen committed Feb 22, 2013
2 parents 4ae3833 + f42756e commit bfcfbf5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
16 changes: 12 additions & 4 deletions socorro/app/generic_app.py
Expand Up @@ -179,6 +179,13 @@ def main(initial_app, values_source_list=None, config_path=None):
config_pathname=config_path
)

def fix_exit_code(code):
# some apps don't return a code so you might get None
# which isn't good enough to send to sys.exit()
if code is None:
return 0
return code

with config_manager.context() as config:
config_manager.log_config(config.logger)

Expand All @@ -192,11 +199,12 @@ def main(initial_app, values_source_list=None, config_path=None):
if isinstance(app, type):
# invocation of the app if the app_object was a class
instance = app(config)
instance.main()
return fix_exit_code(instance.main())
elif inspect.ismodule(app):
# invocation of the app if the app_object was a module
app.main(config)
return fix_exit_code(app.main(config))
elif inspect.isfunction(app):
# invocation of the app if the app_object was a function
app(config)
return 0
return fix_exit_code(app(config))

raise NotImplementedError("The app did not have a callable main function")
28 changes: 28 additions & 0 deletions socorro/unittest/app/test_generic_app.py
Expand Up @@ -28,6 +28,20 @@ def main(self):
self.config.logger.error(self.config.color_or_colour)


class ExitingApp(MyApp):
app_name = 'exitingapp'

required_config = Namespace()
required_config.add_option(
name='exit_code',
default='0',
doc='How do you want it to exit',
)

def main(self):
return self.config.exit_code


class TestGenericAppConfigPathLoading(unittest.TestCase):
"""
Test that it's possible to override the default directory from where
Expand Down Expand Up @@ -67,3 +81,17 @@ def test_overriding_config_path(self, logging):
self.assertEqual(exit_code, 0)

logging.getLogger().error.assert_called_with('color')

@mock.patch('socorro.app.generic_app.logging')
def test_exit_codes(self, logging):
vsl = (ConfigFileFutureProxy, {'exit_code': 123})
exit_code = main(ExitingApp, values_source_list=vsl)
self.assertEqual(exit_code, 123)

vsl = (ConfigFileFutureProxy, {'exit_code': 0})
exit_code = main(ExitingApp, values_source_list=vsl)
self.assertEqual(exit_code, 0)

vsl = (ConfigFileFutureProxy, {'exit_code': None})
exit_code = main(ExitingApp, values_source_list=vsl)
self.assertEqual(exit_code, 0)

0 comments on commit bfcfbf5

Please sign in to comment.