|
13 | 13 | # under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import mock |
| 17 | +from mock import call |
| 18 | + |
| 19 | +from osc_lib import exceptions |
16 | 20 | from osc_lib import utils |
17 | 21 |
|
18 | 22 | from openstackclient.tests.volume.v2 import fakes as volume_fakes |
@@ -156,45 +160,94 @@ def test_qos_create_with_properties(self): |
156 | 160 |
|
157 | 161 | class TestQosDelete(TestQos): |
158 | 162 |
|
159 | | - qos_spec = volume_fakes.FakeQos.create_one_qos() |
| 163 | + qos_specs = volume_fakes.FakeQos.create_qoses(count=2) |
160 | 164 |
|
161 | 165 | def setUp(self): |
162 | 166 | super(TestQosDelete, self).setUp() |
163 | 167 |
|
164 | | - self.qos_mock.get.return_value = self.qos_spec |
| 168 | + self.qos_mock.get = ( |
| 169 | + volume_fakes.FakeQos.get_qoses(self.qos_specs)) |
165 | 170 | # Get the command object to test |
166 | 171 | self.cmd = qos_specs.DeleteQos(self.app, None) |
167 | 172 |
|
168 | 173 | def test_qos_delete(self): |
169 | 174 | arglist = [ |
170 | | - self.qos_spec.id |
| 175 | + self.qos_specs[0].id |
171 | 176 | ] |
172 | 177 | verifylist = [ |
173 | | - ('qos_specs', [self.qos_spec.id]) |
| 178 | + ('qos_specs', [self.qos_specs[0].id]) |
174 | 179 | ] |
175 | 180 | parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
176 | 181 |
|
177 | 182 | result = self.cmd.take_action(parsed_args) |
178 | 183 |
|
179 | | - self.qos_mock.delete.assert_called_with(self.qos_spec.id, False) |
| 184 | + self.qos_mock.delete.assert_called_with( |
| 185 | + self.qos_specs[0].id, False) |
180 | 186 | self.assertIsNone(result) |
181 | 187 |
|
182 | 188 | def test_qos_delete_with_force(self): |
183 | 189 | arglist = [ |
184 | 190 | '--force', |
185 | | - self.qos_spec.id |
| 191 | + self.qos_specs[0].id |
186 | 192 | ] |
187 | 193 | verifylist = [ |
188 | 194 | ('force', True), |
189 | | - ('qos_specs', [self.qos_spec.id]) |
| 195 | + ('qos_specs', [self.qos_specs[0].id]) |
190 | 196 | ] |
191 | 197 | parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
192 | 198 |
|
193 | 199 | result = self.cmd.take_action(parsed_args) |
194 | 200 |
|
195 | | - self.qos_mock.delete.assert_called_with(self.qos_spec.id, True) |
| 201 | + self.qos_mock.delete.assert_called_with( |
| 202 | + self.qos_specs[0].id, True) |
196 | 203 | self.assertIsNone(result) |
197 | 204 |
|
| 205 | + def test_delete_multiple_qoses(self): |
| 206 | + arglist = [] |
| 207 | + for q in self.qos_specs: |
| 208 | + arglist.append(q.id) |
| 209 | + verifylist = [ |
| 210 | + ('qos_specs', arglist), |
| 211 | + ] |
| 212 | + |
| 213 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 214 | + result = self.cmd.take_action(parsed_args) |
| 215 | + |
| 216 | + calls = [] |
| 217 | + for q in self.qos_specs: |
| 218 | + calls.append(call(q.id, False)) |
| 219 | + self.qos_mock.delete.assert_has_calls(calls) |
| 220 | + self.assertIsNone(result) |
| 221 | + |
| 222 | + def test_delete_multiple_qoses_with_exception(self): |
| 223 | + arglist = [ |
| 224 | + self.qos_specs[0].id, |
| 225 | + 'unexist_qos', |
| 226 | + ] |
| 227 | + verifylist = [ |
| 228 | + ('qos_specs', arglist), |
| 229 | + ] |
| 230 | + |
| 231 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 232 | + |
| 233 | + find_mock_result = [self.qos_specs[0], exceptions.CommandError] |
| 234 | + with mock.patch.object(utils, 'find_resource', |
| 235 | + side_effect=find_mock_result) as find_mock: |
| 236 | + try: |
| 237 | + self.cmd.take_action(parsed_args) |
| 238 | + self.fail('CommandError should be raised.') |
| 239 | + except exceptions.CommandError as e: |
| 240 | + self.assertEqual( |
| 241 | + '1 of 2 QoS specifications failed to delete.', str(e)) |
| 242 | + |
| 243 | + find_mock.assert_any_call(self.qos_mock, self.qos_specs[0].id) |
| 244 | + find_mock.assert_any_call(self.qos_mock, 'unexist_qos') |
| 245 | + |
| 246 | + self.assertEqual(2, find_mock.call_count) |
| 247 | + self.qos_mock.delete.assert_called_once_with( |
| 248 | + self.qos_specs[0].id, False |
| 249 | + ) |
| 250 | + |
198 | 251 |
|
199 | 252 | class TestQosDisassociate(TestQos): |
200 | 253 |
|
|
0 commit comments