From c08d6e0391c2e435f2e98918116b968104846327 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 15 Nov 2023 10:21:36 +0000 Subject: [PATCH] parseactions: Use ArgumentError, not ArgumentTypeError If you use the former, you get a pretty error message when there's a failure. If you use the latter, you get an ugly traceback when used with the '--debug' flag. Without this change: $ openstack flavor create ... --property '' foo ... Traceback (most recent call last): File "/tmp/venv/lib/python3.11/site-packages/cliff/app.py", line 402, in run_subcommand parsed_args = cmd_parser.parse_args(sub_argv) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.11/argparse.py", line 1862, in parse_args args, argv = self.parse_known_args(args, namespace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.11/argparse.py", line 1895, in parse_known_args namespace, args = self._parse_known_args(args, namespace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.11/argparse.py", line 2107, in _parse_known_args start_index = consume_optional(start_index) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.11/argparse.py", line 2047, in consume_optional take_action(action, args, option_string) File "/usr/lib64/python3.11/argparse.py", line 1971, in take_action action(self, namespace, argument_values, option_string) File "/tmp/venv/lib/python3.11/site-packages/osc_lib/cli/parseractions.py", line 45, in __call__ raise argparse.ArgumentTypeError(msg % str(values)) argparse.ArgumentTypeError: Expected 'key=value' type, but got: clean_up CreateFlavor: Expected 'key=value' type, but got: With this change: $ openstack flavor create ... --property '' foo ... usage: openstack flavor create [-h] [-f {json,shell,table,value,yaml}] [-c COLUMN] [--noindent] [--prefix PREFIX] [--max-width ] [--fit-width] [--print-empty] [--id ] [--ram ] [--disk ] [--ephemeral ] [--swap ] [--vcpus ] [--rxtx-factor ] [--public | --private] [--property ] [--project ] [--description ] [--project-domain ] openstack flavor create: error: argument --property: Expected 'key=value' type, but got: clean_up CreateFlavor: Change-Id: I9e78b35ad9d016d7a33655141ec579397c5344c0 Signed-off-by: Stephen Finucane --- openstackclient/compute/v2/server.py | 69 +++++++++++++++---- openstackclient/network/v2/port.py | 4 +- .../tests/unit/compute/v2/test_server.py | 51 +++++++------- .../unit/network/v2/test_network_trunk.py | 14 ++-- .../tests/unit/network/v2/test_port.py | 34 +++++---- .../tests/unit/network/v2/test_subnet_pool.py | 24 +++---- openstackclient/tests/unit/utils.py | 7 +- .../tests/unit/volume/v1/test_volume.py | 10 ++- .../tests/unit/volume/v2/test_volume.py | 10 ++- .../unit/volume/v2/test_volume_snapshot.py | 8 +-- 10 files changed, 133 insertions(+), 98 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index d923b30c6..ea8e8f907 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -841,7 +841,7 @@ class NICAction(argparse.Action): "Invalid argument %s; characters ',' and '=' are not " "allowed" ) - raise argparse.ArgumentTypeError(msg % values) + raise argparse.ArgumentError(self, msg % values) values = '='.join([self.key, values]) else: @@ -869,7 +869,7 @@ class NICAction(argparse.Action): "'net-id=net-uuid,port-id=port-uuid,v4-fixed-ip=ip-addr," "v6-fixed-ip=ip-addr,tag=tag'" ) - raise argparse.ArgumentTypeError(msg % values) + raise argparse.ArgumentError(self, msg % values) info[k] = v @@ -878,7 +878,7 @@ class NICAction(argparse.Action): 'Invalid argument %s; either network or port should be ' 'specified but not both' ) - raise argparse.ArgumentTypeError(msg % values) + raise argparse.ArgumenteError(self, msg % values) getattr(namespace, self.dest).append(info) @@ -896,7 +896,7 @@ class BDMLegacyAction(argparse.Action): "Invalid argument %s; argument must be of form " "'dev-name=id[:type[:size[:delete-on-terminate]]]'" ) - raise argparse.ArgumentTypeError(msg % values) + raise argparse.ArgumentError(self, msg % values) mapping = { 'device_name': dev_name, @@ -913,7 +913,7 @@ class BDMLegacyAction(argparse.Action): "Invalid argument %s; 'type' must be one of: volume, " "snapshot, image" ) - raise argparse.ArgumentTypeError(msg % values) + raise argparse.ArgumentError(self, msg % values) mapping['source_type'] = dev_map[1] @@ -966,12 +966,13 @@ class BDMAction(parseractions.MultiKeyValueAction): "Invalid keys %(invalid_keys)s specified.\n" "Valid keys are: %(valid_keys)s" ) - raise argparse.ArgumentTypeError( + raise argparse.ArgumentError( + self, msg % { 'invalid_keys': ', '.join(invalid_keys), 'valid_keys': ', '.join(valid_keys), - } + }, ) missing_keys = [k for k in self.required_keys if k not in keys] @@ -980,12 +981,13 @@ class BDMAction(parseractions.MultiKeyValueAction): "Missing required keys %(missing_keys)s.\n" "Required keys are: %(required_keys)s" ) - raise argparse.ArgumentTypeError( + raise argparse.ArgumentError( + self, msg % { 'missing_keys': ', '.join(missing_keys), 'required_keys': ', '.join(self.required_keys), - } + }, ) def __call__(self, parser, namespace, values, option_string=None): @@ -2086,11 +2088,48 @@ class DeleteServer(command.Command): raise exceptions.CommandError(msg) -def percent_type(x): - x = int(x) - if not 0 < x <= 100: - raise argparse.ArgumentTypeError("Must be between 0 and 100") - return x +class PercentAction(argparse.Action): + def __init__( + self, + option_strings, + dest, + nargs=None, + const=None, + default=None, + type=None, + choices=None, + required=False, + help=None, + metavar=None, + ): + if nargs == 0: + raise ValueError( + 'nargs for store actions must be != 0; if you ' + 'have nothing to store, actions such as store ' + 'true or store const may be more appropriate' + ) + + if const is not None: + raise ValueError('const does not make sense for PercentAction') + + super().__init__( + option_strings=option_strings, + dest=dest, + nargs=nargs, + const=const, + default=default, + type=type, + choices=choices, + required=required, + help=help, + metavar=metavar, + ) + + def __call__(self, parser, namespace, values, option_string=None): + x = int(values) + if not 0 < x <= 100: + raise argparse.ArgumentError(self, "Must be between 0 and 100") + setattr(namespace, self.dest, x) class ListServer(command.Lister): @@ -2243,7 +2282,7 @@ class ListServer(command.Lister): ) parser.add_argument( '--progress', - type=percent_type, + action=PercentAction, default=None, help=_( 'Search by progress value (%%) ' diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 0612b438d..f688aba69 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -124,7 +124,7 @@ class JSONKeyValueAction(argparse.Action): "%(option)s, but encountered JSON parsing error: " "%(error)s" ) % {"option": option_string, "error": e} - raise argparse.ArgumentTypeError(msg) + raise argparse.ArgumentError(self, msg) def _get_attrs(client_manager, parsed_args): @@ -409,7 +409,7 @@ def _validate_port_hints(hints): {'openvswitch': {'other_config': {'tx-steering': 'hash'}}}, ): msg = _("Invalid value to --hints, see --help for valid values.") - raise argparse.ArgumentTypeError(msg) + raise exceptions.CommandError(msg) # When we have multiple hints, we'll need to refactor this to expand aliases diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 683b46cc2..1a577aeea 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -11,8 +11,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse + import collections import copy import getpass @@ -33,11 +32,11 @@ from openstackclient.compute.v2 import server from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes from openstackclient.tests.unit.image.v2 import fakes as image_fakes from openstackclient.tests.unit.network.v2 import fakes as network_fakes -from openstackclient.tests.unit import utils +from openstackclient.tests.unit import utils as test_utils from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes -class TestPowerStateColumn(utils.TestCase): +class TestPowerStateColumn(test_utils.TestCase): def test_human_readable(self): self.assertEqual( 'NOSTATE', server.PowerStateColumn(0x00).human_readable() @@ -1128,7 +1127,7 @@ class TestServerAddVolume(TestServerVolume): ('disable_delete_on_termination', True), ] ex = self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1373,7 +1372,7 @@ class TestServerCreate(TestServer): ] self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2193,7 +2192,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2212,7 +2211,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2231,7 +2230,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2250,7 +2249,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3302,7 +3301,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3320,7 +3319,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3338,7 +3337,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3759,7 +3758,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3777,7 +3776,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3796,7 +3795,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -3814,7 +3813,7 @@ class TestServerCreate(TestServer): self.new_server.name, ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -5187,7 +5186,7 @@ class TestServerList(_TestServerList): ] self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -5446,7 +5445,7 @@ class TestServerListV273(_TestServerList): verifylist = [('locked', True), ('unlocked', True)] ex = self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -6548,7 +6547,7 @@ class TestServerRebuild(TestServer): ('key_name', self.server.key_name), ] self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -6652,7 +6651,11 @@ class TestServerRebuild(TestServer): '--no-user-data', ] self.assertRaises( - utils.ParserException, self.check_parser, self.cmd, arglist, None + test_utils.ParserException, + self.check_parser, + self.cmd, + arglist, + None, ) def test_rebuild_with_trusted_image_cert(self): @@ -7847,7 +7850,7 @@ class TestServerSet(TestServer): ('server', 'foo_vm'), ] self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -8251,7 +8254,7 @@ class TestServerShow(TestServer): verifylist = [] self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -8915,7 +8918,7 @@ class TestServerUnshelve(TestServer): ] ex = self.assertRaises( - utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, diff --git a/openstackclient/tests/unit/network/v2/test_network_trunk.py b/openstackclient/tests/unit/network/v2/test_network_trunk.py index 82c5fec70..9d23a51da 100644 --- a/openstackclient/tests/unit/network/v2/test_network_trunk.py +++ b/openstackclient/tests/unit/network/v2/test_network_trunk.py @@ -9,9 +9,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse import copy from unittest import mock from unittest.mock import call @@ -23,7 +21,7 @@ import testtools from openstackclient.network.v2 import network_trunk from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 from openstackclient.tests.unit.network.v2 import fakes as network_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils # Tests for Neutron trunks @@ -104,7 +102,7 @@ class TestCreateNetworkTrunk(TestNetworkTrunk): verifylist = [] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -289,7 +287,7 @@ class TestCreateNetworkTrunk(TestNetworkTrunk): ), ] - with testtools.ExpectedException(argparse.ArgumentTypeError): + with testtools.ExpectedException(test_utils.ParserException): self.check_parser(self.cmd, arglist, verifylist) @@ -432,7 +430,7 @@ class TestShowNetworkTrunk(TestNetworkTrunk): verifylist = [] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -755,7 +753,7 @@ class TestSetNetworkTrunk(TestNetworkTrunk): ), ] - with testtools.ExpectedException(argparse.ArgumentTypeError): + with testtools.ExpectedException(test_utils.ParserException): self.check_parser(self.cmd, arglist, verifylist) self.network_client.add_trunk_subports.assert_not_called() @@ -949,7 +947,7 @@ class TestUnsetNetworkTrunk(TestNetworkTrunk): ('trunk', self._trunk['name']), ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index 9f2e53fa6..e93cc8c48 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -9,9 +9,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse from unittest import mock from unittest.mock import call @@ -23,7 +21,7 @@ from openstackclient.network.v2 import port from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.network.v2 import fakes as network_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils LIST_FIELDS_TO_RETRIEVE = ('id', 'name', 'mac_address', 'fixed_ips', 'status') @@ -257,7 +255,7 @@ class TestCreatePort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -273,7 +271,7 @@ class TestCreatePort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -733,7 +731,7 @@ class TestCreatePort(TestPort): ) if add_tags: self.network_client.set_tags.assert_called_once_with( - self._port, tests_utils.CompareBySet(['red', 'blue']) + self._port, test_utils.CompareBySet(['red', 'blue']) ) else: self.assertFalse(self.network_client.set_tags.called) @@ -952,7 +950,7 @@ class TestCreatePort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -976,7 +974,7 @@ class TestCreatePort(TestPort): parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises( - argparse.ArgumentTypeError, + exceptions.CommandError, self.cmd.take_action, parsed_args, ) @@ -998,7 +996,7 @@ class TestCreatePort(TestPort): parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises( - argparse.ArgumentTypeError, + exceptions.CommandError, self.cmd.take_action, parsed_args, ) @@ -1905,7 +1903,7 @@ class TestSetPort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1919,7 +1917,7 @@ class TestSetPort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2285,7 +2283,7 @@ class TestSetPort(TestPort): 'test-port', ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2309,7 +2307,7 @@ class TestSetPort(TestPort): self.assertFalse(self.network_client.update_port.called) self.network_client.set_tags.assert_called_once_with( - self._port, tests_utils.CompareBySet(expected_args) + self._port, test_utils.CompareBySet(expected_args) ) self.assertIsNone(result) @@ -2358,7 +2356,7 @@ class TestSetPort(TestPort): 'test-port', ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2377,7 +2375,7 @@ class TestSetPort(TestPort): parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises( - argparse.ArgumentTypeError, + exceptions.CommandError, self.cmd.take_action, parsed_args, ) @@ -2394,7 +2392,7 @@ class TestSetPort(TestPort): parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises( - argparse.ArgumentTypeError, + exceptions.CommandError, self.cmd.take_action, parsed_args, ) @@ -2477,7 +2475,7 @@ class TestShowPort(TestPort): verifylist = [] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -2756,7 +2754,7 @@ class TestUnsetPort(TestPort): self.assertFalse(self.network_client.update_port.called) self.network_client.set_tags.assert_called_once_with( - self._testport, tests_utils.CompareBySet(expected_args) + self._testport, test_utils.CompareBySet(expected_args) ) self.assertIsNone(result) diff --git a/openstackclient/tests/unit/network/v2/test_subnet_pool.py b/openstackclient/tests/unit/network/v2/test_subnet_pool.py index 070cfb734..0035fe8cf 100644 --- a/openstackclient/tests/unit/network/v2/test_subnet_pool.py +++ b/openstackclient/tests/unit/network/v2/test_subnet_pool.py @@ -9,9 +9,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse from unittest import mock from unittest.mock import call @@ -21,7 +19,7 @@ from osc_lib import exceptions from openstackclient.network.v2 import subnet_pool from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 from openstackclient.tests.unit.network.v2 import fakes as network_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils class TestSubnetPool(network_fakes.TestNetworkV2): @@ -98,7 +96,7 @@ class TestCreateSubnetPool(TestSubnetPool): verifylist = [] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -115,7 +113,7 @@ class TestCreateSubnetPool(TestSubnetPool): ('name', self._subnet_pool.name), ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -196,7 +194,7 @@ class TestCreateSubnetPool(TestSubnetPool): ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -368,7 +366,7 @@ class TestCreateSubnetPool(TestSubnetPool): ) if add_tags: self.network_client.set_tags.assert_called_once_with( - self._subnet_pool, tests_utils.CompareBySet(['red', 'blue']) + self._subnet_pool, test_utils.CompareBySet(['red', 'blue']) ) else: self.assertFalse(self.network_client.set_tags.called) @@ -837,7 +835,7 @@ class TestSetSubnetPool(TestSubnetPool): ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -902,7 +900,7 @@ class TestSetSubnetPool(TestSubnetPool): # Exclusive arguments will conflict here. self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -963,7 +961,7 @@ class TestSetSubnetPool(TestSubnetPool): # Exclusive arguments will conflict here. self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1029,7 +1027,7 @@ class TestSetSubnetPool(TestSubnetPool): self.assertFalse(self.network_client.update_subnet_pool.called) self.network_client.set_tags.assert_called_once_with( - self._subnet_pool, tests_utils.CompareBySet(expected_args) + self._subnet_pool, test_utils.CompareBySet(expected_args) ) self.assertIsNone(result) @@ -1093,7 +1091,7 @@ class TestShowSubnetPool(TestSubnetPool): verifylist = [] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1149,7 +1147,7 @@ class TestUnsetSubnetPool(TestSubnetPool): self.assertFalse(self.network_client.update_subnet_pool.called) self.network_client.set_tags.assert_called_once_with( - self._subnetpool, tests_utils.CompareBySet(expected_args) + self._subnetpool, test_utils.CompareBySet(expected_args) ) self.assertIsNone(result) diff --git a/openstackclient/tests/unit/utils.py b/openstackclient/tests/unit/utils.py index 1691424c6..3c8c746c3 100644 --- a/openstackclient/tests/unit/utils.py +++ b/openstackclient/tests/unit/utils.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import argparse import io import os @@ -78,7 +79,11 @@ class TestCommand(TestCase): with fixtures.MonkeyPatch('sys.stderr', stderr): try: parsed_args = cmd_parser.parse_args(args) - except SystemExit: + except ( + SystemExit, + argparse.ArgumentTypeError, + argparse.ArgumentError, + ): raise ParserException( "Argument parse failed: %s" % stderr.getvalue() ) diff --git a/openstackclient/tests/unit/volume/v1/test_volume.py b/openstackclient/tests/unit/volume/v1/test_volume.py index 85610787d..a4fd50368 100644 --- a/openstackclient/tests/unit/volume/v1/test_volume.py +++ b/openstackclient/tests/unit/volume/v1/test_volume.py @@ -11,9 +11,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse from unittest import mock from unittest.mock import call @@ -23,7 +21,7 @@ from osc_lib import utils from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes from openstackclient.tests.unit.image.v1 import fakes as image_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils from openstackclient.tests.unit.volume.v1 import fakes as volume_fakes from openstackclient.volume.v1 import volume @@ -665,7 +663,7 @@ class TestVolumeCreate(TestVolume): ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -973,7 +971,7 @@ class TestVolumeList(TestVolume): ("limit", -2), ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1065,7 +1063,7 @@ class TestVolumeMigrate(TestVolume): ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, diff --git a/openstackclient/tests/unit/volume/v2/test_volume.py b/openstackclient/tests/unit/volume/v2/test_volume.py index c506023c4..e30a312dd 100644 --- a/openstackclient/tests/unit/volume/v2/test_volume.py +++ b/openstackclient/tests/unit/volume/v2/test_volume.py @@ -10,9 +10,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse from unittest import mock from unittest.mock import call @@ -23,7 +21,7 @@ from osc_lib import utils from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.image.v2 import fakes as image_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes from openstackclient.volume.v2 import volume @@ -675,7 +673,7 @@ class TestVolumeCreate(TestVolume): ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1347,7 +1345,7 @@ class TestVolumeList(TestVolume): ("limit", -2), ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -1458,7 +1456,7 @@ class TestVolumeMigrate(TestVolume): ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, diff --git a/openstackclient/tests/unit/volume/v2/test_volume_snapshot.py b/openstackclient/tests/unit/volume/v2/test_volume_snapshot.py index c4762595b..03f2a33fa 100644 --- a/openstackclient/tests/unit/volume/v2/test_volume_snapshot.py +++ b/openstackclient/tests/unit/volume/v2/test_volume_snapshot.py @@ -10,9 +10,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -# -import argparse from unittest import mock from osc_lib.cli import format_columns @@ -20,7 +18,7 @@ from osc_lib import exceptions from osc_lib import utils from openstackclient.tests.unit.identity.v3 import fakes as project_fakes -from openstackclient.tests.unit import utils as tests_utils +from openstackclient.tests.unit import utils as test_utils from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes from openstackclient.volume.v2 import volume_snapshot @@ -117,7 +115,7 @@ class TestVolumeSnapshotCreate(TestVolumeSnapshot): ("volume", self.new_snapshot.volume_id), ] self.assertRaises( - tests_utils.ParserException, + test_utils.ParserException, self.check_parser, self.cmd, arglist, @@ -491,7 +489,7 @@ class TestVolumeSnapshotList(TestVolumeSnapshot): ("limit", -2), ] self.assertRaises( - argparse.ArgumentTypeError, + test_utils.ParserException, self.check_parser, self.cmd, arglist,