Skip to content

Commit

Permalink
Fix --purge / --purge-before-date dependency issue.
Browse files Browse the repository at this point in the history
The dependency check only works if --purge is specified first. If the
--purge is after --purge-before-date, the dependency check action still
report missing require args --purge.

For example, `cou plan --purge-before-date 2000-1-1 --purge` will still
failed.
  • Loading branch information
chanchiwai-ray committed Aug 29, 2024
1 parent cf0767f commit 1f1a788
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 44 deletions.
33 changes: 13 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,11 @@ 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)"
"\nThis option requires --archive."
),
type=batch_size_arg,
default=1000,
)
Expand All @@ -199,12 +185,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 +541,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 getattr(parsed_args, "purge", None):
validation_errors.append("--purge-before-date requires --purge")
if len(validation_errors) > 0:
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

0 comments on commit 1f1a788

Please sign in to comment.