Commit 956631b6 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

cli: Handle negative numbers when parsing key-value assignments



To remove the last disk, or to add a NIC to the end, one can use the
index -1. This wouldn't work as intended as “-” is a special prefix.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 5401c39d
......@@ -552,7 +552,9 @@ def check_ident_key_val(option, opt, value): # pylint: disable=W0613
msg = "Cannot pass options when removing parameter groups: %s" % value
raise errors.ParameterError(msg)
retval = (ident[len(NO_PREFIX):], False)
elif ident.startswith(UN_PREFIX):
elif (ident.startswith(UN_PREFIX) and
(len(ident) <= len(UN_PREFIX) or
not ident[len(UN_PREFIX)][0].isdigit())):
if rest:
msg = "Cannot pass options when removing parameter groups: %s" % value
raise errors.ParameterError(msg)
......
......@@ -85,6 +85,7 @@ class TestSplitKeyVal(unittest.TestCase):
"""Test how we handle splitting an empty string"""
self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
class TestIdentKeyVal(unittest.TestCase):
"""Testing case for cli.check_ident_key_val"""
......@@ -102,6 +103,17 @@ class TestIdentKeyVal(unittest.TestCase):
self.assertEqual(cikv("-foo"), ("foo", None))
self.assertRaises(ParameterError, cikv, "-foo:a=c")
# Check negative numbers
self.assertEqual(cikv("-1:remove"), ("-1", {
"remove": True,
}))
self.assertEqual(cikv("-29447:add,size=4G"), ("-29447", {
"add": True,
"size": "4G",
}))
for i in ["-:", "-"]:
self.assertEqual(cikv(i), ("", None))
class TestToStream(unittest.TestCase):
"""Test the ToStream functions"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment