diff --git a/osism/commands/openstack.py b/osism/commands/openstack.py index b2a482703..84cead9e9 100644 --- a/osism/commands/openstack.py +++ b/osism/commands/openstack.py @@ -18,13 +18,24 @@ class _PassthroughParser(argparse.ArgumentParser): wrapper consume only its own options (``--cloud``) and forward everything else verbatim, preserving order. - Note: cliff's *app-level* parser still consumes its own global options - (``--debug``, ``-v``/``-q``, ``--log-file``) before this parser ever runs, - so those cannot be forwarded to the OpenStack CLI. + cliff's *app-level* parser consumes its own global options (``--debug``, + ``-h``/``--help``, ``-v``/``-q``, ``--version``, ``--log-file``) wherever + they appear, but not after a ``--``. A leading ``--`` is removed, so + ``osism openstack -- --debug server list`` runs ``openstack --debug + server list``. The OpenStack CLI rejects a leading ``--``, so this is + unambiguous; a ``--`` anywhere else is forwarded unchanged. """ def parse_args(self, args=None, namespace=None): namespace, arguments = self.parse_known_args(args, namespace) + if arguments[:1] == ["--"]: + arguments = arguments[1:] + # Cloud setup runs for namespace.cloud; a --cloud after the leading + # "--" would reach the OpenStack CLI instead. A forwarded "--" ends + # the check: after it, --cloud is a literal argument. + options = arguments[: arguments.index("--")] if "--" in arguments else arguments + if any(a == "--cloud" or a.startswith("--cloud=") for a in options): + self.error("--cloud must come before '--'") namespace.arguments = arguments return namespace @@ -37,7 +48,10 @@ class Run(Command): subcommand, e.g. ``osism openstack --os-region-name RegionOne server list``. Select the cloud profile with ``--cloud`` rather than ``--os-cloud``; the latter is injected automatically and rejected when - passed through. + passed through. Options the osism CLI itself uses (--debug, -h/--help, + -v, -q, --version, --log-file) need a leading "--": e.g. "osism + openstack -- --debug server list" or "osism openstack -- server list + --help". """ def get_parser(self, prog_name): diff --git a/tests/unit/commands/test_openstack.py b/tests/unit/commands/test_openstack.py index b4648d367..62b7ec1f0 100644 --- a/tests/unit/commands/test_openstack.py +++ b/tests/unit/commands/test_openstack.py @@ -11,6 +11,9 @@ rather than rejected; - ``--cloud`` selects a different cloud profile; - passing ``--os-cloud`` through is rejected in favour of ``--cloud``; +- a leading ``--`` is removed, so options the osism CLI itself consumes + (``--debug``, ``--help``, ...) reach the ``openstack`` CLI after it, and + ``--cloud`` after it is rejected; any other ``--`` is forwarded; - a failed cloud setup short-circuits before the CLI is invoked but still cleans up the (possibly partial) cloud environment; - the CLI's exit code is propagated as the command's return code; @@ -20,6 +23,8 @@ from unittest.mock import MagicMock, patch +import pytest + from osism.commands import openstack @@ -69,9 +74,9 @@ def test_forwards_leading_global_options(): # parser delegates to parse_known_args to keep these intact. # # cliff's *app-level* parser consumes its own globals (--debug, -v/-q, - # --log-file) before this command's parser runs, so those never reach the - # passthrough and are intentionally not exercised here; --os-interface / - # --os-region-name are forwarded because the app parser does not know them. + # --log-file) unless they follow a "--" (see the separator tests below); + # --os-interface / --os-region-name are forwarded because the app parser + # does not know them. result, setup, cleanup, run = _run( ["--os-interface", "public", "--os-region-name", "RegionOne", "server", "list"], setup_return=(None, [], "/orig", True), @@ -138,6 +143,117 @@ def test_rejects_passed_through_os_cloud(): cleanup.assert_not_called() +def test_separator_is_removed(): + result, setup, cleanup, run = _run( + ["--", "--debug", "server", "list", "--help"], + setup_return=(None, [], "/orig", True), + ) + + assert result == 0 + assert run.call_args.args[0][3:] == ["--debug", "server", "list", "--help"] + + +def test_only_leading_separator_is_removed(): + result, setup, cleanup, run = _run( + ["--", "server", "show", "--", "-weird-name"], + setup_return=(None, [], "/orig", True), + ) + + assert run.call_args.args[0][3:] == ["server", "show", "--", "-weird-name"] + + +def test_separator_after_subcommand_is_forwarded(): + # Regression: an OpenStack end-of-options marker keeps working. + result, setup, cleanup, run = _run( + ["server", "show", "--", "-weird-name"], + setup_return=(None, [], "/orig", True), + ) + + assert run.call_args.args[0][3:] == ["server", "show", "--", "-weird-name"] + + +def test_cloud_after_forwarded_separator_is_literal(): + result, setup, cleanup, run = _run( + ["--", "server", "show", "--", "--cloud"], + setup_return=(None, [], "/orig", True), + ) + + assert result == 0 + setup.assert_called_once_with("admin") + assert run.call_args.args[0][3:] == ["server", "show", "--", "--cloud"] + + +@pytest.mark.parametrize( + "args", + [["--", "--cloud", "other", "server", "list"], ["--", "--cloud=other"]], + ids=["separate", "equals"], +) +def test_cloud_after_separator_is_rejected(args): + cmd = openstack.Run(MagicMock(), MagicMock()) + + with pytest.raises(SystemExit) as excinfo: + cmd.get_parser("test").parse_args(args) + + assert excinfo.value.code == 2 + + +def test_os_cloud_after_separator_is_rejected(): + result, setup, cleanup, run = _run( + ["--", "--os-cloud", "other", "server", "list"], + setup_return=(None, [], "/orig", True), + ) + + assert result == 1 + run.assert_not_called() + + +def _main(argv): + """Run the real ``osism`` entry point, so cliff's app-level parser sees + the arguments first; return what reaches the ``openstack`` CLI.""" + from osism.main import main + + setup = MagicMock(return_value=(None, [], "/orig", True)) + run = MagicMock() + run.return_value.returncode = 0 + with patch( + "osism.tasks.openstack.get_cloud_helpers", + return_value=(setup, MagicMock(), MagicMock()), + ), patch("osism.commands.openstack.subprocess.run", run): + result = main(argv) + + assert result == 0 + return run.call_args.args[0][3:] + + +@pytest.mark.parametrize( + "argv, forwarded", + [ + ( + ["openstack", "--", "--debug", "server", "list"], + ["--debug", "server", "list"], + ), + (["openstack", "--", "server", "list", "--help"], ["server", "list", "--help"]), + ( + ["openstack", "server", "show", "--", "-weird-name"], + ["server", "show", "--", "-weird-name"], + ), + (["openstack", "--", "--version"], ["--version"]), + ( + ["openstack", "--", "--log-file", "/tmp/osc.log", "server", "list"], + ["--log-file", "/tmp/osc.log", "server", "list"], + ), + ], + ids=["debug", "help", "openstack-separator", "version", "log-file"], +) +def test_app_level_forwarding(argv, forwarded): + assert _main(argv) == forwarded + + +def test_app_global_option_without_separator_is_consumed(): + # Documents the cliff behaviour the separator works around. + assert _main(["openstack", "--debug", "server", "list"]) == ["server", "list"] + + def test_propagates_openstack_exit_code(): result, setup, cleanup, run = _run( ["server", "show", "missing"],