Merge "tighten API for main()"

This commit is contained in:
Zuul 2018-10-09 20:16:30 +00:00 committed by Gerrit Code Review
commit 772a99923c
3 changed files with 51 additions and 36 deletions

View File

@ -36,8 +36,11 @@ just be cfg.CONF. For example::
from oslo_config import cfg from oslo_config import cfg
def main(): def main():
inst = ProjectSpecificUpgradeCommands() return upgradecheck.main(
return upgradecheck.main(cfg.CONF, inst.check) conf=cfg.CONF,
project='myprojectname',
upgrade_command=ProjectSpecificUpgradeCommands(),
)
The entry point for the ``$SERVICE-status`` command should then point at this The entry point for the ``$SERVICE-status`` command should then point at this
function. function.

View File

@ -19,8 +19,6 @@ test_upgradecheck
Tests for `upgradecheck` module. Tests for `upgradecheck` module.
""" """
import sys
import mock import mock
from oslo_config import cfg from oslo_config import cfg
from oslotest import base from oslotest import base
@ -72,22 +70,25 @@ class TestUpgradeCommands(base.BaseTestCase):
class TestMain(base.BaseTestCase): class TestMain(base.BaseTestCase):
def _run_test(self, func, expected): def _run_test(self, upgrade_command, expected):
mock_argv = ['test-status', 'upgrade', 'check']
conf = cfg.ConfigOpts() conf = cfg.ConfigOpts()
with mock.patch.object(sys, 'argv', mock_argv, create=True): result = upgradecheck.main(
result = upgradecheck.main(conf, func) conf=conf,
project='oslo-upgradecheck-test',
upgrade_command=upgrade_command,
argv=['upgrade', 'check'],
)
self.assertEqual(expected, result) self.assertEqual(expected, result)
def test_main(self): def test_main(self):
inst = TestCommands() inst = TestCommands()
self._run_test(inst.check, upgradecheck.Code.FAILURE) self._run_test(inst, upgradecheck.Code.FAILURE)
def test_main_exception(self): def test_main_exception(self):
def raises(): raises = mock.Mock()
raise Exception('test exception') raises.check.side_effect = Exception('test exception')
self._run_test(raises, 255) self._run_test(raises, 255)
def test_main_success(self): def test_main_success(self):
inst = SuccessCommands() inst = SuccessCommands()
self._run_test(inst.check, 0) self._run_test(inst, 0)

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import functools
import sys import sys
import textwrap import textwrap
import traceback import traceback
@ -133,17 +132,24 @@ class UpgradeCommands(object):
return return_code return return_code
def _add_parsers(subparsers, check_callback): def _register_cli_options(conf, upgrade_command):
"""Set up the command line options.
Adds a subcommand to support 'upgrade check' on the command line.
"""
def add_parsers(subparsers):
upgrade_action = subparsers.add_parser('upgrade') upgrade_action = subparsers.add_parser('upgrade')
upgrade_action.add_argument('check') upgrade_action.add_argument('check')
upgrade_action.set_defaults(action_fn=check_callback) upgrade_action.set_defaults(action_fn=upgrade_command.check)
opt = cfg.SubCommandOpt('command', handler=add_parsers)
conf.register_cli_opt(opt)
def _init_config(conf): def main(conf, project, upgrade_command,
conf(sys.argv[1:]) argv=sys.argv[1:],
default_config_files=None):
def main(conf, check_callback, config_callback=_init_config):
"""Simple implementation of main for upgrade checks """Simple implementation of main for upgrade checks
This can be used in upgrade check commands to provide the minimum This can be used in upgrade check commands to provide the minimum
@ -151,22 +157,27 @@ def main(conf, check_callback, config_callback=_init_config):
:param conf: An oslo.confg ConfigOpts instance on which to register the :param conf: An oslo.confg ConfigOpts instance on which to register the
upgrade check arguments. upgrade check arguments.
:param check_callback: The check function from the concrete implementation :param project: The name of the project, to be used as an argument
of UpgradeCommands. to the oslo_config.ConfigOpts instance to find
:param config_callback: A function that initializes the conf object. configuration files.
It must take a single argument that is the conf :param upgrade_command: The UpgradeCommands instance.
object to be initialized. The default :param argv: The command line arguments to parse. Defaults to sys.argv[1:].
implementation simply runs :param default_config_files: The configuration files to load. For projects
conf(sys.argv[1:]) that use non-standard default locations for
the configuration files, use this to override
the search behavior in oslo.config.
""" """
add_parsers = functools.partial(_add_parsers, _register_cli_options(conf, upgrade_command)
check_callback=check_callback)
opt = cfg.SubCommandOpt('category', handler=add_parsers) conf(
conf.register_cli_opt(opt) args=argv,
config_callback(conf) project=project,
default_config_files=default_config_files,
)
try: try:
return conf.category.action_fn() return conf.command.action_fn()
except Exception: except Exception:
print(_('Error:\n%s') % traceback.format_exc()) print(_('Error:\n%s') % traceback.format_exc())
# This is 255 so it's not confused with the upgrade check exit codes. # This is 255 so it's not confused with the upgrade check exit codes.