From 7053498f3d8e6d772fc21a7f13e6940b4b28e84a Mon Sep 17 00:00:00 2001 From: Nathan Reller Date: Tue, 5 Feb 2013 19:00:26 -0500 Subject: [PATCH] Correct parsing of volume metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Volume metadata supplied via the command line is incorrectly parsed, converting each character into a key with “None” as the corresponding value. This bug fix corrects the parsing and also includes regression tests when metadata is provided when creating a volume. Change-Id: I8c093df59ea4de2e2a1b7cf3ac7c4624c159fb66 Implements: blueprint encrypt-cinder-volumes --- cinderclient/v1/shell.py | 3 +-- tests/v1/test_shell.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cinderclient/v1/shell.py b/cinderclient/v1/shell.py index 2bb2d4b1b..367ceb5a5 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -94,7 +94,7 @@ def _translate_volume_snapshot_keys(collection): def _extract_metadata(args): metadata = {} - for metadatum in args.metadata[0]: + for metadatum in args.metadata: # unset doesn't require a val, so we have the if/else if '=' in metadatum: (key, value) = metadatum.split('=', 1) @@ -288,7 +288,6 @@ def do_rename(cs, args): @utils.arg('metadata', metavar='', nargs='+', - action='append', default=[], help='Metadata to set/unset (only key is necessary on unset)') @utils.service_type('volume') diff --git a/tests/v1/test_shell.py b/tests/v1/test_shell.py index 7b8f74c4d..ad88918d5 100644 --- a/tests/v1/test_shell.py +++ b/tests/v1/test_shell.py @@ -21,6 +21,7 @@ import fixtures from cinderclient import client from cinderclient import shell +from cinderclient.v1 import shell as shell_v1 from tests.v1 import fakes from tests import utils @@ -70,6 +71,25 @@ class ShellTest(utils.TestCase): def assert_called_anytime(self, method, url, body=None): return self.shell.cs.assert_called_anytime(method, url, body) + def test_extract_metadata(self): + # mimic the result of argparse's parse_args() method + class Arguments: + def __init__(self, metadata=[]): + self.metadata = metadata + + inputs = [ + ([], {}), + (["key=value"], {"key": "value"}), + (["key"], {"key": None}), + (["k1=v1", "k2=v2"], {"k1": "v1", "k2": "v2"}), + (["k1=v1", "k2"], {"k1": "v1", "k2": None}), + (["k1", "k2=v2"], {"k1": None, "k2": "v2"}) + ] + + for input in inputs: + args = Arguments(metadata=input[0]) + self.assertEquals(shell_v1._extract_metadata(args), input[1]) + def test_list(self): self.run_command('list') # NOTE(jdg): we default to detail currently