|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | +from unittest import mock |
| 15 | + |
| 16 | +from osc_lib import exceptions |
| 17 | +from osc_lib import utils |
| 18 | + |
| 19 | +from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes |
| 20 | +from openstackclient.volume.v3 import volume_snapshot |
| 21 | + |
| 22 | + |
| 23 | +class TestVolumeSnapshot(volume_fakes.TestVolume): |
| 24 | + def setUp(self): |
| 25 | + super().setUp() |
| 26 | + |
| 27 | + self.snapshots_mock = self.volume_client.volume_snapshots |
| 28 | + self.snapshots_mock.reset_mock() |
| 29 | + |
| 30 | + |
| 31 | +class TestVolumeSnapshotDelete(TestVolumeSnapshot): |
| 32 | + snapshots = volume_fakes.create_snapshots(count=2) |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + super().setUp() |
| 36 | + |
| 37 | + self.snapshots_mock.get = volume_fakes.get_snapshots(self.snapshots) |
| 38 | + self.snapshots_mock.delete.return_value = None |
| 39 | + |
| 40 | + # Get the command object to mock |
| 41 | + self.cmd = volume_snapshot.DeleteVolumeSnapshot(self.app, None) |
| 42 | + |
| 43 | + def test_snapshot_delete(self): |
| 44 | + arglist = [self.snapshots[0].id] |
| 45 | + verifylist = [("snapshots", [self.snapshots[0].id])] |
| 46 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 47 | + |
| 48 | + result = self.cmd.take_action(parsed_args) |
| 49 | + |
| 50 | + self.snapshots_mock.delete.assert_called_with( |
| 51 | + self.snapshots[0].id, False |
| 52 | + ) |
| 53 | + self.assertIsNone(result) |
| 54 | + |
| 55 | + def test_snapshot_delete_with_force(self): |
| 56 | + arglist = ['--force', self.snapshots[0].id] |
| 57 | + verifylist = [('force', True), ("snapshots", [self.snapshots[0].id])] |
| 58 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 59 | + |
| 60 | + result = self.cmd.take_action(parsed_args) |
| 61 | + |
| 62 | + self.snapshots_mock.delete.assert_called_with( |
| 63 | + self.snapshots[0].id, True |
| 64 | + ) |
| 65 | + self.assertIsNone(result) |
| 66 | + |
| 67 | + def test_delete_multiple_snapshots(self): |
| 68 | + arglist = [] |
| 69 | + for s in self.snapshots: |
| 70 | + arglist.append(s.id) |
| 71 | + verifylist = [ |
| 72 | + ('snapshots', arglist), |
| 73 | + ] |
| 74 | + |
| 75 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 76 | + result = self.cmd.take_action(parsed_args) |
| 77 | + |
| 78 | + calls = [] |
| 79 | + for s in self.snapshots: |
| 80 | + calls.append(mock.call(s.id, False)) |
| 81 | + self.snapshots_mock.delete.assert_has_calls(calls) |
| 82 | + self.assertIsNone(result) |
| 83 | + |
| 84 | + def test_delete_multiple_snapshots_with_exception(self): |
| 85 | + arglist = [ |
| 86 | + self.snapshots[0].id, |
| 87 | + 'unexist_snapshot', |
| 88 | + ] |
| 89 | + verifylist = [ |
| 90 | + ('snapshots', arglist), |
| 91 | + ] |
| 92 | + |
| 93 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 94 | + |
| 95 | + find_mock_result = [self.snapshots[0], exceptions.CommandError] |
| 96 | + with mock.patch.object( |
| 97 | + utils, 'find_resource', side_effect=find_mock_result |
| 98 | + ) as find_mock: |
| 99 | + try: |
| 100 | + self.cmd.take_action(parsed_args) |
| 101 | + self.fail('CommandError should be raised.') |
| 102 | + except exceptions.CommandError as e: |
| 103 | + self.assertEqual('1 of 2 snapshots failed to delete.', str(e)) |
| 104 | + |
| 105 | + find_mock.assert_any_call( |
| 106 | + self.snapshots_mock, self.snapshots[0].id |
| 107 | + ) |
| 108 | + find_mock.assert_any_call(self.snapshots_mock, 'unexist_snapshot') |
| 109 | + |
| 110 | + self.assertEqual(2, find_mock.call_count) |
| 111 | + self.snapshots_mock.delete.assert_called_once_with( |
| 112 | + self.snapshots[0].id, False |
| 113 | + ) |
0 commit comments