diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 8288346f..7f115df5 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 c1a0a2e9..25d7d99e 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -133,9 +133,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]) @@ -145,6 +147,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', @@ -195,9 +200,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]) @@ -206,6 +213,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',