From 72a0ec9adcd19a96644638e8324566fcc4af8799 Mon Sep 17 00:00:00 2001 From: Dinesh Bhor Date: Tue, 14 Feb 2017 13:10:33 +0530 Subject: [PATCH] Fix ValueError when incorrect metadata passed If you pass incorrect formatted metadata to the 'boot', 'rebuild' and 'image-create' apis returns following error: ERROR (ValueError): dictionary update sequence element #0 has length 1; 2 is required Caught the ValuError and raised argparse.ArgumentTypeError with proper error message. Closes-Bug: #1668549 Change-Id: I14a30b93f4a916fc04610f9e475c12eb352e38c5 --- novaclient/tests/unit/v2/test_shell.py | 22 ++++++++++++++++++++++ novaclient/v2/shell.py | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/novaclient/tests/unit/v2/test_shell.py b/novaclient/tests/unit/v2/test_shell.py index b39ace07a..aece4422d 100644 --- a/novaclient/tests/unit/v2/test_shell.py +++ b/novaclient/tests/unit/v2/test_shell.py @@ -614,6 +614,14 @@ class ShellTest(utils.TestCase): }}, ) + def test_boot_with_incorrect_metadata(self): + cmd = ('boot --image %s --flavor 1 --meta foo ' + 'some-server ' % FAKE_UUID_1) + result = self.assertRaises(argparse.ArgumentTypeError, + self.run_command, cmd) + expected = "'['foo']' is not in the format of 'key=value'" + self.assertEqual(expected, result.args[0]) + def test_boot_hints(self): self.run_command('boot --image %s --flavor 1 ' '--hint a=b0=c0 --hint a=b1=c1 some-server ' % @@ -1206,6 +1214,13 @@ class ShellTest(utils.TestCase): {'createImage': {'name': 'mysnapshot', 'metadata': {}}}, ) + def test_create_image_with_incorrect_metadata(self): + cmd = 'image-create sample-server mysnapshot --metadata foo' + result = self.assertRaises(argparse.ArgumentTypeError, + self.run_command, cmd) + expected = "'['foo']' is not in the format of 'key=value'" + self.assertEqual(expected, result.args[0]) + def test_create_image_with_metadata(self): self.run_command( 'image-create sample-server mysnapshot --metadata mykey=123') @@ -1442,6 +1457,13 @@ class ShellTest(utils.TestCase): self.assert_called('GET', '/flavors/1', pos=4) self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_2, pos=5) + def test_rebuild_with_incorrect_metadata(self): + cmd = 'rebuild sample-server %s --name asdf --meta foo' % FAKE_UUID_1 + result = self.assertRaises(argparse.ArgumentTypeError, + self.run_command, cmd) + expected = "'['foo']' is not in the format of 'key=value'" + self.assertEqual(expected, result.args[0]) + def test_start(self): self.run_command('start sample-server') self.assert_called('POST', '/servers/1234/action', {'os-start': None}) diff --git a/novaclient/v2/shell.py b/novaclient/v2/shell.py index d94644552..a1485d9c3 100644 --- a/novaclient/v2/shell.py +++ b/novaclient/v2/shell.py @@ -75,7 +75,11 @@ def _key_value_pairing(text): def _meta_parsing(metadata): - return dict(v.split('=', 1) for v in metadata) + try: + return dict(v.split('=', 1) for v in metadata) + except ValueError: + msg = _("'%s' is not in the format of 'key=value'") % metadata + raise argparse.ArgumentTypeError(msg) def _match_image(cs, wanted_properties):