summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-07-26 14:15:35 +0000
committerGerrit Code Review <review@openstack.org>2017-07-26 14:15:35 +0000
commit982857abc8b9a5c02087358152f8fdba6059e381 (patch)
tree62ce906cc48339198cdf964c9644e4ff60c82175
parentca9ae01841bbf2d8fb69802882d1811f404c080d (diff)
parent1df55dd952fe52c1c1fc2583326d017275b01ddc (diff)
downloadpython-glanceclient-982857abc8b9a5c02087358152f8fdba6059e381.tar.gz
Merge "Validate input args before trying image download"
-rw-r--r--glanceclient/tests/unit/v2/test_shell_v2.py14
-rw-r--r--glanceclient/v2/shell.py15
2 files changed, 22 insertions, 7 deletions
diff --git a/glanceclient/tests/unit/v2/test_shell_v2.py b/glanceclient/tests/unit/v2/test_shell_v2.py
index fed1f72..4ac8c17 100644
--- a/glanceclient/tests/unit/v2/test_shell_v2.py
+++ b/glanceclient/tests/unit/v2/test_shell_v2.py
@@ -590,6 +590,20 @@ class ShellV2Test(testtools.TestCase):
test_shell.do_image_download(self.gc, args)
mocked_data.assert_called_once_with('IMG-01')
+ @mock.patch.object(utils, 'exit')
+ @mock.patch('sys.stdout', autospec=True)
+ def test_image_download_no_file_arg(self, mocked_stdout,
+ mocked_utils_exit):
+ # Indicate that no file name was given as command line argument
+ args = self._make_args({'id': '1234', 'file': None, 'progress': False})
+ # Indicate that no file is specified for output redirection
+ mocked_stdout.isatty = lambda: True
+ test_shell.do_image_download(self.gc, args)
+ mocked_utils_exit.assert_called_once_with(
+ 'No redirection or local file specified for downloaded image'
+ ' data. Please specify a local file with --file to save'
+ ' downloaded image or redirect output to another source.')
+
def test_do_image_delete(self):
args = argparse.Namespace(id=['image1', 'image2'])
with mock.patch.object(self.gc.images, 'delete') as mocked_delete:
diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py
index 5354db4..9ce6f96 100644
--- a/glanceclient/v2/shell.py
+++ b/glanceclient/v2/shell.py
@@ -278,6 +278,12 @@ def do_explain(gc, args):
help=_('Show download progress bar.'))
def do_image_download(gc, args):
"""Download a specific image."""
+ if sys.stdout.isatty() and (args.file is None):
+ msg = ('No redirection or local file specified for downloaded image '
+ 'data. Please specify a local file with --file to save '
+ 'downloaded image or redirect output to another source.')
+ utils.exit(msg)
+
try:
body = gc.images.data(args.id)
except (exc.HTTPForbidden, exc.HTTPException) as e:
@@ -290,13 +296,8 @@ def do_image_download(gc, args):
if args.progress:
body = progressbar.VerboseIteratorWrapper(body, len(body))
- if not (sys.stdout.isatty() and args.file is None):
- utils.save_image(body, args.file)
- else:
- msg = ('No redirection or local file specified for downloaded image '
- 'data. Please specify a local file with --file to save '
- 'downloaded image or redirect output to another source.')
- utils.exit(msg)
+
+ utils.save_image(body, args.file)
@utils.arg('--file', metavar='<FILE>',