Skip to content

Commit 13fe801

Browse files
Hang Yangstephenfin
authored andcommitted
Support image save with chunk-size option
Add '--chunk-size' option to 'image save' command to control the size of bytes to read at one time. Change-Id: I0a02323384433010b8c6804a4337040acb13da8f Signed-off-by: Hang Yang <hangyang@verizonmedia.com>
1 parent 37228ae commit 13fe801

5 files changed

Lines changed: 107 additions & 3 deletions

File tree

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
@@ -1066,6 +1066,16 @@ class SaveImage(command.Command):
10661066

10671067
def get_parser(self, prog_name):
10681068
parser = super().get_parser(prog_name)
1069+
parser.add_argument(
1070+
"--chunk-size",
1071+
type=int,
1072+
default=1024,
1073+
metavar="<chunk-size>",
1074+
help=_(
1075+
"Size in bytes to read from the wire and buffer at one "
1076+
"time (default: 1024)"
1077+
),
1078+
)
10691079
parser.add_argument(
10701080
"--file",
10711081
metavar="<filename>",
@@ -1090,7 +1100,12 @@ def take_action(self, parsed_args):
10901100
if output_file is None:
10911101
output_file = getattr(sys.stdout, "buffer", sys.stdout)
10921102

1093-
image_client.download_image(image.id, stream=True, output=output_file)
1103+
image_client.download_image(
1104+
image.id,
1105+
stream=True,
1106+
output=output_file,
1107+
chunk_size=parsed_args.chunk_size,
1108+
)
10941109

10951110

10961111
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
@@ -2228,7 +2228,29 @@ def test_save_data(self):
22282228
self.cmd.take_action(parsed_args)
22292229

22302230
self.image_client.download_image.assert_called_once_with(
2231-
self.image.id, stream=True, output='/path/to/file'
2231+
self.image.id, output='/path/to/file', stream=True, chunk_size=1024
2232+
)
2233+
2234+
def test_save_data_with_chunk_size(self):
2235+
arglist = [
2236+
'--file',
2237+
'/path/to/file',
2238+
'--chunk-size',
2239+
'2048',
2240+
self.image.id,
2241+
]
2242+
2243+
verifylist = [
2244+
('filename', '/path/to/file'),
2245+
('chunk_size', 2048),
2246+
('image', self.image.id),
2247+
]
2248+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
2249+
2250+
self.cmd.take_action(parsed_args)
2251+
2252+
self.image_client.download_image.assert_called_once_with(
2253+
self.image.id, output='/path/to/file', stream=True, chunk_size=2048
22322254
)
22332255

22342256

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)