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
This commit is contained in:
yatin karel 2015-03-22 16:52:34 +05:30
parent 26280ed58b
commit c149a94ee1
3 changed files with 29 additions and 4 deletions

View File

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

View File

@ -169,12 +169,14 @@ def do_image_list(gc, args):
@utils.arg('id', metavar='<IMAGE_ID>', 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='<integer>', 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='<IMAGE_ID>', required=True,

View File

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