Merge "Support running on openstack which enabled https"
[functest.git] / functest / tests / unit / utils / test_openstack_clean.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 import logging
9 import mock
10 import unittest
11
12 from functest.utils import openstack_clean
13 from functest.tests.unit import test_utils
14
15
16 class OSCleanTesting(unittest.TestCase):
17
18     logging.disable(logging.CRITICAL)
19
20     def _get_instance(self, key):
21         mock_obj = mock.Mock()
22         attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
23                  'ip': 'ip' + str(key), 'status': 'ACTIVE',
24                  'OS-EXT-STS:task_state': '-'}
25         mock_obj.configure_mock(**attrs)
26         return mock_obj
27
28     def _get_instance_deleted(self, key):
29         mock_obj = mock.Mock()
30         attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
31                  'ip': 'ip' + str(key), 'status': 'DELETED',
32                  'OS-EXT-STS:task_state': '-'}
33         mock_obj.configure_mock(**attrs)
34         return mock_obj
35
36     def _get_instance_deleting(self, key):
37         mock_obj = mock.Mock()
38         attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
39                  'ip': 'ip' + str(key), 'status': 'BUILD',
40                  'OS-EXT-STS:task_state': 'deleting'}
41         mock_obj.configure_mock(**attrs)
42         return mock_obj
43
44     def _get_instance_other(self, key):
45         mock_obj = mock.Mock()
46         attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
47                  'ip': 'ip' + str(key), 'status': 'BUILD',
48                  'OS-EXT-STS:task_state': 'networking'}
49         mock_obj.configure_mock(**attrs)
50         return mock_obj
51
52     def setUp(self):
53         self.client = mock.Mock()
54         self.test_list = [self._get_instance(1), self._get_instance(2)]
55         self.deleted_list = [self._get_instance_deleted(5),
56                              self._get_instance_deleting(6)]
57         self.other_list = [self._get_instance_other(7)]
58         self.update_list = {'id1': 'name1', 'id2': 'name2'}
59         self.remove_list = {'id3': 'name3', 'id4': 'name4'}
60         self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1',
61                                 'router:external': False,
62                                 'external_gateway_info': None},
63                                {'id': 'id2', 'name': 'name2', 'ip': 'ip2',
64                                 'router:external': False,
65                                 'external_gateway_info': None}]
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', return_value=self.test_list):
260             openstack_clean.remove_floatingips(self.client, self.update_list)
261             mock_logger_debug.assert_any_call("Removing floating IPs...")
262             mock_logger_debug.assert_any_call("   > this is a default "
263                                               "floating IP and will "
264                                               "NOT be deleted.")
265
266     @mock.patch('functest.utils.openstack_clean.logger.debug')
267     def test_remove_floatingips_missing_floatingips(self, mock_logger_debug):
268         with mock.patch('functest.utils.openstack_clean.os_utils'
269                         '.get_floating_ips', return_value=[]):
270             openstack_clean.remove_floatingips(self.client, self.update_list)
271             mock_logger_debug.assert_any_call("Removing floating IPs...")
272             mock_logger_debug.assert_any_call("No floating IPs found.")
273
274     @mock.patch('functest.utils.openstack_clean.logger.debug')
275     def test_remove_floatingips_delete_success(self, mock_logger_debug):
276         with mock.patch('functest.utils.openstack_clean.os_utils'
277                         '.get_floating_ips', return_value=self.test_list), \
278                 mock.patch('functest.utils.openstack_clean.os_utils'
279                            '.delete_volume', return_value=True):
280             openstack_clean.remove_floatingips(self.client, self.remove_list)
281             mock_logger_debug.assert_any_call("Removing floating IPs...")
282             mock_logger_debug.assert_any_call("  > Done!")
283             mock_logger_debug.assert_any_call(test_utils.
284                                               RegexMatch("Removing floating "
285                                                          "IP \s*\S+ ..."))
286
287     @mock.patch('functest.utils.openstack_clean.logger.error')
288     @mock.patch('functest.utils.openstack_clean.logger.debug')
289     def test_remove_floatingips_delete_failed(self, mock_logger_debug,
290                                               mock_logger_error):
291         with mock.patch('functest.utils.openstack_clean.os_utils'
292                         '.get_floating_ips', return_value=self.test_list), \
293                 mock.patch('functest.utils.openstack_clean.os_utils'
294                            '.delete_floating_ip', return_value=False):
295             openstack_clean.remove_floatingips(self.client, self.remove_list)
296             mock_logger_debug.assert_any_call("Removing floating IPs...")
297             mock_logger_error.assert_any_call(test_utils.
298                                               RegexMatch("There has been a "
299                                                          "problem removing "
300                                                          "the floating IP "
301                                                          "\s*\S+..."))
302             mock_logger_debug.assert_any_call(test_utils.
303                                               RegexMatch("Removing floating "
304                                                          "IP \s*\S+ ..."))
305
306     @mock.patch('functest.utils.openstack_clean.remove_routers')
307     @mock.patch('functest.utils.openstack_clean.remove_ports')
308     @mock.patch('functest.utils.openstack_clean.logger.debug')
309     def test_remove_networks(self, mock_logger_debug,
310                              mock_remove_ports,
311                              mock_remove_routers):
312         with mock.patch('functest.utils.openstack_clean.os_utils'
313                         '.get_network_list',
314                         return_value=self.test_dict_list), \
315                 mock.patch('functest.utils.openstack_clean.os_utils'
316                            '.get_port_list', return_value=self.ports), \
317                 mock.patch('functest.utils.openstack_clean.os_utils'
318                            '.get_router_list', return_value=self.routers):
319             openstack_clean.remove_networks(self.client, self.update_list,
320                                             self.update_list)
321             mock_logger_debug.assert_any_call("Removing Neutron objects")
322             mock_logger_debug.assert_any_call("   > this is a default "
323                                               "network and will "
324                                               "NOT be deleted.")
325             mock_remove_ports.assert_called_once_with(self.client, self.ports,
326                                                       [])
327             mock_remove_routers.assert_called_once_with(self.client,
328                                                         self.routers,
329                                                         self.update_list)
330
331     @mock.patch('functest.utils.openstack_clean.remove_routers')
332     @mock.patch('functest.utils.openstack_clean.remove_ports')
333     @mock.patch('functest.utils.openstack_clean.logger.debug')
334     def test_remove_networks_missing_networks(self, mock_logger_debug,
335                                               mock_remove_ports,
336                                               mock_remove_routers):
337         with mock.patch('functest.utils.openstack_clean.os_utils'
338                         '.get_network_list', return_value=None), \
339                 mock.patch('functest.utils.openstack_clean.os_utils'
340                            '.get_port_list', return_value=self.ports), \
341                 mock.patch('functest.utils.openstack_clean.os_utils'
342                            '.get_router_list', return_value=self.routers):
343             openstack_clean.remove_networks(self.client, self.update_list,
344                                             self.update_list)
345             mock_logger_debug.assert_any_call("Removing Neutron objects")
346             mock_logger_debug.assert_any_call("There are no networks in the"
347                                               " deployment. ")
348             mock_remove_ports.assert_called_once_with(self.client, self.ports,
349                                                       [])
350             mock_remove_routers.assert_called_once_with(self.client,
351                                                         self.routers,
352                                                         self.update_list)
353
354     @mock.patch('functest.utils.openstack_clean.remove_routers')
355     @mock.patch('functest.utils.openstack_clean.remove_ports')
356     @mock.patch('functest.utils.openstack_clean.logger.debug')
357     def test_remove_networks_delete_success(self, mock_logger_debug,
358                                             mock_remove_ports,
359                                             mock_remove_routers):
360
361         with mock.patch('functest.utils.openstack_clean.os_utils'
362                         '.get_network_list',
363                         return_value=self.test_dict_list), \
364                 mock.patch('functest.utils.openstack_clean.os_utils'
365                            '.delete_neutron_net', return_value=True), \
366                 mock.patch('functest.utils.openstack_clean.os_utils'
367                            '.get_port_list', return_value=self.ports), \
368                 mock.patch('functest.utils.openstack_clean.os_utils'
369                            '.get_router_list', return_value=self.routers):
370             openstack_clean.remove_networks(self.client, self.remove_list,
371                                             self.remove_list)
372             mock_logger_debug.assert_any_call("Removing Neutron objects")
373             mock_logger_debug.assert_any_call("   > this network will be "
374                                               "deleted.")
375             mock_logger_debug.assert_any_call("  > Done!")
376             mock_logger_debug.assert_any_call(test_utils.
377                                               RegexMatch("Removing network "
378                                                          "\s*\S+ ..."))
379             mock_remove_ports.assert_called_once_with(self.client, self.ports,
380                                                       ['id1', 'id2'])
381             mock_remove_routers.assert_called_once_with(self.client,
382                                                         self.routers,
383                                                         self.remove_list)
384
385     @mock.patch('functest.utils.openstack_clean.remove_routers')
386     @mock.patch('functest.utils.openstack_clean.remove_ports')
387     @mock.patch('functest.utils.openstack_clean.logger.error')
388     @mock.patch('functest.utils.openstack_clean.logger.debug')
389     def test_remove_networks_delete_failed(self, mock_logger_debug,
390                                            mock_logger_error,
391                                            mock_remove_ports,
392                                            mock_remove_routers):
393         with mock.patch('functest.utils.openstack_clean.os_utils'
394                         '.get_network_list',
395                         return_value=self.test_dict_list), \
396                 mock.patch('functest.utils.openstack_clean.os_utils'
397                            '.delete_neutron_net', return_value=False), \
398                 mock.patch('functest.utils.openstack_clean.os_utils'
399                            '.get_port_list', return_value=self.ports), \
400                 mock.patch('functest.utils.openstack_clean.os_utils'
401                            '.get_router_list', return_value=self.routers):
402             openstack_clean.remove_networks(self.client, self.remove_list,
403                                             self.remove_list)
404             mock_logger_debug.assert_any_call("Removing Neutron objects")
405             mock_logger_error.assert_any_call(test_utils.
406                                               RegexMatch("There has been a"
407                                                          " problem removing"
408                                                          " the network \s*\S+"
409                                                          "..."))
410             mock_logger_debug.assert_any_call(test_utils.
411                                               RegexMatch("Removing network "
412                                                          "\s*\S+ ..."))
413             mock_remove_ports.assert_called_once_with(self.client, self.ports,
414                                                       ['id1', 'id2'])
415             mock_remove_routers.assert_called_once_with(self.client,
416                                                         self.routers,
417                                                         self.remove_list)
418
419     # TODO: ports
420     @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
421     @mock.patch('functest.utils.openstack_clean.logger.error')
422     @mock.patch('functest.utils.openstack_clean.logger.debug')
423     def test_force_remove_port(self, mock_logger_debug,
424                                mock_logger_error,
425                                mock_update_neutron_port):
426         with mock.patch('functest.utils.openstack_clean.os_utils'
427                         '.delete_neutron_port',
428                         return_value=True):
429             openstack_clean.force_remove_port(self.client, 'id')
430             mock_logger_debug.assert_any_call("  > Done!")
431             mock_logger_debug.assert_any_call(test_utils.
432                                               RegexMatch("Clearing device_"
433                                                          "owner for port "
434                                                          "\s*\S+ ..."))
435
436     @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port')
437     @mock.patch('functest.utils.openstack_clean.logger.error')
438     @mock.patch('functest.utils.openstack_clean.logger.debug')
439     def test_force_remove_port_failed(self, mock_logger_debug,
440                                       mock_logger_error,
441                                       mock_update_neutron_port):
442         with mock.patch('functest.utils.openstack_clean.os_utils'
443                         '.delete_neutron_port',
444                         return_value=False):
445             openstack_clean.force_remove_port(self.client, 'id')
446             mock_logger_error.assert_any_call("There has been a "
447                                               "problem removing "
448                                               "the port id...")
449             mock_logger_debug.assert_any_call(test_utils.
450                                               RegexMatch("Clearing device_"
451                                                          "owner for port "
452                                                          "\s*\S+ ..."))
453
454     @mock.patch('functest.utils.openstack_clean.logger.debug')
455     def test_remove_routers_missing_routers(self, mock_logger_debug):
456         with mock.patch('functest.utils.openstack_clean.os_utils'
457                         '.delete_neutron_router',
458                         return_value=True):
459             openstack_clean.remove_routers(self.client, self.test_dict_list,
460                                            self.remove_list)
461             mock_logger_debug.assert_any_call("Router is not connected"
462                                               " to anything."
463                                               "Ready to remove...")
464             mock_logger_debug.assert_any_call("  > Done!")
465             mock_logger_debug.assert_any_call(test_utils.
466                                               RegexMatch("Removing router "
467                                                          "\s*\S+(\s*\S+) ..."))
468
469     @mock.patch('functest.utils.openstack_clean.logger.error')
470     @mock.patch('functest.utils.openstack_clean.logger.debug')
471     def test_remove_routers_failed(self, mock_logger_debug,
472                                    mock_logger_error):
473         with mock.patch('functest.utils.openstack_clean.os_utils'
474                         '.delete_neutron_router',
475                         return_value=False):
476             openstack_clean.remove_routers(self.client, self.test_dict_list,
477                                            self.remove_list)
478             mock_logger_debug.assert_any_call("Router is not connected"
479                                               " to anything."
480                                               "Ready to remove...")
481             mock_logger_debug.assert_any_call(test_utils.
482                                               RegexMatch("Removing router "
483                                                          "\s*\S+(\s*\S+) ..."))
484             mock_logger_error.assert_any_call(test_utils.
485                                               RegexMatch("There has been "
486                                                          "a problem"
487                                                          " removing the "
488                                                          "router \s*\S+("
489                                                          "\s*\S+)..."))
490
491     @mock.patch('functest.utils.openstack_clean.logger.error')
492     @mock.patch('functest.utils.openstack_clean.logger.debug')
493     def test_remove_missing_external_gateway(self, mock_logger_debug,
494                                              mock_logger_error):
495         with mock.patch('functest.utils.openstack_clean.os_utils'
496                         '.delete_neutron_router',
497                         return_value=False), \
498                 mock.patch('functest.utils.openstack_clean.os_utils'
499                            '.remove_gateway_router',
500                            return_value=False):
501             self.test_dict_list[0]['external_gateway_info'] = mock.Mock()
502             openstack_clean.remove_routers(self.client, self.test_dict_list,
503                                            self.remove_list)
504             mock_logger_debug.assert_any_call("Router has gateway to external"
505                                               " network.Removing link...")
506             mock_logger_error.assert_any_call("There has been a problem "
507                                               "removing the gateway...")
508             mock_logger_debug.assert_any_call(test_utils.
509                                               RegexMatch("Removing router "
510                                                          "\s*\S+(\s*\S+) ..."))
511             mock_logger_error.assert_any_call(test_utils.
512                                               RegexMatch("There has been "
513                                                          "a problem"
514                                                          " removing the "
515                                                          "router \s*\S+("
516                                                          "\s*\S+)..."))
517
518     @mock.patch('functest.utils.openstack_clean.logger.debug')
519     def remove_security_groups(self, mock_logger_debug):
520         with mock.patch('functest.utils.openstack_clean.os_utils'
521                         '.get_security_groups',
522                         return_value=self.test_dict_list):
523             openstack_clean.remove_security_groups(self.client,
524                                                    self.update_list)
525             mock_logger_debug.assert_any_call("Removing Security groups...")
526             mock_logger_debug.assert_any_call("   > this is a default "
527                                               "security group and will NOT "
528                                               "be deleted.")
529
530     @mock.patch('functest.utils.openstack_clean.logger.debug')
531     def test_remove_security_groups_missing_sec_group(self, mock_logger_debug):
532         with mock.patch('functest.utils.openstack_clean.os_utils'
533                         '.get_security_groups', return_value=[]):
534             openstack_clean.remove_security_groups(self.client,
535                                                    self.update_list)
536             mock_logger_debug.assert_any_call("Removing Security groups...")
537             mock_logger_debug.assert_any_call("No security groups found.")
538
539     @mock.patch('functest.utils.openstack_clean.logger.debug')
540     def test_remove_security_groups_delete_success(self, mock_logger_debug):
541         with mock.patch('functest.utils.openstack_clean.os_utils'
542                         '.get_security_groups',
543                         return_value=self.test_dict_list), \
544                 mock.patch('functest.utils.openstack_clean.os_utils'
545                            '.delete_security_group', return_value=True):
546             openstack_clean.remove_security_groups(self.client,
547                                                    self.remove_list)
548             mock_logger_debug.assert_any_call("Removing Security groups...")
549             mock_logger_debug.assert_any_call("  > Done!")
550             mock_logger_debug.assert_any_call(test_utils.
551                                               RegexMatch(" Removing \s*\S+"
552                                                          "..."))
553
554     @mock.patch('functest.utils.openstack_clean.logger.error')
555     @mock.patch('functest.utils.openstack_clean.logger.debug')
556     def test_remove_security_groups_delete_failed(self, mock_logger_debug,
557                                                   mock_logger_error):
558         with mock.patch('functest.utils.openstack_clean.os_utils'
559                         '.get_security_groups',
560                         return_value=self.test_dict_list), \
561                 mock.patch('functest.utils.openstack_clean.os_utils'
562                            '.delete_security_group', return_value=False):
563             openstack_clean.remove_security_groups(self.client,
564                                                    self.remove_list)
565             mock_logger_debug.assert_any_call("Removing Security groups...")
566             mock_logger_error.assert_any_call(test_utils.
567                                               RegexMatch("There has been a "
568                                                          "problem removing "
569                                                          "the security group"
570                                                          " \s*\S+..."))
571             mock_logger_debug.assert_any_call(test_utils.
572                                               RegexMatch(" Removing \s*\S+"
573                                                          "..."))
574
575     @mock.patch('functest.utils.openstack_clean.logger.debug')
576     def test_remove_users(self, mock_logger_debug):
577         with mock.patch('functest.utils.openstack_clean.os_utils'
578                         '.get_users', return_value=self.test_list):
579             openstack_clean.remove_users(self.client, self.update_list)
580             mock_logger_debug.assert_any_call("Removing Users...")
581             mock_logger_debug.assert_any_call("   > this is a default "
582                                               "user and will "
583                                               "NOT be deleted.")
584
585     @mock.patch('functest.utils.openstack_clean.logger.debug')
586     def test_remove_users_missing_users(self, mock_logger_debug):
587         with mock.patch('functest.utils.openstack_clean.os_utils'
588                         '.get_users', return_value=None):
589             openstack_clean.remove_users(self.client, self.update_list)
590             mock_logger_debug.assert_any_call("Removing Users...")
591             mock_logger_debug.assert_any_call("There are no users in"
592                                               " the deployment. ")
593
594     @mock.patch('functest.utils.openstack_clean.logger.debug')
595     def test_remove_users_delete_success(self, mock_logger_debug):
596         with mock.patch('functest.utils.openstack_clean.os_utils'
597                         '.get_users', return_value=self.test_list), \
598                 mock.patch('functest.utils.openstack_clean.os_utils'
599                            '.delete_user', return_value=True):
600             openstack_clean.remove_users(self.client, self.remove_list)
601             mock_logger_debug.assert_any_call("Removing Users...")
602             mock_logger_debug.assert_any_call("  > Done!")
603             mock_logger_debug.assert_any_call(test_utils.
604                                               RegexMatch(" Removing "
605                                                          "\s*\S+..."))
606
607     @mock.patch('functest.utils.openstack_clean.logger.error')
608     @mock.patch('functest.utils.openstack_clean.logger.debug')
609     def test_remove_users_delete_failed(self, mock_logger_debug,
610                                         mock_logger_error):
611         with mock.patch('functest.utils.openstack_clean.os_utils'
612                         '.get_users', return_value=self.test_list), \
613                 mock.patch('functest.utils.openstack_clean.os_utils'
614                            '.delete_user', return_value=False):
615             openstack_clean.remove_users(self.client, self.remove_list)
616             mock_logger_debug.assert_any_call("Removing Users...")
617             mock_logger_error.assert_any_call(test_utils.
618                                               RegexMatch("There has been a "
619                                                          "problem removing "
620                                                          "the user \s*\S+"
621                                                          "..."))
622             mock_logger_debug.assert_any_call(test_utils.
623                                               RegexMatch(" Removing "
624                                                          "\s*\S+..."))
625
626     @mock.patch('functest.utils.openstack_clean.logger.debug')
627     def test_remove_tenants(self, mock_logger_debug):
628         with mock.patch('functest.utils.openstack_clean.os_utils'
629                         '.get_tenants', return_value=self.test_list):
630             openstack_clean.remove_tenants(self.client, self.update_list)
631             mock_logger_debug.assert_any_call("Removing Tenants...")
632             mock_logger_debug.assert_any_call("   > this is a default"
633                                               " tenant and will "
634                                               "NOT be deleted.")
635
636     @mock.patch('functest.utils.openstack_clean.logger.debug')
637     def test_remove_tenants_missing_tenants(self, mock_logger_debug):
638         with mock.patch('functest.utils.openstack_clean.os_utils'
639                         '.get_tenants', return_value=None):
640             openstack_clean.remove_tenants(self.client, self.update_list)
641             mock_logger_debug.assert_any_call("Removing Tenants...")
642             mock_logger_debug.assert_any_call("There are no tenants in"
643                                               " the deployment. ")
644
645     @mock.patch('functest.utils.openstack_clean.logger.debug')
646     def test_remove_tenants_delete_success(self, mock_logger_debug):
647         with mock.patch('functest.utils.openstack_clean.os_utils'
648                         '.get_tenants', return_value=self.test_list), \
649                 mock.patch('functest.utils.openstack_clean.os_utils'
650                            '.delete_tenant', return_value=True):
651             openstack_clean.remove_tenants(self.client, self.remove_list)
652             mock_logger_debug.assert_any_call("Removing Tenants...")
653             mock_logger_debug.assert_any_call("  > Done!")
654             mock_logger_debug.assert_any_call(test_utils.
655                                               RegexMatch(" Removing "
656                                                          "\s*\S+..."))
657
658     @mock.patch('functest.utils.openstack_clean.logger.error')
659     @mock.patch('functest.utils.openstack_clean.logger.debug')
660     def test_remove_tenants_delete_failed(self, mock_logger_debug,
661                                           mock_logger_error):
662         with mock.patch('functest.utils.openstack_clean.os_utils'
663                         '.get_tenants', return_value=self.test_list), \
664                 mock.patch('functest.utils.openstack_clean.os_utils'
665                            '.delete_tenant', return_value=False):
666             openstack_clean.remove_tenants(self.client, self.remove_list)
667             mock_logger_debug.assert_any_call("Removing Tenants...")
668             mock_logger_error.assert_any_call(test_utils.
669                                               RegexMatch("There has been a "
670                                                          "problem removing "
671                                                          "the tenant \s*\S+"
672                                                          "..."))
673             mock_logger_debug.assert_any_call(test_utils.
674                                               RegexMatch(" Removing "
675                                                          "\s*\S+..."))
676
677     @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
678     @mock.patch('functest.utils.openstack_clean.os_utils'
679                 '.get_keystone_client')
680     @mock.patch('functest.utils.openstack_clean.os_utils'
681                 '.get_neutron_client')
682     @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client')
683     @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials',
684                 return_value=True)
685     @mock.patch('functest.utils.openstack_clean.logger.info')
686     @mock.patch('functest.utils.openstack_clean.logger.debug')
687     def test_main_default(self, mock_logger_debug, mock_logger_info,
688                           mock_creds, mock_nova, mock_neutron,
689                           mock_keystone, mock_cinder):
690
691         with mock.patch('functest.utils.openstack_clean.remove_instances') \
692             as mock_remove_instances, \
693             mock.patch('functest.utils.openstack_clean.remove_images') \
694             as mock_remove_images, \
695             mock.patch('functest.utils.openstack_clean.remove_volumes') \
696             as mock_remove_volumes, \
697             mock.patch('functest.utils.openstack_clean.remove_floatingips') \
698             as mock_remove_floatingips, \
699             mock.patch('functest.utils.openstack_clean.remove_networks') \
700             as mock_remove_networks, \
701             mock.patch('functest.utils.openstack_clean.'
702                        'remove_security_groups') \
703             as mock_remove_security_groups, \
704             mock.patch('functest.utils.openstack_clean.remove_users') \
705             as mock_remove_users, \
706             mock.patch('functest.utils.openstack_clean.remove_tenants') \
707             as mock_remove_tenants, \
708             mock.patch('functest.utils.openstack_clean.yaml.safe_load',
709                        return_value=mock.Mock()), \
710                 mock.patch('__builtin__.open', mock.mock_open()) as m:
711             openstack_clean.main()
712             self.assertTrue(mock_remove_instances)
713             self.assertTrue(mock_remove_images)
714             self.assertTrue(mock_remove_volumes)
715             self.assertTrue(mock_remove_floatingips)
716             self.assertTrue(mock_remove_networks)
717             self.assertTrue(mock_remove_security_groups)
718             self.assertTrue(mock_remove_users)
719             self.assertTrue(mock_remove_tenants)
720             m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE)
721             mock_logger_info.assert_called_once_with("Cleaning OpenStack "
722                                                      "resources...")
723
724
725 if __name__ == "__main__":
726     unittest.main(verbosity=2)