Skip to content

Commit

Permalink
Move _verify_channel to sanity checks (#358)
Browse files Browse the repository at this point in the history
Renamed the _verify_channel to _check_channel and move it to upgrade
plan sanity checks.

fixes: #357

---------

Co-authored-by: TQ X <[email protected]>
  • Loading branch information
rgildein and agileshaw authored Apr 8, 2024
1 parent 4d6fa9f commit 8e3abf2
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 159 deletions.
49 changes: 23 additions & 26 deletions cou/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ class OpenStackApplication(Application):
wait_timeout: int = field(default=STANDARD_IDLE_TIMEOUT, init=False)
wait_for_model: bool = field(default=False, init=False) # waiting only for application itself

def __post_init__(self) -> None:
"""Initialize the Application dataclass."""
self._verify_channel()

def __hash__(self) -> int:
"""Hash magic method for Application.
Expand Down Expand Up @@ -135,27 +131,6 @@ def __str__(self) -> str:

return yaml.dump(summary, sort_keys=False)

def _verify_channel(self) -> None:
"""Verify app channel from current data.
:raises ApplicationError: Exception raised when channel is not a valid OpenStack channel.
"""
if (
self.is_from_charm_store
or self.channel == LATEST_STABLE
or self.is_valid_track(self.channel)
):
logger.debug("%s app has proper channel %s", self.name, self.channel)
return

raise ApplicationError(
f"Channel: {self.channel} for charm '{self.charm}' on series "
f"'{self.series}' is currently not supported in this tool. Please take a look at the "
"documentation: "
"https://docs.openstack.org/charm-guide/latest/project/charm-delivery.html to see if "
"you are using the right track."
)

@property
def apt_source_codename(self) -> Optional[OpenStackRelease]:
"""Identify the OpenStack release set on "openstack-origin" or "source" config.
Expand Down Expand Up @@ -367,11 +342,12 @@ def upgrade_plan_sanity_checks(
:type target: OpenStackRelease
:param units: Units to generate upgrade plan, defaults to None
:type units: Optional[list[Unit]], optional
:raises ApplicationError: When enable-auto-restarts is not enabled.
:raises ApplicationError: When application is wrongly configured.
:raises HaltUpgradePlanGeneration: When the application halt the upgrade plan generation.
:raises MismatchedOpenStackVersions: When the units of the app are running
different OpenStack versions.
"""
self._check_channel()
self._check_application_target(target)
self._check_mismatched_versions(units)
self._check_auto_restarts()
Expand Down Expand Up @@ -760,6 +736,27 @@ def _get_wait_step(self) -> PostUpgradeStep:
coro=self.model.wait_for_active_idle(self.wait_timeout, apps=apps),
)

def _check_channel(self) -> None:
"""Check app channel from current data.
:raises ApplicationError: Exception raised when channel is not a valid OpenStack channel.
"""
if (
self.is_from_charm_store
or self.channel == LATEST_STABLE
or self.is_valid_track(self.channel)
):
logger.debug("%s app has proper channel %s", self.name, self.channel)
return

raise ApplicationError(
f"Channel: {self.channel} for charm '{self.charm}' on series "
f"'{self.series}' is currently not supported in this tool. Please take a look at the "
"documentation: "
"https://docs.openstack.org/charm-guide/latest/project/charm-delivery.html to see if "
"you are using the right track."
)

def _check_auto_restarts(self) -> None:
"""Check if enable-auto-restarts is enabled.
Expand Down
219 changes: 113 additions & 106 deletions tests/unit/apps/test_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,61 +341,60 @@ def test_auxiliary_upgrade_plan_unknown_track(model):
"to see if you are using the right track."
)
machines = {"0": MagicMock(spec_set=Machine)}
app = RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="3.9/stable",
charm="rabbitmq-server",
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)

with pytest.raises(ApplicationError, match=exp_msg):
RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="3.9/stable",
charm="rabbitmq-server",
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)
app._check_channel()


def test_auxiliary_app_unknown_version_raise_ApplicationError(model):
"""Test auxiliary upgrade plan with unknown version."""
"""Test auxiliary application with unknown workload version."""
version = "80.5"
charm = "rabbitmq-server"
exp_msg = f"'{charm}' with workload version {version} has no compatible OpenStack release."

