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 OSSnapshotTesting(unittest.TestCase):
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)
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.floatingips_list = [{'id': 'id1', 'floating_ip_address': 'ip1'},
30 {'id': 'id2', 'floating_ip_address': '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'
144 return_value=self.floatingips_list):
145 resp = openstack_snapshot.get_floatingips(self.client)
146 mock_logger_debug.assert_called_once_with("Getting Floating "
148 self.assertDictEqual(resp, {'floatingips':
149 self.update_floatingips})
151 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
152 def test_get_floatingips_missing_floatingips(self, mock_logger_debug):
153 with mock.patch('functest.utils.openstack_snapshot.os_utils'
154 '.get_floating_ips', return_value=[]):
155 resp = openstack_snapshot.get_floatingips(self.client)
156 mock_logger_debug.assert_called_once_with("Getting Floating "
158 self.assertDictEqual(resp, {'floatingips': {}})
160 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
161 def test_get_users(self, mock_logger_debug):
162 with mock.patch('functest.utils.openstack_snapshot.os_utils'
163 '.get_users', return_value=self.test_list):
164 resp = openstack_snapshot.get_users(self.client)
165 mock_logger_debug.assert_called_once_with("Getting users...")
166 self.assertDictEqual(resp, {'users': self.update_list})
168 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
169 def test_get_users_missing_users(self, mock_logger_debug):
170 with mock.patch('functest.utils.openstack_snapshot.os_utils'
171 '.get_users', return_value=[]):
172 resp = openstack_snapshot.get_users(self.client)
173 mock_logger_debug.assert_called_once_with("Getting users...")
174 self.assertDictEqual(resp, {'users': {}})
176 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
177 def test_get_tenants(self, mock_logger_debug):
178 with mock.patch('functest.utils.openstack_snapshot.os_utils'
179 '.get_tenants', return_value=self.test_list):
180 resp = openstack_snapshot.get_tenants(self.client)
181 mock_logger_debug.assert_called_once_with("Getting tenants...")
182 self.assertDictEqual(resp, {'tenants': self.update_list})
184 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
185 def test_get_tenants_missing_tenants(self, mock_logger_debug):
186 with mock.patch('functest.utils.openstack_snapshot.os_utils'
187 '.get_tenants', return_value=[]):
188 resp = openstack_snapshot.get_tenants(self.client)
189 mock_logger_debug.assert_called_once_with("Getting tenants...")
190 self.assertDictEqual(resp, {'tenants': {}})
192 @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
193 @mock.patch('functest.utils.openstack_snapshot.os_utils.get_cinder_client')
194 @mock.patch('functest.utils.openstack_snapshot.os_utils'
195 '.get_keystone_client')
196 @mock.patch('functest.utils.openstack_snapshot.os_utils'
197 '.get_neutron_client')
198 @mock.patch('functest.utils.openstack_snapshot.os_utils.get_nova_client')
199 @mock.patch('functest.utils.openstack_snapshot.os_utils.check_credentials')
200 @mock.patch('functest.utils.openstack_snapshot.logger.info')
201 @mock.patch('functest.utils.openstack_snapshot.logger.debug')
202 def test_main_default(self, mock_logger_debug, mock_logger_info,
203 mock_creds, mock_nova, mock_neutron,
204 mock_keystone, mock_cinder, mock_glance):
205 with mock.patch('functest.utils.openstack_snapshot.get_instances',
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_images',
210 return_value=self.update_list), \
211 mock.patch('functest.utils.openstack_snapshot.get_volumes',
212 return_value=self.update_list), \
213 mock.patch('functest.utils.openstack_snapshot.get_networks',
214 return_value=self.update_list), \
215 mock.patch('functest.utils.openstack_snapshot.get_routers',
216 return_value=self.update_list), \
217 mock.patch('functest.utils.openstack_snapshot.get_security_groups',
218 return_value=self.update_list), \
219 mock.patch('functest.utils.openstack_snapshot.get_floatingips',
220 return_value=self.update_floatingips), \
221 mock.patch('functest.utils.openstack_snapshot.get_users',
222 return_value=self.update_list), \
223 mock.patch('functest.utils.openstack_snapshot.get_tenants',
224 return_value=self.update_list), \
225 mock.patch('six.moves.builtins.open', mock.mock_open()) as m:
226 openstack_snapshot.main()
227 mock_logger_info.assert_called_once_with("Generating OpenStack "
229 m.assert_called_once_with(openstack_snapshot.OS_SNAPSHOT_FILE,
231 mock_logger_debug.assert_any_call("NOTE: These objects will "
232 "NOT be deleted after " +
236 if __name__ == "__main__":
237 logging.disable(logging.CRITICAL)
238 unittest.main(verbosity=2)