Debian Codegraph @

neutronclient/tests/unit/osc/v2/vpnaas/test_ipsecpolicy.py

raw
1 # Copyright 2017 FUJITSU LIMITED
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 from unittest import mock
18
19 from osc_lib.tests import utils as tests_utils
20
21 from neutronclient.osc import utils as osc_utils
22 from neutronclient.osc.v2.vpnaas import ipsecpolicy
23 from neutronclient.tests.unit.osc.v2 import fakes as test_fakes
24 from neutronclient.tests.unit.osc.v2.vpnaas import common
25 from neutronclient.tests.unit.osc.v2.vpnaas import fakes
26
27
28 _ipsecpolicy = fakes.IPSecPolicy().create()
29 CONVERT_MAP = {
30 'project': 'project_id',
31 }
32
33
34 def _generate_data(ordered_dict=None, data=None):
35 source = ordered_dict if ordered_dict else _ipsecpolicy
36 if data:
37 source.update(data)
38 return source
39
40
41 def _generate_req_and_res(verifylist):
42 request = dict(verifylist)
43 response = _ipsecpolicy
44 for key, val in verifylist:
45 converted = CONVERT_MAP.get(key, key)
46 del request[key]
47 new_value = val
48 request[converted] = new_value
49 response[converted] = new_value
50 return request, response
51
52
53 class TestIPSecPolicy(test_fakes.TestNeutronClientOSCV2):
54
55 def check_results(self, headers, data, exp_req, is_list=False):
56 if is_list:
57 req_body = {self.res_plural: list(exp_req)}
58 else:
59 req_body = exp_req
60 self.mocked.assert_called_once_with(**req_body)
61 self.assertEqual(self.ordered_headers, headers)
62 self.assertEqual(self.ordered_data, data)
63
64 def setUp(self):
65 super(TestIPSecPolicy, self).setUp()
66
67 def _mock_ipsecpolicy(*args, **kwargs):
68 self.networkclient.find_vpn_ipsec_policy.assert_called_once_with(
69 self.resource['id'], ignore_missing=False)
70 return {'id': args[0]}
71
72 self.networkclient.find_vpn_ipsec_policy.side_effect = mock.Mock(
73 side_effect=_mock_ipsecpolicy)
74 osc_utils.find_project = mock.Mock()
75 osc_utils.find_project.id = _ipsecpolicy['project_id']
76 self.res = 'ipsecpolicy'
77 self.res_plural = 'ipsecpolicies'
78 self.resource = _ipsecpolicy
79 self.headers = (
80 'ID',
81 'Name',
82 'Authentication Algorithm',
83 'Encapsulation Mode',
84 'Transform Protocol',
85 'Encryption Algorithm',
86 'Perfect Forward Secrecy (PFS)',
87 'Description',
88 'Project',
89 'Lifetime',
90 )
91 self.data = _generate_data()
92 self.ordered_headers = (
93 'Authentication Algorithm',
94 'Description',
95 'Encapsulation Mode',
96 'Encryption Algorithm',
97 'ID',
98 'Lifetime',
99 'Name',
100 'Perfect Forward Secrecy (PFS)',
101 'Project',
102 'Transform Protocol',
103 )
104 self.ordered_data = (
105 _ipsecpolicy['auth_algorithm'],
106 _ipsecpolicy['description'],
107 _ipsecpolicy['encapsulation_mode'],
108 _ipsecpolicy['encryption_algorithm'],
109 _ipsecpolicy['id'],
110 _ipsecpolicy['lifetime'],
111 _ipsecpolicy['name'],
112 _ipsecpolicy['pfs'],
113 _ipsecpolicy['project_id'],
114 _ipsecpolicy['transform_protocol'],
115 )
116 self.ordered_columns = (
117 'auth_algorithm',
118 'description',
119 'encapsulation_mode',
120 'encryption_algorithm',
121 'id',
122 'lifetime',
123 'name',
124 'pfs',
125 'project_id',
126 'transform_protocol',
127 )
128
129
130 class TestCreateIPSecPolicy(TestIPSecPolicy, common.TestCreateVPNaaS):
131
132 def setUp(self):
133 super(TestCreateIPSecPolicy, self).setUp()
134 self.networkclient.create_vpn_ipsec_policy = mock.Mock(
135 return_value=_ipsecpolicy)
136 self.mocked = self.networkclient.create_vpn_ipsec_policy
137 self.cmd = ipsecpolicy.CreateIPsecPolicy(self.app, self.namespace)
138
139 def _update_expect_response(self, request, response):
140 """Set expected request and response
141
142 :param request
143 A dictionary of request body(dict of verifylist)
144 :param response
145 A OrderedDict of request body
146 """
147 # Update response body
148 self.networkclient.create_vpn_ipsec_policy.return_value = \
149 response
150 osc_utils.find_project.return_value.id = response['project_id']
151 # Update response(finally returns 'data')
152 self.data = _generate_data(ordered_dict=response)
153 self.ordered_data = tuple(
154 response[column] for column in self.ordered_columns
155 )
156
157 def _set_all_params(self, args={}):
158 name = args.get('name') or 'my-name'
159 auth_algorithm = args.get('auth_algorithm') or 'sha1'
160 encapsulation_mode = args.get('encapsulation_mode') or 'tunnel'
161 transform_protocol = args.get('transform_protocol') or 'esp'
162 encryption_algorithm = args.get('encryption_algorithm') or 'aes-128'
163 pfs = args.get('pfs') or 'group5'
164 description = args.get('description') or 'my-desc'
165 project_id = args.get('project_id') or 'my-project'
166 arglist = [
167 name,
168 '--auth-algorithm', auth_algorithm,
169 '--encapsulation-mode', encapsulation_mode,
170 '--transform-protocol', transform_protocol,
171 '--encryption-algorithm', encryption_algorithm,
172 '--pfs', pfs,
173 '--description', description,
174 '--project', project_id,
175 ]
176 verifylist = [
177 ('name', name),
178 ('auth_algorithm', auth_algorithm),
179 ('encapsulation_mode', encapsulation_mode),
180 ('transform_protocol', transform_protocol),
181 ('encryption_algorithm', encryption_algorithm),
182 ('pfs', pfs),
183 ('description', description),
184 ('project', project_id),
185 ]
186 return arglist, verifylist
187
188 def _test_create_with_all_params(self, args={}):
189 arglist, verifylist = self._set_all_params(args)
190 request, response = _generate_req_and_res(verifylist)
191 self._update_expect_response(request, response)
192 parsed_args = self.check_parser(self.cmd, arglist, verifylist)
193 headers, data = self.cmd.take_action(parsed_args)
194 self.check_results(headers, data, request)
195
196 def test_create_with_no_options(self):
197 arglist = []
198 verifylist = []
199
200 self.assertRaises(tests_utils.ParserException, self.check_parser,
201 self.cmd, arglist, verifylist)
202
203 def test_create_with_all_params(self):
204 self._test_create_with_all_params()
205
206 def test_create_with_all_params_name(self):
207 self._test_create_with_all_params({'name': 'new_ipsecpolicy'})
208
209
210 class TestDeleteIPSecPolicy(TestIPSecPolicy, common.TestDeleteVPNaaS):
211
212 def setUp(self):
213 super(TestDeleteIPSecPolicy, self).setUp()
214 self.networkclient.delete_vpn_ipsec_policy = mock.Mock()
215 self.mocked = self.networkclient.delete_vpn_ipsec_policy
216 self.cmd = ipsecpolicy.DeleteIPsecPolicy(self.app, self.namespace)
217
218
219 class TestListIPSecPolicy(TestIPSecPolicy):
220
221 def setUp(self):
222 super(TestListIPSecPolicy, self).setUp()
223 self.cmd = ipsecpolicy.ListIPsecPolicy(self.app, self.namespace)
224
225 self.short_header = (
226 'ID',
227 'Name',
228 'Authentication Algorithm',
229 'Encapsulation Mode',
230 'Transform Protocol',
231 'Encryption Algorithm',
232 )
233
234 self.short_data = (
235 _ipsecpolicy['id'],
236 _ipsecpolicy['name'],
237 _ipsecpolicy['auth_algorithm'],
238 _ipsecpolicy['encapsulation_mode'],
239 _ipsecpolicy['transform_protocol'],
240 _ipsecpolicy['encryption_algorithm'],
241 )
242
243 self.networkclient.vpn_ipsec_policies = mock.Mock(
244 return_value=[_ipsecpolicy])
245 self.mocked = self.networkclient.vpn_ipsec_policies
246
247 def test_list_with_long_option(self):
248 arglist = ['--long']
249 verifylist = [('long', True)]
250 parsed_args = self.check_parser(self.cmd, arglist, verifylist)
251 headers, data = self.cmd.take_action(parsed_args)
252
253 self.mocked.assert_called_once_with()
254 self.assertEqual(list(self.headers), headers)
255
256 def test_list_with_no_option(self):
257 arglist = []
258 verifylist = []
259 parsed_args = self.check_parser(self.cmd, arglist, verifylist)
260 headers, data = self.cmd.take_action(parsed_args)
261
262 self.mocked.assert_called_once_with()
263 self.assertEqual(list(self.short_header), headers)
264 self.assertEqual([self.short_data], list(data))
265
266
267 class TestSetIPSecPolicy(TestIPSecPolicy, common.TestSetVPNaaS):
268
269 def setUp(self):
270 super(TestSetIPSecPolicy, self).setUp()
271 self.networkclient.update_vpn_ipsec_policy = mock.Mock(
272 return_value=_ipsecpolicy)
273 self.mocked = self.networkclient.update_vpn_ipsec_policy
274 self.cmd = ipsecpolicy.SetIPsecPolicy(self.app, self.namespace)
275
276 def test_set_auth_algorithm_with_sha256(self):
277 target = self.resource['id']
278 auth_algorithm = 'sha256'
279 arglist = [target, '--auth-algorithm', auth_algorithm]
280 verifylist = [
281 (self.res, target),
282 ('auth_algorithm', auth_algorithm),
283 ]
284 parsed_args = self.check_parser(self.cmd, arglist, verifylist)
285 result = self.cmd.take_action(parsed_args)
286
287 self.mocked.assert_called_once_with(
288 target, **{'auth_algorithm': 'sha256'})
289 self.assertIsNone(result)
290
291
292 class TestShowIPSecPolicy(TestIPSecPolicy, common.TestShowVPNaaS):
293
294 def setUp(self):
295 super(TestShowIPSecPolicy, self).setUp()
296 self.networkclient.get_vpn_ipsec_policy = mock.Mock(
297 return_value=_ipsecpolicy)
298 self.mocked = self.networkclient.get_vpn_ipsec_policy
299 self.cmd = ipsecpolicy.ShowIPsecPolicy(self.app, self.namespace)