3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
12 from functest.utils import openstack_clean
13 from functest.tests.unit import test_utils
16 class OSCleanTesting(unittest.TestCase):
18 def _get_instance(self, key):
19 mock_obj = mock.Mock()
20 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
21 'ip': 'ip' + str(key), 'status': 'ACTIVE',
22 'OS-EXT-STS:task_state': '-'}
23 mock_obj.configure_mock(**attrs)
26 def _get_instance_deleted(self, key):
27 mock_obj = mock.Mock()
28 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
29 'ip': 'ip' + str(key), 'status': 'DELETED',
30 'OS-EXT-STS:task_state': '-'}
31 mock_obj.configure_mock(**attrs)
34 def _get_instance_deleting(self, key):
35 mock_obj = mock.Mock()
36 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
37 'ip': 'ip' + str(key), 'status': 'BUILD',
38 'OS-EXT-STS:task_state': 'deleting'}
39 mock_obj.configure_mock(**attrs)
42 def _get_instance_other(self, key):
43 mock_obj = mock.Mock()
44 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
45 'ip': 'ip' + str(key), 'status': 'BUILD',
46 'OS-EXT-STS:task_state': 'networking'}
47 mock_obj.configure_mock(**attrs)
51 self.client = mock.Mock()
52 self.test_list = [self._get_instance(1), self._get_instance(2)]
53 self.deleted_list = [self._get_instance_deleted(5),
54 self._get_instance_deleting(6)]
55 self.other_list = [self._get_instance_other(7)]
56 self.update_list = {'id1': 'name1', 'id2': 'name2'}
57 self.remove_list = {'id3': 'name3', 'id4': 'name4'}
58 self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1',
59 'router:external': False,
60 'external_gateway_info': None},
61 {'id': 'id2', 'name': 'name2', 'ip': 'ip2',
62 'router:external': False,
63 'external_gateway_info': None}]
64 self.routers = [mock.Mock()]
65 self.ports = [mock.Mock()]
67 @mock.patch('functest.utils.openstack_clean.logger.debug')
68 def test_separator(self, mock_logger_debug):
69 openstack_clean.separator()
70 mock_logger_debug.assert_called_once_with("-----------------"
74 @mock.patch('functest.utils.openstack_clean.logger.debug')
75 def test_remove_instances(self, mock_logger_debug):
76 with mock.patch('functest.utils.openstack_clean.os_utils'
77 '.get_instances', return_value=self.test_list):
78 openstack_clean.remove_instances(self.client, self.update_list)
79 mock_logger_debug.assert_any_call("Removing Nova instances...")
80 mock_logger_debug.assert_any_call(" > this is a default "
84 @mock.patch('functest.utils.openstack_clean.logger.debug')
85 def test_remove_instances_missing_instances(self, mock_logger_debug):
86 with mock.patch('functest.utils.openstack_clean.os_utils'
87 '.get_instances', return_value=[]):
88 openstack_clean.remove_instances(self.client, self.update_list)
89 mock_logger_debug.assert_any_call("Removing Nova instances...")
90 mock_logger_debug.assert_any_call("No instances found.")
92 @mock.patch('functest.utils.openstack_clean.logger.debug')
93 def test_remove_instances_delete_success(self, mock_logger_debug):
94 with mock.patch('functest.utils.openstack_clean.os_utils'
95 '.get_instances', return_value=self.test_list), \
96 mock.patch('functest.utils.openstack_clean.os_utils'
97 '.delete_instance', return_value=True):
98 openstack_clean.remove_instances(self.client, self.remove_list)
99 mock_logger_debug.assert_any_call("Removing Nova instances...")
100 mock_logger_debug.assert_any_call(" > Request sent.")
101 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
106 @mock.patch('functest.utils.openstack_clean.logger.debug')
107 def test_remove_instances_pending_delete_success(self, mock_logger_debug):
108 with mock.patch('functest.utils.openstack_clean.os_utils'
109 '.get_instances', return_value=self.deleted_list), \
110 mock.patch('functest.utils.openstack_clean.os_utils'
111 '.delete_instance', return_value=True):
112 openstack_clean.remove_instances(self.client, self.remove_list)
113 mock_logger_debug.assert_any_call("Removing Nova instances...")
114 mock_logger_debug.test_utils.RegexMatch("Removing"
117 " ...").assert_not_called()
119 @mock.patch('functest.utils.openstack_clean.logger.debug')
120 def test_remove_instances_other_delete_success(self, mock_logger_debug):
121 with mock.patch('functest.utils.openstack_clean.os_utils'
122 '.get_instances', return_value=self.other_list), \
123 mock.patch('functest.utils.openstack_clean.os_utils'
124 '.delete_instance', return_value=True):
125 openstack_clean.remove_instances(self.client, self.remove_list)
126 mock_logger_debug.assert_any_call("Removing Nova instances...")
127 mock_logger_debug.assert_any_call(" > Request sent.")
128 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
133 @mock.patch('functest.utils.openstack_clean.logger.error')
134 @mock.patch('functest.utils.openstack_clean.logger.debug')
135 def test_remove_instances_delete_failed(self, mock_logger_debug,
137 with mock.patch('functest.utils.openstack_clean.os_utils'
138 '.get_instances', return_value=self.test_list), \
139 mock.patch('functest.utils.openstack_clean.os_utils'
140 '.delete_instance', return_value=False):
141 openstack_clean.remove_instances(self.client, self.remove_list)
142 mock_logger_debug.assert_any_call("Removing Nova instances...")
143 mock_logger_error.assert_any_call(test_utils.
144 RegexMatch("There has been a "
146 "the instance \s*\S+"
148 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
153 @mock.patch('functest.utils.openstack_clean.logger.debug')
154 def test_remove_images(self, mock_logger_debug):
155 with mock.patch('functest.utils.openstack_clean.os_utils'
156 '.get_images', return_value=self.test_list):
157 openstack_clean.remove_images(self.client, self.update_list)
158 mock_logger_debug.assert_any_call("Removing Glance images...")
159 mock_logger_debug.assert_any_call(" > this is a default "
163 @mock.patch('functest.utils.openstack_clean.logger.debug')
164 def test_remove_images_missing_images(self, mock_logger_debug):
165 with mock.patch('functest.utils.openstack_clean.os_utils'
166 '.get_images', return_value=[]):
167 openstack_clean.remove_images(self.client, self.update_list)
168 mock_logger_debug.assert_any_call("Removing Glance images...")
169 mock_logger_debug.assert_any_call("No images found.")
171 @mock.patch('functest.utils.openstack_clean.logger.debug')
172 def test_remove_images_delete_success(self, mock_logger_debug):
173 with mock.patch('functest.utils.openstack_clean.os_utils'
174 '.get_images', return_value=self.test_list), \
175 mock.patch('functest.utils.openstack_clean.os_utils'
176 '.delete_glance_image', return_value=True):
177 openstack_clean.remove_images(self.client, self.remove_list)
178 mock_logger_debug.assert_any_call("Removing Glance images...")
179 mock_logger_debug.assert_any_call(" > Done!")
180 mock_logger_debug.assert_any_call(test_utils.
181 RegexMatch("Removing image "
185 @mock.patch('functest.utils.openstack_clean.logger.error')
186 @mock.patch('functest.utils.openstack_clean.logger.debug')
187 def test_remove_images_delete_failed(self, mock_logger_debug,
189 with mock.patch('functest.utils.openstack_clean.os_utils'
190 '.get_images', return_value=self.test_list), \
191 mock.patch('functest.utils.openstack_clean.os_utils'
192 '.delete_glance_image', return_value=False):
193 openstack_clean.remove_images(self.client, self.remove_list)
194 mock_logger_debug.assert_any_call("Removing Glance images...")
195 mock_logger_error.assert_any_call(test_utils.
196 RegexMatch("There has been a "
197 "problem removing the"
199 mock_logger_debug.assert_any_call(test_utils.
200 RegexMatch("Removing image "
204 @mock.patch('functest.utils.openstack_clean.logger.debug')
205 def test_remove_volumes(self, mock_logger_debug):
206 with mock.patch('functest.utils.openstack_clean.os_utils'
207 '.get_volumes', return_value=self.test_list):
208 openstack_clean.remove_volumes(self.client, self.update_list)
209 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
210 mock_logger_debug.assert_any_call(" > this is a default "
214 @mock.patch('functest.utils.openstack_clean.logger.debug')
215 def test_remove_volumes_missing_volumes(self, mock_logger_debug):
216 with mock.patch('functest.utils.openstack_clean.os_utils'
217 '.get_volumes', return_value=[]):
218 openstack_clean.remove_volumes(self.client, self.update_list)
219 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
220 mock_logger_debug.assert_any_call("No volumes found.")
222 @mock.patch('functest.utils.openstack_clean.logger.debug')
223 def test_remove_volumes_delete_success(self, mock_logger_debug):
224 with mock.patch('functest.utils.openstack_clean.os_utils'
225 '.get_volumes', return_value=self.test_list), \
226 mock.patch('functest.utils.openstack_clean.os_utils'
227 '.delete_volume', return_value=True):
228 openstack_clean.remove_volumes(self.client, self.remove_list)
229 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
230 mock_logger_debug.assert_any_call(" > Done!")
231 mock_logger_debug.assert_any_call(test_utils.
232 RegexMatch("Removing cinder "
233 "volume \s*\S+ ..."))
235 @mock.patch('functest.utils.openstack_clean.logger.error')
236 @mock.patch('functest.utils.openstack_clean.logger.debug')
237 def test_remove_volumes_delete_failed(self, mock_logger_debug,
239 with mock.patch('functest.utils.openstack_clean.os_utils'
240 '.get_volumes', return_value=self.test_list), \
241 mock.patch('functest.utils.openstack_clean.os_utils'
242 '.delete_volume', return_value=False):
243 openstack_clean.remove_volumes(self.client, self.remove_list)
244 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
245 mock_logger_error.assert_any_call(test_utils.
246 RegexMatch("There has been a "
250 mock_logger_debug.assert_any_call(test_utils.
251 RegexMatch("Removing cinder "
252 "volume \s*\S+ ..."))
254 @mock.patch('functest.utils.openstack_clean.logger.debug')
255 def test_remove_floatingips(self, mock_logger_debug):
256 with mock.patch('functest.utils.openstack_clean.os_utils'
257 '.get_floating_ips', return_value=self.test_list):
258 openstack_clean.remove_floatingips(self.client, self.update_list)
259 mock_logger_debug.assert_any_call("Removing floating IPs...")
260 mock_logger_debug.assert_any_call(" > this is a default "
261 "floating IP and will "
264 @mock.patch('functest.utils.openstack_clean.logger.debug')
265 def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
266 with mock.patch('functest.utils.openstack_clean.os_utils'
267 '.get_floating_ips', return_value=[]):
268 openstack_clean.remove_floatingips(self.client, self.update_list)
269 mock_logger_debug.assert_any_call("Removing floating IPs...")
270 mock_logger_debug.assert_any_call("No floating IPs found.")
272 @mock.patch('functest.utils.openstack_clean.logger.debug')
273 def test_remove_floatingips_delete_success(self, mock_logger_debug):
274 with mock.patch('functest.utils.openstack_clean.os_utils'
275 '.get_floating_ips', return_value=self.test_list), \
276 mock.patch('functest.utils.openstack_clean.os_utils'
277 '.delete_volume', return_value=True):
278 openstack_clean.remove_floatingips(self.client, self.remove_list)
279 mock_logger_debug.assert_any_call("Removing floating IPs...")
280 mock_logger_debug.assert_any_call(" > Done!")
281 mock_logger_debug.assert_any_call(test_utils.
282 RegexMatch("Removing floating "
285 @mock.patch('functest.utils.openstack_clean.logger.error')
286 @mock.patch('functest.utils.openstack_clean.logger.debug')
287 def test_remove_floatingips_delete_failed(self, mock_logger_debug,
289 with mock.patch('functest.utils.openstack_clean.os_utils'
290 '.get_floating_ips', return_value=self.test_list), \
291 mock.patch('functest.utils.openstack_clean.os_utils'
292 '.delete_floating_ip', return_value=False):
293 openstack_clean.remove_floatingips(self.client, self.remove_list)
294 mock_logger_debug.assert_any_call("Removing floating IPs...")
295 mock_logger_error.assert_any_call(test_utils.
296 RegexMatch("There has been a "
300 mock_logger_debug.assert_any_call(test_utils.
301 RegexMatch("Removing floating "
304 @mock.patch('functest.utils.openstack_clean.remove_routers')
305 @mock.patch('functest.utils.openstack_clean.remove_ports')
306 @mock.patch('functest.utils.openstack_clean.logger.debug')
307 def test_remove_networks(self, mock_logger_debug,
309 mock_remove_routers):
310 with mock.patch('functest.utils.openstack_clean.os_utils'
312 return_value=self.test_dict_list), \
313 mock.patch('functest.utils.openstack_clean.os_utils'
314 '.get_port_list', return_value=self.ports), \
315 mock.patch('functest.utils.openstack_clean.os_utils'
316 '.get_router_list', return_value=self.routers):
317 openstack_clean.remove_networks(self.client, self.update_list,
319 mock_logger_debug.assert_any_call("Removing Neutron objects")
320 mock_logger_debug.assert_any_call(" > this is a default "
323 mock_remove_ports.assert_called_once_with(self.client, self.ports,
325 mock_remove_routers.assert_called_once_with(self.client,
329 @mock.patch('functest.utils.openstack_clean.remove_routers')
330 @mock.patch('functest.utils.openstack_clean.remove_ports')
331 @mock.patch('functest.utils.openstack_clean.logger.debug')
332 def test_remove_networks_missing_networks(self, mock_logger_debug,
334 mock_remove_routers):
335 with mock.patch('functest.utils.openstack_clean.os_utils'
336 '.get_network_list', return_value=None), \
337 mock.patch('functest.utils.openstack_clean.os_utils'
338 '.get_port_list', return_value=self.ports), \
339 mock.patch('functest.utils.openstack_clean.os_utils'
340 '.get_router_list', return_value=self.routers):
341 openstack_clean.remove_networks(self.client, self.update_list,
343 mock_logger_debug.assert_any_call("Removing Neutron objects")
344 mock_logger_debug.assert_any_call("There are no networks in the"
346 mock_remove_ports.assert_called_once_with(self.client, self.ports,
348 mock_remove_routers.assert_called_once_with(self.client,
352 @mock.patch('functest.utils.openstack_clean.remove_routers')
353 @mock.patch('functest.utils.openstack_clean.remove_ports')
354 @mock.patch('functest.utils.openstack_clean.logger.debug')
355 def test_remove_networks_delete_success(self, mock_logger_debug,
357 mock_remove_routers):
359 with mock.patch('functest.utils.openstack_clean.os_utils'
361 return_value=self.test_dict_list), \
362 mock.patch('functest.utils.openstack_clean.os_utils'
363 '.delete_neutron_net', return_value=True), \
364 mock.patch('functest.utils.openstack_clean.os_utils'
365 '.get_port_list', return_value=self.ports), \
366 mock.patch('functest.utils.openstack_clean.os_utils'
367 '.get_router_list', return_value=self.routers):
368 openstack_clean.remove_networks(self.client, self.remove_list,
370 mock_logger_debug.assert_any_call("Removing Neutron objects")
371 mock_logger_debug.assert_any_call(" > this network will be "
373 mock_logger_debug.assert_any_call(" > Done!")
374 mock_logger_debug.assert_any_call(test_utils.
375 RegexMatch("Removing network "
377 mock_remove_ports.assert_called_once_with(self.client, self.ports,
379 mock_remove_routers.assert_called_once_with(self.client,
383 @mock.patch('functest.utils.openstack_clean.remove_routers')
384 @mock.patch('functest.utils.openstack_clean.remove_ports')
385 @mock.patch('functest.utils.openstack_clean.logger.error')
386 @mock.patch('functest.utils.openstack_clean.logger.debug')
387 def test_remove_networks_delete_failed(self, mock_logger_debug,
390 mock_remove_routers):
391 with mock.patch('functest.utils.openstack_clean.os_utils'
393 return_value=self.test_dict_list), \
394 mock.patch('functest.utils.openstack_clean.os_utils'
395 '.delete_neutron_net', return_value=False), \
396 mock.patch('functest.utils.openstack_clean.os_utils'
397 '.get_port_list', return_value=self.ports), \
398 mock.patch('functest.utils.openstack_clean.os_utils'
399 '.get_router_list', return_value=self.routers):
400 openstack_clean.remove_networks(self.client, self.remove_list,
402 mock_logger_debug.assert_any_call("Removing Neutron objects")
403 mock_logger_error.assert_any_call(test_utils.
404 RegexMatch("There has been a"
406 " the network \s*\S+"
408 mock_logger_debug.assert_any_call(test_utils.
409 RegexMatch("Removing network "
411 mock_remove_ports.assert_called_once_with(self.client, self.ports,
413 mock_remove_routers.assert_called_once_with(self.client,
418 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
419 @mock.patch('functest.utils.openstack_clean.logger.error')
420 @mock.patch('functest.utils.openstack_clean.logger.debug')
421 def test_force_remove_port(self, mock_logger_debug,
423 mock_update_neutron_port):
424 with mock.patch('functest.utils.openstack_clean.os_utils'
425 '.delete_neutron_port',
427 openstack_clean.force_remove_port(self.client, 'id')
428 mock_logger_debug.assert_any_call(" > Done!")
429 mock_logger_debug.assert_any_call(test_utils.
430 RegexMatch("Clearing device_"
434 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
435 @mock.patch('functest.utils.openstack_clean.logger.error')
436 @mock.patch('functest.utils.openstack_clean.logger.debug')
437 def test_force_remove_port_failed(self, mock_logger_debug,
439 mock_update_neutron_port):
440 with mock.patch('functest.utils.openstack_clean.os_utils'
441 '.delete_neutron_port',
443 openstack_clean.force_remove_port(self.client, 'id')
444 mock_logger_error.assert_any_call("There has been a "
447 mock_logger_debug.assert_any_call(test_utils.
448 RegexMatch("Clearing device_"
452 @mock.patch('functest.utils.openstack_clean.logger.debug')
453 def test_remove_routers_missing_routers(self, mock_logger_debug):
454 with mock.patch('functest.utils.openstack_clean.os_utils'
455 '.delete_neutron_router',
457 openstack_clean.remove_routers(self.client, self.test_dict_list,
459 mock_logger_debug.assert_any_call("Router is not connected"
461 "Ready to remove...")
462 mock_logger_debug.assert_any_call(" > Done!")
463 mock_logger_debug.assert_any_call(test_utils.
464 RegexMatch("Removing router "
465 "\s*\S+(\s*\S+) ..."))
467 @mock.patch('functest.utils.openstack_clean.logger.error')
468 @mock.patch('functest.utils.openstack_clean.logger.debug')
469 def test_remove_routers_failed(self, mock_logger_debug,
471 with mock.patch('functest.utils.openstack_clean.os_utils'
472 '.delete_neutron_router',
474 openstack_clean.remove_routers(self.client, self.test_dict_list,
476 mock_logger_debug.assert_any_call("Router is not connected"
478 "Ready to remove...")
479 mock_logger_debug.assert_any_call(test_utils.
480 RegexMatch("Removing router "
481 "\s*\S+(\s*\S+) ..."))
482 mock_logger_error.assert_any_call(test_utils.
483 RegexMatch("There has been "
489 @mock.patch('functest.utils.openstack_clean.logger.error')
490 @mock.patch('functest.utils.openstack_clean.logger.debug')
491 def test_remove_missing_external_gateway(self, mock_logger_debug,
493 with mock.patch('functest.utils.openstack_clean.os_utils'
494 '.delete_neutron_router',
495 return_value=False), \
496 mock.patch('functest.utils.openstack_clean.os_utils'
497 '.remove_gateway_router',
499 self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
500 openstack_clean.remove_routers(self.client, self.test_dict_list,
502 mock_logger_debug.assert_any_call("Router has gateway to external"
503 " network.Removing link...")
504 mock_logger_error.assert_any_call("There has been a problem "
505 "removing the gateway...")
506 mock_logger_debug.assert_any_call(test_utils.
507 RegexMatch("Removing router "
508 "\s*\S+(\s*\S+) ..."))
509 mock_logger_error.assert_any_call(test_utils.
510 RegexMatch("There has been "
516 @mock.patch('functest.utils.openstack_clean.logger.debug')
517 def remove_security_groups(self, mock_logger_debug):
518 with mock.patch('functest.utils.openstack_clean.os_utils'
519 '.get_security_groups',
520 return_value=self.test_dict_list):
521 openstack_clean.remove_security_groups(self.client,
523 mock_logger_debug.assert_any_call("Removing Security groups...")
524 mock_logger_debug.assert_any_call(" > this is a default "
525 "security group and will NOT "
528 @mock.patch('functest.utils.openstack_clean.logger.debug')
529 def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
530 with mock.patch('functest.utils.openstack_clean.os_utils'
531 '.get_security_groups', return_value=[]):
532 openstack_clean.remove_security_groups(self.client,
534 mock_logger_debug.assert_any_call("Removing Security groups...")
535 mock_logger_debug.assert_any_call("No security groups found.")
537 @mock.patch('functest.utils.openstack_clean.logger.debug')
538 def test_remove_security_groups_delete_success(self, mock_logger_debug):
539 with mock.patch('functest.utils.openstack_clean.os_utils'
540 '.get_security_groups',
541 return_value=self.test_dict_list), \
542 mock.patch('functest.utils.openstack_clean.os_utils'
543 '.delete_security_group', return_value=True):
544 openstack_clean.remove_security_groups(self.client,
546 mock_logger_debug.assert_any_call("Removing Security groups...")
547 mock_logger_debug.assert_any_call(" > Done!")
548 mock_logger_debug.assert_any_call(test_utils.
549 RegexMatch(" Removing \s*\S+"
552 @mock.patch('functest.utils.openstack_clean.logger.error')
553 @mock.patch('functest.utils.openstack_clean.logger.debug')
554 def test_remove_security_groups_delete_failed(self, mock_logger_debug,
556 with mock.patch('functest.utils.openstack_clean.os_utils'
557 '.get_security_groups',
558 return_value=self.test_dict_list), \
559 mock.patch('functest.utils.openstack_clean.os_utils'
560 '.delete_security_group', return_value=False):
561 openstack_clean.remove_security_groups(self.client,
563 mock_logger_debug.assert_any_call("Removing Security groups...")
564 mock_logger_error.assert_any_call(test_utils.
565 RegexMatch("There has been a "
569 mock_logger_debug.assert_any_call(test_utils.
570 RegexMatch(" Removing \s*\S+"
573 @mock.patch('functest.utils.openstack_clean.logger.debug')
574 def test_remove_users(self, mock_logger_debug):
575 with mock.patch('functest.utils.openstack_clean.os_utils'
576 '.get_users', return_value=self.test_list):
577 openstack_clean.remove_users(self.client, self.update_list)
578 mock_logger_debug.assert_any_call("Removing Users...")
579 mock_logger_debug.assert_any_call(" > this is a default "
583 @mock.patch('functest.utils.openstack_clean.logger.debug')
584 def test_remove_users_missing_users(self, mock_logger_debug):
585 with mock.patch('functest.utils.openstack_clean.os_utils'
586 '.get_users', return_value=None):
587 openstack_clean.remove_users(self.client, self.update_list)
588 mock_logger_debug.assert_any_call("Removing Users...")
589 mock_logger_debug.assert_any_call("There are no users in"
592 @mock.patch('functest.utils.openstack_clean.logger.debug')
593 def test_remove_users_delete_success(self, mock_logger_debug):
594 with mock.patch('functest.utils.openstack_clean.os_utils'
595 '.get_users', return_value=self.test_list), \
596 mock.patch('functest.utils.openstack_clean.os_utils'
597 '.delete_user', return_value=True):
598 openstack_clean.remove_users(self.client, self.remove_list)
599 mock_logger_debug.assert_any_call("Removing Users...")
600 mock_logger_debug.assert_any_call(" > Done!")
601 mock_logger_debug.assert_any_call(test_utils.
602 RegexMatch(" Removing "
605 @mock.patch('functest.utils.openstack_clean.logger.error')
606 @mock.patch('functest.utils.openstack_clean.logger.debug')
607 def test_remove_users_delete_failed(self, mock_logger_debug,
609 with mock.patch('functest.utils.openstack_clean.os_utils'
610 '.get_users', return_value=self.test_list), \
611 mock.patch('functest.utils.openstack_clean.os_utils'
612 '.delete_user', return_value=False):
613 openstack_clean.remove_users(self.client, self.remove_list)
614 mock_logger_debug.assert_any_call("Removing Users...")
615 mock_logger_error.assert_any_call(test_utils.
616 RegexMatch("There has been a "
620 mock_logger_debug.assert_any_call(test_utils.
621 RegexMatch(" Removing "
624 @mock.patch('functest.utils.openstack_clean.logger.debug')
625 def test_remove_tenants(self, mock_logger_debug):
626 with mock.patch('functest.utils.openstack_clean.os_utils'
627 '.get_tenants', return_value=self.test_list):
628 openstack_clean.remove_tenants(self.client, self.update_list)
629 mock_logger_debug.assert_any_call("Removing Tenants...")
630 mock_logger_debug.assert_any_call(" > this is a default"
634 @mock.patch('functest.utils.openstack_clean.logger.debug')
635 def test_remove_tenants_missing_tenants(self, mock_logger_debug):
636 with mock.patch('functest.utils.openstack_clean.os_utils'
637 '.get_tenants', return_value=None):
638 openstack_clean.remove_tenants(self.client, self.update_list)
639 mock_logger_debug.assert_any_call("Removing Tenants...")
640 mock_logger_debug.assert_any_call("There are no tenants in"
643 @mock.patch('functest.utils.openstack_clean.logger.debug')
644 def test_remove_tenants_delete_success(self, mock_logger_debug):
645 with mock.patch('functest.utils.openstack_clean.os_utils'
646 '.get_tenants', return_value=self.test_list), \
647 mock.patch('functest.utils.openstack_clean.os_utils'
648 '.delete_tenant', return_value=True):
649 openstack_clean.remove_tenants(self.client, self.remove_list)
650 mock_logger_debug.assert_any_call("Removing Tenants...")
651 mock_logger_debug.assert_any_call(" > Done!")
652 mock_logger_debug.assert_any_call(test_utils.
653 RegexMatch(" Removing "
656 @mock.patch('functest.utils.openstack_clean.logger.error')
657 @mock.patch('functest.utils.openstack_clean.logger.debug')
658 def test_remove_tenants_delete_failed(self, mock_logger_debug,
660 with mock.patch('functest.utils.openstack_clean.os_utils'
661 '.get_tenants', return_value=self.test_list), \
662 mock.patch('functest.utils.openstack_clean.os_utils'
663 '.delete_tenant', return_value=False):
664 openstack_clean.remove_tenants(self.client, self.remove_list)
665 mock_logger_debug.assert_any_call("Removing Tenants...")
666 mock_logger_error.assert_any_call(test_utils.
667 RegexMatch("There has been a "
671 mock_logger_debug.assert_any_call(test_utils.
672 RegexMatch(" Removing "
675 @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
676 @mock.patch('functest.utils.openstack_clean.os_utils'
677 '.get_keystone_client')
678 @mock.patch('functest.utils.openstack_clean.os_utils'
679 '.get_neutron_client')
680 @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
681 @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
683 @mock.patch('functest.utils.openstack_clean.logger.info')
684 @mock.patch('functest.utils.openstack_clean.logger.debug')
685 def test_main_default(self, mock_logger_debug, mock_logger_info,
686 mock_creds, mock_nova, mock_neutron,
687 mock_keystone, mock_cinder):
689 with mock.patch('functest.utils.openstack_clean.remove_instances') \
690 as mock_remove_instances, \
691 mock.patch('functest.utils.openstack_clean.remove_images') \
692 as mock_remove_images, \
693 mock.patch('functest.utils.openstack_clean.remove_volumes') \
694 as mock_remove_volumes, \
695 mock.patch('functest.utils.openstack_clean.remove_floatingips') \
696 as mock_remove_floatingips, \
697 mock.patch('functest.utils.openstack_clean.remove_networks') \
698 as mock_remove_networks, \
699 mock.patch('functest.utils.openstack_clean.'
700 'remove_security_groups') \
701 as mock_remove_security_groups, \
702 mock.patch('functest.utils.openstack_clean.remove_users') \
703 as mock_remove_users, \
704 mock.patch('functest.utils.openstack_clean.remove_tenants') \
705 as mock_remove_tenants, \
706 mock.patch('functest.utils.openstack_clean.yaml.safe_load',
707 return_value=mock.Mock()), \
708 mock.patch('__builtin__.open', mock.mock_open()) as m:
709 openstack_clean.main()
710 self.assertTrue(mock_remove_instances)
711 self.assertTrue(mock_remove_images)
712 self.assertTrue(mock_remove_volumes)
713 self.assertTrue(mock_remove_floatingips)
714 self.assertTrue(mock_remove_networks)
715 self.assertTrue(mock_remove_security_groups)
716 self.assertTrue(mock_remove_users)
717 self.assertTrue(mock_remove_tenants)
718 m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
719 mock_logger_info.assert_called_once_with("Cleaning OpenStack "
723 if __name__ == "__main__":
724 logging.disable(logging.CRITICAL)
725 unittest.main(verbosity=2)