Skip to content

Commit

Permalink
Add add_steps function to BaseStep (#292)
Browse files Browse the repository at this point in the history
Add `add_steps` to BaseStep and use it, where is needed.
  • Loading branch information
rgildein authored Mar 14, 2024
1 parent 376522e commit d26a9fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
20 changes: 9 additions & 11 deletions cou/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,9 @@ def generate_upgrade_plan(self, target: OpenStackRelease) -> ApplicationUpgradeP
upgrade_plan = ApplicationUpgradePlan(
description=f"Upgrade plan for '{self.name}' to {target}",
)
upgrade_plan.sub_steps = [
*self.pre_upgrade_steps(target),
*self.upgrade_steps(target),
*self.post_upgrade_steps(target),
]
upgrade_plan.add_steps(self.pre_upgrade_steps(target))
upgrade_plan.add_steps(self.upgrade_steps(target))
upgrade_plan.add_steps(self.post_upgrade_steps(target))

return upgrade_plan

Expand All @@ -522,13 +520,13 @@ def _get_upgrade_current_release_packages_step(self) -> PreUpgradeStep:
),
parallel=True,
)
for unit in self.units:
step.add_step(
UnitUpgradeStep(
description=f"Upgrade software packages on unit {unit.name}",
coro=upgrade_packages(unit.name, self.model, self.packages_to_hold),
)
step.add_steps(
UnitUpgradeStep(
description=f"Upgrade software packages on unit {unit.name}",
coro=upgrade_packages(unit.name, self.model, self.packages_to_hold),
)
for unit in self.units
)

return step

Expand Down
9 changes: 9 additions & 0 deletions cou/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ def add_step(self, step: BaseStep) -> None:

self._sub_steps.append(step)

def add_steps(self, steps: Iterable[BaseStep]) -> None:
"""Add multiple steps.
:param steps: Sequence of BaseStep to be added as sub steps.
:type steps: Iterable[BaseStep]
"""
for step in steps:
self.add_step(step)

def cancel(self, safe: bool = True) -> None:
"""Cancel step and all its sub steps.
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/steps/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def test_step_add_step_failed():
plan.add_step(MagicMock())


def test_step_add_steps():
"""Test BaseStep adding sub steps at once."""
exp_sub_steps = 3
plan = BaseStep(description="plan")
plan.add_steps(
[BaseStep(description=f"sub-step-{i}", coro=mock_coro()) for i in range(exp_sub_steps)]
+ [BaseStep(description="empty-step")] # we also check that empty step will not be added
)

assert len(plan.sub_steps) == exp_sub_steps


def test_step_cancel_safe():
"""Test step safe cancel."""
plan = BaseStep(description="plan")
Expand Down

0 comments on commit d26a9fa

Please sign in to comment.