machines = {"0": MagicMock(spec_set=Machine)}
unit = Unit(name=f"{charm}/0", workload_version=version, machine=machines["0"])
app = RabbitMQServer(
name=charm,
can_upgrade_to="3.8/stable",
charm=charm,
channel="3.8/stable",
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={unit.name: unit},
workload_version=version,
)

with pytest.raises(ApplicationError, match=exp_msg):
RabbitMQServer(
name=charm,
can_upgrade_to="3.8/stable",
charm=charm,
channel="3.8/stable",
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
f"{charm}/0": Unit(
name=f"{charm}/0",
workload_version=version,
machine=machines["0"],
)
},
workload_version=version,
)
app.get_latest_os_version(unit)


def test_auxiliary_raise_error_unknown_series(model):
"""Test auxiliary upgrade plan with unknown series."""
"""Test auxiliary application with unknown series."""
series = "foo"
channel = "3.8/stable"
exp_msg = (
Expand All @@ -405,27 +404,28 @@ def test_auxiliary_raise_error_unknown_series(model):
"to see if you are using the right track."
)
machines = {"0": MagicMock(spec_set=Machine)}
app = RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="3.9/stable",
charm="rabbitmq-server",
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series=series,
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)
with pytest.raises(ApplicationError, match=exp_msg):
RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="3.9/stable",
charm="rabbitmq-server",
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series=series,
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)
app._check_channel()


@patch("cou.apps.core.OpenStackApplication.current_os_release")
Expand All @@ -435,29 +435,36 @@ def test_auxiliary_raise_error_os_not_on_lookup(current_os_release, model):
Using OpenStack release version that is not on openstack_to_track_mapping.csv table.
"""
current_os_release.return_value = OpenStackRelease("diablo")

exp_error_msg = (
"Channel: 3.8/stable for charm 'rabbitmq-server' on series 'focal' is currently not "
"supported in this tool. Please take a look at the documentation: "
"https://docs.openstack.org/charm-guide/latest/project/charm-delivery.html to see if you "
"are using the right track."
)
machines = {"0": MagicMock(spec_set=Machine)}
with pytest.raises(ApplicationError):
RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="",
charm="rabbitmq-server",
channel="3.8/stable",
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)
app = RabbitMQServer(
name="rabbitmq-server",
can_upgrade_to="",
charm="rabbitmq-server",
channel="3.8/stable",
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
"rabbitmq-server/0": Unit(
name="rabbitmq-server/0",
workload_version="3.8",
machine=machines["0"],
)
},
workload_version="3.8",
)

with pytest.raises(ApplicationError, match=exp_error_msg):
app._check_channel()


def test_auxiliary_raise_halt_upgrade(model):
Expand Down Expand Up @@ -840,28 +847,29 @@ def test_ovn_no_compatible_os_release(channel, model):
"https://docs.openstack.org/charm-guide/latest/project/charm-delivery.html "
"to see if you are using the right track."
)
app = OvnPrincipal(
name=charm,
can_upgrade_to="quincy/stable",
charm=charm,
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
f"{charm}/0": Unit(
name=f"{charm}/0",
workload_version="22.03",
machine=machines["0"],
)
},
workload_version="22.03",
)

with pytest.raises(ApplicationError, match=exp_msg):
OvnPrincipal(
name=charm,
can_upgrade_to="quincy/stable",
charm=charm,
channel=channel,
config={"source": {"value": "distro"}},
machines=machines,
model=model,
origin="ch",
series="focal",
subordinate_to=[],
units={
f"{charm}/0": Unit(
name=f"{charm}/0",
workload_version="22.03",
machine=machines["0"],
)
},
workload_version="22.03",
)
app._check_channel()


def test_ovn_principal_upgrade_plan(model):
Expand Down Expand Up @@ -1280,10 +1288,9 @@ def test_ceph_osd_upgrade_plan(model):
),
],
)
@patch("cou.apps.auxiliary.AuxiliaryApplication._verify_channel", return_value=None)
@patch("cou.apps.auxiliary.TRACK_TO_OPENSTACK_MAPPING")
def test_need_current_channel_refresh_auxiliary(
mock_track_os_mapping, _, model, can_upgrade_to, compatible_os_releases, exp_result
mock_track_os_mapping, model, can_upgrade_to, compatible_os_releases, exp_result
):
mock_track_os_mapping.__getitem__.return_value = compatible_os_releases
target = OpenStackRelease("victoria")
Expand Down
Loading

0 comments on commit 8e3abf2

Please sign in to comment.