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