|
15 | 15 | # under the License. |
16 | 16 | import json |
17 | 17 | import mock |
| 18 | +import os |
| 19 | +import tempfile |
18 | 20 | import testtools |
19 | 21 |
|
20 | 22 | from glanceclient.common import utils |
@@ -150,6 +152,47 @@ def test_do_image_create_no_user_props(self): |
150 | 152 | 'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd', |
151 | 153 | 'container_format': 'bare'}) |
152 | 154 |
|
| 155 | + def test_do_image_create_with_file(self): |
| 156 | + try: |
| 157 | + file_name = None |
| 158 | + with open(tempfile.mktemp(), 'w+') as f: |
| 159 | + f.write('Some data here') |
| 160 | + f.flush() |
| 161 | + f.seek(0) |
| 162 | + file_name = f.name |
| 163 | + temp_args = {'name': 'IMG-01', |
| 164 | + 'disk_format': 'vhd', |
| 165 | + 'container_format': 'bare', |
| 166 | + 'file': file_name, |
| 167 | + 'progress': False} |
| 168 | + args = self._make_args(temp_args) |
| 169 | + with mock.patch.object(self.gc.images, 'create') as mocked_create: |
| 170 | + with mock.patch.object(self.gc.images, 'get') as mocked_get: |
| 171 | + |
| 172 | + ignore_fields = ['self', 'access', 'schema'] |
| 173 | + expect_image = dict([(field, field) for field in |
| 174 | + ignore_fields]) |
| 175 | + expect_image['id'] = 'pass' |
| 176 | + expect_image['name'] = 'IMG-01' |
| 177 | + expect_image['disk_format'] = 'vhd' |
| 178 | + expect_image['container_format'] = 'bare' |
| 179 | + mocked_create.return_value = expect_image |
| 180 | + mocked_get.return_value = expect_image |
| 181 | + |
| 182 | + test_shell.do_image_create(self.gc, args) |
| 183 | + |
| 184 | + temp_args.pop('file', None) |
| 185 | + mocked_create.assert_called_once_with(**temp_args) |
| 186 | + mocked_get.assert_called_once_with('pass') |
| 187 | + utils.print_dict.assert_called_once_with({ |
| 188 | + 'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd', |
| 189 | + 'container_format': 'bare'}) |
| 190 | + finally: |
| 191 | + try: |
| 192 | + os.remove(f.name) |
| 193 | + except Exception: |
| 194 | + pass |
| 195 | + |
153 | 196 | def test_do_image_create_with_user_props(self): |
154 | 197 | args = self._make_args({'name': 'IMG-01', |
155 | 198 | 'property': ['myprop=myval']}) |
|
0 commit comments