From 6d21959e15b2b19c5e884a84d9d49c0892b87605 Mon Sep 17 00:00:00 2001 From: Stuart McLaren Date: Tue, 6 Jan 2015 17:17:10 +0000 Subject: [PATCH] v2: Allow upload from stdin on image-create For example: $ glance --os-image-api-version 2 image-create < /tmp/data This is consistent with v1. DocImpact Closes-bug: 1408033 Change-Id: Ifed4ece9e4e02a46d80b49a8e4fc372f1a304241 --- glanceclient/v2/shell.py | 5 +++-- tests/v2/test_shell_v2.py | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index e6ab8bac..978db4ff 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -43,7 +43,8 @@ def get_image_schema(): ' May be used multiple times.')) @utils.arg('--file', metavar='', help='Local file that contains disk image to be uploaded ' - 'during creation.') + 'during creation. Alternatively, images can be passed ' + 'to the client via stdin.') @utils.arg('--progress', action='store_true', default=False, help='Show upload progress bar.') def do_image_create(gc, args): @@ -66,7 +67,7 @@ def do_image_create(gc, args): "privileges to it" % file_name) image = gc.images.create(**fields) try: - if file_name is not None: + if utils.get_data_file(args) is not None: args.id = image['id'] args.size = None do_image_upload(gc, args) diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index 3480e5c9..fa150ada 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -131,9 +131,11 @@ class ShellV2Test(testtools.TestCase): utils.print_dict.assert_called_once_with({'id': 'pass'}, max_column_width=120) - def test_do_image_create_no_user_props(self): + @mock.patch('sys.stdin', autospec=True) + def test_do_image_create_no_user_props(self, mock_stdin): args = self._make_args({'name': 'IMG-01', 'disk_format': 'vhd', - 'container_format': 'bare'}) + 'container_format': 'bare', + 'file': None}) with mock.patch.object(self.gc.images, 'create') as mocked_create: ignore_fields = ['self', 'access', 'file', 'schema'] expect_image = dict([(field, field) for field in ignore_fields]) @@ -143,6 +145,9 @@ class ShellV2Test(testtools.TestCase): expect_image['container_format'] = 'bare' mocked_create.return_value = expect_image + # Ensure that the test stdin is not considered + # to be supplying image data + mock_stdin.isatty = lambda: True test_shell.do_image_create(self.gc, args) mocked_create.assert_called_once_with(name='IMG-01', @@ -193,9 +198,11 @@ class ShellV2Test(testtools.TestCase): except Exception: pass - def test_do_image_create_with_user_props(self): + @mock.patch('sys.stdin', autospec=True) + def test_do_image_create_with_user_props(self, mock_stdin): args = self._make_args({'name': 'IMG-01', - 'property': ['myprop=myval']}) + 'property': ['myprop=myval'], + 'file': None}) with mock.patch.object(self.gc.images, 'create') as mocked_create: ignore_fields = ['self', 'access', 'file', 'schema'] expect_image = dict([(field, field) for field in ignore_fields]) @@ -204,6 +211,9 @@ class ShellV2Test(testtools.TestCase): expect_image['myprop'] = 'myval' mocked_create.return_value = expect_image + # Ensure that the test stdin is not considered + # to be supplying image data + mock_stdin.isatty = lambda: True test_shell.do_image_create(self.gc, args) mocked_create.assert_called_once_with(name='IMG-01',