From c149a94ee1c8c5a04b984063cadf1dbc934eeb8b Mon Sep 17 00:00:00 2001 From: yatin karel Date: Sun, 22 Mar 2015 16:52:34 +0530 Subject: [PATCH] glance image-show now have --human-readable option Added option '--human-readable' to image-show cli which allows users to display image size in human-readable format. Change-Id: Ic3452ce4560d3cf90fa7f59f98e5ff42e804f8c9 Closes-Bug: #1434381 --- glanceclient/common/utils.py | 4 +++- glanceclient/v2/shell.py | 4 +++- tests/v2/test_shell_v2.py | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index e51643fd..b199bf35 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -372,10 +372,12 @@ def strip_version(endpoint): return endpoint, version -def print_image(image_obj, max_col_width=None): +def print_image(image_obj, human_readable=False, max_col_width=None): ignore = ['self', 'access', 'file', 'schema'] image = dict([item for item in six.iteritems(image_obj) if item[0] not in ignore]) + if human_readable: + image['size'] = make_size_human_readable(image['size']) if str(max_col_width).isdigit(): print_dict(image, max_column_width=max_col_width) else: diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 31d02532..2d79dbf9 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -169,12 +169,14 @@ def do_image_list(gc, args): @utils.arg('id', metavar='', help='ID of image to describe.') +@utils.arg('--human-readable', action='store_true', default=False, + help='Print image size in a human-friendly format.') @utils.arg('--max-column-width', metavar='', default=80, help='The max column width of the printed table.') def do_image_show(gc, args): """Describe a specific image.""" image = gc.images.get(args.id) - utils.print_image(image, int(args.max_column_width)) + utils.print_image(image, args.human_readable, int(args.max_column_width)) @utils.arg('--image-id', metavar='', required=True, diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index fc8f2db5..33985b90 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -194,19 +194,40 @@ class ShellV2Test(testtools.TestCase): filters=exp_img_filters) utils.print_list.assert_called_once_with({}, ['ID', 'Name']) - def test_do_image_show(self): + def test_do_image_show_human_readable(self): args = self._make_args({'id': 'pass', 'page_size': 18, + 'human_readable': True, 'max_column_width': 120}) with mock.patch.object(self.gc.images, 'get') as mocked_list: ignore_fields = ['self', 'access', 'file', 'schema'] expect_image = dict([(field, field) for field in ignore_fields]) expect_image['id'] = 'pass' + expect_image['size'] = 1024 mocked_list.return_value = expect_image test_shell.do_image_show(self.gc, args) mocked_list.assert_called_once_with('pass') - utils.print_dict.assert_called_once_with({'id': 'pass'}, + utils.print_dict.assert_called_once_with({'id': 'pass', + 'size': '1kB'}, + max_column_width=120) + + def test_do_image_show(self): + args = self._make_args({'id': 'pass', 'page_size': 18, + 'human_readable': False, + 'max_column_width': 120}) + with mock.patch.object(self.gc.images, 'get') as mocked_list: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['size'] = 1024 + mocked_list.return_value = expect_image + + test_shell.do_image_show(self.gc, args) + + mocked_list.assert_called_once_with('pass') + utils.print_dict.assert_called_once_with({'id': 'pass', + 'size': 1024}, max_column_width=120) @mock.patch('sys.stdin', autospec=True)