Skip to content

Commit

Permalink
- added unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcocenza committed Jan 16, 2024
1 parent d12f67f commit 615b82b
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 151 deletions.
31 changes: 11 additions & 20 deletions cou/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

"""Entrypoint for 'charmed-openstack-upgrader'."""
import argparse
import asyncio
import logging
import logging.handlers
Expand All @@ -23,17 +22,13 @@

from juju.errors import JujuError

from cou.commands import parse_args
from cou.commands import Namespace, parse_args
from cou.exceptions import COUException, HighestReleaseAchieved, TimeoutException
from cou.logging import setup_logging
from cou.steps import UpgradePlan
from cou.steps.analyze import Analysis
from cou.steps.execute import apply_step
from cou.steps.plan import (
generate_plan,
manually_upgrade_data_plane,
pre_plan_sane_checks,
)
from cou.steps.plan import generate_plan, manually_upgrade_data_plane
from cou.utils import print_and_debug, progress_indicator, prompt_input
from cou.utils.cli import interrupt_handler
from cou.utils.juju_utils import COUModel
Expand Down Expand Up @@ -110,11 +105,11 @@ async def continue_upgrade() -> bool:
return False


async def analyze_and_plan(args: argparse.Namespace) -> tuple[Analysis, UpgradePlan]:
async def analyze_and_plan(args: Namespace) -> tuple[Analysis, UpgradePlan]:
"""Analyze cloud and generate the upgrade plan with steps.
:param args: CLI arguments
:type args: argparse.Namespace
:type args: Namespace
:return: Generated analysis and upgrade plan.
:rtype: tuple[Analysis, UpgradePlan]
"""
Expand All @@ -129,22 +124,18 @@ async def analyze_and_plan(args: argparse.Namespace) -> tuple[Analysis, UpgradeP
logger.info(analysis_result)
progress_indicator.succeed()

progress_indicator.start("Making sane checks to upgrade...")
pre_plan_sane_checks(args.upgrade_group, analysis_result)
progress_indicator.succeed()

progress_indicator.start("Generating upgrade plan...")
upgrade_plan = await generate_plan(analysis_result, args.backup)
upgrade_plan = await generate_plan(analysis_result, args)
progress_indicator.succeed()

return analysis_result, upgrade_plan


async def get_upgrade_plan(args: argparse.Namespace) -> None:
async def get_upgrade_plan(args: Namespace) -> None:
"""Get upgrade plan and print to console.
:param args: CLI arguments
:type args: argparse.Namespace
:type args: Namespace
"""
analysis_result, upgrade_plan = await analyze_and_plan(args)
print_and_debug(upgrade_plan)
Expand All @@ -155,11 +146,11 @@ async def get_upgrade_plan(args: argparse.Namespace) -> None:
)


async def run_upgrade(args: argparse.Namespace) -> None:
async def run_upgrade(args: Namespace) -> None:
"""Run cloud upgrade.
:param args: CLI arguments
:type args: argparse.Namespace
:type args: Namespace
"""
prompt = not args.auto_approve
analysis_result, upgrade_plan = await analyze_and_plan(args)
Expand All @@ -182,11 +173,11 @@ async def run_upgrade(args: argparse.Namespace) -> None:
print("Upgrade completed.")


async def _run_command(args: argparse.Namespace) -> None:
async def _run_command(args: Namespace) -> None:
"""Run 'charmed-openstack-upgrade' command.
:param args: CLI arguments
:type args: argparse.Namespace
:type args: Namespace
"""
match args.command:
case "plan":
Expand Down
36 changes: 29 additions & 7 deletions cou/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

"""Command line arguments parsing for 'charmed-openstack-upgrader'."""
import argparse
from typing import Any, Iterable, Optional
from typing import Any, Iterable, NamedTuple, Optional

import pkg_resources

CONTROL_PLANE = "control-plane"
DATA_PLANE = "data-plane"


class CapitalizeHelpFormatter(argparse.RawTextHelpFormatter):
"""Capitalize message prefix."""
Expand Down Expand Up @@ -194,15 +197,15 @@ def create_plan_subparser(
# help="For more information about a upgrade group, run 'cou plan <upgrade-group>' -h.",
)
plan_subparser.add_parser(
"control-plane",
CONTROL_PLANE,
description="Show the steps for upgrading the control-plane components.",
help="Show the steps for upgrading the control-plane components.",
usage="cou plan control-plane [options]",
parents=[subcommand_common_opts_parser],
formatter_class=CapitalizeHelpFormatter,
)
plan_subparser.add_parser(
"data-plane",
DATA_PLANE,
description="Show the steps for upgrading the data-plane components.\nThis is possible "
"only if control-plane has been fully upgraded,\notherwise an error will be thrown.",
help="Show the steps for upgrading the data-plane components.\nThis is possible "
Expand Down Expand Up @@ -316,13 +319,32 @@ def create_subparsers(parser: argparse.ArgumentParser) -> argparse._SubParsersAc
return subparsers


def parse_args(args: Any) -> argparse.Namespace: # pylint: disable=inconsistent-return-statements
class Namespace(NamedTuple):
"""Mock Namespace used for type hinting purposes.
Keep in sync with the argument parser defined in parse_args
"""

command: str
verbosity: int = 0
backup: bool = True
quiet: bool = False
auto_approve: bool = False
model_name: Optional[str] = None
upgrade_group: Optional[str] = None
subcommand: Optional[str] = None # for help option
machines: Optional[list[str]] = None
hostnames: Optional[list[str]] = None
availability_zones: Optional[list[str]] = None


def parse_args(args: Any) -> Namespace: # pylint: disable=inconsistent-return-statements
"""Parse cli arguments.
:param args: Arguments parser.
:type args: Any
:return: argparse.Namespace
:rtype: argparse.Namespace
:return: Namespace custom object.
:rtype: Namespace
:raises argparse.ArgumentError: Unexpected arguments input.
"""
# Configure top level argparser and its options
Expand Down Expand Up @@ -354,7 +376,7 @@ def parse_args(args: Any) -> argparse.Namespace: # pylint: disable=inconsistent
parser.exit()

try:
parsed_args = parser.parse_args(args)
parsed_args = Namespace(**vars(parser.parse_args(args)))

# print help messages for an available sub-command
if parsed_args.command == "help":
Expand Down
Loading

0 comments on commit 615b82b

Please sign in to comment.