From 10857169715f00d5e5ff255eec92954aa45ae4a7 Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 17 Jan 2024 12:33:58 -0500 Subject: [PATCH] Return error for invalid filesystem selection When using --uuid/--name option to specify a filesystem, the pool name is required, otherwise the --name option might not select a unique filesystem. Return the error in the action code because the argparse library does not have faciliites to do this checking. Signed-off-by: mulhern --- src/stratis_cli/_actions/_logical.py | 4 +++- src/stratis_cli/_errors.py | 13 +++++++++++++ tests/whitebox/integration/test_parser.py | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/stratis_cli/_actions/_logical.py b/src/stratis_cli/_actions/_logical.py index 859ac888e..484fc45c4 100644 --- a/src/stratis_cli/_actions/_logical.py +++ b/src/stratis_cli/_actions/_logical.py @@ -23,6 +23,7 @@ StratisCliEngineError, StratisCliFsSizeLimitChangeError, StratisCliIncoherenceError, + StratisCliMissingPoolNameError, StratisCliNoChangeError, StratisCliPartialChangeError, ) @@ -125,7 +126,8 @@ def list_volumes(namespace): getattr(namespace, "name", None), ) - assert (fs_name is None and fs_uuid is None) or pool_name is not None + if not (fs_name is None and fs_uuid is None) and pool_name is None: + raise StratisCliMissingPoolNameError() uuid_formatter = get_uuid_formatter(namespace.unhyphenated_uuids) diff --git a/src/stratis_cli/_errors.py b/src/stratis_cli/_errors.py index ef86a8438..09869d8bc 100644 --- a/src/stratis_cli/_errors.py +++ b/src/stratis_cli/_errors.py @@ -262,6 +262,19 @@ def __str__(self): ) +class StratisCliMissingPoolNameError(StratisCliParserError): + """ + Raised during filesystem listing if the user specified filesystem name or + UUID but failed to specify a pool name. + """ + + def __str__(self): + return ( + "A value for the pool name must be supplied when " + "specifying the UUID or name of the filesystem" + ) + + class StratisCliIncoherenceError(StratisCliRuntimeError): """ Raised if there was a disagreement about state between the CLI diff --git a/tests/whitebox/integration/test_parser.py b/tests/whitebox/integration/test_parser.py index 5368db876..8785479ee 100644 --- a/tests/whitebox/integration/test_parser.py +++ b/tests/whitebox/integration/test_parser.py @@ -280,3 +280,12 @@ def test_create_with_clevis_2(self): "/dev/n", ] self.check_error(StratisCliMissingClevisThumbprintError, command_line, 1) + + def test_stratis_list_filesystem_with_name_no_pool(self): + """ + We want to get a parse error if filesystem UUID is specified but no + name. + """ + command_line = ["filesystem", "list", "--name=bogus"] + for prefix in [[], ["--propagate"]]: + self.check_system_exit(prefix + command_line, _PARSE_ERROR)