|
27 | 27 |
|
28 | 28 | from openstackclient.image.v2 import image as _image |
29 | 29 | from openstackclient.tests.unit.image.v2 import fakes as image_fakes |
| 30 | +from openstackclient.tests.unit import utils as test_utils |
30 | 31 | from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes |
31 | 32 |
|
32 | 33 |
|
@@ -235,6 +236,128 @@ def test_image_create_file(self): |
235 | 236 | self.assertEqual(self.expected_columns, columns) |
236 | 237 | self.assertCountEqual(self.expected_data, data) |
237 | 238 |
|
| 239 | + def test_image_create_file_with_size(self): |
| 240 | + imagefile = tempfile.NamedTemporaryFile(delete=False) |
| 241 | + imagefile.write(b'\0') |
| 242 | + imagefile.close() |
| 243 | + |
| 244 | + arglist = [ |
| 245 | + '--file', |
| 246 | + imagefile.name, |
| 247 | + '--size', |
| 248 | + '2048', |
| 249 | + ( |
| 250 | + '--unprotected' |
| 251 | + if not self.new_image.is_protected |
| 252 | + else '--protected' |
| 253 | + ), |
| 254 | + ( |
| 255 | + '--public' |
| 256 | + if self.new_image.visibility == 'public' |
| 257 | + else '--private' |
| 258 | + ), |
| 259 | + '--property', |
| 260 | + 'Alpha=1', |
| 261 | + '--property', |
| 262 | + 'Beta=2', |
| 263 | + '--tag', |
| 264 | + self.new_image.tags[0], |
| 265 | + '--tag', |
| 266 | + self.new_image.tags[1], |
| 267 | + self.new_image.name, |
| 268 | + ] |
| 269 | + verifylist = [ |
| 270 | + ('filename', imagefile.name), |
| 271 | + ('size', 2048), |
| 272 | + ('is_protected', self.new_image.is_protected), |
| 273 | + ('visibility', self.new_image.visibility), |
| 274 | + ('properties', {'Alpha': '1', 'Beta': '2'}), |
| 275 | + ('tags', self.new_image.tags), |
| 276 | + ('name', self.new_image.name), |
| 277 | + ] |
| 278 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 279 | + |
| 280 | + columns, data = self.cmd.take_action(parsed_args) |
| 281 | + |
| 282 | + self.image_client.create_image.assert_called_with( |
| 283 | + name=self.new_image.name, |
| 284 | + allow_duplicates=True, |
| 285 | + container_format=_image.DEFAULT_CONTAINER_FORMAT, |
| 286 | + disk_format=_image.DEFAULT_DISK_FORMAT, |
| 287 | + is_protected=self.new_image.is_protected, |
| 288 | + visibility=self.new_image.visibility, |
| 289 | + Alpha='1', |
| 290 | + Beta='2', |
| 291 | + tags=self.new_image.tags, |
| 292 | + filename=imagefile.name, |
| 293 | + size=2048, |
| 294 | + ) |
| 295 | + self.image_client.get_image.assert_called_once_with(self.new_image) |
| 296 | + |
| 297 | + self.assertEqual(self.expected_columns, columns) |
| 298 | + self.assertCountEqual(self.expected_data, data) |
| 299 | + |
| 300 | + @mock.patch('sys.stdin', side_effect=[None]) |
| 301 | + def test_image_create_size_requires_upload(self, raw_input): |
| 302 | + arglist = [ |
| 303 | + '--size', |
| 304 | + '2048', |
| 305 | + self.new_image.name, |
| 306 | + ] |
| 307 | + verifylist = [ |
| 308 | + ('size', 2048), |
| 309 | + ('name', self.new_image.name), |
| 310 | + ] |
| 311 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 312 | + |
| 313 | + self.assertRaises( |
| 314 | + exceptions.CommandError, |
| 315 | + self.cmd.take_action, |
| 316 | + parsed_args, |
| 317 | + ) |
| 318 | + |
| 319 | + def test_image_create_size_must_be_positive(self): |
| 320 | + arglist = [ |
| 321 | + '--size', |
| 322 | + '0', |
| 323 | + self.new_image.name, |
| 324 | + ] |
| 325 | + self.assertRaises( |
| 326 | + test_utils.ParserException, |
| 327 | + self.check_parser, |
| 328 | + self.cmd, |
| 329 | + arglist, |
| 330 | + [], |
| 331 | + ) |
| 332 | + |
| 333 | + @mock.patch('openstackclient.image.v2.image.get_data_from_stdin') |
| 334 | + def test_image_create_stdin_with_size(self, mock_get_data_from_stdin): |
| 335 | + fake_stdin = io.BytesIO(b'some fake data') |
| 336 | + mock_get_data_from_stdin.return_value = fake_stdin |
| 337 | + |
| 338 | + arglist = [ |
| 339 | + '--size', |
| 340 | + '2048', |
| 341 | + self.new_image.name, |
| 342 | + ] |
| 343 | + verifylist = [ |
| 344 | + ('size', 2048), |
| 345 | + ('name', self.new_image.name), |
| 346 | + ] |
| 347 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 348 | + |
| 349 | + self.cmd.take_action(parsed_args) |
| 350 | + |
| 351 | + self.image_client.create_image.assert_called_with( |
| 352 | + name=self.new_image.name, |
| 353 | + allow_duplicates=True, |
| 354 | + container_format=_image.DEFAULT_CONTAINER_FORMAT, |
| 355 | + disk_format=_image.DEFAULT_DISK_FORMAT, |
| 356 | + data=fake_stdin, |
| 357 | + validate_checksum=False, |
| 358 | + size=2048, |
| 359 | + ) |
| 360 | + |
238 | 361 | @mock.patch('openstackclient.image.v2.image.get_data_from_stdin') |
239 | 362 | def test_image_create__progress_ignore_with_stdin( |
240 | 363 | self, |
@@ -2033,6 +2156,52 @@ def test_stage_image__from_stdin(self, mock_get_data_from_stdin): |
2033 | 2156 | data=fake_stdin, |
2034 | 2157 | ) |
2035 | 2158 |
|
| 2159 | + def test_stage_image__with_size(self): |
| 2160 | + imagefile = tempfile.NamedTemporaryFile(delete=False) |
| 2161 | + imagefile.write(b'\0' * 1024) |
| 2162 | + imagefile.close() |
| 2163 | + |
| 2164 | + arglist = [ |
| 2165 | + '--file', |
| 2166 | + imagefile.name, |
| 2167 | + '--size', |
| 2168 | + '2048', |
| 2169 | + self.image.name, |
| 2170 | + ] |
| 2171 | + verifylist = [ |
| 2172 | + ('filename', imagefile.name), |
| 2173 | + ('size', 2048), |
| 2174 | + ('image', self.image.name), |
| 2175 | + ] |
| 2176 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 2177 | + |
| 2178 | + self.cmd.take_action(parsed_args) |
| 2179 | + |
| 2180 | + self.image_client.stage_image.assert_called_once_with( |
| 2181 | + self.image, |
| 2182 | + filename=imagefile.name, |
| 2183 | + size=2048, |
| 2184 | + ) |
| 2185 | + |
| 2186 | + @mock.patch('sys.stdin', side_effect=[None]) |
| 2187 | + def test_stage_image__size_requires_upload(self, raw_input): |
| 2188 | + arglist = [ |
| 2189 | + '--size', |
| 2190 | + '2048', |
| 2191 | + self.image.name, |
| 2192 | + ] |
| 2193 | + verifylist = [ |
| 2194 | + ('size', 2048), |
| 2195 | + ('image', self.image.name), |
| 2196 | + ] |
| 2197 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 2198 | + |
| 2199 | + self.assertRaises( |
| 2200 | + exceptions.CommandError, |
| 2201 | + self.cmd.take_action, |
| 2202 | + parsed_args, |
| 2203 | + ) |
| 2204 | + |
2036 | 2205 |
|
2037 | 2206 | class TestImageImport(image_fakes.TestImagev2): |
2038 | 2207 | image = image_fakes.create_one_image( |
|
0 commit comments