Skip to content

Commit d90e18b

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Support image save with chunk-size option"
2 parents 6ba9473 + 13fe801 commit d90e18b

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

openstackclient/image/v1/image.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ class SaveImage(command.Command):
528528

529529
def get_parser(self, prog_name):
530530
parser = super().get_parser(prog_name)
531+
parser.add_argument(
532+
"--chunk-size",
533+
type=int,
534+
default=1024,
535+
metavar="<chunk-size>",
536+
help=_(
537+
"Size in bytes to read from the wire and buffer at one "
538+
"time (default: 1024)"
539+
),
540+
)
531541
parser.add_argument(
532542
"--file",
533543
metavar="<filename>",
@@ -550,7 +560,12 @@ def take_action(self, parsed_args):
550560
if output_file is None:
551561
output_file = getattr(sys.stdout, "buffer", sys.stdout)
552562

553-
image_client.download_image(image.id, stream=True, output=output_file)
563+
image_client.download_image(
564+
image.id,
565+
stream=True,
566+
output=output_file,
567+
chunk_size=parsed_args.chunk_size,
568+
)
554569

555570

556571
class SetImage(command.Command):

openstackclient/image/v2/image.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,16 @@ class SaveImage(command.Command):
10731073

10741074
def get_parser(self, prog_name):
10751075
parser = super().get_parser(prog_name)
1076+
parser.add_argument(
1077+
"--chunk-size",
1078+
type=int,
1079+
default=1024,
1080+
metavar="<chunk-size>",
1081+
help=_(
1082+
"Size in bytes to read from the wire and buffer at one "
1083+
"time (default: 1024)"
1084+
),
1085+
)
10761086
parser.add_argument(
10771087
"--file",
10781088
metavar="<filename>",
@@ -1097,7 +1107,12 @@ def take_action(self, parsed_args):
10971107
if output_file is None:
10981108
output_file = getattr(sys.stdout, "buffer", sys.stdout)
10991109

1100-
image_client.download_image(image.id, stream=True, output=output_file)
1110+
image_client.download_image(
1111+
image.id,
1112+
stream=True,
1113+
output=output_file,
1114+
chunk_size=parsed_args.chunk_size,
1115+
)
11011116

11021117

11031118
class SetImage(command.Command):

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,50 @@ def test_image_show_human_readable(self):
757757

758758
size_index = columns.index('size')
759759
self.assertEqual(data[size_index].human_readable(), '2K')
760+
761+
762+
class TestImageSave(image_fakes.TestImagev1):
763+
image = image_fakes.create_one_image({})
764+
765+
def setUp(self):
766+
super().setUp()
767+
768+
self.image_client.find_image.return_value = self.image
769+
self.image_client.download_image.return_value = self.image
770+
771+
# Get the command object to test
772+
self.cmd = image.SaveImage(self.app, None)
773+
774+
def test_save_data(self):
775+
arglist = ['--file', '/path/to/file', self.image.id]
776+
777+
verifylist = [('file', '/path/to/file'), ('image', self.image.id)]
778+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
779+
780+
self.cmd.take_action(parsed_args)
781+
782+
self.image_client.download_image.assert_called_once_with(
783+
self.image.id, output='/path/to/file', stream=True, chunk_size=1024
784+
)
785+
786+
def test_save_data_with_chunk_size(self):
787+
arglist = [
788+
'--file',
789+
'/path/to/file',
790+
'--chunk-size',
791+
'2048',
792+
self.image.id,
793+
]
794+
795+
verifylist = [
796+
('file', '/path/to/file'),
797+
('chunk_size', 2048),
798+
('image', self.image.id),
799+
]
800+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
801+
802+
self.cmd.take_action(parsed_args)
803+
804+
self.image_client.download_image.assert_called_once_with(
805+
self.image.id, output='/path/to/file', stream=True, chunk_size=2048
806+
)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,29 @@ def test_save_data(self):
22382238
self.cmd.take_action(parsed_args)
22392239

22402240
self.image_client.download_image.assert_called_once_with(
2241-
self.image.id, stream=True, output='/path/to/file'
2241+
self.image.id, output='/path/to/file', stream=True, chunk_size=1024
2242+
)
2243+
2244+
def test_save_data_with_chunk_size(self):
2245+
arglist = [
2246+
'--file',
2247+
'/path/to/file',
2248+
'--chunk-size',
2249+
'2048',
2250+
self.image.id,
2251+
]
2252+
2253+
verifylist = [
2254+
('filename', '/path/to/file'),
2255+
('chunk_size', 2048),
2256+
('image', self.image.id),
2257+
]
2258+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
2259+
2260+
self.cmd.take_action(parsed_args)
2261+
2262+
self.image_client.download_image.assert_called_once_with(
2263+
self.image.id, output='/path/to/file', stream=True, chunk_size=2048
22422264
)
22432265

22442266

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``--chunk-size`` option to ``image save`` command to control the size
5+
of bytes to read at one time.

0 commit comments

Comments
 (0)