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