Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --purge / --purge-before-date dependency issue #532

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions cou/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,6 @@ def purge_before_arg(value: str) -> str:
raise argparse.ArgumentTypeError("purge before format must be YYYY-MM-DD[HH:mm][:ss]")


class PurgeBeforeArgumentAction(argparse.Action):
"""Custom action to make sure the arguments dependency."""

required_arg = "purge"

def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Any,
option_string: Optional[str] = None,
) -> None:
if getattr(namespace, self.required_arg) is False:
parser.error(f"--{self.dest} requires --{self.required_arg}")
setattr(namespace, self.dest, values)


def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
"""Create a shared parser for options specific to subcommands.

Expand Down Expand Up @@ -185,8 +168,10 @@ def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
)
subcommand_common_opts_parser.add_argument(
"--archive-batch-size",
help="Batch size for nova old database data archiving.\n"
"Decrease the batch size if performance issues are detected.\n(default: 1000)",
help=(
"Batch size for nova old database data archiving."
"\nDecrease the batch size if performance issues are detected.\n(default: 1000)"
),
type=batch_size_arg,
default=1000,
)
Expand All @@ -199,12 +184,12 @@ def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
subcommand_common_opts_parser.add_argument(
"--purge-before-date",
dest="purge_before",
action=PurgeBeforeArgumentAction,
help=(
"Providing this argument will delete data from all shadow tables"
"\nthat is older than the date provided."
"\nDate string format should be YYYY-MM-DD[HH:mm][:ss]."
"\nWithout --purge-before-date the purge step will delete all the data."
"\nThis option requires --purge."
),
type=purge_before_arg,
required=False,
Expand Down Expand Up @@ -555,6 +540,13 @@ def parse_args(args: Any) -> CLIargs: # pylint: disable=inconsistent-return-sta
subparsers.choices["upgrade"].print_help()
parser.exit()

# validate arguments
validation_errors = []
if parsed_args.purge_before and not parsed_args.purge:
validation_errors.append("--purge-before-date requires --purge")
if validation_errors:
parser.error("\n" + "\n".join(validation_errors))

return parsed_args
except argparse.ArgumentError as exc:
parser.error(
Expand Down
26 changes: 2 additions & 24 deletions tests/unit/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,31 +935,9 @@ def test_purge_before_arg(val, raise_err):


@patch("cou.commands.setattr")
def test_purge_before_argument_action(mock_setattr):
parser = ArgumentParser()
parser.add_argument("--purge", action="store_true", default=False)
parser.add_argument(
"--purge-before-date",
dest="purge_before",
type=str,
action=commands.PurgeBeforeArgumentAction,
)
parser.parse_args("--purge --purge-before-date 2000-01-02".split())
mock_setattr.assert_called()


@patch("cou.commands.setattr")
def test_purge_before_argument_action_failed(mock_setattr):
parser = ArgumentParser()
parser.add_argument("--purge", action="store_true", default=False)
parser.add_argument(
"--purge-before-date",
dest="purge_before",
type=str,
action=commands.PurgeBeforeArgumentAction,
)
def test_purge_before_argument_missing_dependency(mock_setattr):
with pytest.raises(SystemExit, match="2"):
parser.parse_args("--purge-before-date 2000-01-02".split())
commands.parse_args("plan --purge-before-date 2000-01-02".split())


@patch("cou.commands.setattr")
Expand Down
Loading