|
12 | 12 | # under the License. |
13 | 13 | # |
14 | 14 |
|
| 15 | +import mock |
| 16 | +from mock import call |
| 17 | + |
| 18 | +from osc_lib import exceptions |
15 | 19 | from osc_lib import utils |
16 | 20 |
|
17 | 21 | from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes |
@@ -161,6 +165,103 @@ def test_consistency_group_create_from_source(self): |
161 | 165 | self.assertEqual(self.data, data) |
162 | 166 |
|
163 | 167 |
|
| 168 | +class TestConsistencyGroupDelete(TestConsistencyGroup): |
| 169 | + |
| 170 | + consistency_groups =\ |
| 171 | + volume_fakes.FakeConsistencyGroup.create_consistency_groups(count=2) |
| 172 | + |
| 173 | + def setUp(self): |
| 174 | + super(TestConsistencyGroupDelete, self).setUp() |
| 175 | + |
| 176 | + self.consistencygroups_mock.get = volume_fakes.FakeConsistencyGroup.\ |
| 177 | + get_consistency_groups(self.consistency_groups) |
| 178 | + self.consistencygroups_mock.delete.return_value = None |
| 179 | + |
| 180 | + # Get the command object to mock |
| 181 | + self.cmd = consistency_group.DeleteConsistencyGroup(self.app, None) |
| 182 | + |
| 183 | + def test_consistency_group_delete(self): |
| 184 | + arglist = [ |
| 185 | + self.consistency_groups[0].id |
| 186 | + ] |
| 187 | + verifylist = [ |
| 188 | + ("consistency_groups", [self.consistency_groups[0].id]) |
| 189 | + ] |
| 190 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 191 | + |
| 192 | + result = self.cmd.take_action(parsed_args) |
| 193 | + |
| 194 | + self.consistencygroups_mock.delete.assert_called_with( |
| 195 | + self.consistency_groups[0].id, False) |
| 196 | + self.assertIsNone(result) |
| 197 | + |
| 198 | + def test_consistency_group_delete_with_force(self): |
| 199 | + arglist = [ |
| 200 | + '--force', |
| 201 | + self.consistency_groups[0].id, |
| 202 | + ] |
| 203 | + verifylist = [ |
| 204 | + ('force', True), |
| 205 | + ("consistency_groups", [self.consistency_groups[0].id]) |
| 206 | + ] |
| 207 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 208 | + |
| 209 | + result = self.cmd.take_action(parsed_args) |
| 210 | + |
| 211 | + self.consistencygroups_mock.delete.assert_called_with( |
| 212 | + self.consistency_groups[0].id, True) |
| 213 | + self.assertIsNone(result) |
| 214 | + |
| 215 | + def test_delete_multiple_consistency_groups(self): |
| 216 | + arglist = [] |
| 217 | + for b in self.consistency_groups: |
| 218 | + arglist.append(b.id) |
| 219 | + verifylist = [ |
| 220 | + ('consistency_groups', arglist), |
| 221 | + ] |
| 222 | + |
| 223 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 224 | + result = self.cmd.take_action(parsed_args) |
| 225 | + |
| 226 | + calls = [] |
| 227 | + for b in self.consistency_groups: |
| 228 | + calls.append(call(b.id, False)) |
| 229 | + self.consistencygroups_mock.delete.assert_has_calls(calls) |
| 230 | + self.assertIsNone(result) |
| 231 | + |
| 232 | + def test_delete_multiple_consistency_groups_with_exception(self): |
| 233 | + arglist = [ |
| 234 | + self.consistency_groups[0].id, |
| 235 | + 'unexist_consistency_group', |
| 236 | + ] |
| 237 | + verifylist = [ |
| 238 | + ('consistency_groups', arglist), |
| 239 | + ] |
| 240 | + |
| 241 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 242 | + |
| 243 | + find_mock_result = [self.consistency_groups[0], |
| 244 | + exceptions.CommandError] |
| 245 | + with mock.patch.object(utils, 'find_resource', |
| 246 | + side_effect=find_mock_result) as find_mock: |
| 247 | + try: |
| 248 | + self.cmd.take_action(parsed_args) |
| 249 | + self.fail('CommandError should be raised.') |
| 250 | + except exceptions.CommandError as e: |
| 251 | + self.assertEqual('1 of 2 consistency groups failed to delete.', |
| 252 | + str(e)) |
| 253 | + |
| 254 | + find_mock.assert_any_call(self.consistencygroups_mock, |
| 255 | + self.consistency_groups[0].id) |
| 256 | + find_mock.assert_any_call(self.consistencygroups_mock, |
| 257 | + 'unexist_consistency_group') |
| 258 | + |
| 259 | + self.assertEqual(2, find_mock.call_count) |
| 260 | + self.consistencygroups_mock.delete.assert_called_once_with( |
| 261 | + self.consistency_groups[0].id, False |
| 262 | + ) |
| 263 | + |
| 264 | + |
164 | 265 | class TestConsistencyGroupList(TestConsistencyGroup): |
165 | 266 |
|
166 | 267 | consistency_groups = ( |
|
0 commit comments