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
This commit is contained in:
Dinesh Bhor 2017-02-14 13:10:33 +05:30 committed by dineshbhor
parent 45c501ff51
commit 72a0ec9adc
2 changed files with 27 additions and 1 deletions

View File

@ -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})

View File

@ -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):