Skip to content

Commit 3707717

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "To display image size in human friendly format"
2 parents d034b98 + 6962cc9 commit 3707717

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
@@ -713,6 +713,12 @@ class ShowImage(command.ShowOne):
713713

714714
def get_parser(self, prog_name):
715715
parser = super(ShowImage, self).get_parser(prog_name)
716+
parser.add_argument(
717+
"--human-readable",
718+
default=False,
719+
action='store_true',
720+
help=_("Print image size in a human-friendly format."),
721+
)
716722
parser.add_argument(
717723
"image",
718724
metavar="<image>",
@@ -729,5 +735,8 @@ def take_action(self, parsed_args):
729735

730736
info = {}
731737
info.update(image._info)
738+
if parsed_args.human_readable:
739+
if 'size' in info:
740+
info['size'] = utils.format_size(info['size'])
732741
info['properties'] = utils.format_dict(info.get('properties', {}))
733742
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
@@ -905,6 +905,12 @@ class ShowImage(command.ShowOne):
905905

906906
def get_parser(self, prog_name):
907907
parser = super(ShowImage, self).get_parser(prog_name)
908+
parser.add_argument(
909+
"--human-readable",
910+
default=False,
911+
action='store_true',
912+
help=_("Print image size in a human-friendly format."),
913+
)
908914
parser.add_argument(
909915
"image",
910916
metavar="<image>",
@@ -918,6 +924,8 @@ def take_action(self, parsed_args):
918924
image_client.images,
919925
parsed_args.image,
920926
)
927+
if parsed_args.human_readable:
928+
image['size'] = utils.format_size(image['size'])
921929

922930
info = _format_image(image)
923931
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
@@ -1288,6 +1288,9 @@ def test_image_set_dead_options(self):
12881288

12891289
class TestImageShow(TestImage):
12901290

1291+
new_image = image_fakes.FakeImage.create_one_image(
1292+
attrs={'size': 1000})
1293+
12911294
def setUp(self):
12921295
super(TestImageShow, self).setUp()
12931296

@@ -1322,6 +1325,29 @@ def test_image_show(self):
13221325
self.assertEqual(image_fakes.IMAGE_columns, columns)
13231326
self.assertEqual(image_fakes.IMAGE_SHOW_data, data)
13241327

1328+
def test_image_show_human_readable(self):
1329+
self.images_mock.get.return_value = self.new_image
1330+
arglist = [
1331+
'--human-readable',
1332+
self.new_image.id,
1333+
]
1334+
verifylist = [
1335+
('human_readable', True),
1336+
('image', self.new_image.id),
1337+
]
1338+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1339+
1340+
# In base command class ShowOne in cliff, abstract method take_action()
1341+
# returns a two-part tuple with a tuple of column names and a tuple of
1342+
# data to be shown.
1343+
columns, data = self.cmd.take_action(parsed_args)
1344+
self.images_mock.get.assert_called_with(
1345+
self.new_image.id,
1346+
)
1347+
1348+
size_index = columns.index('size')
1349+
self.assertEqual(data[size_index], '1K')
1350+
13251351

13261352
class TestImageUnset(TestImage):
13271353

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)