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.floatingips_list = [{'id': 'id1', 'floating_ip_address': 'ip1'},
65 {'id': 'id2', 'floating_ip_address': 'ip2'}]
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('time.sleep')
77 @mock.patch('functest.utils.openstack_clean.logger.debug')
78 def test_remove_instances(self, mock_logger_debug, *args):
79 with mock.patch('functest.utils.openstack_clean.os_utils'
80 '.get_instances', return_value=self.test_list):
81 openstack_clean.remove_instances(self.client, self.update_list)
82 mock_logger_debug.assert_any_call("Removing Nova instances...")
83 mock_logger_debug.assert_any_call(" > this is a default "
87 @mock.patch('time.sleep')
88 @mock.patch('functest.utils.openstack_clean.logger.debug')
89 def test_remove_instances_missing_instances(self, mock_logger_debug,
91 with mock.patch('functest.utils.openstack_clean.os_utils'
92 '.get_instances', return_value=[]):
93 openstack_clean.remove_instances(self.client, self.update_list)
94 mock_logger_debug.assert_any_call("Removing Nova instances...")
95 mock_logger_debug.assert_any_call("No instances found.")
97 @mock.patch('time.sleep')
98 @mock.patch('functest.utils.openstack_clean.logger.debug')
99 def test_remove_instances_delete_success(self, mock_logger_debug, *args):
100 with mock.patch('functest.utils.openstack_clean.os_utils'
101 '.get_instances', return_value=self.test_list), \
102 mock.patch('functest.utils.openstack_clean.os_utils'
103 '.delete_instance', return_value=True):
104 openstack_clean.remove_instances(self.client, self.remove_list)
105 mock_logger_debug.assert_any_call("Removing Nova instances...")
106 mock_logger_debug.assert_any_call(" > Request sent.")
107 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
112 @mock.patch('time.sleep')
113 @mock.patch('functest.utils.openstack_clean.logger.debug')
114 def test_remove_instances_pending_delete_success(self, mock_logger_debug,
116 with mock.patch('functest.utils.openstack_clean.os_utils'
117 '.get_instances', return_value=self.deleted_list), \
118 mock.patch('functest.utils.openstack_clean.os_utils'
119 '.delete_instance', return_value=True):
120 openstack_clean.remove_instances(self.client, self.remove_list)
121 mock_logger_debug.assert_any_call("Removing Nova instances...")
122 mock_logger_debug.test_utils.RegexMatch("Removing"
125 " ...").assert_not_called()
127 @mock.patch('time.sleep')
128 @mock.patch('functest.utils.openstack_clean.logger.debug')
129 def test_remove_instances_other_delete_success(self, mock_logger_debug,
131 with mock.patch('functest.utils.openstack_clean.os_utils'
132 '.get_instances', return_value=self.other_list), \
133 mock.patch('functest.utils.openstack_clean.os_utils'
134 '.delete_instance', return_value=True):
135 openstack_clean.remove_instances(self.client, self.remove_list)
136 mock_logger_debug.assert_any_call("Removing Nova instances...")
137 mock_logger_debug.assert_any_call(" > Request sent.")
138 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
143 @mock.patch('time.sleep')
144 @mock.patch('functest.utils.openstack_clean.logger.error')
145 @mock.patch('functest.utils.openstack_clean.logger.debug')
146 def test_remove_instances_delete_failed(self, mock_logger_debug,
147 mock_logger_error, *args):
148 with mock.patch('functest.utils.openstack_clean.os_utils'
149 '.get_instances', return_value=self.test_list), \
150 mock.patch('functest.utils.openstack_clean.os_utils'
151 '.delete_instance', return_value=False):
152 openstack_clean.remove_instances(self.client, self.remove_list)
153 mock_logger_debug.assert_any_call("Removing Nova instances...")
154 mock_logger_error.assert_any_call(test_utils.
155 RegexMatch("There has been a "
157 "the instance \s*\S+"
159 mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
164 @mock.patch('functest.utils.openstack_clean.logger.debug')
165 def test_remove_images(self, mock_logger_debug):
166 with mock.patch('functest.utils.openstack_clean.os_utils'
167 '.get_images', return_value=self.test_list):
168 openstack_clean.remove_images(self.client, self.update_list)
169 mock_logger_debug.assert_any_call("Removing Glance images...")
170 mock_logger_debug.assert_any_call(" > this is a default "
174 @mock.patch('functest.utils.openstack_clean.logger.debug')
175 def test_remove_images_missing_images(self, mock_logger_debug):
176 with mock.patch('functest.utils.openstack_clean.os_utils'
177 '.get_images', return_value=[]):
178 openstack_clean.remove_images(self.client, self.update_list)
179 mock_logger_debug.assert_any_call("Removing Glance images...")
180 mock_logger_debug.assert_any_call("No images found.")
182 @mock.patch('functest.utils.openstack_clean.logger.debug')
183 def test_remove_images_delete_success(self, mock_logger_debug):
184 with mock.patch('functest.utils.openstack_clean.os_utils'
185 '.get_images', return_value=self.test_list), \
186 mock.patch('functest.utils.openstack_clean.os_utils'
187 '.delete_glance_image', return_value=True):
188 openstack_clean.remove_images(self.client, self.remove_list)
189 mock_logger_debug.assert_any_call("Removing Glance images...")
190 mock_logger_debug.assert_any_call(" > Done!")
191 mock_logger_debug.assert_any_call(test_utils.
192 RegexMatch("Removing image "
196 @mock.patch('functest.utils.openstack_clean.logger.error')
197 @mock.patch('functest.utils.openstack_clean.logger.debug')
198 def test_remove_images_delete_failed(self, mock_logger_debug,
200 with mock.patch('functest.utils.openstack_clean.os_utils'
201 '.get_images', return_value=self.test_list), \
202 mock.patch('functest.utils.openstack_clean.os_utils'
203 '.delete_glance_image', return_value=False):
204 openstack_clean.remove_images(self.client, self.remove_list)
205 mock_logger_debug.assert_any_call("Removing Glance images...")
206 mock_logger_error.assert_any_call(test_utils.
207 RegexMatch("There has been a "
208 "problem removing the"
210 mock_logger_debug.assert_any_call(test_utils.
211 RegexMatch("Removing image "
215 @mock.patch('functest.utils.openstack_clean.logger.debug')
216 def test_remove_volumes(self, mock_logger_debug):
217 with mock.patch('functest.utils.openstack_clean.os_utils'
218 '.get_volumes', return_value=self.test_list):
219 openstack_clean.remove_volumes(self.client, self.update_list)
220 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
221 mock_logger_debug.assert_any_call(" > this is a default "
225 @mock.patch('functest.utils.openstack_clean.logger.debug')
226 def test_remove_volumes_missing_volumes(self, mock_logger_debug):
227 with mock.patch('functest.utils.openstack_clean.os_utils'
228 '.get_volumes', return_value=[]):
229 openstack_clean.remove_volumes(self.client, self.update_list)
230 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
231 mock_logger_debug.assert_any_call("No volumes found.")
233 @mock.patch('functest.utils.openstack_clean.logger.debug')
234 def test_remove_volumes_delete_success(self, mock_logger_debug):
235 with mock.patch('functest.utils.openstack_clean.os_utils'
236 '.get_volumes', return_value=self.test_list), \
237 mock.patch('functest.utils.openstack_clean.os_utils'
238 '.delete_volume', return_value=True):
239 openstack_clean.remove_volumes(self.client, self.remove_list)
240 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
241 mock_logger_debug.assert_any_call(" > Done!")
242 mock_logger_debug.assert_any_call(test_utils.
243 RegexMatch("Removing cinder "
244 "volume \s*\S+ ..."))
246 @mock.patch('functest.utils.openstack_clean.logger.error')
247 @mock.patch('functest.utils.openstack_clean.logger.debug')
248 def test_remove_volumes_delete_failed(self, mock_logger_debug,
250 with mock.patch('functest.utils.openstack_clean.os_utils'
251 '.get_volumes', return_value=self.test_list), \
252 mock.patch('functest.utils.openstack_clean.os_utils'
253 '.delete_volume', return_value=False):
254 openstack_clean.remove_volumes(self.client, self.remove_list)
255 mock_logger_debug.assert_any_call("Removing Cinder volumes...")
256 mock_logger_error.assert_any_call(test_utils.
257 RegexMatch("There has been a "
261 mock_logger_debug.assert_any_call(test_utils.
262 RegexMatch("Removing cinder "
263 "volume \s*\S+ ..."))
265 @mock.patch('functest.utils.openstack_clean.logger.debug')
266 def test_remove_floatingips(self, mock_logger_debug):
267 with mock.patch('functest.utils.openstack_clean.os_utils'
269 return_value=self.floatingips_list):
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(" > this is a default "
273 "floating IP and will "
276 @mock.patch('functest.utils.openstack_clean.logger.debug')
277 def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
278 with mock.patch('functest.utils.openstack_clean.os_utils'
279 '.get_floating_ips', return_value=[]):
280 openstack_clean.remove_floatingips(self.client, self.update_list)
281 mock_logger_debug.assert_any_call("Removing floating IPs...")
282 mock_logger_debug.assert_any_call("No floating IPs found.")
284 @mock.patch('functest.utils.openstack_clean.logger.debug')
285 def test_remove_floatingips_delete_success(self, mock_logger_debug):
286 with mock.patch('functest.utils.openstack_clean.os_utils'
288 side_effect=[self.floatingips_list, None]), \
289 mock.patch('functest.utils.openstack_clean.os_utils'
290 '.delete_floating_ip', return_value=True):
291 openstack_clean.remove_floatingips(self.client, self.remove_list)
292 mock_logger_debug.assert_any_call("Removing floating IPs...")
293 mock_logger_debug.assert_any_call(" > Done!")
294 mock_logger_debug.assert_any_call(test_utils.
295 RegexMatch("Removing floating "
298 @mock.patch('functest.utils.openstack_clean.logger.error')
299 @mock.patch('functest.utils.openstack_clean.logger.debug')
300 def test_remove_floatingips_delete_failed(self, mock_logger_debug,
302 with mock.patch('functest.utils.openstack_clean.os_utils'
304 return_value=self.floatingips_list), \
305 mock.patch('functest.utils.openstack_clean.os_utils'
306 '.delete_floating_ip', return_value=False):
307 openstack_clean.remove_floatingips(self.client, self.remove_list)
308 mock_logger_debug.assert_any_call("Removing floating IPs...")
309 mock_logger_error.assert_any_call(test_utils.
310 RegexMatch("There has been a "
314 mock_logger_debug.assert_any_call(test_utils.
315 RegexMatch("Removing floating "
318 @mock.patch('time.sleep')
319 @mock.patch('functest.utils.openstack_clean.remove_routers')
320 @mock.patch('functest.utils.openstack_clean.remove_ports')
321 @mock.patch('functest.utils.openstack_clean.logger.debug')
322 def test_remove_networks(self, mock_logger_debug,
324 mock_remove_routers, *args):
325 with mock.patch('functest.utils.openstack_clean.os_utils'
327 return_value=self.test_dict_list), \
328 mock.patch('functest.utils.openstack_clean.os_utils'
329 '.get_port_list', return_value=self.ports), \
330 mock.patch('functest.utils.openstack_clean.os_utils'
331 '.get_router_list', return_value=self.routers):
332 openstack_clean.remove_networks(self.client, self.update_list,
334 mock_logger_debug.assert_any_call("Removing Neutron objects")
335 mock_logger_debug.assert_any_call(" > this is a default "
338 mock_remove_ports.assert_called_once_with(self.client, self.ports,
340 mock_remove_routers.assert_called_once_with(self.client,
344 @mock.patch('time.sleep')
345 @mock.patch('functest.utils.openstack_clean.remove_routers')
346 @mock.patch('functest.utils.openstack_clean.remove_ports')
347 @mock.patch('functest.utils.openstack_clean.logger.debug')
348 def test_remove_networks_missing_networks(self, mock_logger_debug,
350 mock_remove_routers, *args):
351 with mock.patch('functest.utils.openstack_clean.os_utils'
352 '.get_network_list', return_value=None), \
353 mock.patch('functest.utils.openstack_clean.os_utils'
354 '.get_port_list', return_value=self.ports), \
355 mock.patch('functest.utils.openstack_clean.os_utils'
356 '.get_router_list', return_value=self.routers):
357 openstack_clean.remove_networks(self.client, self.update_list,
359 mock_logger_debug.assert_any_call("Removing Neutron objects")
360 mock_logger_debug.assert_any_call("There are no networks in the"
362 mock_remove_ports.assert_called_once_with(self.client, self.ports,
364 mock_remove_routers.assert_called_once_with(self.client,
368 @mock.patch('time.sleep')
369 @mock.patch('functest.utils.openstack_clean.remove_routers')
370 @mock.patch('functest.utils.openstack_clean.remove_ports')
371 @mock.patch('functest.utils.openstack_clean.logger.debug')
372 def test_remove_networks_delete_success(self, mock_logger_debug,
374 mock_remove_routers, *args):
376 with mock.patch('functest.utils.openstack_clean.os_utils'
378 return_value=self.test_dict_list), \
379 mock.patch('functest.utils.openstack_clean.os_utils'
380 '.delete_neutron_net', return_value=True), \
381 mock.patch('functest.utils.openstack_clean.os_utils'
382 '.get_port_list', return_value=self.ports), \
383 mock.patch('functest.utils.openstack_clean.os_utils'
384 '.get_router_list', return_value=self.routers):
385 openstack_clean.remove_networks(self.client, self.remove_list,
387 mock_logger_debug.assert_any_call("Removing Neutron objects")
388 mock_logger_debug.assert_any_call(" > this network will be "
390 mock_logger_debug.assert_any_call(" > Done!")
391 mock_logger_debug.assert_any_call(test_utils.
392 RegexMatch("Removing network "
394 mock_remove_ports.assert_called_once_with(self.client, self.ports,
396 mock_remove_routers.assert_called_once_with(self.client,
400 @mock.patch('time.sleep')
401 @mock.patch('functest.utils.openstack_clean.remove_routers')
402 @mock.patch('functest.utils.openstack_clean.remove_ports')
403 @mock.patch('functest.utils.openstack_clean.logger.error')
404 @mock.patch('functest.utils.openstack_clean.logger.debug')
405 def test_remove_networks_delete_failed(self, mock_logger_debug,
408 mock_remove_routers, *args):
409 with mock.patch('functest.utils.openstack_clean.os_utils'
411 return_value=self.test_dict_list), \
412 mock.patch('functest.utils.openstack_clean.os_utils'
413 '.delete_neutron_net', return_value=False), \
414 mock.patch('functest.utils.openstack_clean.os_utils'
415 '.get_port_list', return_value=self.ports), \
416 mock.patch('functest.utils.openstack_clean.os_utils'
417 '.get_router_list', return_value=self.routers):
418 openstack_clean.remove_networks(self.client, self.remove_list,
420 mock_logger_debug.assert_any_call("Removing Neutron objects")
421 mock_logger_error.assert_any_call(test_utils.
422 RegexMatch("There has been a"
424 " the network \s*\S+"
426 mock_logger_debug.assert_any_call(test_utils.
427 RegexMatch("Removing network "
429 mock_remove_ports.assert_called_once_with(self.client, self.ports,
431 mock_remove_routers.assert_called_once_with(self.client,
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(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_debug.assert_any_call(" > Done!")
447 mock_logger_debug.assert_any_call(test_utils.
448 RegexMatch("Clearing device_"
452 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
453 @mock.patch('functest.utils.openstack_clean.logger.error')
454 @mock.patch('functest.utils.openstack_clean.logger.debug')
455 def test_force_remove_port_failed(self, mock_logger_debug,
457 mock_update_neutron_port):
458 with mock.patch('functest.utils.openstack_clean.os_utils'
459 '.delete_neutron_port',
461 openstack_clean.force_remove_port(self.client, 'id')
462 mock_logger_error.assert_any_call("There has been a "
465 mock_logger_debug.assert_any_call(test_utils.
466 RegexMatch("Clearing device_"
470 @mock.patch('functest.utils.openstack_clean.logger.debug')
471 def test_remove_routers_missing_routers(self, mock_logger_debug):
472 with mock.patch('functest.utils.openstack_clean.os_utils'
473 '.delete_neutron_router',
475 openstack_clean.remove_routers(self.client, self.test_dict_list,
477 mock_logger_debug.assert_any_call("Router is not connected"
479 "Ready to remove...")
480 mock_logger_debug.assert_any_call(" > Done!")
481 mock_logger_debug.assert_any_call(test_utils.
482 RegexMatch("Removing router "
483 "\s*\S+(\s*\S+) ..."))
485 @mock.patch('functest.utils.openstack_clean.logger.error')
486 @mock.patch('functest.utils.openstack_clean.logger.debug')
487 def test_remove_routers_failed(self, mock_logger_debug,
489 with mock.patch('functest.utils.openstack_clean.os_utils'
490 '.delete_neutron_router',
492 openstack_clean.remove_routers(self.client, self.test_dict_list,
494 mock_logger_debug.assert_any_call("Router is not connected"
496 "Ready to remove...")
497 mock_logger_debug.assert_any_call(test_utils.
498 RegexMatch("Removing router "
499 "\s*\S+(\s*\S+) ..."))
500 mock_logger_error.assert_any_call(test_utils.
501 RegexMatch("There has been "
507 @mock.patch('functest.utils.openstack_clean.logger.error')
508 @mock.patch('functest.utils.openstack_clean.logger.debug')
509 def test_remove_missing_external_gateway(self, mock_logger_debug,
511 with mock.patch('functest.utils.openstack_clean.os_utils'
512 '.delete_neutron_router',
513 return_value=False), \
514 mock.patch('functest.utils.openstack_clean.os_utils'
515 '.remove_gateway_router',
517 self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
518 openstack_clean.remove_routers(self.client, self.test_dict_list,
520 mock_logger_debug.assert_any_call("Router has gateway to external"
521 " network.Removing link...")
522 mock_logger_error.assert_any_call("There has been a problem "
523 "removing the gateway...")
524 mock_logger_debug.assert_any_call(test_utils.
525 RegexMatch("Removing router "
526 "\s*\S+(\s*\S+) ..."))
527 mock_logger_error.assert_any_call(test_utils.
528 RegexMatch("There has been "
534 @mock.patch('functest.utils.openstack_clean.logger.debug')
535 def remove_security_groups(self, mock_logger_debug):
536 with mock.patch('functest.utils.openstack_clean.os_utils'
537 '.get_security_groups',
538 return_value=self.test_dict_list):
539 openstack_clean.remove_security_groups(self.client,
541 mock_logger_debug.assert_any_call("Removing Security groups...")
542 mock_logger_debug.assert_any_call(" > this is a default "
543 "security group and will NOT "
546 @mock.patch('functest.utils.openstack_clean.logger.debug')
547 def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
548 with mock.patch('functest.utils.openstack_clean.os_utils'
549 '.get_security_groups', return_value=[]):
550 openstack_clean.remove_security_groups(self.client,
552 mock_logger_debug.assert_any_call("Removing Security groups...")
553 mock_logger_debug.assert_any_call("No security groups found.")
555 @mock.patch('functest.utils.openstack_clean.logger.debug')
556 def test_remove_security_groups_delete_success(self, mock_logger_debug):
557 with mock.patch('functest.utils.openstack_clean.os_utils'
558 '.get_security_groups',
559 return_value=self.test_dict_list), \
560 mock.patch('functest.utils.openstack_clean.os_utils'
561 '.delete_security_group', return_value=True):
562 openstack_clean.remove_security_groups(self.client,
564 mock_logger_debug.assert_any_call("Removing Security groups...")
565 mock_logger_debug.assert_any_call(" > Done!")
566 mock_logger_debug.assert_any_call(test_utils.
567 RegexMatch(" Removing \s*\S+"
570 @mock.patch('functest.utils.openstack_clean.logger.error')
571 @mock.patch('functest.utils.openstack_clean.logger.debug')
572 def test_remove_security_groups_delete_failed(self, mock_logger_debug,
574 with mock.patch('functest.utils.openstack_clean.os_utils'
575 '.get_security_groups',
576 return_value=self.test_dict_list), \
577 mock.patch('functest.utils.openstack_clean.os_utils'
578 '.delete_security_group', return_value=False):
579 openstack_clean.remove_security_groups(self.client,
581 mock_logger_debug.assert_any_call("Removing Security groups...")
582 mock_logger_error.assert_any_call(test_utils.
583 RegexMatch("There has been a "
587 mock_logger_debug.assert_any_call(test_utils.
588 RegexMatch(" Removing \s*\S+"
591 @mock.patch('functest.utils.openstack_clean.logger.debug')
592 def test_remove_users(self, mock_logger_debug):
593 with mock.patch('functest.utils.openstack_clean.os_utils'
594 '.get_users', return_value=self.test_list):
595 openstack_clean.remove_users(self.client, self.update_list)
596 mock_logger_debug.assert_any_call("Removing Users...")
597 mock_logger_debug.assert_any_call(" > this is a default "
601 @mock.patch('functest.utils.openstack_clean.logger.debug')
602 def test_remove_users_missing_users(self, mock_logger_debug):
603 with mock.patch('functest.utils.openstack_clean.os_utils'
604 '.get_users', return_value=None):
605 openstack_clean.remove_users(self.client, self.update_list)
606 mock_logger_debug.assert_any_call("Removing Users...")
607 mock_logger_debug.assert_any_call("There are no users in"
610 @mock.patch('functest.utils.openstack_clean.logger.debug')
611 def test_remove_users_delete_success(self, mock_logger_debug):
612 with mock.patch('functest.utils.openstack_clean.os_utils'
613 '.get_users', return_value=self.test_list), \
614 mock.patch('functest.utils.openstack_clean.os_utils'
615 '.delete_user', return_value=True):
616 openstack_clean.remove_users(self.client, self.remove_list)
617 mock_logger_debug.assert_any_call("Removing Users...")
618 mock_logger_debug.assert_any_call(" > Done!")
619 mock_logger_debug.assert_any_call(test_utils.
620 RegexMatch(" Removing "
623 @mock.patch('functest.utils.openstack_clean.logger.error')
624 @mock.patch('functest.utils.openstack_clean.logger.debug')
625 def test_remove_users_delete_failed(self, mock_logger_debug,
627 with mock.patch('functest.utils.openstack_clean.os_utils'
628 '.get_users', return_value=self.test_list), \
629 mock.patch('functest.utils.openstack_clean.os_utils'
630 '.delete_user', return_value=False):
631 openstack_clean.remove_users(self.client, self.remove_list)
632 mock_logger_debug.assert_any_call("Removing Users...")
633 mock_logger_error.assert_any_call(test_utils.
634 RegexMatch("There has been a "
638 mock_logger_debug.assert_any_call(test_utils.
639 RegexMatch(" Removing "
642 @mock.patch('functest.utils.openstack_clean.logger.debug')
643 def test_remove_tenants(self, mock_logger_debug):
644 with mock.patch('functest.utils.openstack_clean.os_utils'
645 '.get_tenants', return_value=self.test_list):
646 openstack_clean.remove_tenants(self.client, self.update_list)
647 mock_logger_debug.assert_any_call("Removing Tenants...")
648 mock_logger_debug.assert_any_call(" > this is a default"
652 @mock.patch('functest.utils.openstack_clean.logger.debug')
653 def test_remove_tenants_missing_tenants(self, mock_logger_debug):
654 with mock.patch('functest.utils.openstack_clean.os_utils'
655 '.get_tenants', return_value=None):
656 openstack_clean.remove_tenants(self.client, self.update_list)
657 mock_logger_debug.assert_any_call("Removing Tenants...")
658 mock_logger_debug.assert_any_call("There are no tenants in"
661 @mock.patch('functest.utils.openstack_clean.logger.debug')
662 def test_remove_tenants_delete_success(self, mock_logger_debug):
663 with mock.patch('functest.utils.openstack_clean.os_utils'
664 '.get_tenants', return_value=self.test_list), \
665 mock.patch('functest.utils.openstack_clean.os_utils'
666 '.delete_tenant', return_value=True):
667 openstack_clean.remove_tenants(self.client, self.remove_list)
668 mock_logger_debug.assert_any_call("Removing Tenants...")
669 mock_logger_debug.assert_any_call(" > Done!")
670 mock_logger_debug.assert_any_call(test_utils.
671 RegexMatch(" Removing "
674 @mock.patch('functest.utils.openstack_clean.logger.error')
675 @mock.patch('functest.utils.openstack_clean.logger.debug')
676 def test_remove_tenants_delete_failed(self, mock_logger_debug,
678 with mock.patch('functest.utils.openstack_clean.os_utils'
679 '.get_tenants', return_value=self.test_list), \
680 mock.patch('functest.utils.openstack_clean.os_utils'
681 '.delete_tenant', return_value=False):
682 openstack_clean.remove_tenants(self.client, self.remove_list)
683 mock_logger_debug.assert_any_call("Removing Tenants...")
684 mock_logger_error.assert_any_call(test_utils.
685 RegexMatch("There has been a "
689 mock_logger_debug.assert_any_call(test_utils.
690 RegexMatch(" Removing "
693 @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
694 @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
695 @mock.patch('functest.utils.openstack_clean.os_utils'
696 '.get_keystone_client')
697 @mock.patch('functest.utils.openstack_clean.os_utils'
698 '.get_neutron_client')
699 @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
700 @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
702 @mock.patch('functest.utils.openstack_clean.logger.info')
703 @mock.patch('functest.utils.openstack_clean.logger.debug')
704 def test_main_default(self, mock_logger_debug, mock_logger_info,
705 mock_creds, mock_nova, mock_neutron,
706 mock_keystone, mock_cinder, mock_glance):
708 with mock.patch('functest.utils.openstack_clean.remove_instances') \
709 as mock_remove_instances, \
710 mock.patch('functest.utils.openstack_clean.remove_images') \
711 as mock_remove_images, \
712 mock.patch('functest.utils.openstack_clean.remove_volumes') \
713 as mock_remove_volumes, \
714 mock.patch('functest.utils.openstack_clean.remove_floatingips') \
715 as mock_remove_floatingips, \
716 mock.patch('functest.utils.openstack_clean.remove_networks') \
717 as mock_remove_networks, \
718 mock.patch('functest.utils.openstack_clean.'
719 'remove_security_groups') \
720 as mock_remove_security_groups, \
721 mock.patch('functest.utils.openstack_clean.remove_users') \
722 as mock_remove_users, \
723 mock.patch('functest.utils.openstack_clean.remove_tenants') \
724 as mock_remove_tenants, \
725 mock.patch('functest.utils.openstack_clean.yaml.safe_load',
726 return_value=mock.Mock()), \
727 mock.patch('six.moves.builtins.open', mock.mock_open()) as m:
728 openstack_clean.main()
729 self.assertTrue(mock_remove_instances)
730 self.assertTrue(mock_remove_images)
731 self.assertTrue(mock_remove_volumes)
732 self.assertTrue(mock_remove_floatingips)
733 self.assertTrue(mock_remove_networks)
734 self.assertTrue(mock_remove_security_groups)
735 self.assertTrue(mock_remove_users)
736 self.assertTrue(mock_remove_tenants)
737 m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
738 mock_logger_info.assert_called_once_with("Cleaning OpenStack "
742 if __name__ == "__main__":
743 logging.disable(logging.CRITICAL)
744 unittest.main(verbosity=2)