Get auth token when checking deployment
[functest-xtesting.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     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)
24         return mock_obj
25
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)
32         return mock_obj
33
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)
40         return mock_obj
41
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)
48         return mock_obj
49
50     def setUp(self):
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()]
68
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("-----------------"
73                                                   "-----------------"
74                                                   "---------")
75
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 "
84                                               "instance and will "
85                                               "NOT be deleted.")
86
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,
90                                                 *args):
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.")
96
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"
108                                                                     " instance"
109                                                                     " '\s*\S+'"
110                                                                     " ..."))
111
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,
115                                                      *args):
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"
123                                                     " instance"
124                                                     " '\s*\S+'"
125                                                     " ...").assert_not_called()
126
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,
130                                                    *args):
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"
139                                                                     " instance"
140                                                                     " '\s*\S+'"
141                                                                     " ..."))
142
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 "
156                                                          "problem removing "
157                                                          "the instance \s*\S+"
158                                                          "..."))
159             mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing"
160                                                                     " instance"
161                                                                     " '\s*\S+'"
162                                                                     " ..."))
163
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 "
171                                               "image and will "
172                                               "NOT be deleted.")
173
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.")
181
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 "
193                                                          "\s*\S+,"
194                                                          " ID=\s*\S+ ..."))
195
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,
199                                          mock_logger_error):
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"
209                                                          "image \s*\S+..."))
210             mock_logger_debug.assert_any_call(test_utils.
211                                               RegexMatch("Removing image "
212                                                          "\s*\S+,"
213                                                          " ID=\s*\S+ ..."))
214
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 "
222                                               "volume and will "
223                                               "NOT be deleted.")
224
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.")
232
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+ ..."))
245
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,
249                                           mock_logger_error):
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 "
258                                                          "problem removing "
259                                                          "the "
260                                                          "volume \s*\S+..."))
261             mock_logger_debug.assert_any_call(test_utils.
262                                               RegexMatch("Removing cinder "
263                                                          "volume \s*\S+ ..."))
264
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'
268                         '.get_floating_ips',
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 "
274                                               "NOT be deleted.")
275
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.")
283
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'
287                         '.get_floating_ips',
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 "
296                                                          "IP \s*\S+ ..."))
297
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,
301                                               mock_logger_error):
302         with mock.patch('functest.utils.openstack_clean.os_utils'
303                         '.get_floating_ips',
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 "
311                                                          "problem removing "
312                                                          "the floating IP "
313                                                          "\s*\S+..."))
314             mock_logger_debug.assert_any_call(test_utils.
315                                               RegexMatch("Removing floating "
316                                                          "IP \s*\S+ ..."))
317
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,
323                              mock_remove_ports,
324                              mock_remove_routers, *args):
325         with mock.patch('functest.utils.openstack_clean.os_utils'
326                         '.get_network_list',
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,
333                                             self.update_list)
334             mock_logger_debug.assert_any_call("Removing Neutron objects")
335             mock_logger_debug.assert_any_call("   > this is a default "
336                                               "network and will "
337                                               "NOT be deleted.")
338             mock_remove_ports.assert_called_once_with(self.client, self.ports,
339                                                       [])
340             mock_remove_routers.assert_called_once_with(self.client,
341                                                         self.routers,
342                                                         self.update_list)
343
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,
349                                               mock_remove_ports,
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,
358                                             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"
361                                               " deployment. ")
362             mock_remove_ports.assert_called_once_with(self.client, self.ports,
363                                                       [])
364             mock_remove_routers.assert_called_once_with(self.client,
365                                                         self.routers,
366                                                         self.update_list)
367
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,
373                                             mock_remove_ports,
374                                             mock_remove_routers, *args):
375
376         with mock.patch('functest.utils.openstack_clean.os_utils'
377                         '.get_network_list',
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,
386                                             self.remove_list)
387             mock_logger_debug.assert_any_call("Removing Neutron objects")
388             mock_logger_debug.assert_any_call("   > this network will be "
389                                               "deleted.")
390             mock_logger_debug.assert_any_call("  > Done!")
391             mock_logger_debug.assert_any_call(test_utils.
392                                               RegexMatch("Removing network "
393                                                          "\s*\S+ ..."))
394             mock_remove_ports.assert_called_once_with(self.client, self.ports,
395                                                       ['id1', 'id2'])
396             mock_remove_routers.assert_called_once_with(self.client,
397                                                         self.routers,
398                                                         self.remove_list)
399
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,
406                                            mock_logger_error,
407                                            mock_remove_ports,
408                                            mock_remove_routers, *args):
409         with mock.patch('functest.utils.openstack_clean.os_utils'
410                         '.get_network_list',
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,
419                                             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"
423                                                          " problem removing"
424                                                          " the network \s*\S+"
425                                                          "..."))
426             mock_logger_debug.assert_any_call(test_utils.
427                                               RegexMatch("Removing network "
428                                                          "\s*\S+ ..."))
429             mock_remove_ports.assert_called_once_with(self.client, self.ports,
430                                                       ['id1', 'id2'])
431             mock_remove_routers.assert_called_once_with(self.client,
432                                                         self.routers,
433                                                         self.remove_list)
434
435     # TODO: ports
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,
440                                mock_logger_error,
441                                mock_update_neutron_port):
442         with mock.patch('functest.utils.openstack_clean.os_utils'
443                         '.delete_neutron_port',
444                         return_value=True):
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_"
449                                                          "owner for port "
450                                                          "\s*\S+ ..."))
451
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,
456                                       mock_logger_error,
457                                       mock_update_neutron_port):
458         with mock.patch('functest.utils.openstack_clean.os_utils'
459                         '.delete_neutron_port',
460                         return_value=False):
461             openstack_clean.force_remove_port(self.client, 'id')
462             mock_logger_error.assert_any_call("There has been a "
463                                               "problem removing "
464                                               "the port id...")
465             mock_logger_debug.assert_any_call(test_utils.
466                                               RegexMatch("Clearing device_"
467                                                          "owner for port "
468                                                          "\s*\S+ ..."))
469
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',
474                         return_value=True):
475             openstack_clean.remove_routers(self.client, self.test_dict_list,
476                                            self.remove_list)
477             mock_logger_debug.assert_any_call("Router is not connected"
478                                               " to anything."
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+) ..."))
484
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,
488                                    mock_logger_error):
489         with mock.patch('functest.utils.openstack_clean.os_utils'
490                         '.delete_neutron_router',
491                         return_value=False):
492             openstack_clean.remove_routers(self.client, self.test_dict_list,
493                                            self.remove_list)
494             mock_logger_debug.assert_any_call("Router is not connected"
495                                               " to anything."
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 "
502                                                          "a problem"
503                                                          " removing the "
504                                                          "router \s*\S+("
505                                                          "\s*\S+)..."))
506
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,
510                                              mock_logger_error):
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',
516                            return_value=False):
517             self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
518             openstack_clean.remove_routers(self.client, self.test_dict_list,
519                                            self.remove_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 "
529                                                          "a problem"
530                                                          " removing the "
531                                                          "router \s*\S+("
532                                                          "\s*\S+)..."))
533
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,
540                                                    self.update_list)
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 "
544                                               "be deleted.")
545
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,
551                                                    self.update_list)
552             mock_logger_debug.assert_any_call("Removing Security groups...")
553             mock_logger_debug.assert_any_call("No security groups found.")
554
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,
563                                                    self.remove_list)
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+"
568                                                          "..."))
569
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,
573                                                   mock_logger_error):
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,
580                                                    self.remove_list)
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 "
584                                                          "problem removing "
585                                                          "the security group"
586                                                          " \s*\S+..."))
587             mock_logger_debug.assert_any_call(test_utils.
588                                               RegexMatch(" Removing \s*\S+"
589                                                          "..."))
590
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 "
598                                               "user and will "
599                                               "NOT be deleted.")
600
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"
608                                               " the deployment. ")
609
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 "
621                                                          "\s*\S+..."))
622
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,
626                                         mock_logger_error):
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 "
635                                                          "problem removing "
636                                                          "the user \s*\S+"
637                                                          "..."))
638             mock_logger_debug.assert_any_call(test_utils.
639                                               RegexMatch(" Removing "
640                                                          "\s*\S+..."))
641
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"
649                                               " tenant and will "
650                                               "NOT be deleted.")
651
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"
659                                               " the deployment. ")
660
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 "
672                                                          "\s*\S+..."))
673
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,
677                                           mock_logger_error):
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 "
686                                                          "problem removing "
687                                                          "the tenant \s*\S+"
688                                                          "..."))
689             mock_logger_debug.assert_any_call(test_utils.
690                                               RegexMatch(" Removing "
691                                                          "\s*\S+..."))
692
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',
701                 return_value=True)
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):
707
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 "
739                                                      "resources...")
740
741
742 if __name__ == "__main__":
743     logging.disable(logging.CRITICAL)
744     unittest.main(verbosity=2)