Merge "Added unit tests for utils/openstack_utils.py"
[functest.git] / functest / tests / unit / utils / test_openstack_clean.py
1 #!/usr/bin/env python
2
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
7
8 import logging
9 import mock
10 import unittest
11
12 from functest.utils import openstack_clean
13 from functest.tests.unit import test_utils
14
15
16 class OSCleanTesting(unittest.TestCase):
17
18     logging.disable(logging.CRITICAL)
19
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)}
24         mock_obj.configure_mock(**attrs)
25         return mock_obj
26
27     def setUp(self):
28         self.client = mock.Mock()
29         self.test_list = [self._get_instance(1), self._get_instance(2)]
30         self.update_list = {'id1': 'name1', 'id2': 'name2'}
31         self.remove_list = {'id3': 'name3', 'id4': 'name4'}
32         self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1',
33                                 'router:external': False,
34                                 'external_gateway_info': None},
35                                {'id': 'id2', 'name': 'name2', 'ip': 'ip2',
36                                 'router:external': False,
37                                 'external_gateway_info': None}]
38         self.routers = [mock.Mock()]
39         self.ports = [mock.Mock()]
40
41     @mock.patch('functest.utils.openstack_clean.logger.debug')
42     def test_separator(self, mock_logger_debug):
43         openstack_clean.separator()
44         mock_logger_debug.assert_called_once_with("-----------------"
45                                                   "-----------------"
46                                                   "---------")
47
48     @mock.patch('functest.utils.openstack_clean.logger.debug')
49     def test_remove_instances(self, mock_logger_debug):
50         with mock.patch('functest.utils.openstack_clean.os_utils'
51                         '.get_instances', return_value=self.test_list):
52             openstack_clean.remove_instances(self.client, self.update_list)
53             mock_logger_debug.assert_any_call("Removing Nova instances...")
54             mock_logger_debug.assert_any_call("   > this is a default "
55                                               "instance and will "
56                                               "NOT be deleted.")
57
58     @mock.patch('functest.utils.openstack_clean.logger.debug')
59     def test_remove_instances_missing_instances(self, mock_logger_debug):
60         with mock.patch('functest.utils.openstack_clean.os_utils'
61                         '.get_instances', return_value=[]):
62             openstack_clean.remove_instances(self.client, self.update_list)
63             mock_logger_debug.assert_any_call("Removing Nova instances...")
64             mock_logger_debug.assert_any_call("No instances found.")
65
66     @mock.patch('functest.utils.openstack_clean.logger.debug')
67     def test_remove_instances_delete_success(self, mock_logger_debug):
68         with mock.patch('functest.utils.openstack_clean.os_utils'
69                         '.get_instances', return_value=self.test_list), \
70                 mock.patch('functest.utils.openstack_clean.os_utils'
71                            '.delete_instance', return_value=True):
72             openstack_clean.remove_instances(self.client, self.remove_list)
73             mock_logger_debug.assert_any_call("Removing Nova instances...")
74             mock_logger_debug.assert_any_call("  > Request sent.")
75             mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
76                                                                     " instance"
77                                                                     " '\s*\S+'"
78                                                                     " ..."))
79
80     @mock.patch('functest.utils.openstack_clean.logger.error')
81     @mock.patch('functest.utils.openstack_clean.logger.debug')
82     def test_remove_instances_delete_failed(self, mock_logger_debug,
83                                             mock_logger_error):
84         with mock.patch('functest.utils.openstack_clean.os_utils'
85                         '.get_instances', return_value=self.test_list), \
86                 mock.patch('functest.utils.openstack_clean.os_utils'
87                            '.delete_instance', return_value=False):
88             openstack_clean.remove_instances(self.client, self.remove_list)
89             mock_logger_debug.assert_any_call("Removing Nova instances...")
90             mock_logger_error.assert_any_call(test_utils.
91                                               RegexMatch("There has been a "
92                                                          "problem removing "
93                                                          "the instance \s*\S+"
94                                                          "..."))
95             mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
96                                                                     " instance"
97                                                                     " '\s*\S+'"
98                                                                     " ..."))
99
100     @mock.patch('functest.utils.openstack_clean.logger.debug')
101     def test_remove_images(self, mock_logger_debug):
102         with mock.patch('functest.utils.openstack_clean.os_utils'
103                         '.get_images', return_value=self.test_list):
104             openstack_clean.remove_images(self.client, self.update_list)
105             mock_logger_debug.assert_any_call("Removing Glance images...")
106             mock_logger_debug.assert_any_call("   > this is a default "
107                                               "image and will "
108                                               "NOT be deleted.")
109
110     @mock.patch('functest.utils.openstack_clean.logger.debug')
111     def test_remove_images_missing_images(self, mock_logger_debug):
112         with mock.patch('functest.utils.openstack_clean.os_utils'
113                         '.get_images', return_value=[]):
114             openstack_clean.remove_images(self.client, self.update_list)
115             mock_logger_debug.assert_any_call("Removing Glance images...")
116             mock_logger_debug.assert_any_call("No images found.")
117
118     @mock.patch('functest.utils.openstack_clean.logger.debug')
119     def test_remove_images_delete_success(self, mock_logger_debug):
120         with mock.patch('functest.utils.openstack_clean.os_utils'
121                         '.get_images', return_value=self.test_list), \
122                 mock.patch('functest.utils.openstack_clean.os_utils'
123                            '.delete_glance_image', return_value=True):
124             openstack_clean.remove_images(self.client, self.remove_list)
125             mock_logger_debug.assert_any_call("Removing Glance images...")
126             mock_logger_debug.assert_any_call("  > Done!")
127             mock_logger_debug.assert_any_call(test_utils.
128                                               RegexMatch("Removing image "
129                                                          "\s*\S+,"
130                                                          " ID=\s*\S+ ..."))
131
132     @mock.patch('functest.utils.openstack_clean.logger.error')
133     @mock.patch('functest.utils.openstack_clean.logger.debug')
134     def test_remove_images_delete_failed(self, mock_logger_debug,
135                                          mock_logger_error):
136         with mock.patch('functest.utils.openstack_clean.os_utils'
137                         '.get_images', return_value=self.test_list), \
138                 mock.patch('functest.utils.openstack_clean.os_utils'
139                            '.delete_glance_image', return_value=False):
140             openstack_clean.remove_images(self.client, self.remove_list)
141             mock_logger_debug.assert_any_call("Removing Glance images...")
142             mock_logger_error.assert_any_call(test_utils.
143                                               RegexMatch("There has been a "
144                                                          "problem removing the"
145                                                          "image \s*\S+..."))
146             mock_logger_debug.assert_any_call(test_utils.
147                                               RegexMatch("Removing image "
148                                                          "\s*\S+,"
149                                                          " ID=\s*\S+ ..."))
150
151     @mock.patch('functest.utils.openstack_clean.logger.debug')
152     def test_remove_volumes(self, mock_logger_debug):
153         with mock.patch('functest.utils.openstack_clean.os_utils'
154                         '.get_volumes', return_value=self.test_list):
155             openstack_clean.remove_volumes(self.client, self.update_list)
156             mock_logger_debug.assert_any_call("Removing Cinder volumes...")
157             mock_logger_debug.assert_any_call("   > this is a default "
158                                               "volume and will "
159                                               "NOT be deleted.")
160
161     @mock.patch('functest.utils.openstack_clean.logger.debug')
162     def test_remove_volumes_missing_volumes(self, mock_logger_debug):
163         with mock.patch('functest.utils.openstack_clean.os_utils'
164                         '.get_volumes', return_value=[]):
165             openstack_clean.remove_volumes(self.client, self.update_list)
166             mock_logger_debug.assert_any_call("Removing Cinder volumes...")
167             mock_logger_debug.assert_any_call("No volumes found.")
168
169     @mock.patch('functest.utils.openstack_clean.logger.debug')
170     def test_remove_volumes_delete_success(self, mock_logger_debug):
171         with mock.patch('functest.utils.openstack_clean.os_utils'
172                         '.get_volumes', return_value=self.test_list), \
173                 mock.patch('functest.utils.openstack_clean.os_utils'
174                            '.delete_volume', return_value=True):
175             openstack_clean.remove_volumes(self.client, self.remove_list)
176             mock_logger_debug.assert_any_call("Removing Cinder volumes...")
177             mock_logger_debug.assert_any_call("  > Done!")
178             mock_logger_debug.assert_any_call(test_utils.
179                                               RegexMatch("Removing cinder "
180                                                          "volume \s*\S+ ..."))
181
182     @mock.patch('functest.utils.openstack_clean.logger.error')
183     @mock.patch('functest.utils.openstack_clean.logger.debug')
184     def test_remove_volumes_delete_failed(self, mock_logger_debug,
185                                           mock_logger_error):
186         with mock.patch('functest.utils.openstack_clean.os_utils'
187                         '.get_volumes', return_value=self.test_list), \
188                 mock.patch('functest.utils.openstack_clean.os_utils'
189                            '.delete_volume', return_value=False):
190             openstack_clean.remove_volumes(self.client, self.remove_list)
191             mock_logger_debug.assert_any_call("Removing Cinder volumes...")
192             mock_logger_error.assert_any_call(test_utils.
193                                               RegexMatch("There has been a "
194                                                          "problem removing "
195                                                          "the "
196                                                          "volume \s*\S+..."))
197             mock_logger_debug.assert_any_call(test_utils.
198                                               RegexMatch("Removing cinder "
199                                                          "volume \s*\S+ ..."))
200
201     @mock.patch('functest.utils.openstack_clean.logger.debug')
202     def test_remove_floatingips(self, mock_logger_debug):
203         with mock.patch('functest.utils.openstack_clean.os_utils'
204                         '.get_floating_ips', return_value=self.test_list):
205             openstack_clean.remove_floatingips(self.client, self.update_list)
206             mock_logger_debug.assert_any_call("Removing floating IPs...")
207             mock_logger_debug.assert_any_call("   > this is a default "
208                                               "floating IP and will "
209                                               "NOT be deleted.")
210
211     @mock.patch('functest.utils.openstack_clean.logger.debug')
212     def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
213         with mock.patch('functest.utils.openstack_clean.os_utils'
214                         '.get_floating_ips', return_value=[]):
215             openstack_clean.remove_floatingips(self.client, self.update_list)
216             mock_logger_debug.assert_any_call("Removing floating IPs...")
217             mock_logger_debug.assert_any_call("No floating IPs found.")
218
219     @mock.patch('functest.utils.openstack_clean.logger.debug')
220     def test_remove_floatingips_delete_success(self, mock_logger_debug):
221         with mock.patch('functest.utils.openstack_clean.os_utils'
222                         '.get_floating_ips', return_value=self.test_list), \
223                 mock.patch('functest.utils.openstack_clean.os_utils'
224                            '.delete_volume', return_value=True):
225             openstack_clean.remove_floatingips(self.client, self.remove_list)
226             mock_logger_debug.assert_any_call("Removing floating IPs...")
227             mock_logger_debug.assert_any_call("  > Done!")
228             mock_logger_debug.assert_any_call(test_utils.
229                                               RegexMatch("Removing floating "
230                                                          "IP \s*\S+ ..."))
231
232     @mock.patch('functest.utils.openstack_clean.logger.error')
233     @mock.patch('functest.utils.openstack_clean.logger.debug')
234     def test_remove_floatingips_delete_failed(self, mock_logger_debug,
235                                               mock_logger_error):
236         with mock.patch('functest.utils.openstack_clean.os_utils'
237                         '.get_floating_ips', return_value=self.test_list), \
238                 mock.patch('functest.utils.openstack_clean.os_utils'
239                            '.delete_floating_ip', return_value=False):
240             openstack_clean.remove_floatingips(self.client, self.remove_list)
241             mock_logger_debug.assert_any_call("Removing floating IPs...")
242             mock_logger_error.assert_any_call(test_utils.
243                                               RegexMatch("There has been a "
244                                                          "problem removing "
245                                                          "the floating IP "
246                                                          "\s*\S+..."))
247             mock_logger_debug.assert_any_call(test_utils.
248                                               RegexMatch("Removing floating "
249                                                          "IP \s*\S+ ..."))
250
251     @mock.patch('functest.utils.openstack_clean.remove_routers')
252     @mock.patch('functest.utils.openstack_clean.remove_ports')
253     @mock.patch('functest.utils.openstack_clean.logger.debug')
254     def test_remove_networks(self, mock_logger_debug,
255                              mock_remove_ports,
256                              mock_remove_routers):
257         with mock.patch('functest.utils.openstack_clean.os_utils'
258                         '.get_network_list',
259                         return_value=self.test_dict_list), \
260                 mock.patch('functest.utils.openstack_clean.os_utils'
261                            '.get_port_list', return_value=self.ports), \
262                 mock.patch('functest.utils.openstack_clean.os_utils'
263                            '.get_router_list', return_value=self.routers):
264             openstack_clean.remove_networks(self.client, self.update_list,
265                                             self.update_list)
266             mock_logger_debug.assert_any_call("Removing Neutron objects")
267             mock_logger_debug.assert_any_call("   > this is a default "
268                                               "network and will "
269                                               "NOT be deleted.")
270             mock_remove_ports.assert_called_once_with(self.client, self.ports,
271                                                       [])
272             mock_remove_routers.assert_called_once_with(self.client,
273                                                         self.routers,
274                                                         self.update_list)
275
276     @mock.patch('functest.utils.openstack_clean.remove_routers')
277     @mock.patch('functest.utils.openstack_clean.remove_ports')
278     @mock.patch('functest.utils.openstack_clean.logger.debug')
279     def test_remove_networks_missing_networks(self, mock_logger_debug,
280                                               mock_remove_ports,
281                                               mock_remove_routers):
282         with mock.patch('functest.utils.openstack_clean.os_utils'
283                         '.get_network_list', return_value=None), \
284                 mock.patch('functest.utils.openstack_clean.os_utils'
285                            '.get_port_list', return_value=self.ports), \
286                 mock.patch('functest.utils.openstack_clean.os_utils'
287                            '.get_router_list', return_value=self.routers):
288             openstack_clean.remove_networks(self.client, self.update_list,
289                                             self.update_list)
290             mock_logger_debug.assert_any_call("Removing Neutron objects")
291             mock_logger_debug.assert_any_call("There are no networks in the"
292                                               " deployment. ")
293             mock_remove_ports.assert_called_once_with(self.client, self.ports,
294                                                       [])
295             mock_remove_routers.assert_called_once_with(self.client,
296                                                         self.routers,
297                                                         self.update_list)
298
299     @mock.patch('functest.utils.openstack_clean.remove_routers')
300     @mock.patch('functest.utils.openstack_clean.remove_ports')
301     @mock.patch('functest.utils.openstack_clean.logger.debug')
302     def test_remove_networks_delete_success(self, mock_logger_debug,
303                                             mock_remove_ports,
304                                             mock_remove_routers):
305
306         with mock.patch('functest.utils.openstack_clean.os_utils'
307                         '.get_network_list',
308                         return_value=self.test_dict_list), \
309                 mock.patch('functest.utils.openstack_clean.os_utils'
310                            '.delete_neutron_net', return_value=True), \
311                 mock.patch('functest.utils.openstack_clean.os_utils'
312                            '.get_port_list', return_value=self.ports), \
313                 mock.patch('functest.utils.openstack_clean.os_utils'
314                            '.get_router_list', return_value=self.routers):
315             openstack_clean.remove_networks(self.client, self.remove_list,
316                                             self.remove_list)
317             mock_logger_debug.assert_any_call("Removing Neutron objects")
318             mock_logger_debug.assert_any_call("   > this network will be "
319                                               "deleted.")
320             mock_logger_debug.assert_any_call("  > Done!")
321             mock_logger_debug.assert_any_call(test_utils.
322                                               RegexMatch("Removing network "
323                                                          "\s*\S+ ..."))
324             mock_remove_ports.assert_called_once_with(self.client, self.ports,
325                                                       ['id1', 'id2'])
326             mock_remove_routers.assert_called_once_with(self.client,
327                                                         self.routers,
328                                                         self.remove_list)
329
330     @mock.patch('functest.utils.openstack_clean.remove_routers')
331     @mock.patch('functest.utils.openstack_clean.remove_ports')
332     @mock.patch('functest.utils.openstack_clean.logger.error')
333     @mock.patch('functest.utils.openstack_clean.logger.debug')
334     def test_remove_networks_delete_failed(self, mock_logger_debug,
335                                            mock_logger_error,
336                                            mock_remove_ports,
337                                            mock_remove_routers):
338         with mock.patch('functest.utils.openstack_clean.os_utils'
339                         '.get_network_list',
340                         return_value=self.test_dict_list), \
341                 mock.patch('functest.utils.openstack_clean.os_utils'
342                            '.delete_neutron_net', return_value=False), \
343                 mock.patch('functest.utils.openstack_clean.os_utils'
344                            '.get_port_list', return_value=self.ports), \
345                 mock.patch('functest.utils.openstack_clean.os_utils'
346                            '.get_router_list', return_value=self.routers):
347             openstack_clean.remove_networks(self.client, self.remove_list,
348                                             self.remove_list)
349             mock_logger_debug.assert_any_call("Removing Neutron objects")
350             mock_logger_error.assert_any_call(test_utils.
351                                               RegexMatch("There has been a"
352                                                          " problem removing"
353                                                          " the network \s*\S+"
354                                                          "..."))
355             mock_logger_debug.assert_any_call(test_utils.
356                                               RegexMatch("Removing network "
357                                                          "\s*\S+ ..."))
358             mock_remove_ports.assert_called_once_with(self.client, self.ports,
359                                                       ['id1', 'id2'])
360             mock_remove_routers.assert_called_once_with(self.client,
361                                                         self.routers,
362                                                         self.remove_list)
363
364     # TODO: ports
365     @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
366     @mock.patch('functest.utils.openstack_clean.logger.error')
367     @mock.patch('functest.utils.openstack_clean.logger.debug')
368     def test_force_remove_port(self, mock_logger_debug,
369                                mock_logger_error,
370                                mock_update_neutron_port):
371         with mock.patch('functest.utils.openstack_clean.os_utils'
372                         '.delete_neutron_port',
373                         return_value=True):
374             openstack_clean.force_remove_port(self.client, 'id')
375             mock_logger_debug.assert_any_call("  > Done!")
376             mock_logger_debug.assert_any_call(test_utils.
377                                               RegexMatch("Clearing device_"
378                                                          "owner for port "
379                                                          "\s*\S+ ..."))
380
381     @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
382     @mock.patch('functest.utils.openstack_clean.logger.error')
383     @mock.patch('functest.utils.openstack_clean.logger.debug')
384     def test_force_remove_port_failed(self, mock_logger_debug,
385                                       mock_logger_error,
386                                       mock_update_neutron_port):
387         with mock.patch('functest.utils.openstack_clean.os_utils'
388                         '.delete_neutron_port',
389                         return_value=False):
390             openstack_clean.force_remove_port(self.client, 'id')
391             mock_logger_error.assert_any_call("There has been a "
392                                               "problem removing "
393                                               "the port id...")
394             mock_logger_debug.assert_any_call(test_utils.
395                                               RegexMatch("Clearing device_"
396                                                          "owner for port "
397                                                          "\s*\S+ ..."))
398
399     @mock.patch('functest.utils.openstack_clean.logger.debug')
400     def test_remove_routers_missing_routers(self, mock_logger_debug):
401         with mock.patch('functest.utils.openstack_clean.os_utils'
402                         '.delete_neutron_router',
403                         return_value=True):
404             openstack_clean.remove_routers(self.client, self.test_dict_list,
405                                            self.remove_list)
406             mock_logger_debug.assert_any_call("Router is not connected"
407                                               " to anything."
408                                               "Ready to remove...")
409             mock_logger_debug.assert_any_call("  > Done!")
410             mock_logger_debug.assert_any_call(test_utils.
411                                               RegexMatch("Removing router "
412                                                          "\s*\S+(\s*\S+) ..."))
413
414     @mock.patch('functest.utils.openstack_clean.logger.error')
415     @mock.patch('functest.utils.openstack_clean.logger.debug')
416     def test_remove_routers_failed(self, mock_logger_debug,
417                                    mock_logger_error):
418         with mock.patch('functest.utils.openstack_clean.os_utils'
419                         '.delete_neutron_router',
420                         return_value=False):
421             openstack_clean.remove_routers(self.client, self.test_dict_list,
422                                            self.remove_list)
423             mock_logger_debug.assert_any_call("Router is not connected"
424                                               " to anything."
425                                               "Ready to remove...")
426             mock_logger_debug.assert_any_call(test_utils.
427                                               RegexMatch("Removing router "
428                                                          "\s*\S+(\s*\S+) ..."))
429             mock_logger_error.assert_any_call(test_utils.
430                                               RegexMatch("There has been "
431                                                          "a problem"
432                                                          " removing the "
433                                                          "router \s*\S+("
434                                                          "\s*\S+)..."))
435
436     @mock.patch('functest.utils.openstack_clean.logger.error')
437     @mock.patch('functest.utils.openstack_clean.logger.debug')
438     def test_remove_missing_external_gateway(self, mock_logger_debug,
439                                              mock_logger_error):
440         with mock.patch('functest.utils.openstack_clean.os_utils'
441                         '.delete_neutron_router',
442                         return_value=False), \
443                 mock.patch('functest.utils.openstack_clean.os_utils'
444                            '.remove_gateway_router',
445                            return_value=False):
446             self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
447             openstack_clean.remove_routers(self.client, self.test_dict_list,
448                                            self.remove_list)
449             mock_logger_debug.assert_any_call("Router has gateway to external"
450                                               " network.Removing link...")
451             mock_logger_error.assert_any_call("There has been a problem "
452                                               "removing the gateway...")
453             mock_logger_debug.assert_any_call(test_utils.
454                                               RegexMatch("Removing router "
455                                                          "\s*\S+(\s*\S+) ..."))
456             mock_logger_error.assert_any_call(test_utils.
457                                               RegexMatch("There has been "
458                                                          "a problem"
459                                                          " removing the "
460                                                          "router \s*\S+("
461                                                          "\s*\S+)..."))
462
463     @mock.patch('functest.utils.openstack_clean.logger.debug')
464     def remove_security_groups(self, mock_logger_debug):
465         with mock.patch('functest.utils.openstack_clean.os_utils'
466                         '.get_security_groups',
467                         return_value=self.test_dict_list):
468             openstack_clean.remove_security_groups(self.client,
469                                                    self.update_list)
470             mock_logger_debug.assert_any_call("Removing Security groups...")
471             mock_logger_debug.assert_any_call("   > this is a default "
472                                               "security group and will NOT "
473                                               "be deleted.")
474
475     @mock.patch('functest.utils.openstack_clean.logger.debug')
476     def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
477         with mock.patch('functest.utils.openstack_clean.os_utils'
478                         '.get_security_groups', return_value=[]):
479             openstack_clean.remove_security_groups(self.client,
480                                                    self.update_list)
481             mock_logger_debug.assert_any_call("Removing Security groups...")
482             mock_logger_debug.assert_any_call("No security groups found.")
483
484     @mock.patch('functest.utils.openstack_clean.logger.debug')
485     def test_remove_security_groups_delete_success(self, mock_logger_debug):
486         with mock.patch('functest.utils.openstack_clean.os_utils'
487                         '.get_security_groups',
488                         return_value=self.test_dict_list), \
489                 mock.patch('functest.utils.openstack_clean.os_utils'
490                            '.delete_security_group', return_value=True):
491             openstack_clean.remove_security_groups(self.client,
492                                                    self.remove_list)
493             mock_logger_debug.assert_any_call("Removing Security groups...")
494             mock_logger_debug.assert_any_call("  > Done!")
495             mock_logger_debug.assert_any_call(test_utils.
496                                               RegexMatch(" Removing \s*\S+"
497                                                          "..."))
498
499     @mock.patch('functest.utils.openstack_clean.logger.error')
500     @mock.patch('functest.utils.openstack_clean.logger.debug')
501     def test_remove_security_groups_delete_failed(self, mock_logger_debug,
502                                                   mock_logger_error):
503         with mock.patch('functest.utils.openstack_clean.os_utils'
504                         '.get_security_groups',
505                         return_value=self.test_dict_list), \
506                 mock.patch('functest.utils.openstack_clean.os_utils'
507                            '.delete_security_group', return_value=False):
508             openstack_clean.remove_security_groups(self.client,
509                                                    self.remove_list)
510             mock_logger_debug.assert_any_call("Removing Security groups...")
511             mock_logger_error.assert_any_call(test_utils.
512                                               RegexMatch("There has been a "
513                                                          "problem removing "
514                                                          "the security group"
515                                                          " \s*\S+..."))
516             mock_logger_debug.assert_any_call(test_utils.
517                                               RegexMatch(" Removing \s*\S+"
518                                                          "..."))
519
520     @mock.patch('functest.utils.openstack_clean.logger.debug')
521     def test_remove_users(self, mock_logger_debug):
522         with mock.patch('functest.utils.openstack_clean.os_utils'
523                         '.get_users', return_value=self.test_list):
524             openstack_clean.remove_users(self.client, self.update_list)
525             mock_logger_debug.assert_any_call("Removing Users...")
526             mock_logger_debug.assert_any_call("   > this is a default "
527                                               "user and will "
528                                               "NOT be deleted.")
529
530     @mock.patch('functest.utils.openstack_clean.logger.debug')
531     def test_remove_users_missing_users(self, mock_logger_debug):
532         with mock.patch('functest.utils.openstack_clean.os_utils'
533                         '.get_users', return_value=None):
534             openstack_clean.remove_users(self.client, self.update_list)
535             mock_logger_debug.assert_any_call("Removing Users...")
536             mock_logger_debug.assert_any_call("There are no users in"
537                                               " the deployment. ")
538
539     @mock.patch('functest.utils.openstack_clean.logger.debug')
540     def test_remove_users_delete_success(self, mock_logger_debug):
541         with mock.patch('functest.utils.openstack_clean.os_utils'
542                         '.get_users', return_value=self.test_list), \
543                 mock.patch('functest.utils.openstack_clean.os_utils'
544                            '.delete_user', return_value=True):
545             openstack_clean.remove_users(self.client, self.remove_list)
546             mock_logger_debug.assert_any_call("Removing Users...")
547             mock_logger_debug.assert_any_call("  > Done!")
548             mock_logger_debug.assert_any_call(test_utils.
549                                               RegexMatch(" Removing "
550                                                          "\s*\S+..."))
551
552     @mock.patch('functest.utils.openstack_clean.logger.error')
553     @mock.patch('functest.utils.openstack_clean.logger.debug')
554     def test_remove_users_delete_failed(self, mock_logger_debug,
555                                         mock_logger_error):
556         with mock.patch('functest.utils.openstack_clean.os_utils'
557                         '.get_users', return_value=self.test_list), \
558                 mock.patch('functest.utils.openstack_clean.os_utils'
559                            '.delete_user', return_value=False):
560             openstack_clean.remove_users(self.client, self.remove_list)
561             mock_logger_debug.assert_any_call("Removing Users...")
562             mock_logger_error.assert_any_call(test_utils.
563                                               RegexMatch("There has been a "
564                                                          "problem removing "
565                                                          "the user \s*\S+"
566                                                          "..."))
567             mock_logger_debug.assert_any_call(test_utils.
568                                               RegexMatch(" Removing "
569                                                          "\s*\S+..."))
570
571     @mock.patch('functest.utils.openstack_clean.logger.debug')
572     def test_remove_tenants(self, mock_logger_debug):
573         with mock.patch('functest.utils.openstack_clean.os_utils'
574                         '.get_tenants', return_value=self.test_list):
575             openstack_clean.remove_tenants(self.client, self.update_list)
576             mock_logger_debug.assert_any_call("Removing Tenants...")
577             mock_logger_debug.assert_any_call("   > this is a default"
578                                               " tenant and will "
579                                               "NOT be deleted.")
580
581     @mock.patch('functest.utils.openstack_clean.logger.debug')
582     def test_remove_tenants_missing_tenants(self, mock_logger_debug):
583         with mock.patch('functest.utils.openstack_clean.os_utils'
584                         '.get_tenants', return_value=None):
585             openstack_clean.remove_tenants(self.client, self.update_list)
586             mock_logger_debug.assert_any_call("Removing Tenants...")
587             mock_logger_debug.assert_any_call("There are no tenants in"
588                                               " the deployment. ")
589
590     @mock.patch('functest.utils.openstack_clean.logger.debug')
591     def test_remove_tenants_delete_success(self, mock_logger_debug):
592         with mock.patch('functest.utils.openstack_clean.os_utils'
593                         '.get_tenants', return_value=self.test_list), \
594                 mock.patch('functest.utils.openstack_clean.os_utils'
595                            '.delete_tenant', return_value=True):
596             openstack_clean.remove_tenants(self.client, self.remove_list)
597             mock_logger_debug.assert_any_call("Removing Tenants...")
598             mock_logger_debug.assert_any_call("  > Done!")
599             mock_logger_debug.assert_any_call(test_utils.
600                                               RegexMatch(" Removing "
601                                                          "\s*\S+..."))
602
603     @mock.patch('functest.utils.openstack_clean.logger.error')
604     @mock.patch('functest.utils.openstack_clean.logger.debug')
605     def test_remove_tenants_delete_failed(self, mock_logger_debug,
606                                           mock_logger_error):
607         with mock.patch('functest.utils.openstack_clean.os_utils'
608                         '.get_tenants', return_value=self.test_list), \
609                 mock.patch('functest.utils.openstack_clean.os_utils'
610                            '.delete_tenant', return_value=False):
611             openstack_clean.remove_tenants(self.client, self.remove_list)
612             mock_logger_debug.assert_any_call("Removing Tenants...")
613             mock_logger_error.assert_any_call(test_utils.
614                                               RegexMatch("There has been a "
615                                                          "problem removing "
616                                                          "the tenant \s*\S+"
617                                                          "..."))
618             mock_logger_debug.assert_any_call(test_utils.
619                                               RegexMatch(" Removing "
620                                                          "\s*\S+..."))
621
622     @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
623     @mock.patch('functest.utils.openstack_clean.os_utils'
624                 '.get_keystone_client')
625     @mock.patch('functest.utils.openstack_clean.os_utils'
626                 '.get_neutron_client')
627     @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
628     @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
629                 return_value=True)
630     @mock.patch('functest.utils.openstack_clean.logger.info')
631     @mock.patch('functest.utils.openstack_clean.logger.debug')
632     def test_main_default(self, mock_logger_debug, mock_logger_info,
633                           mock_creds, mock_nova, mock_neutron,
634                           mock_keystone, mock_cinder):
635
636         with mock.patch('functest.utils.openstack_clean.remove_instances') \
637             as mock_remove_instances, \
638             mock.patch('functest.utils.openstack_clean.remove_images') \
639             as mock_remove_images, \
640             mock.patch('functest.utils.openstack_clean.remove_volumes') \
641             as mock_remove_volumes, \
642             mock.patch('functest.utils.openstack_clean.remove_floatingips') \
643             as mock_remove_floatingips, \
644             mock.patch('functest.utils.openstack_clean.remove_networks') \
645             as mock_remove_networks, \
646             mock.patch('functest.utils.openstack_clean.'
647                        'remove_security_groups') \
648             as mock_remove_security_groups, \
649             mock.patch('functest.utils.openstack_clean.remove_users') \
650             as mock_remove_users, \
651             mock.patch('functest.utils.openstack_clean.remove_tenants') \
652             as mock_remove_tenants, \
653             mock.patch('functest.utils.openstack_clean.yaml.safe_load',
654                        return_value=mock.Mock()), \
655                 mock.patch('__builtin__.open', mock.mock_open()) as m:
656             openstack_clean.main()
657             self.assertTrue(mock_remove_instances)
658             self.assertTrue(mock_remove_images)
659             self.assertTrue(mock_remove_volumes)
660             self.assertTrue(mock_remove_floatingips)
661             self.assertTrue(mock_remove_networks)
662             self.assertTrue(mock_remove_security_groups)
663             self.assertTrue(mock_remove_users)
664             self.assertTrue(mock_remove_tenants)
665             m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
666             mock_logger_info.assert_called_once_with("Cleaning OpenStack "
667                                                      "resources...")
668
669
670 if __name__ == "__main__":
671     unittest.main(verbosity=2)