From 3e5b43363b7b8018b58493677309756bb51346b7 Mon Sep 17 00:00:00 2001 From: Robert Gildein Date: Fri, 15 Mar 2024 16:16:51 +0100 Subject: [PATCH] Add dependent option to BaseStep (#296) This option will be used later for data-plane upgrade, where we do unit by unit. --------- Co-authored-by: TQ X --- cou/steps/__init__.py | 9 ++++++++- tests/unit/steps/test_steps.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cou/steps/__init__.py b/cou/steps/__init__.py index 89cdfa10..dcbded49 100644 --- a/cou/steps/__init__.py +++ b/cou/steps/__init__.py @@ -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: @@ -71,6 +72,7 @@ def __init__( description: str = "", parallel: bool = False, coro: Optional[Coroutine] = None, + dependent: bool = False, ): """Initialize BaseStep. @@ -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 @@ -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 diff --git a/tests/unit/steps/test_steps.py b/tests/unit/steps/test_steps.py index 2b92ecce..b159c027 100644 --- a/tests/unit/steps/test_steps.py +++ b/tests/unit/steps/test_steps.py @@ -21,6 +21,7 @@ from cou.exceptions import CanceledStep from cou.steps import ( + DEPENDENCY_DESCRIPTION_PREFIX, BaseStep, PostUpgradeStep, PreUpgradeStep, @@ -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")