|
13 | 13 |
|
14 | 14 | import copy |
15 | 15 | import mock |
| 16 | +from mock import call |
16 | 17 |
|
| 18 | +from osc_lib import exceptions |
17 | 19 | from osc_lib import utils |
18 | 20 |
|
19 | 21 | from openstackclient.network.v2 import subnet as subnet_v2 |
@@ -361,32 +363,82 @@ def test_create_options_subnet_range_ipv6(self): |
361 | 363 |
|
362 | 364 | class TestDeleteSubnet(TestSubnet): |
363 | 365 |
|
364 | | - # The subnet to delete. |
365 | | - _subnet = network_fakes.FakeSubnet.create_one_subnet() |
| 366 | + # The subnets to delete. |
| 367 | + _subnets = network_fakes.FakeSubnet.create_subnets(count=2) |
366 | 368 |
|
367 | 369 | def setUp(self): |
368 | 370 | super(TestDeleteSubnet, self).setUp() |
369 | 371 |
|
370 | 372 | self.network.delete_subnet = mock.Mock(return_value=None) |
371 | 373 |
|
372 | | - self.network.find_subnet = mock.Mock(return_value=self._subnet) |
| 374 | + self.network.find_subnet = ( |
| 375 | + network_fakes.FakeSubnet.get_subnets(self._subnets)) |
373 | 376 |
|
374 | 377 | # Get the command object to test |
375 | 378 | self.cmd = subnet_v2.DeleteSubnet(self.app, self.namespace) |
376 | 379 |
|
377 | | - def test_delete(self): |
| 380 | + def test_subnet_delete(self): |
378 | 381 | arglist = [ |
379 | | - self._subnet.name, |
| 382 | + self._subnets[0].name, |
380 | 383 | ] |
381 | 384 | verifylist = [ |
382 | | - ('subnet', self._subnet.name), |
| 385 | + ('subnet', [self._subnets[0].name]), |
383 | 386 | ] |
384 | 387 | parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
385 | 388 |
|
386 | 389 | result = self.cmd.take_action(parsed_args) |
387 | | - self.network.delete_subnet.assert_called_once_with(self._subnet) |
| 390 | + self.network.delete_subnet.assert_called_once_with(self._subnets[0]) |
388 | 391 | self.assertIsNone(result) |
389 | 392 |
|
| 393 | + def test_multi_subnets_delete(self): |
| 394 | + arglist = [] |
| 395 | + verifylist = [] |
| 396 | + |
| 397 | + for s in self._subnets: |
| 398 | + arglist.append(s.name) |
| 399 | + verifylist = [ |
| 400 | + ('subnet', arglist), |
| 401 | + ] |
| 402 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 403 | + |
| 404 | + result = self.cmd.take_action(parsed_args) |
| 405 | + |
| 406 | + calls = [] |
| 407 | + for s in self._subnets: |
| 408 | + calls.append(call(s)) |
| 409 | + self.network.delete_subnet.assert_has_calls(calls) |
| 410 | + self.assertIsNone(result) |
| 411 | + |
| 412 | + def test_multi_subnets_delete_with_exception(self): |
| 413 | + arglist = [ |
| 414 | + self._subnets[0].name, |
| 415 | + 'unexist_subnet', |
| 416 | + ] |
| 417 | + verifylist = [ |
| 418 | + ('subnet', |
| 419 | + [self._subnets[0].name, 'unexist_subnet']), |
| 420 | + ] |
| 421 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 422 | + |
| 423 | + find_mock_result = [self._subnets[0], exceptions.CommandError] |
| 424 | + self.network.find_subnet = ( |
| 425 | + mock.MagicMock(side_effect=find_mock_result) |
| 426 | + ) |
| 427 | + |
| 428 | + try: |
| 429 | + self.cmd.take_action(parsed_args) |
| 430 | + self.fail('CommandError should be raised.') |
| 431 | + except exceptions.CommandError as e: |
| 432 | + self.assertEqual('1 of 2 subnets failed to delete.', str(e)) |
| 433 | + |
| 434 | + self.network.find_subnet.assert_any_call( |
| 435 | + self._subnets[0].name, ignore_missing=False) |
| 436 | + self.network.find_subnet.assert_any_call( |
| 437 | + 'unexist_subnet', ignore_missing=False) |
| 438 | + self.network.delete_subnet.assert_called_once_with( |
| 439 | + self._subnets[0] |
| 440 | + ) |
| 441 | + |
390 | 442 |
|
391 | 443 | class TestListSubnet(TestSubnet): |
392 | 444 | # The subnets going to be listed up. |
|
0 commit comments