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