Skip to content

Commit 6962cc9

Browse files
PriyaBoopalanstevemar
authored andcommitted
To display image size in human friendly format
Include option '--human-readable' to 'image show' command. This option displays image size in human readable format (such as K, M, G, T,..) Related Commit: I0ef74c2ec978483fe49156c88acf5c369a8fa5c2 Closes-Bug: #1640086 Change-Id: I28cd5702925d51303d0607ed8dccf12c56434682
1 parent 5bf5de6 commit 6962cc9

7 files changed

Lines changed: 83 additions & 4 deletions

File tree

openstackclient/image/v1/image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,12 @@ class ShowImage(command.ShowOne):
706706

707707
def get_parser(self, prog_name):
708708
parser = super(ShowImage, self).get_parser(prog_name)
709+
parser.add_argument(
710+
"--human-readable",
711+
default=False,
712+
action='store_true',
713+
help=_("Print image size in a human-friendly format."),
714+
)
709715
parser.add_argument(
710716
"image",
711717
metavar="<image>",
@@ -722,5 +728,8 @@ def take_action(self, parsed_args):
722728

723729
info = {}
724730
info.update(image._info)
731+
if parsed_args.human_readable:
732+
if 'size' in info:
733+
info['size'] = utils.format_size(info['size'])
725734
info['properties'] = utils.format_dict(info.get('properties', {}))
726735
return zip(*sorted(six.iteritems(info)))

openstackclient/image/v2/image.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,12 @@ class ShowImage(command.ShowOne):
848848

849849
def get_parser(self, prog_name):
850850
parser = super(ShowImage, self).get_parser(prog_name)
851+
parser.add_argument(
852+
"--human-readable",
853+
default=False,
854+
action='store_true',
855+
help=_("Print image size in a human-friendly format."),
856+
)
851857
parser.add_argument(
852858
"image",
853859
metavar="<image>",
@@ -861,6 +867,8 @@ def take_action(self, parsed_args):
861867
image_client.images,
862868
parsed_args.image,
863869
)
870+
if parsed_args.human_readable:
871+
image['size'] = utils.format_size(image['size'])
864872

865873
info = _format_image(image)
866874
return zip(*sorted(six.iteritems(info)))

openstackclient/tests/unit/image/v1/fakes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
}
3535
image_properties_str = "Alpha='a', Beta='b', Gamma='g'"
3636
image_data = 'line 1\nline 2\n'
37+
image_size = 0
3738

3839
IMAGE = {
3940
'id': image_id,
@@ -46,6 +47,7 @@
4647
'is_public': image_public,
4748
'protected': image_protected,
4849
'properties': image_properties,
50+
'size': image_size,
4951
}
5052

5153
IMAGE_columns = tuple(sorted(IMAGE))

openstackclient/tests/unit/image/v1/test_image.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ def test_image_update_volume(self):
692692

693693
class TestImageShow(TestImage):
694694

695-
_image = image_fakes.FakeImage.create_one_image()
695+
_image = image_fakes.FakeImage.create_one_image(
696+
attrs={'size': 2000})
696697
columns = (
697698
'container_format',
698699
'disk_format',
@@ -704,6 +705,7 @@ class TestImageShow(TestImage):
704705
'owner',
705706
'properties',
706707
'protected',
708+
'size',
707709
)
708710
data = (
709711
_image.container_format,
@@ -716,6 +718,7 @@ class TestImageShow(TestImage):
716718
_image.owner,
717719
utils.format_dict(_image.properties),
718720
_image.protected,
721+
_image.size,
719722
)
720723

721724
def setUp(self):
@@ -745,3 +748,25 @@ def test_image_show(self):
745748

746749
self.assertEqual(self.columns, columns)
747750
self.assertEqual(self.data, data)
751+
752+
def test_image_show_human_readable(self):
753+
arglist = [
754+
'--human-readable',
755+
self._image.id,
756+
]
757+
verifylist = [
758+
('human_readable', True),
759+
('image', self._image.id),
760+
]
761+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
762+
763+
# In base command class ShowOne in cliff, abstract method take_action()
764+
# returns a two-part tuple with a tuple of column names and a tuple of
765+
# data to be shown.
766+
columns, data = self.cmd.take_action(parsed_args)
767+
self.images_mock.get.assert_called_with(
768+
self._image.id,
769+
)
770+
771+
size_index = columns.index('size')
772+
self.assertEqual(data[size_index], '2K')

openstackclient/tests/unit/image/v2/fakes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@
3232
image_protected = False
3333
image_visibility = 'public'
3434
image_tags = []
35+
image_size = 0
3536

3637
IMAGE = {
3738
'id': image_id,
3839
'name': image_name,
3940
'owner': image_owner,
4041
'protected': image_protected,
4142
'visibility': image_visibility,
42-
'tags': image_tags
43+
'tags': image_tags,
44+
'size': image_size
4345
}
4446

4547
IMAGE_columns = tuple(sorted(IMAGE))
@@ -106,7 +108,8 @@
106108
"size": {
107109
"type": [
108110
"null",
109-
"integer"
111+
"integer",
112+
"string"
110113
],
111114
"description": "Size of image file in bytes (READ-ONLY)"
112115
},
@@ -185,7 +188,7 @@ def create_one_image(attrs=None):
185188
A dictionary with all attrbutes of image
186189
:return:
187190
A FakeResource object with id, name, owner, protected,
188-
visibility and tags attrs
191+
visibility, tags and size attrs
189192
"""
190193
attrs = attrs or {}
191194

openstackclient/tests/unit/image/v2/test_image.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,9 @@ def test_image_set_dead_options(self):
11771177

11781178
class TestImageShow(TestImage):
11791179

1180+
new_image = image_fakes.FakeImage.create_one_image(
1181+
attrs={'size': 1000})
1182+
11801183
def setUp(self):
11811184
super(TestImageShow, self).setUp()
11821185

@@ -1211,6 +1214,29 @@ def test_image_show(self):
12111214
self.assertEqual(image_fakes.IMAGE_columns, columns)
12121215
self.assertEqual(image_fakes.IMAGE_SHOW_data, data)
12131216

1217+
def test_image_show_human_readable(self):
1218+
self.images_mock.get.return_value = self.new_image
1219+
arglist = [
1220+
'--human-readable',
1221+
self.new_image.id,
1222+
]
1223+
verifylist = [
1224+
('human_readable', True),
1225+
('image', self.new_image.id),
1226+
]
1227+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1228+
1229+
# In base command class ShowOne in cliff, abstract method take_action()
1230+
# returns a two-part tuple with a tuple of column names and a tuple of
1231+
# data to be shown.
1232+
columns, data = self.cmd.take_action(parsed_args)
1233+
self.images_mock.get.assert_called_with(
1234+
self.new_image.id,
1235+
)
1236+
1237+
size_index = columns.index('size')
1238+
self.assertEqual(data[size_index], '1K')
1239+
12141240

12151241
class TestImageUnset(TestImage):
12161242

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--human-readable`` option to ``image show`` to display
5+
image size in human readable format (such as K, M, G, T,..)
6+
[Bug `1640086 <https://bugs.launchpad.net/bugs/1640086>`_]

0 commit comments

Comments
 (0)