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('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'
260 return_value=self.floatingips_list):
261 openstack_clean.remove_floatingips(self.client, self.update_list)
262 mock_logger_debug.assert_any_call("Removing floating IPs...")
263 mock_logger_debug.assert_any_call(" > this is a default "
264 "floating IP and will "
267 @mock.patch('functest.utils.openstack_clean.logger.debug')
268 def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
269 with mock.patch('functest.utils.openstack_clean.os_utils'
270 '.get_floating_ips', return_value=[]):
271 openstack_clean.remove_floatingips(self.client, self.update_list)
272 mock_logger_debug.assert_any_call("Removing floating IPs...")
273 mock_logger_debug.assert_any_call("No floating IPs found.")
275 @mock.patch('functest.utils.openstack_clean.logger.debug')
276 def test_remove_floatingips_delete_success(self, mock_logger_debug):
277 with mock.patch('functest.utils.openstack_clean.os_utils'
279 return_value=self.floatingips_list), \
280 mock.patch('functest.utils.openstack_clean.os_utils'
281 '.delete_floating_ip', return_value=True):
282 openstack_clean.remove_floatingips(self.client, self.remove_list)
283 mock_logger_debug.assert_any_call("Removing floating IPs...")
284 mock_logger_debug.assert_any_call(" > Done!")
285 mock_logger_debug.assert_any_call(test_utils.
286 RegexMatch("Removing floating "
289 @mock.patch('functest.utils.openstack_clean.logger.error')
290 @mock.patch('functest.utils.openstack_clean.logger.debug')
291 def test_remove_floatingips_delete_failed(self, mock_logger_debug,
293 with mock.patch('functest.utils.openstack_clean.os_utils'
295 return_value=self.floatingips_list), \
296 mock.patch('functest.utils.openstack_clean.os_utils'
297 '.delete_floating_ip', return_value=False):
298 openstack_clean.remove_floatingips(self.client, self.remove_list)
299 mock_logger_debug.assert_any_call("Removing floating IPs...")
300 mock_logger_error.assert_any_call(test_utils.
301 RegexMatch("There has been a "
305 mock_logger_debug.assert_any_call(test_utils.
306 RegexMatch("Removing floating "
309 @mock.patch('functest.utils.openstack_clean.remove_routers')
310 @mock.patch('functest.utils.openstack_clean.remove_ports')
311 @mock.patch('functest.utils.openstack_clean.logger.debug')
312 def test_remove_networks(self, mock_logger_debug,
314 mock_remove_routers):
315 with mock.patch('functest.utils.openstack_clean.os_utils'
317 return_value=self.test_dict_list), \
318 mock.patch('functest.utils.openstack_clean.os_utils'
319 '.get_port_list', return_value=self.ports), \
320 mock.patch('functest.utils.openstack_clean.os_utils'
321 '.get_router_list', return_value=self.routers):
322 openstack_clean.remove_networks(self.client, self.update_list,
324 mock_logger_debug.assert_any_call("Removing Neutron objects")
325 mock_logger_debug.assert_any_call(" > this is a default "
328 mock_remove_ports.assert_called_once_with(self.client, self.ports,
330 mock_remove_routers.assert_called_once_with(self.client,
334 @mock.patch('functest.utils.openstack_clean.remove_routers')
335 @mock.patch('functest.utils.openstack_clean.remove_ports')
336 @mock.patch('functest.utils.openstack_clean.logger.debug')
337 def test_remove_networks_missing_networks(self, mock_logger_debug,
339 mock_remove_routers):
340 with mock.patch('functest.utils.openstack_clean.os_utils'
341 '.get_network_list', return_value=None), \
342 mock.patch('functest.utils.openstack_clean.os_utils'
343 '.get_port_list', return_value=self.ports), \
344 mock.patch('functest.utils.openstack_clean.os_utils'
345 '.get_router_list', return_value=self.routers):
346 openstack_clean.remove_networks(self.client, self.update_list,
348 mock_logger_debug.assert_any_call("Removing Neutron objects")
349 mock_logger_debug.assert_any_call("There are no networks in the"
351 mock_remove_ports.assert_called_once_with(self.client, self.ports,
353 mock_remove_routers.assert_called_once_with(self.client,
357 @mock.patch('functest.utils.openstack_clean.remove_routers')
358 @mock.patch('functest.utils.openstack_clean.remove_ports')
359 @mock.patch('functest.utils.openstack_clean.logger.debug')
360 def test_remove_networks_delete_success(self, mock_logger_debug,
362 mock_remove_routers):
364 with mock.patch('functest.utils.openstack_clean.os_utils'
366 return_value=self.test_dict_list), \
367 mock.patch('functest.utils.openstack_clean.os_utils'
368 '.delete_neutron_net', return_value=True), \
369 mock.patch('functest.utils.openstack_clean.os_utils'
370 '.get_port_list', return_value=self.ports), \
371 mock.patch('functest.utils.openstack_clean.os_utils'
372 '.get_router_list', return_value=self.routers):
373 openstack_clean.remove_networks(self.client, self.remove_list,
375 mock_logger_debug.assert_any_call("Removing Neutron objects")
376 mock_logger_debug.assert_any_call(" > this network will be "
378 mock_logger_debug.assert_any_call(" > Done!")
379 mock_logger_debug.assert_any_call(test_utils.
380 RegexMatch("Removing network "
382 mock_remove_ports.assert_called_once_with(self.client, self.ports,
384 mock_remove_routers.assert_called_once_with(self.client,
388 @mock.patch('functest.utils.openstack_clean.remove_routers')
389 @mock.patch('functest.utils.openstack_clean.remove_ports')
390 @mock.patch('functest.utils.openstack_clean.logger.error')
391 @mock.patch('functest.utils.openstack_clean.logger.debug')
392 def test_remove_networks_delete_failed(self, mock_logger_debug,
395 mock_remove_routers):
396 with mock.patch('functest.utils.openstack_clean.os_utils'
398 return_value=self.test_dict_list), \
399 mock.patch('functest.utils.openstack_clean.os_utils'
400 '.delete_neutron_net', return_value=False), \
401 mock.patch('functest.utils.openstack_clean.os_utils'
402 '.get_port_list', return_value=self.ports), \
403 mock.patch('functest.utils.openstack_clean.os_utils'
404 '.get_router_list', return_value=self.routers):
405 openstack_clean.remove_networks(self.client, self.remove_list,
407 mock_logger_debug.assert_any_call("Removing Neutron objects")
408 mock_logger_error.assert_any_call(test_utils.
409 RegexMatch("There has been a"
411 " the network \s*\S+"
413 mock_logger_debug.assert_any_call(test_utils.
414 RegexMatch("Removing network "
416 mock_remove_ports.assert_called_once_with(self.client, self.ports,
418 mock_remove_routers.assert_called_once_with(self.client,
423 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
424 @mock.patch('functest.utils.openstack_clean.logger.error')
425 @mock.patch('functest.utils.openstack_clean.logger.debug')
426 def test_force_remove_port(self, mock_logger_debug,
428 mock_update_neutron_port):
429 with mock.patch('functest.utils.openstack_clean.os_utils'
430 '.delete_neutron_port',
432 openstack_clean.force_remove_port(self.client, 'id')
433 mock_logger_debug.assert_any_call(" > Done!")
434 mock_logger_debug.assert_any_call(test_utils.
435 RegexMatch("Clearing device_"
439 @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
440 @mock.patch('functest.utils.openstack_clean.logger.error')
441 @mock.patch('functest.utils.openstack_clean.logger.debug')
442 def test_force_remove_port_failed(self, mock_logger_debug,
444 mock_update_neutron_port):
445 with mock.patch('functest.utils.openstack_clean.os_utils'
446 '.delete_neutron_port',
448 openstack_clean.force_remove_port(self.client, 'id')
449 mock_logger_error.assert_any_call("There has been a "
452 mock_logger_debug.assert_any_call(test_utils.
453 RegexMatch("Clearing device_"
457 @mock.patch('functest.utils.openstack_clean.logger.debug')
458 def test_remove_routers_missing_routers(self, mock_logger_debug):
459 with mock.patch('functest.utils.openstack_clean.os_utils'
460 '.delete_neutron_router',
462 openstack_clean.remove_routers(self.client, self.test_dict_list,
464 mock_logger_debug.assert_any_call("Router is not connected"
466 "Ready to remove...")
467 mock_logger_debug.assert_any_call(" > Done!")
468 mock_logger_debug.assert_any_call(test_utils.
469 RegexMatch("Removing router "
470 "\s*\S+(\s*\S+) ..."))
472 @mock.patch('functest.utils.openstack_clean.logger.error')
473 @mock.patch('functest.utils.openstack_clean.logger.debug')
474 def test_remove_routers_failed(self, mock_logger_debug,
476 with mock.patch('functest.utils.openstack_clean.os_utils'
477 '.delete_neutron_router',
479 openstack_clean.remove_routers(self.client, self.test_dict_list,
481 mock_logger_debug.assert_any_call("Router is not connected"
483 "Ready to remove...")
484 mock_logger_debug.assert_any_call(test_utils.
485 RegexMatch("Removing router "
486 "\s*\S+(\s*\S+) ..."))
487 mock_logger_error.assert_any_call(test_utils.
488 RegexMatch("There has been "
494 @mock.patch('functest.utils.openstack_clean.logger.error')
495 @mock.patch('functest.utils.openstack_clean.logger.debug')
496 def test_remove_missing_external_gateway(self, mock_logger_debug,
498 with mock.patch('functest.utils.openstack_clean.os_utils'
499 '.delete_neutron_router',
500 return_value=False), \
501 mock.patch('functest.utils.openstack_clean.os_utils'
502 '.remove_gateway_router',
504 self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
505 openstack_clean.remove_routers(self.client, self.test_dict_list,
507 mock_logger_debug.assert_any_call("Router has gateway to external"
508 " network.Removing link...")
509 mock_logger_error.assert_any_call("There has been a problem "
510 "removing the gateway...")
511 mock_logger_debug.assert_any_call(test_utils.
512 RegexMatch("Removing router "
513 "\s*\S+(\s*\S+) ..."))
514 mock_logger_error.assert_any_call(test_utils.
515 RegexMatch("There has been "
521 @mock.patch('functest.utils.openstack_clean.logger.debug')
522 def remove_security_groups(self, mock_logger_debug):
523 with mock.patch('functest.utils.openstack_clean.os_utils'
524 '.get_security_groups',
525 return_value=self.test_dict_list):
526 openstack_clean.remove_security_groups(self.client,
528 mock_logger_debug.assert_any_call("Removing Security groups...")
529 mock_logger_debug.assert_any_call(" > this is a default "
530 "security group and will NOT "
533 @mock.patch('functest.utils.openstack_clean.logger.debug')
534 def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
535 with mock.patch('functest.utils.openstack_clean.os_utils'
536 '.get_security_groups', return_value=[]):
537 openstack_clean.remove_security_groups(self.client,
539 mock_logger_debug.assert_any_call("Removing Security groups...")
540 mock_logger_debug.assert_any_call("No security groups found.")
542 @mock.patch('functest.utils.openstack_clean.logger.debug')
543 def test_remove_security_groups_delete_success(self, mock_logger_debug):
544 with mock.patch('functest.utils.openstack_clean.os_utils'
545 '.get_security_groups',
546 return_value=self.test_dict_list), \
547 mock.patch('functest.utils.openstack_clean.os_utils'
548 '.delete_security_group', return_value=True):
549 openstack_clean.remove_security_groups(self.client,
551 mock_logger_debug.assert_any_call("Removing Security groups...")
552 mock_logger_debug.assert_any_call(" > Done!")
553 mock_logger_debug.assert_any_call(test_utils.
554 RegexMatch(" Removing \s*\S+"
557 @mock.patch('functest.utils.openstack_clean.logger.error')
558 @mock.patch('functest.utils.openstack_clean.logger.debug')
559 def test_remove_security_groups_delete_failed(self, mock_logger_debug,
561 with mock.patch('functest.utils.openstack_clean.os_utils'
562 '.get_security_groups',
563 return_value=self.test_dict_list), \
564 mock.patch('functest.utils.openstack_clean.os_utils'
565 '.delete_security_group', return_value=False):
566 openstack_clean.remove_security_groups(self.client,
568 mock_logger_debug.assert_any_call("Removing Security groups...")
569 mock_logger_error.assert_any_call(test_utils.
570 RegexMatch("There has been a "
574 mock_logger_debug.assert_any_call(test_utils.
575 RegexMatch(" Removing \s*\S+"
578 @mock.patch('functest.utils.openstack_clean.logger.debug')
579 def test_remove_users(self, mock_logger_debug):
580 with mock.patch('functest.utils.openstack_clean.os_utils'
581 '.get_users', return_value=self.test_list):
582 openstack_clean.remove_users(self.client, self.update_list)
583 mock_logger_debug.assert_any_call("Removing Users...")
584 mock_logger_debug.assert_any_call(" > this is a default "
588 @mock.patch('functest.utils.openstack_clean.logger.debug')
589 def test_remove_users_missing_users(self, mock_logger_debug):
590 with mock.patch('functest.utils.openstack_clean.os_utils'
591 '.get_users', return_value=None):
592 openstack_clean.remove_users(self.client, self.update_list)
593 mock_logger_debug.assert_any_call("Removing Users...")
594 mock_logger_debug.assert_any_call("There are no users in"
597 @mock.patch('functest.utils.openstack_clean.logger.debug')
598 def test_remove_users_delete_success(self, mock_logger_debug):
599 with mock.patch('functest.utils.openstack_clean.os_utils'
600 '.get_users', return_value=self.test_list), \
601 mock.patch('functest.utils.openstack_clean.os_utils'
602 '.delete_user', return_value=True):
603 openstack_clean.remove_users(self.client, self.remove_list)
604 mock_logger_debug.assert_any_call("Removing Users...")
605 mock_logger_debug.assert_any_call(" > Done!")
606 mock_logger_debug.assert_any_call(test_utils.
607 RegexMatch(" Removing "
610 @mock.patch('functest.utils.openstack_clean.logger.error')
611 @mock.patch('functest.utils.openstack_clean.logger.debug')
612 def test_remove_users_delete_failed(self, mock_logger_debug,
614 with mock.patch('functest.utils.openstack_clean.os_utils'
615 '.get_users', return_value=self.test_list), \
616 mock.patch('functest.utils.openstack_clean.os_utils'
617 '.delete_user', return_value=False):
618 openstack_clean.remove_users(self.client, self.remove_list)
619 mock_logger_debug.assert_any_call("Removing Users...")
620 mock_logger_error.assert_any_call(test_utils.
621 RegexMatch("There has been a "
625 mock_logger_debug.assert_any_call(test_utils.
626 RegexMatch(" Removing "
629 @mock.patch('functest.utils.openstack_clean.logger.debug')
630 def test_remove_tenants(self, mock_logger_debug):
631 with mock.patch('functest.utils.openstack_clean.os_utils'
632 '.get_tenants', return_value=self.test_list):
633 openstack_clean.remove_tenants(self.client, self.update_list)
634 mock_logger_debug.assert_any_call("Removing Tenants...")
635 mock_logger_debug.assert_any_call(" > this is a default"
639 @mock.patch('functest.utils.openstack_clean.logger.debug')
640 def test_remove_tenants_missing_tenants(self, mock_logger_debug):
641 with mock.patch('functest.utils.openstack_clean.os_utils'
642 '.get_tenants', return_value=None):
643 openstack_clean.remove_tenants(self.client, self.update_list)
644 mock_logger_debug.assert_any_call("Removing Tenants...")
645 mock_logger_debug.assert_any_call("There are no tenants in"
648 @mock.patch('functest.utils.openstack_clean.logger.debug')
649 def test_remove_tenants_delete_success(self, mock_logger_debug):
650 with mock.patch('functest.utils.openstack_clean.os_utils'
651 '.get_tenants', return_value=self.test_list), \
652 mock.patch('functest.utils.openstack_clean.os_utils'
653 '.delete_tenant', return_value=True):
654 openstack_clean.remove_tenants(self.client, self.remove_list)
655 mock_logger_debug.assert_any_call("Removing Tenants...")
656 mock_logger_debug.assert_any_call(" > Done!")
657 mock_logger_debug.assert_any_call(test_utils.
658 RegexMatch(" Removing "
661 @mock.patch('functest.utils.openstack_clean.logger.error')
662 @mock.patch('functest.utils.openstack_clean.logger.debug')
663 def test_remove_tenants_delete_failed(self, mock_logger_debug,
665 with mock.patch('functest.utils.openstack_clean.os_utils'
666 '.get_tenants', return_value=self.test_list), \
667 mock.patch('functest.utils.openstack_clean.os_utils'
668 '.delete_tenant', return_value=False):
669 openstack_clean.remove_tenants(self.client, self.remove_list)
670 mock_logger_debug.assert_any_call("Removing Tenants...")
671 mock_logger_error.assert_any_call(test_utils.
672 RegexMatch("There has been a "
676 mock_logger_debug.assert_any_call(test_utils.
677 RegexMatch(" Removing "
680 @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
681 @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
682 @mock.patch('functest.utils.openstack_clean.os_utils'
683 '.get_keystone_client')
684 @mock.patch('functest.utils.openstack_clean.os_utils'
685 '.get_neutron_client')
686 @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
687 @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
689 @mock.patch('functest.utils.openstack_clean.logger.info')
690 @mock.patch('functest.utils.openstack_clean.logger.debug')
691 def test_main_default(self, mock_logger_debug, mock_logger_info,
692 mock_creds, mock_nova, mock_neutron,
693 mock_keystone, mock_cinder, mock_glance):
695 with mock.patch('functest.utils.openstack_clean.remove_instances') \
696 as mock_remove_instances, \
697 mock.patch('functest.utils.openstack_clean.remove_images') \
698 as mock_remove_images, \
699 mock.patch('functest.utils.openstack_clean.remove_volumes') \
700 as mock_remove_volumes, \
701 mock.patch('functest.utils.openstack_clean.remove_floatingips') \
702 as mock_remove_floatingips, \
703 mock.patch('functest.utils.openstack_clean.remove_networks') \
704 as mock_remove_networks, \
705 mock.patch('functest.utils.openstack_clean.'
706 'remove_security_groups') \
707 as mock_remove_security_groups, \
708 mock.patch('functest.utils.openstack_clean.remove_users') \
709 as mock_remove_users, \
710 mock.patch('functest.utils.openstack_clean.remove_tenants') \
711 as mock_remove_tenants, \
712 mock.patch('functest.utils.openstack_clean.yaml.safe_load',
713 return_value=mock.Mock()), \
714 mock.patch('__builtin__.open', mock.mock_open()) as m:
715 openstack_clean.main()
716 self.assertTrue(mock_remove_instances)
717 self.assertTrue(mock_remove_images)
718 self.assertTrue(mock_remove_volumes)
719 self.assertTrue(mock_remove_floatingips)
720 self.assertTrue(mock_remove_networks)
721 self.assertTrue(mock_remove_security_groups)
722 self.assertTrue(mock_remove_users)
723 self.assertTrue(mock_remove_tenants)
724 m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
725 mock_logger_info.assert_called_once_with("Cleaning OpenStack "
729 if __name__ == "__main__":
730 logging.disable(logging.CRITICAL)
731 unittest.main(verbosity=2)