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