Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions osism/commands/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
122 changes: 119 additions & 3 deletions tests/unit/commands/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +23,8 @@

from unittest.mock import MagicMock, patch

import pytest

from osism.commands import openstack


Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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"],
Expand Down