Skip to content
Closed
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
825 changes: 389 additions & 436 deletions sunbeam-python/pyproject.toml

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions sunbeam-python/sunbeam/feature_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ def __init__(
# Initialize parent with filtered choices
super().__init__(enabled_choices, case_sensitive=case_sensitive)

def get_metavar(self, param: click.Parameter) -> str:
def get_metavar(
self, param: click.Parameter, ctx: click.Context | None = None
) -> str:
"""Get metavar showing all possible choices including gated ones.

This ensures help text shows all choices, even if some are currently
Expand All @@ -408,7 +410,9 @@ def get_metavar(self, param: click.Parameter) -> str:
return f"[{choices_str}] Note: {', '.join(gated_info)}"
return f"[{choices_str}]"

def get_missing_message(self, param: click.Parameter) -> str:
def get_missing_message(
self, param: click.Parameter, ctx: click.Context | None = None
) -> str:
"""Get error message when a gated choice is used but gate is disabled."""
return (
f"Invalid value. Available choices: {', '.join(self.choices)}. "
Expand Down
2 changes: 1 addition & 1 deletion sunbeam-python/sunbeam/features/interface/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ def commands(
commands.update(self.enabled_commands())
return commands

def toggle_commands(self) -> tuple[click.BaseCommand, click.BaseCommand]:
def toggle_commands(self) -> tuple[click.Command, click.Command]:
"""Return enable and disable commands."""
return self.enable_cmd, self.disable_cmd

Expand Down
4 changes: 3 additions & 1 deletion sunbeam-python/sunbeam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ def convert(self, value: str, param, ctx) -> tuple[str, str]:
return (split[0], split[1])
self.fail(f"Invalid mapping '{value}'.", param, ctx)

def get_metavar(self, param: click.Parameter) -> str:
def get_metavar(
self, param: click.Parameter, ctx: click.Context | None = None
) -> str:
"""Define metavar representation for the parameter."""
repr_str = f"{self.key_name}|{self.key_name}{self.sep}{self.value_name}"

Expand Down
27 changes: 17 additions & 10 deletions sunbeam-python/tests/unit/sunbeam/test_feature_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest.mock import MagicMock, patch

import pytest
from click.testing import CliRunner
from snaphelpers import UnknownConfigKey

from sunbeam.clusterd.service import ConfigItemNotFoundException
Expand Down Expand Up @@ -659,8 +660,8 @@ def mock_gate_enabled(gate_key):
assert "experimental" not in choice_type.choices
assert "control" in choice_type.choices

def test_get_metavar_shows_all_choices(self):
"""Test get_metavar shows all choices including gated ones."""
def test_cli_help_and_missing_choice(self):
"""Show gated choices in help and explain missing required choices."""
import click

with patch("sunbeam.feature_gates.is_feature_gate_enabled", return_value=False):
Expand All @@ -669,15 +670,21 @@ def test_get_metavar_shows_all_choices(self):
gated_choices={"feature.multi-region": ["region_controller"]},
)

param = MagicMock(spec=click.Parameter)
metavar = choice_type.get_metavar(param)
@click.command()
@click.option("--role", type=choice_type, required=True)
def command(role):
pass

runner = CliRunner()
help_result = runner.invoke(command, ["--help"])
assert help_result.exit_code == 0, help_result.exception
assert "[control|compute|region_controller]" in help_result.output
assert "requires feature.multi-region" in help_result.output

# Should show all choices
assert "control" in metavar
assert "compute" in metavar
assert "region_controller" in metavar
# Should indicate which are gated
assert "feature.multi-region" in metavar
missing_result = runner.invoke(command, [])
assert missing_result.exit_code == 2, missing_result.exception
assert "Missing option '--role'" in missing_result.output
assert "Available choices: control, compute" in missing_result.output

def test_choice_validation_accepts_enabled_choice(self):
"""Test that validation accepts a choice that is enabled."""
Expand Down
14 changes: 14 additions & 0 deletions sunbeam-python/tests/unit/sunbeam/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import textwrap
from unittest.mock import mock_open, patch

import click
import pytest
from click.testing import CliRunner

import sunbeam.utils as utils

Expand Down Expand Up @@ -47,6 +49,18 @@ def ifaddresses():


class TestUtils:
def test_defaultable_mapping_argument_help(self):
@click.command()
@click.argument(
"mapping", type=utils.DefaultableMappingParameter("key", "value")
)
def command(mapping):
pass

result = CliRunner().invoke(command, ["--help"])
assert result.exit_code == 0, result.exception
assert "{key|key:value}" in result.output

def test_get_fqdn(self, mocker):
gethostname = mocker.patch("sunbeam.utils.socket.gethostname")
gethostname.return_value = "myhost"
Expand Down
4 changes: 2 additions & 2 deletions sunbeam-python/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ commands =

[testenv:docs]
deps =
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/2026.1}
-r{toxinidir}/doc/requirements.txt
commands =
sphinx-build -a -E -W -d doc/build/doctrees -b html doc/source doc/build/html
Expand All @@ -99,7 +99,7 @@ commands =

[testenv:releasenotes]
deps =
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master}
-c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/2026.1}
-r{toxinidir}/doc/requirements.txt
commands =
sphinx-build -a -E -W -d releasenotes/build/doctrees \
Expand Down
Loading
Loading