From e911859388d5a6b05dbd0857a70b225ba219b732 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sat, 10 Feb 2018 19:31:10 +0000 Subject: [PATCH] Avoid empty values in 'mounts' options The python-zunclient sends the 'mounts' parameter to server in one of the following forms: 1. {'source': xxx, 'destination': xxx, 'size': ''} 2. {'size': xxx, 'destination': xxx, 'source': ''} This patch changes it to: 1. {'source': xxx, 'destination': xxx} 2. {'size': xxx, 'destination': xxx} The new form doesn't contain a key with empty string value. This allows a better validation on the server side in the future. Change-Id: Ic073098c8cd30bbf7e477c22bbdc59cb6f6bd18f Closes-Bug: #1747282 --- zunclient/common/utils.py | 11 ++++++----- zunclient/tests/unit/v1/test_containers_shell.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/zunclient/common/utils.py b/zunclient/common/utils.py index 242fafc0..9dc97155 100644 --- a/zunclient/common/utils.py +++ b/zunclient/common/utils.py @@ -205,7 +205,8 @@ def parse_mounts(mounts): "and mount to the container") parsed_mounts = [] for mount in mounts: - mount_info = {"source": "", "destination": "", "size": ""} + keys = ["source", "destination", "size"] + mount_info = {} for mnt in mount.split(","): try: k, v = mnt.split("=", 1) @@ -213,17 +214,17 @@ def parse_mounts(mounts): v = v.strip() except ValueError: raise apiexec.CommandError(err_msg % mnt) - if k in mount_info: - if mount_info[k]: + if k in keys: + if mount_info.get(k): raise apiexec.CommandError(err_msg % mnt) mount_info[k] = v else: raise apiexec.CommandError(err_msg % mnt) - if not mount_info['destination']: + if not mount_info.get('destination'): raise apiexec.CommandError(err_msg % mnt) - if not mount_info['source'] and not mount_info['size']: + if not mount_info.get('source') and not mount_info.get('size'): raise apiexec.CommandError(err_msg % mnt) parsed_mounts.append(mount_info) diff --git a/zunclient/tests/unit/v1/test_containers_shell.py b/zunclient/tests/unit/v1/test_containers_shell.py index 2aa27fe0..96bbbc4d 100644 --- a/zunclient/tests/unit/v1/test_containers_shell.py +++ b/zunclient/tests/unit/v1/test_containers_shell.py @@ -199,7 +199,7 @@ class ShellTest(shell_test_base.TestCommandLineArgument): self._test_arg_success( 'run --mount source=s,destination=d x') mock_show_container.assert_called_once_with('container') - mounts = [{'source': 's', 'destination': 'd', 'size': ''}] + mounts = [{'source': 's', 'destination': 'd'}] mock_run.assert_called_with( **_get_container_args(image='x', mounts=mounts))