From cec37d5a67b2500263e033b7241606174513a38f Mon Sep 17 00:00:00 2001 From: "Haomeng, Wang" Date: Sat, 18 Apr 2015 11:30:55 +0000 Subject: [PATCH] Ensure *-show input uuid is not empty Ironic *-show does not ensure that uuid are not empty. This patch adds checks to the client to ensure that command line arguments are not empty strings or only whitespace. It will raise a command exception if *-show command line argument is an empty string or only whitespace. Change-Id: Ia34957922006da5ecbbfc7483040d67ac51f0ada Closes-Bug: 1442436 --- ironicclient/common/utils.py | 6 ++++ .../tests/unit/v1/test_chassis_shell.py | 17 +++++++++ ironicclient/tests/unit/v1/test_node_shell.py | 36 +++++++++++++++++++ ironicclient/tests/unit/v1/test_port_shell.py | 19 ++++++++++ ironicclient/v1/chassis_shell.py | 1 + ironicclient/v1/node_shell.py | 1 + ironicclient/v1/port_shell.py | 1 + 7 files changed, 81 insertions(+) diff --git a/ironicclient/common/utils.py b/ironicclient/common/utils.py index 22fc624ee..1f64a609b 100644 --- a/ironicclient/common/utils.py +++ b/ironicclient/common/utils.py @@ -245,3 +245,9 @@ def make_configdrive(path): tmpzipfile.seek(0) return base64.b64encode(tmpzipfile.read()) + + +def check_empty_arg(arg, arg_descriptor): + if not arg.strip(): + raise exc.CommandError(_('%(arg)s cannot be empty or only have blank' + ' spaces') % {'arg': arg_descriptor}) diff --git a/ironicclient/tests/unit/v1/test_chassis_shell.py b/ironicclient/tests/unit/v1/test_chassis_shell.py index 21d036647..07d19dba2 100644 --- a/ironicclient/tests/unit/v1/test_chassis_shell.py +++ b/ironicclient/tests/unit/v1/test_chassis_shell.py @@ -16,6 +16,7 @@ import mock +from ironicclient.openstack.common.apiclient import exceptions from ironicclient.openstack.common import cliutils from ironicclient.tests.unit import utils import ironicclient.v1.chassis_shell as c_shell @@ -31,3 +32,19 @@ class ChassisShellTest(utils.BaseTestCase): exp = ['created_at', 'description', 'extra', 'updated_at', 'uuid'] act = actual.keys() self.assertEqual(sorted(exp), sorted(act)) + + def test_do_chassis_show_space_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.chassis = ' ' + self.assertRaises(exceptions.CommandError, + c_shell.do_chassis_show, + client_mock, args) + + def test_do_chassis_show_empty_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.chassis = '' + self.assertRaises(exceptions.CommandError, + c_shell.do_chassis_show, + client_mock, args) diff --git a/ironicclient/tests/unit/v1/test_node_shell.py b/ironicclient/tests/unit/v1/test_node_shell.py index 86807b63a..c44ea17ab 100644 --- a/ironicclient/tests/unit/v1/test_node_shell.py +++ b/ironicclient/tests/unit/v1/test_node_shell.py @@ -179,6 +179,42 @@ class NodeShellTest(utils.BaseTestCase): # assert get() wasn't called self.assertFalse(client_mock.node.get.called) + def test_do_node_show_by_space_node_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.node = ' ' + args.instance_uuid = False + self.assertRaises(exceptions.CommandError, + n_shell.do_node_show, + client_mock, args) + + def test_do_node_show_by_space_instance_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.node = ' ' + args.instance_uuid = True + self.assertRaises(exceptions.CommandError, + n_shell.do_node_show, + client_mock, args) + + def test_do_node_show_by_empty_node_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.node = '' + args.instance_uuid = False + self.assertRaises(exceptions.CommandError, + n_shell.do_node_show, + client_mock, args) + + def test_do_node_show_by_empty_instance_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.node = '' + args.instance_uuid = True + self.assertRaises(exceptions.CommandError, + n_shell.do_node_show, + client_mock, args) + def test_do_node_set_maintenance_true(self): client_mock = mock.MagicMock() args = mock.MagicMock() diff --git a/ironicclient/tests/unit/v1/test_port_shell.py b/ironicclient/tests/unit/v1/test_port_shell.py index 716f37d11..34793c21c 100644 --- a/ironicclient/tests/unit/v1/test_port_shell.py +++ b/ironicclient/tests/unit/v1/test_port_shell.py @@ -17,6 +17,7 @@ import mock from ironicclient.common import utils as commonutils +from ironicclient.openstack.common.apiclient import exceptions from ironicclient.openstack.common import cliutils from ironicclient.tests.unit import utils import ironicclient.v1.port_shell as p_shell @@ -46,6 +47,24 @@ class PortShellTest(utils.BaseTestCase): # assert get_by_address() wasn't called self.assertFalse(client_mock.port.get_by_address.called) + def test_do_port_show_space_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.port = ' ' + args.address = False + self.assertRaises(exceptions.CommandError, + p_shell.do_port_show, + client_mock, args) + + def test_do_port_show_empty_uuid(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + args.port = '' + args.address = False + self.assertRaises(exceptions.CommandError, + p_shell.do_port_show, + client_mock, args) + def test_do_port_show_by_address(self): client_mock = mock.MagicMock() args = mock.MagicMock() diff --git a/ironicclient/v1/chassis_shell.py b/ironicclient/v1/chassis_shell.py index acd75a967..898f57fac 100644 --- a/ironicclient/v1/chassis_shell.py +++ b/ironicclient/v1/chassis_shell.py @@ -29,6 +29,7 @@ def _print_chassis_show(chassis): @cliutils.arg('chassis', metavar='', help="UUID of the chassis.") def do_chassis_show(cc, args): """Show detailed information about a chassis.""" + utils.check_empty_arg(args.chassis, '') chassis = cc.chassis.get(args.chassis) _print_chassis_show(chassis) diff --git a/ironicclient/v1/node_shell.py b/ironicclient/v1/node_shell.py index 761fdf72a..ba85a36e1 100644 --- a/ironicclient/v1/node_shell.py +++ b/ironicclient/v1/node_shell.py @@ -44,6 +44,7 @@ def _print_node_show(node): help=' is an instance UUID.') def do_node_show(cc, args): """Show detailed information about a node.""" + utils.check_empty_arg(args.node, '') if args.instance_uuid: node = cc.node.get_by_instance_uuid(args.node) else: diff --git a/ironicclient/v1/port_shell.py b/ironicclient/v1/port_shell.py index fd78a1288..cce8321ab 100644 --- a/ironicclient/v1/port_shell.py +++ b/ironicclient/v1/port_shell.py @@ -42,6 +42,7 @@ def do_port_show(cc, args): if args.address: port = cc.port.get_by_address(args.port) else: + utils.check_empty_arg(args.port, '') port = cc.port.get(args.port) _print_port_show(port)