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 logging.disable(logging.CRITICAL)
20 def _get_instance(self, key):
21 mock_obj = mock.Mock()
22 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
23 'ip': 'ip' + str(key), 'status': 'ACTIVE',
24 'OS-EXT-STS:task_state': '-'}
25 mock_obj.configure_mock(**attrs)
28 def _get_instance_deleted(self, key):
29 mock_obj = mock.Mock()
30 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
31 'ip': 'ip' + str(key), 'status': 'DELETED',
32 'OS-EXT-STS:task_state': '-'}
33 mock_obj.configure_mock(**attrs)
36 def _get_instance_deleting(self, key):
37 mock_obj = mock.Mock()
38 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
39 'ip': 'ip' + str(key), 'status': 'BUILD',
40 'OS-EXT-STS:task_state': 'deleting'}
41 mock_obj.configure_mock(**attrs)
44 def _get_instance_other(self, key):
45 mock_obj = mock.Mock()
46 attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
47 'ip': 'ip' + str(key), 'status': 'BUILD',
48 'OS-EXT-STS:task_state': 'networking'}
49 mock_obj.configure_mock(**attrs)
53 self.client = mock.Mock()
54 self.test_list = [self._get_instance(1), self._get_instance(2)]
55 self.deleted_list = [self._get_instance_deleted(5),
56 self._get_instance_deleting(6)]
57 self.other_list = [self._get_instance_other(7)]
58 self.update_list = {'id1': 'name1', 'id2': 'name2'}
59 self.remove_list = {'id3': 'name3', 'id4': 'name4'}
60 self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1',
61 'router:external': False,
62 'external_gateway_info': None},
63 {'id': 'id2', 'name': 'name2', 'ip': 'ip2',
64 'router:external': False,
65 'external_gateway_info': None}]
66 self.routers = [mock.Mock()]
67 self.ports = [mock.Mock()]
69 @mock.patch('functest.utils.openstack_clean.logger.debug')
70 def test_separator(self, mock_logger_debug):
71 openstack_clean.separator()
72 mock_logger_debug.assert_called_once_with("-----------------"
76 @mock.patch('functest.utils.openstack_clean.logger.debug')
77 def test_remove_instances(self, mock_logger_debug):
78 with mock.patch('functest.utils.openstack_clean.os_utils'
79 '.get_instances', return_value=self.test_list):
80 openstack_clean.remove_instances(self.client, self.update_list)
81 mock_logger_debug.assert_any_call("Removing Nova instances...")
82 mock_logger_debug.assert_any_call(" > this is a default "
86 @mock.patch('functest.utils.openstack_clean.logger.debug')
87 def test_remove_instances_missing_instances(self, mock_logger_debug):
88 with mock.patch('functest.utils.openstack_clean.os_utils'
89 '.get_instances', return_value=[]):
90 openstack_clean.remove_instances(self.client, self.update_list)
91 mock_logger_debug.assert_any_call("Removing Nova instances...")
92 mock_logger_debug.assert_any_call("No instances found.")
94 @mock.patch('functest.utils.openstack_clean.logger.debug')
95 def test_remove_instances_delete_success(self, mock_logger_debug):
96 with mock.patch('functest.utils.openstack_clean.os_utils'
97 '.get_instances', return_value=self.test_list), \
98 mock.patch('functest.utils.openstack_clean.os_utils'
99 '.delete_instance', return_value=True):
100 openstack_clean.remove_instances(self.client, self.remove_list)
101 mock_logger_debug.assert_any_call("Removing Nova instances...")
102 mock_logger_debug.assert_any_call(" > Request sent.")
103 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
108 @mock.patch('functest.utils.openstack_clean.logger.debug')
109 def test_remove_instances_pending_delete_success(self, mock_logger_debug):
110 with mock.patch('functest.utils.openstack_clean.os_utils'
111 '.get_instances', return_value=self.deleted_list), \
112 mock.patch('functest.utils.openstack_clean.os_utils'
113 '.delete_instance', return_value=True):
114 openstack_clean.remove_instances(self.client, self.remove_list)
115 mock_logger_debug.assert_any_call("Removing Nova instances...")
116 mock_logger_debug.test_utils.RegexMatch("Removing"
119 " ...").assert_not_called()
121 @mock.patch('functest.utils.openstack_clean.logger.debug')
122 def test_remove_instances_other_delete_success(self, mock_logger_debug):
123 with mock.patch('functest.utils.openstack_clean.os_utils'
124 '.get_instances', return_value=self.other_list), \
125 mock.patch('functest.utils.openstack_clean.os_utils'
126 '.delete_instance', return_value=True):
127 openstack_clean.remove_instances(self.client, self.remove_list)
128 mock_logger_debug.assert_any_call("Removing Nova instances...")
129 mock_logger_debug.assert_any_call(" > Request sent.")
130 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
135 @mock.patch('functest.utils.openstack_clean.logger.error')
136 @mock.patch('functest.utils.openstack_clean.logger.debug')
137 def test_remove_instances_delete_failed(self, mock_logger_debug,
139 with mock.patch('functest.utils.openstack_clean.os_utils'
140 '.get_instances', return_value=self.test_list), \
141 mock.patch('functest.utils.openstack_clean.os_utils'
142 '.delete_instance', return_value=False):
143 openstack_clean.remove_instances(self.client, self.remove_list)
144 mock_logger_debug.assert_any_call("Removing Nova instances...")
145 mock_logger_error.assert_any_call(test_utils.
146 RegexMatch("There has been a "
148 "the instance \s*\S+"
150 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
155 @mock.patch('functest.utils.openstack_clean.logger.debug')
156 def test_remove_images(self, mock_logger_debug):
157 with mock.patch('functest.utils.openstack_clean.os_utils'
158 '.get_images', return_value=self.test_list):
159 openstack_clean.remove_images(self.client, self.update_list)
160 mock_logger_debug.assert_any_call("Removing Glance images...")
161 mock_logger_debug.assert_any_call(" > this is a default "
165 @mock.patch('functest.utils.openstack_clean.logger.debug')
166 def test_remove_images_missing_images(self, mock_logger_debug):
167 with mock.patch('functest.utils.openstack_clean.os_utils'
168 '.get_images', return_value=[]):
169 openstack_clean.remove_images(self.client, self.update_list)
170 mock_logger_debug.assert_any_call("Removing Glance images...")
171 mock_logger_debug.assert_any_call("No images found.")
173 @mock.patch('functest.utils.openstack_clean.logger.debug')
174 def test_remove_images_delete_success(self, mock_logger_debug):
175 with mock.patch('functest.utils.openstack_clean.os_utils'
176 '.get_images', return_value=self.test_list), \
177 mock.patch('functest.utils.openstack_clean.os_utils'
178 '.delete_glance_image', return_value=True):
179 openstack_clean.remove_images(self.client, self.remove_list)
180 mock_logger_debug.assert_any_call("Removing Glance images...")
181 mock_logger_debug.assert_any_call(" > Done!")
182 mock_logger_debug.assert_any_call(test_utils.
183 RegexMatch("Removing image "
187 @mock.patch('functest.utils.openstack_clean.logger.error')
188 @mock.patch('functest.utils.openstack_clean.logger.debug')
189 def test_remove_images_delete_failed(self, mock_logger_debug,
191 with mock.patch('functest.utils.openstack_clean.os_utils'
192 '.get_images', return_value=self.test_list), \
193 mock.patch('functest.utils.openstack_clean.os_utils'
194 '.delete_glance_image', return_value=False):
195 openstack_clean.remove_images(self.client, self.remove_list)
196 mock_logger_debug.assert_any_call("Removing Glance images...")
197 mock_logger_error.assert_any_call(test_utils.
198 RegexMatch("There has been a "
199 "problem removing the"
201 mock_logger_debug.assert_any_call(test_utils.
202 RegexMatch("Removing image "
206 @mock.patch('functest.utils.openstack_clean.logger.debug')
207 def test_remove_volumes(self, mock_logger_debug):
208 with mock.patch('functest.utils.openstack_clean.os_utils'
209 '.get_volumes', return_value=self.test_list):
210 openstack_clean.remove_volumes(self.client, self.update_list)
211 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
212 mock_logger_debug.assert_any_call(" > this is a default "
216 @mock.patch('functest.utils.openstack_clean.logger.debug')
217 def test_remove_volumes_missing_volumes(self, mock_logger_debug):
218 with mock.patch('functest.utils.openstack_clean.os_utils'
219 '.get_volumes', return_value=[]):
220 openstack_clean.remove_volumes(self.client, self.update_list)
221 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
222 mock_logger_debug.assert_any_call("No volumes found.")
224 @mock.patch('functest.utils.openstack_clean.logger.debug')
225 def test_remove_volumes_delete_success(self, mock_logger_debug):
226 with mock.patch('functest.utils.openstack_clean.os_utils'
227 '.get_volumes', return_value=self.test_list), \
228 mock.patch('functest.utils.openstack_clean.os_utils'
229 '.delete_volume', return_value=True):
230 openstack_clean.remove_volumes(self.client, self.remove_list)
231 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
232 mock_logger_debug.assert_any_call(" > Done!")
233 mock_logger_debug.assert_any_call(test_utils.
234 RegexMatch("Removing cinder "
235 "volume \s*\S+ ..."))
237 @mock.patch('functest.utils.openstack_clean.logger.error')
238 @mock.patch('functest.utils.openstack_clean.logger.debug')
239 def test_remove_volumes_delete_failed(self, mock_logger_debug,
241 with mock.patch('functest.utils.openstack_clean.os_utils'
242 '.get_volumes', return_value=self.test_list), \
243 mock.patch('functest.utils.openstack_clean.os_utils'
244 '.delete_volume', return_value=False):
245 openstack_clean.remove_volumes(self.client, self.remove_list)
246 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
247 mock_logger_error.assert_any_call(test_utils.
248 RegexMatch("There has been a "
252 mock_logger_debug.assert_any_call(test_utils.
253 RegexMatch("Removing cinder "
254 "volume \s*\S+ ..."))
256 @mock.patch('functest.utils.openstack_clean.logger.debug')
257 def test_remove_floatingips(self, mock_logger_debug):
258 with mock.patch('functest.utils.openstack_clean.os_utils'
259 '.get_floating_ips', return_value=self.test_list):
260 openstack_clean.remove_floatingips(self.client, self.update_list)
261 mock_logger_debug.assert_any_call("Removing floating IPs...")
262 mock_logger_debug.assert_any_call(" > this is a default "
263 "floating IP and will "
266 @mock.patch('functest.utils.openstack_clean.logger.debug')
267 def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
268 with mock.patch('functest.utils.openstack_clean.os_utils'
269 '.get_floating_ips', return_value=[]):
270 openstack_clean.remove_floatingips(self.client, self.update_list)
271 mock_logger_debug.assert_any_call("Removing floating IPs...")
272 mock_logger_debug.assert_any_call("No floating IPs found.")
274 @mock.patch('functest.utils.openstack_clean.logger.debug')
275 def test_remove_floatingips_delete_success(self, mock_logger_debug):
276 with mock.patch('functest.utils.openstack_clean.os_utils'
277 '.get_floating_ips', return_value=self.test_list), \
278 mock.patch('functest.utils.openstack_clean.os_utils'
279 '.delete_volume', return_value=True):
280 openstack_clean.remove_floatingips(self.client, self.remove_list)
281 mock_logger_debug.assert_any_call("Removing floating IPs...")
282 mock_logger_debug.assert_any_call(" > Done!")
283 mock_logger_debug.assert_any_call(test_utils.
284 RegexMatch("Removing floating "
287 @mock.patch('functest.utils.openstack_clean.logger.error')
288 @mock.patch('functest.utils.openstack_clean.logger.debug')
289 def test_remove_floatingips_delete_failed(self, mock_logger_debug,
291 with mock.patch('functest.utils.openstack_clean.os_utils'
292 '.get_floating_ips', return_value=self.test_list), \
293 mock.patch('functest.utils.openstack_clean.os_utils'
294 '.delete_floating_ip', return_value=False):
295 openstack_clean.remove_floatingips(self.client, self.remove_list)
296 mock_logger_debug.assert_any_call("Removing floating IPs...")
297 mock_logger_error.assert_any_call(test_utils.
298 RegexMatch("There has been a "
302 mock_logger_debug.assert_any_call(test_utils.
303 RegexMatch("Removing floating "
306 @mock.patch('functest.utils.openstack_clean.remove_routers')
307 @mock.patch('functest.utils.openstack_clean.remove_ports')
308 @mock.patch('functest.utils.openstack_clean.logger.debug')
309 def test_remove_networks(self, mock_logger_debug,
311 mock_remove_routers):
312 with mock.patch('functest.utils.openstack_clean.os_utils'
314 return_value=self.test_dict_list), \
315 mock.patch('functest.utils.openstack_clean.os_utils'
316 '.get_port_list', return_value=self.ports), \
317 mock.patch('functest.utils.openstack_clean.os_utils'
318 '.get_router_list', return_value=self.routers):
319 openstack_clean.remove_networks(self.client, self.update_list,
321 mock_logger_debug.assert_any_call("Removing Neutron objects")
322 mock_logger_debug.assert_any_call(" > this is a default "
325 mock_remove_ports.assert_called_once_with(self.client, self.ports,
327 mock_remove_routers.assert_called_once_with(self.client,
331 @mock.patch('functest.utils.openstack_clean.remove_routers')
332 @mock.patch('functest.utils.openstack_clean.remove_ports')
333 @mock.patch('functest.utils.openstack_clean.logger.debug')
334 def test_remove_networks_missing_networks(self, mock_logger_debug,
336 mock_remove_routers):
337 with mock.patch('functest.utils.openstack_clean.os_utils'
338 '.get_network_list', return_value=None), \
339 mock.patch('functest.utils.openstack_clean.os_utils'
340 '.get_port_list', return_value=self.ports), \
341 mock.patch('functest.utils.openstack_clean.os_utils'
342 '.get_router_list', return_value=self.routers):
343 openstack_clean.remove_networks(self.client, self.update_list,
345 mock_logger_debug.assert_any_call("Removing Neutron objects")
346 mock_logger_debug.assert_any_call("There are no networks in the"
348 mock_remove_ports.assert_called_once_with(self.client, self.ports,
350 mock_remove_routers.assert_called_once_with(self.client,
354 @mock.patch('functest.utils.openstack_clean.remove_routers')
355 @mock.patch('functest.utils.openstack_clean.remove_ports')
356 @mock.patch('functest.utils.openstack_clean.logger.debug')
357 def test_remove_networks_delete_success(self, mock_logger_debug,
359 mock_remove_routers):
361 with mock.patch('functest.utils.openstack_clean.os_utils'
363 return_value=self.test_dict_list), \
364 mock.patch('functest.utils.openstack_clean.os_utils'
365 '.delete_neutron_net', return_value=True), \
366 mock.patch('functest.utils.openstack_clean.os_utils'
367 '.get_port_list', return_value=self.ports), \
368 mock.patch('functest.utils.openstack_clean.os_utils'
369 '.get_router_list', return_value=self.routers):
370 openstack_clean.remove_networks(self.client, self.remove_list,
372 mock_logger_debug.assert_any_call("Removing Neutron objects")
373 mock_logger_debug.assert_any_call(" > this network will be "
375 mock_logger_debug.assert_any_call(" > Done!")
376 mock_logger_debug.assert_any_call(test_utils.
377 RegexMatch("Removing network "
379 mock_remove_ports.assert_called_once_with(self.client, self.ports,
381 mock_remove_routers.assert_called_once_with(self.client,
385 @mock.patch('functest.utils.openstack_clean.remove_routers')
386 @mock.patch('functest.utils.openstack_clean.remove_ports')
387 @mock.patch('functest.utils.openstack_clean.logger.error')
388 @mock.patch('functest.utils.openstack_clean.logger.debug')
389 def test_remove_networks_delete_failed(self, mock_logger_debug,
392 mock_remove_routers):
393 with mock.patch('functest.utils.openstack_clean.os_utils'
395 return_value=self.test_dict_list), \
396 mock.patch('functest.utils.openstack_clean.os_utils'
397 '.delete_neutron_net', return_value=False), \
398 mock.patch('functest.utils.openstack_clean.os_utils'
399 '.get_port_list', return_value=self.ports), \
400 mock.patch('functest.utils.openstack_clean.os_utils'
401 '.get_router_list', return_value=self.routers):
402 openstack_clean.remove_networks(self.client, self.remove_list,
404 mock_logger_debug.assert_any_call("Removing Neutron objects")
405 mock_logger_error.assert_any_call(test_utils.
406 RegexMatch("There has been a"
408 " the network \s*\S+"
410 mock_logger_debug.assert_any_call(test_utils.
411 RegexMatch("Removing network "
413 mock_remove_ports.assert_called_once_with(self.client, self.ports,
415 mock_remove_routers.assert_called_once_with(self.client,
420 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
421 @mock.patch('functest.utils.openstack_clean.logger.error')
422 @mock.patch('functest.utils.openstack_clean.logger.debug')
423 def test_force_remove_port(self, mock_logger_debug,
425 mock_update_neutron_port):
426 with mock.patch('functest.utils.openstack_clean.os_utils'
427 '.delete_neutron_port',
429 openstack_clean.force_remove_port(self.client, 'id')
430 mock_logger_debug.assert_any_call(" > Done!")
431 mock_logger_debug.assert_any_call(test_utils.
432 RegexMatch("Clearing device_"
436 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
437 @mock.patch('functest.utils.openstack_clean.logger.error')
438 @mock.patch('functest.utils.openstack_clean.logger.debug')
439 def test_force_remove_port_failed(self, mock_logger_debug,
441 mock_update_neutron_port):
442 with mock.patch('functest.utils.openstack_clean.os_utils'
443 '.delete_neutron_port',
445 openstack_clean.force_remove_port(self.client, 'id')
446 mock_logger_error.assert_any_call("There has been a "
449 mock_logger_debug.assert_any_call(test_utils.
450 RegexMatch("Clearing device_"
454 @mock.patch('functest.utils.openstack_clean.logger.debug')
455 def test_remove_routers_missing_routers(self, mock_logger_debug):
456 with mock.patch('functest.utils.openstack_clean.os_utils'
457 '.delete_neutron_router',
459 openstack_clean.remove_routers(self.client, self.test_dict_list,
461 mock_logger_debug.assert_any_call("Router is not connected"
463 "Ready to remove...")
464 mock_logger_debug.assert_any_call(" > Done!")
465 mock_logger_debug.assert_any_call(test_utils.
466 RegexMatch("Removing router "
467 "\s*\S+(\s*\S+) ..."))
469 @mock.patch('functest.utils.openstack_clean.logger.error')
470 @mock.patch('functest.utils.openstack_clean.logger.debug')
471 def test_remove_routers_failed(self, mock_logger_debug,
473 with mock.patch('functest.utils.openstack_clean.os_utils'
474 '.delete_neutron_router',
476 openstack_clean.remove_routers(self.client, self.test_dict_list,
478 mock_logger_debug.assert_any_call("Router is not connected"
480 "Ready to remove...")
481 mock_logger_debug.assert_any_call(test_utils.
482 RegexMatch("Removing router "
483 "\s*\S+(\s*\S+) ..."))
484 mock_logger_error.assert_any_call(test_utils.
485 RegexMatch("There has been "
491 @mock.patch('functest.utils.openstack_clean.logger.error')
492 @mock.patch('functest.utils.openstack_clean.logger.debug')
493 def test_remove_missing_external_gateway(self, mock_logger_debug,
495 with mock.patch('functest.utils.openstack_clean.os_utils'
496 '.delete_neutron_router',
497 return_value=False), \
498 mock.patch('functest.utils.openstack_clean.os_utils'
499 '.remove_gateway_router',
501 self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
502 openstack_clean.remove_routers(self.client, self.test_dict_list,
504 mock_logger_debug.assert_any_call("Router has gateway to external"
505 " network.Removing link...")
506 mock_logger_error.assert_any_call("There has been a problem "
507 "removing the gateway...")
508 mock_logger_debug.assert_any_call(test_utils.
509 RegexMatch("Removing router "
510 "\s*\S+(\s*\S+) ..."))
511 mock_logger_error.assert_any_call(test_utils.
512 RegexMatch("There has been "
518 @mock.patch('functest.utils.openstack_clean.logger.debug')
519 def remove_security_groups(self, mock_logger_debug):
520 with mock.patch('functest.utils.openstack_clean.os_utils'
521 '.get_security_groups',
522 return_value=self.test_dict_list):
523 openstack_clean.remove_security_groups(self.client,
525 mock_logger_debug.assert_any_call("Removing Security groups...")
526 mock_logger_debug.assert_any_call(" > this is a default "
527 "security group and will NOT "
530 @mock.patch('functest.utils.openstack_clean.logger.debug')
531 def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
532 with mock.patch('functest.utils.openstack_clean.os_utils'
533 '.get_security_groups', return_value=[]):
534 openstack_clean.remove_security_groups(self.client,
536 mock_logger_debug.assert_any_call("Removing Security groups...")
537 mock_logger_debug.assert_any_call("No security groups found.")
539 @mock.patch('functest.utils.openstack_clean.logger.debug')
540 def test_remove_security_groups_delete_success(self, mock_logger_debug):
541 with mock.patch('functest.utils.openstack_clean.os_utils'
542 '.get_security_groups',
543 return_value=self.test_dict_list), \
544 mock.patch('functest.utils.openstack_clean.os_utils'
545 '.delete_security_group', return_value=True):
546 openstack_clean.remove_security_groups(self.client,
548 mock_logger_debug.assert_any_call("Removing Security groups...")
549 mock_logger_debug.assert_any_call(" > Done!")
550 mock_logger_debug.assert_any_call(test_utils.
551 RegexMatch(" Removing \s*\S+"
554 @mock.patch('functest.utils.openstack_clean.logger.error')
555 @mock.patch('functest.utils.openstack_clean.logger.debug')
556 def test_remove_security_groups_delete_failed(self, mock_logger_debug,
558 with mock.patch('functest.utils.openstack_clean.os_utils'
559 '.get_security_groups',
560 return_value=self.test_dict_list), \
561 mock.patch('functest.utils.openstack_clean.os_utils'
562 '.delete_security_group', return_value=False):
563 openstack_clean.remove_security_groups(self.client,
565 mock_logger_debug.assert_any_call("Removing Security groups...")
566 mock_logger_error.assert_any_call(test_utils.
567 RegexMatch("There has been a "
571 mock_logger_debug.assert_any_call(test_utils.
572 RegexMatch(" Removing \s*\S+"
575 @mock.patch('functest.utils.openstack_clean.logger.debug')
576 def test_remove_users(self, mock_logger_debug):
577 with mock.patch('functest.utils.openstack_clean.os_utils'
578 '.get_users', return_value=self.test_list):
579 openstack_clean.remove_users(self.client, self.update_list)
580 mock_logger_debug.assert_any_call("Removing Users...")
581 mock_logger_debug.assert_any_call(" > this is a default "
585 @mock.patch('functest.utils.openstack_clean.logger.debug')
586 def test_remove_users_missing_users(self, mock_logger_debug):
587 with mock.patch('functest.utils.openstack_clean.os_utils'
588 '.get_users', return_value=None):
589 openstack_clean.remove_users(self.client, self.update_list)
590 mock_logger_debug.assert_any_call("Removing Users...")
591 mock_logger_debug.assert_any_call("There are no users in"
594 @mock.patch('functest.utils.openstack_clean.logger.debug')
595 def test_remove_users_delete_success(self, mock_logger_debug):
596 with mock.patch('functest.utils.openstack_clean.os_utils'
597 '.get_users', return_value=self.test_list), \
598 mock.patch('functest.utils.openstack_clean.os_utils'
599 '.delete_user', return_value=True):
600 openstack_clean.remove_users(self.client, self.remove_list)
601 mock_logger_debug.assert_any_call("Removing Users...")
602 mock_logger_debug.assert_any_call(" > Done!")
603 mock_logger_debug.assert_any_call(test_utils.
604 RegexMatch(" Removing "
607 @mock.patch('functest.utils.openstack_clean.logger.error')
608 @mock.patch('functest.utils.openstack_clean.logger.debug')
609 def test_remove_users_delete_failed(self, mock_logger_debug,
611 with mock.patch('functest.utils.openstack_clean.os_utils'
612 '.get_users', return_value=self.test_list), \
613 mock.patch('functest.utils.openstack_clean.os_utils'
614 '.delete_user', return_value=False):
615 openstack_clean.remove_users(self.client, self.remove_list)
616 mock_logger_debug.assert_any_call("Removing Users...")
617 mock_logger_error.assert_any_call(test_utils.
618 RegexMatch("There has been a "
622 mock_logger_debug.assert_any_call(test_utils.
623 RegexMatch(" Removing "
626 @mock.patch('functest.utils.openstack_clean.logger.debug')
627 def test_remove_tenants(self, mock_logger_debug):
628 with mock.patch('functest.utils.openstack_clean.os_utils'
629 '.get_tenants', return_value=self.test_list):
630 openstack_clean.remove_tenants(self.client, self.update_list)
631 mock_logger_debug.assert_any_call("Removing Tenants...")
632 mock_logger_debug.assert_any_call(" > this is a default"
636 @mock.patch('functest.utils.openstack_clean.logger.debug')
637 def test_remove_tenants_missing_tenants(self, mock_logger_debug):
638 with mock.patch('functest.utils.openstack_clean.os_utils'
639 '.get_tenants', return_value=None):
640 openstack_clean.remove_tenants(self.client, self.update_list)
641 mock_logger_debug.assert_any_call("Removing Tenants...")
642 mock_logger_debug.assert_any_call("There are no tenants in"
645 @mock.patch('functest.utils.openstack_clean.logger.debug')
646 def test_remove_tenants_delete_success(self, mock_logger_debug):
647 with mock.patch('functest.utils.openstack_clean.os_utils'
648 '.get_tenants', return_value=self.test_list), \
649 mock.patch('functest.utils.openstack_clean.os_utils'
650 '.delete_tenant', return_value=True):
651 openstack_clean.remove_tenants(self.client, self.remove_list)
652 mock_logger_debug.assert_any_call("Removing Tenants...")
653 mock_logger_debug.assert_any_call(" > Done!")
654 mock_logger_debug.assert_any_call(test_utils.
655 RegexMatch(" Removing "
658 @mock.patch('functest.utils.openstack_clean.logger.error')
659 @mock.patch('functest.utils.openstack_clean.logger.debug')
660 def test_remove_tenants_delete_failed(self, mock_logger_debug,
662 with mock.patch('functest.utils.openstack_clean.os_utils'
663 '.get_tenants', return_value=self.test_list), \
664 mock.patch('functest.utils.openstack_clean.os_utils'
665 '.delete_tenant', return_value=False):
666 openstack_clean.remove_tenants(self.client, self.remove_list)
667 mock_logger_debug.assert_any_call("Removing Tenants...")
668 mock_logger_error.assert_any_call(test_utils.
669 RegexMatch("There has been a "
673 mock_logger_debug.assert_any_call(test_utils.
674 RegexMatch(" Removing "
677 @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
678 @mock.patch('functest.utils.openstack_clean.os_utils'
679 '.get_keystone_client')
680 @mock.patch('functest.utils.openstack_clean.os_utils'
681 '.get_neutron_client')
682 @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
683 @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
685 @mock.patch('functest.utils.openstack_clean.logger.info')
686 @mock.patch('functest.utils.openstack_clean.logger.debug')
687 def test_main_default(self, mock_logger_debug, mock_logger_info,
688 mock_creds, mock_nova, mock_neutron,
689 mock_keystone, mock_cinder):
691 with mock.patch('functest.utils.openstack_clean.remove_instances') \
692 as mock_remove_instances, \
693 mock.patch('functest.utils.openstack_clean.remove_images') \
694 as mock_remove_images, \
695 mock.patch('functest.utils.openstack_clean.remove_volumes') \
696 as mock_remove_volumes, \
697 mock.patch('functest.utils.openstack_clean.remove_floatingips') \
698 as mock_remove_floatingips, \
699 mock.patch('functest.utils.openstack_clean.remove_networks') \
700 as mock_remove_networks, \
701 mock.patch('functest.utils.openstack_clean.'
702 'remove_security_groups') \
703 as mock_remove_security_groups, \
704 mock.patch('functest.utils.openstack_clean.remove_users') \
705 as mock_remove_users, \
706 mock.patch('functest.utils.openstack_clean.remove_tenants') \
707 as mock_remove_tenants, \
708 mock.patch('functest.utils.openstack_clean.yaml.safe_load',
709 return_value=mock.Mock()), \
710 mock.patch('__builtin__.open', mock.mock_open()) as m:
711 openstack_clean.main()
712 self.assertTrue(mock_remove_instances)
713 self.assertTrue(mock_remove_images)
714 self.assertTrue(mock_remove_volumes)
715 self.assertTrue(mock_remove_floatingips)
716 self.assertTrue(mock_remove_networks)
717 self.assertTrue(mock_remove_security_groups)
718 self.assertTrue(mock_remove_users)
719 self.assertTrue(mock_remove_tenants)
720 m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
721 mock_logger_info.assert_called_once_with("Cleaning OpenStack "
725 if __name__ == "__main__":
726 unittest.main(verbosity=2)