Use glance to list images
[functest.git] / functest / tests / unit / utils / test_openstack_snapshot.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_snapshot
13
14
15 class OSTackerTesting(unittest.TestCase):
16
17     def _get_instance(self, key):
18         mock_obj = mock.Mock()
19         attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
20                  'ip': 'ip' + str(key)}
21         mock_obj.configure_mock(**attrs)
22         return mock_obj
23
24     def setUp(self):
25         self.client = mock.Mock()
26         self.test_list = [self._get_instance(1), self._get_instance(2)]
27         self.update_list = {'id1': 'name1', 'id2': 'name2'}
28         self.update_floatingips = {'id1': 'ip1', 'id2': 'ip2'}
29         self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1'},
30                                {'id': 'id2', 'name': 'name2', 'ip': 'ip2'}]
31
32     @mock.patch('functest.utils.openstack_snapshot.logger.info')
33     def test_separator(self, mock_logger_info):
34         openstack_snapshot.separator()
35         mock_logger_info.assert_called_once_with("-----------------"
36                                                  "-----------------"
37                                                  "---------")
38
39     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
40     def test_get_instances(self, mock_logger_debug):
41         with mock.patch('functest.utils.openstack_snapshot.os_utils'
42                         '.get_instances', return_value=self.test_list):
43             resp = openstack_snapshot.get_instances(self.client)
44             mock_logger_debug.assert_called_once_with("Getting instances...")
45             self.assertDictEqual(resp, {'instances': self.update_list})
46
47     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
48     def test_get_instances_missing_instances(self, mock_logger_debug):
49         with mock.patch('functest.utils.openstack_snapshot.os_utils'
50                         '.get_instances', return_value=[]):
51             resp = openstack_snapshot.get_instances(self.client)
52             mock_logger_debug.assert_called_once_with("Getting instances...")
53             self.assertDictEqual(resp, {'instances': {}})
54
55     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
56     def test_get_images(self, mock_logger_debug):
57         with mock.patch('functest.utils.openstack_snapshot.os_utils'
58                         '.get_images', return_value=self.test_list):
59             resp = openstack_snapshot.get_images(self.client)
60             mock_logger_debug.assert_called_once_with("Getting images...")
61             self.assertDictEqual(resp, {'images': self.update_list})
62
63     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
64     def test_get_images_missing_images(self, mock_logger_debug):
65         with mock.patch('functest.utils.openstack_snapshot.os_utils'
66                         '.get_images', return_value=[]):
67             resp = openstack_snapshot.get_images(self.client)
68             mock_logger_debug.assert_called_once_with("Getting images...")
69             self.assertDictEqual(resp, {'images': {}})
70
71     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
72     def test_get_volumes(self, mock_logger_debug):
73         with mock.patch('functest.utils.openstack_snapshot.os_utils'
74                         '.get_volumes', return_value=self.test_list):
75             resp = openstack_snapshot.get_volumes(self.client)
76             mock_logger_debug.assert_called_once_with("Getting volumes...")
77             self.assertDictEqual(resp, {'volumes': self.update_list})
78
79     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
80     def test_get_volumes_missing_volumes(self, mock_logger_debug):
81         with mock.patch('functest.utils.openstack_snapshot.os_utils'
82                         '.get_volumes', return_value=[]):
83             resp = openstack_snapshot.get_volumes(self.client)
84             mock_logger_debug.assert_called_once_with("Getting volumes...")
85             self.assertDictEqual(resp, {'volumes': {}})
86
87     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
88     def test_get_networks(self, mock_logger_debug):
89         with mock.patch('functest.utils.openstack_snapshot.os_utils'
90                         '.get_network_list', return_value=self.test_dict_list):
91             resp = openstack_snapshot.get_networks(self.client)
92             mock_logger_debug.assert_called_once_with("Getting networks")
93             self.assertDictEqual(resp, {'networks': self.update_list})
94
95     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
96     def test_get_networks_missing_networks(self, mock_logger_debug):
97         with mock.patch('functest.utils.openstack_snapshot.os_utils'
98                         '.get_network_list', return_value=[]):
99             resp = openstack_snapshot.get_networks(self.client)
100             mock_logger_debug.assert_called_once_with("Getting networks")
101             self.assertDictEqual(resp, {'networks': {}})
102
103     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
104     def test_get_routers(self, mock_logger_debug):
105         with mock.patch('functest.utils.openstack_snapshot.os_utils'
106                         '.get_router_list', return_value=self.test_dict_list):
107             resp = openstack_snapshot.get_routers(self.client)
108             mock_logger_debug.assert_called_once_with("Getting routers")
109             self.assertDictEqual(resp, {'routers': self.update_list})
110
111     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
112     def test_get_routers_missing_routers(self, mock_logger_debug):
113         with mock.patch('functest.utils.openstack_snapshot.os_utils'
114                         '.get_router_list', return_value=[]):
115             resp = openstack_snapshot.get_routers(self.client)
116             mock_logger_debug.assert_called_once_with("Getting routers")
117             self.assertDictEqual(resp, {'routers': {}})
118
119     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
120     def test_get_secgroups(self, mock_logger_debug):
121         with mock.patch('functest.utils.openstack_snapshot.os_utils'
122                         '.get_security_groups',
123                         return_value=self.test_dict_list):
124             resp = openstack_snapshot.get_security_groups(self.client)
125             mock_logger_debug.assert_called_once_with("Getting Security "
126                                                       "groups...")
127             self.assertDictEqual(resp, {'secgroups': self.update_list})
128
129     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
130     def test_get_secgroups_missing_secgroups(self, mock_logger_debug):
131         with mock.patch('functest.utils.openstack_snapshot.os_utils'
132                         '.get_security_groups', return_value=[]):
133             resp = openstack_snapshot.get_security_groups(self.client)
134             mock_logger_debug.assert_called_once_with("Getting Security "
135                                                       "groups...")
136             self.assertDictEqual(resp, {'secgroups': {}})
137
138     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
139     def test_get_floatingips(self, mock_logger_debug):
140         with mock.patch('functest.utils.openstack_snapshot.os_utils'
141                         '.get_floating_ips', return_value=self.test_list):
142             resp = openstack_snapshot.get_floatinips(self.client)
143             mock_logger_debug.assert_called_once_with("Getting Floating "
144                                                       "IPs...")
145             self.assertDictEqual(resp, {'floatingips':
146                                         self.update_floatingips})
147
148     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
149     def test_get_floatingips_missing_floatingips(self, mock_logger_debug):
150         with mock.patch('functest.utils.openstack_snapshot.os_utils'
151                         '.get_floating_ips', return_value=[]):
152             resp = openstack_snapshot.get_floatinips(self.client)
153             mock_logger_debug.assert_called_once_with("Getting Floating "
154                                                       "IPs...")
155             self.assertDictEqual(resp, {'floatingips': {}})
156
157     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
158     def test_get_users(self, mock_logger_debug):
159         with mock.patch('functest.utils.openstack_snapshot.os_utils'
160                         '.get_users', return_value=self.test_list):
161             resp = openstack_snapshot.get_users(self.client)
162             mock_logger_debug.assert_called_once_with("Getting users...")
163             self.assertDictEqual(resp, {'users': self.update_list})
164
165     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
166     def test_get_users_missing_users(self, mock_logger_debug):
167         with mock.patch('functest.utils.openstack_snapshot.os_utils'
168                         '.get_users', return_value=[]):
169             resp = openstack_snapshot.get_users(self.client)
170             mock_logger_debug.assert_called_once_with("Getting users...")
171             self.assertDictEqual(resp, {'users': {}})
172
173     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
174     def test_get_tenants(self, mock_logger_debug):
175         with mock.patch('functest.utils.openstack_snapshot.os_utils'
176                         '.get_tenants', return_value=self.test_list):
177             resp = openstack_snapshot.get_tenants(self.client)
178             mock_logger_debug.assert_called_once_with("Getting tenants...")
179             self.assertDictEqual(resp, {'tenants': self.update_list})
180
181     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
182     def test_get_tenants_missing_tenants(self, mock_logger_debug):
183         with mock.patch('functest.utils.openstack_snapshot.os_utils'
184                         '.get_tenants', return_value=[]):
185             resp = openstack_snapshot.get_tenants(self.client)
186             mock_logger_debug.assert_called_once_with("Getting tenants...")
187             self.assertDictEqual(resp, {'tenants': {}})
188
189     @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
190     @mock.patch('functest.utils.openstack_snapshot.os_utils.get_cinder_client')
191     @mock.patch('functest.utils.openstack_snapshot.os_utils'
192                 '.get_keystone_client')
193     @mock.patch('functest.utils.openstack_snapshot.os_utils'
194                 '.get_neutron_client')
195     @mock.patch('functest.utils.openstack_snapshot.os_utils.get_nova_client')
196     @mock.patch('functest.utils.openstack_snapshot.os_utils.check_credentials')
197     @mock.patch('functest.utils.openstack_snapshot.logger.info')
198     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
199     def test_main_default(self, mock_logger_debug, mock_logger_info,
200                           mock_creds, mock_nova, mock_neutron,
201                           mock_keystone, mock_cinder, mock_glance):
202         with mock.patch('functest.utils.openstack_snapshot.get_instances',
203                         return_value=self.update_list), \
204             mock.patch('functest.utils.openstack_snapshot.get_images',
205                        return_value=self.update_list), \
206             mock.patch('functest.utils.openstack_snapshot.get_images',
207                        return_value=self.update_list), \
208             mock.patch('functest.utils.openstack_snapshot.get_volumes',
209                        return_value=self.update_list), \
210             mock.patch('functest.utils.openstack_snapshot.get_networks',
211                        return_value=self.update_list), \
212             mock.patch('functest.utils.openstack_snapshot.get_routers',
213                        return_value=self.update_list), \
214             mock.patch('functest.utils.openstack_snapshot.get_security_groups',
215                        return_value=self.update_list), \
216             mock.patch('functest.utils.openstack_snapshot.get_floatinips',
217                        return_value=self.update_floatingips), \
218             mock.patch('functest.utils.openstack_snapshot.get_users',
219                        return_value=self.update_list), \
220             mock.patch('functest.utils.openstack_snapshot.get_tenants',
221                        return_value=self.update_list), \
222                 mock.patch('__builtin__.open', mock.mock_open()) as m:
223             openstack_snapshot.main()
224             mock_logger_info.assert_called_once_with("Generating OpenStack "
225                                                      "snapshot...")
226             m.assert_called_once_with(openstack_snapshot.OS_SNAPSHOT_FILE,
227                                       'w+')
228             mock_logger_debug.assert_any_call("NOTE: These objects will "
229                                               "NOT be deleted after " +
230                                               "running the test.")
231
232
233 if __name__ == "__main__":
234     logging.disable(logging.CRITICAL)
235     unittest.main(verbosity=2)