From 86191005979256512ecbd026828d1968ab3b7780 Mon Sep 17 00:00:00 2001 From: wanghong Date: Mon, 24 Mar 2014 19:01:04 +0800 Subject: [PATCH] progress flag not supported in v2 API Currently only download method supports --progress flag in v2 API. This patch let upload method in v2 API support this flag too. Change-Id: I1d22379c320adb47a2178697e546413b9257f987 Closes-Bug: #1286265 --- glanceclient/v2/shell.py | 9 +++++++-- tests/v2/test_shell_v2.py | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index c769a8f7..36cb1771 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -215,19 +215,24 @@ def do_image_download(gc, args): @utils.arg('--file', metavar='', - help=('Local file that contains disk image to be uploaded' - ' during creation. Alternatively, images can be passed' + help=('Local file that contains disk image to be uploaded.' + ' Alternatively, images can be passed' ' to the client via stdin.')) @utils.arg('--size', metavar='', type=int, help='Size in bytes of image to be uploaded. Default is to get ' 'size from provided data object but this is supported in case ' 'where size cannot be inferred.', default=None) +@utils.arg('--progress', action='store_true', default=False, + help='Show upload progress bar.') @utils.arg('id', metavar='', help='ID of image to upload data to.') def do_image_upload(gc, args): """Upload data for a specific image.""" image_data = utils.get_data_file(args) + if args.progress: + filesize = utils.get_file_size(image_data) + image_data = progressbar.VerboseFileWrapper(image_data, filesize) gc.images.upload(args.id, image_data, args.size) diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index ff144704..fb541f84 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -221,7 +221,8 @@ class ShellV2Test(testtools.TestCase): self.gc.schemas.get.assert_called_once_with('test') def test_image_upload(self): - args = self._make_args({'id': 'IMG-01', 'file': 'test', 'size': 1024}) + args = self._make_args( + {'id': 'IMG-01', 'file': 'test', 'size': 1024, 'progress': False}) with mock.patch.object(self.gc.images, 'upload') as mocked_upload: utils.get_data_file = mock.Mock(return_value='testfile') @@ -229,6 +230,18 @@ class ShellV2Test(testtools.TestCase): test_shell.do_image_upload(self.gc, args) mocked_upload.assert_called_once_with('IMG-01', 'testfile', 1024) + def test_image_upload_with_progressbar(self): + args = self._make_args( + {'id': 'IMG-01', 'file': 'test', 'size': 1024, 'progress': True}) + + with mock.patch.object(self.gc.images, 'upload') as mocked_upload: + utils.get_data_file = mock.Mock(return_value='testfile') + utils.get_file_size = mock.Mock(return_value=8) + mocked_upload.return_value = None + test_shell.do_image_upload(self.gc, args) + self.assertIsInstance(mocked_upload.call_args[0][1], + progressbar.VerboseFileWrapper) + def test_image_download(self): args = self._make_args( {'id': 'pass', 'file': 'test', 'progress': False})