Skip to content

Commit

Permalink
Add dependent option to BaseStep (canonical#296)
Browse files Browse the repository at this point in the history
This option will be used later for data-plane upgrade, where we do unit
by unit.

---------

Co-authored-by: TQ X <[email protected]>
  • Loading branch information
rgildein and agileshaw authored Mar 15, 2024
1 parent 748cbdb commit 3e5b433
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cou/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from cou.exceptions import CanceledStep

logger = logging.getLogger(__name__)
DEPENDENCY_DESCRIPTION_PREFIX = "├── "


def compare_step_coroutines(coro1: Optional[Coroutine], coro2: Optional[Coroutine]) -> bool:
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(
description: str = "",
parallel: bool = False,
coro: Optional[Coroutine] = None,
dependent: bool = False,
):
"""Initialize BaseStep.
Expand All @@ -80,6 +82,8 @@ def __init__(
:type parallel: bool
:param coro: Step coroutine
:type coro: Optional[coroutine]
:param dependent: Whether the step is dependent on another step.
:type dependent: bool, defaults to False
"""
if coro is not None:
# NOTE(rgildein): We need to ignore coroutine not to be awaited if step is not run
Expand All @@ -89,7 +93,10 @@ def __init__(

self._coro: Optional[Coroutine] = coro
self.parallel = parallel
self.description = description
self.dependent = dependent
self.description = (
DEPENDENCY_DESCRIPTION_PREFIX + description if dependent else description
)
self._sub_steps: List[BaseStep] = []
self._canceled: bool = False
self._task: Optional[asyncio.Task] = None
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/steps/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from cou.exceptions import CanceledStep
from cou.steps import (
DEPENDENCY_DESCRIPTION_PREFIX,
BaseStep,
PostUpgradeStep,
PreUpgradeStep,
Expand Down Expand Up @@ -136,6 +137,23 @@ def test_step_str():
assert str(plan) == expected


def test_step_str_dependent():
"""Test BaseStep string representation."""
expected = (
f"a\n\ta.a\n\t\t{DEPENDENCY_DESCRIPTION_PREFIX}a.a.a\n"
f"\t\t{DEPENDENCY_DESCRIPTION_PREFIX}a.a.b\n\ta.b\n"
)
plan = BaseStep(description="a")
sub_step = BaseStep(description="a.a")
sub_step.sub_steps = [
BaseStep(description="a.a.a", coro=mock_coro("a.a.a"), dependent=True),
BaseStep(description="a.a.b", coro=mock_coro("a.a.b"), dependent=True),
]
plan.sub_steps = [sub_step, BaseStep(description="a.b", coro=mock_coro("a.b"))]

assert str(plan) == expected


def test_step_str_not_show():
"""Test BaseStep string representation when does not print because it's empty."""
plan = BaseStep(description="a")
Expand Down

0 comments on commit 3e5b433

Please sign in to comment.