Debian Codegraph @

neutron_vpnaas/tests/unit/db/vpn/test_vpn_db.py

raw
1 # (c) Copyright 2013 Hewlett-Packard Development Company, L.P.
2 # (c) Copyright 2015 Cisco Systems Inc.
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 import contextlib
18 import copy
19 import os
20 from unittest import mock
21
22 from neutron.api import extensions as api_extensions
23 from neutron.common import config
24 from neutron.db import agentschedulers_db
25 from neutron.db import l3_agentschedulers_db
26 from neutron.db import servicetype_db as sdb
27 from neutron import extensions as nextensions
28 from neutron.scheduler import l3_agent_scheduler
29 from neutron.tests.common import test_db_base_plugin_v2 as test_db_plugin
30 from neutron.tests.unit.extensions import test_l3 as test_l3_plugin
31 from neutron_lib.api.definitions import vpn
32 from neutron_lib.callbacks import events
33 from neutron_lib import constants as lib_constants
34 from neutron_lib import context
35 from neutron_lib.db import api as db_api
36 from neutron_lib.exceptions import l3 as l3_exception
37 from neutron_lib.exceptions import vpn as vpn_exception
38 from neutron_lib.plugins import constants as nconstants
39 from neutron_lib.plugins import directory
40 from oslo_db import exception as db_exc
41 from oslo_utils import uuidutils
42 import webob.exc
43
44 from neutron_vpnaas.db.vpn import vpn_db
45 from neutron_vpnaas.db.vpn import vpn_models
46 from neutron_vpnaas.services.vpn.common import constants
47 from neutron_vpnaas.services.vpn import plugin as vpn_plugin
48 from neutron_vpnaas.tests import base
49
50 from neutron_vpnaas import extensions
51
52 DB_CORE_PLUGIN_KLASS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
53 DB_VPN_PLUGIN_KLASS = "neutron_vpnaas.services.vpn.plugin.VPNPlugin"
54 FLAVOR_PLUGIN_KLASS = "neutron.services.flavors.flavors_plugin.FlavorsPlugin"
55 ROOTDIR = os.path.normpath(os.path.join(
56 os.path.dirname(__file__),
57 '..', '..', '..', '..'))
58
59 extensions_path = ':'.join(extensions.__path__ + nextensions.__path__)
60
61 _uuid = uuidutils.generate_uuid
62
63
64 class TestVpnCorePlugin(test_l3_plugin.TestL3NatIntPlugin,
65 l3_agentschedulers_db.L3AgentSchedulerDbMixin,
66 agentschedulers_db.DhcpAgentSchedulerDbMixin):
67 def __init__(self, configfile=None):
68 super().__init__()
69 self.router_scheduler = l3_agent_scheduler.ChanceScheduler()
70
71
72 class VPNTestMixin:
73 resource_prefix_map = {
74 k.replace('_', '-'):
75 "/vpn"
76 for k in vpn.RESOURCE_ATTRIBUTE_MAP
77 }
78
79 def _create_ikepolicy(self, fmt,
80 name='ikepolicy1',
81 auth_algorithm='sha1',
82 encryption_algorithm='aes-128',
83 phase1_negotiation_mode='main',
84 lifetime_units='seconds',
85 lifetime_value=3600,
86 ike_version='v1',
87 pfs='group5',
88 expected_res_status=None, **kwargs):
89
90 data = {'ikepolicy': {
91 'name': name,
92 'auth_algorithm': auth_algorithm,
93 'encryption_algorithm': encryption_algorithm,
94 'phase1_negotiation_mode': phase1_negotiation_mode,
95 'lifetime': {
96 'units': lifetime_units,
97 'value': lifetime_value},
98 'ike_version': ike_version,
99 'pfs': pfs,
100 'tenant_id': self._project_id
101 }}
102 if kwargs.get('description') is not None:
103 data['ikepolicy']['description'] = kwargs['description']
104
105 ikepolicy_req = self.new_create_request('ikepolicies', data, fmt)
106 ikepolicy_res = ikepolicy_req.get_response(self.ext_api)
107 if expected_res_status:
108 self.assertEqual(ikepolicy_res.status_int, expected_res_status)
109
110 return ikepolicy_res
111
112 @contextlib.contextmanager
113 def ikepolicy(self, fmt=None,
114 name='ikepolicy1',
115 auth_algorithm='sha1',
116 encryption_algorithm='aes-128',
117 phase1_negotiation_mode='main',
118 lifetime_units='seconds',
119 lifetime_value=3600,
120 ike_version='v1',
121 pfs='group5',
122 do_delete=True,
123 **kwargs):
124 if not fmt:
125 fmt = self.fmt
126 res = self._create_ikepolicy(fmt,
127 name,
128 auth_algorithm,
129 encryption_algorithm,
130 phase1_negotiation_mode,
131 lifetime_units,
132 lifetime_value,
133 ike_version,
134 pfs,
135 **kwargs)
136 if res.status_int >= 400:
137 raise webob.exc.HTTPClientError(code=res.status_int)
138 ikepolicy = self.deserialize(fmt or self.fmt, res)
139 yield ikepolicy
140 if do_delete:
141 self._delete('ikepolicies', ikepolicy['ikepolicy']['id'])
142
143 def _create_ipsecpolicy(self, fmt,
144 name='ipsecpolicy1',
145 auth_algorithm='sha1',
146 encryption_algorithm='aes-128',
147 encapsulation_mode='tunnel',
148 transform_protocol='esp',
149 lifetime_units='seconds',
150 lifetime_value=3600,
151 pfs='group5',
152 expected_res_status=None,
153 **kwargs):
154
155 data = {'ipsecpolicy': {'name': name,
156 'auth_algorithm': auth_algorithm,
157 'encryption_algorithm': encryption_algorithm,
158 'encapsulation_mode': encapsulation_mode,
159 'transform_protocol': transform_protocol,
160 'lifetime': {'units': lifetime_units,
161 'value': lifetime_value},
162 'pfs': pfs,
163 'tenant_id': self._project_id}}
164 if kwargs.get('description') is not None:
165 data['ipsecpolicy']['description'] = kwargs['description']
166 ipsecpolicy_req = self.new_create_request('ipsecpolicies', data, fmt)
167 ipsecpolicy_res = ipsecpolicy_req.get_response(self.ext_api)
168 if expected_res_status:
169 self.assertEqual(ipsecpolicy_res.status_int, expected_res_status)
170
171 return ipsecpolicy_res
172
173 @contextlib.contextmanager
174 def ipsecpolicy(self, fmt=None,
175 name='ipsecpolicy1',
176 auth_algorithm='sha1',
177 encryption_algorithm='aes-128',
178 encapsulation_mode='tunnel',
179 transform_protocol='esp',
180 lifetime_units='seconds',
181 lifetime_value=3600,
182 pfs='group5',
183 do_delete=True, **kwargs):
184 if not fmt:
185 fmt = self.fmt
186 res = self._create_ipsecpolicy(fmt,
187 name,
188 auth_algorithm,
189 encryption_algorithm,
190 encapsulation_mode,
191 transform_protocol,
192 lifetime_units,
193 lifetime_value,
194 pfs,
195 **kwargs)
196 if res.status_int >= 400:
197 raise webob.exc.HTTPClientError(code=res.status_int)
198 ipsecpolicy = self.deserialize(fmt or self.fmt, res)
199 yield ipsecpolicy
200 if do_delete:
201 self._delete('ipsecpolicies', ipsecpolicy['ipsecpolicy']['id'])
202
203 def _create_vpnservice(self, fmt, name,
204 admin_state_up,
205 router_id, subnet_id,
206 expected_res_status=None, **kwargs):
207 tenant_id = kwargs.get('tenant_id', self._project_id)
208 data = {'vpnservice': {'name': name,
209 'subnet_id': subnet_id,
210 'router_id': router_id,
211 'admin_state_up': admin_state_up,
212 'tenant_id': tenant_id}}
213 if kwargs.get('description') is not None:
214 data['vpnservice']['description'] = kwargs['description']
215 if kwargs.get('flavor_id') is not None:
216 data['vpnservice']['flavor_id'] = kwargs['flavor_id']
217 vpnservice_req = self.new_create_request('vpnservices', data, fmt)
218 if (kwargs.get('set_context') and
219 'tenant_id' in kwargs):
220 # create a specific auth context for this request
221 vpnservice_req.environ['neutron.context'] = context.Context(
222 '', kwargs['tenant_id'])
223 vpnservice_res = vpnservice_req.get_response(self.ext_api)
224 if expected_res_status:
225 self.assertEqual(vpnservice_res.status_int, expected_res_status)
226 return vpnservice_res
227
228 @contextlib.contextmanager
229 def vpnservice(self, fmt=None, name='vpnservice1',
230 subnet=None,
231 router=None,
232 admin_state_up=True,
233 do_delete=True,
234 plug_subnet=True,
235 external_subnet_cidr='192.168.100.0/24',
236 external_router=True,
237 **kwargs):
238 if not fmt:
239 fmt = self.fmt
240 with test_db_plugin.optional_ctx(subnet, self.subnet) as tmp_subnet, \
241 test_db_plugin.optional_ctx(router,
242 self.router) as tmp_router, \
243 self.subnet(cidr=external_subnet_cidr) as public_sub:
244 if external_router:
245 self._set_net_external(
246 public_sub['subnet']['network_id'])
247 self._add_external_gateway_to_router(
248 tmp_router['router']['id'],
249 public_sub['subnet']['network_id'])
250 tmp_router['router']['external_gateway_info'] = {
251 'network_id': public_sub['subnet']['network_id']}
252 if plug_subnet:
253 self._router_interface_action(
254 'add',
255 tmp_router['router']['id'],
256 tmp_subnet['subnet']['id'], None)
257
258 res = self._create_vpnservice(fmt,
259 name,
260 admin_state_up,
261 router_id=(tmp_router['router']
262 ['id']),
263 subnet_id=(tmp_subnet['subnet']
264 ['id']),
265 **kwargs)
266 vpnservice = self.deserialize(fmt or self.fmt, res)
267 if res.status_int < 400:
268 yield vpnservice
269
270 if do_delete and vpnservice.get('vpnservice'):
271 self._delete('vpnservices',
272 vpnservice['vpnservice']['id'])
273 if plug_subnet:
274 self._router_interface_action(
275 'remove',
276 tmp_router['router']['id'],
277 tmp_subnet['subnet']['id'], None)
278 if external_router:
279 external_gateway = tmp_router['router'].get(
280 'external_gateway_info')
281 if external_gateway:
282 network_id = external_gateway['network_id']
283 self._remove_external_gateway_from_router(
284 tmp_router['router']['id'], network_id)
285 if res.status_int >= 400:
286 raise webob.exc.HTTPClientError(
287 code=res.status_int, detail=vpnservice)
288 self._delete('subnets', public_sub['subnet']['id'])
289 if not subnet:
290 self._delete('subnets', tmp_subnet['subnet']['id'])
291
292 def _create_ipsec_site_connection(self, fmt, name='test',
293 peer_address='192.168.1.10',
294 peer_id='192.168.1.10',
295 peer_cidrs=None,
296 mtu=1500,
297 psk='abcdefg',
298 initiator='bi-directional',
299 dpd_action='hold',
300 dpd_interval=30,
301 dpd_timeout=120,
302 vpnservice_id='fake_id',
303 ikepolicy_id='fake_id',
304 ipsecpolicy_id='fake_id',
305 admin_state_up=True,
306 local_ep_group_id=None,
307 peer_ep_group_id=None,
308 expected_res_status=None, **kwargs):
309 data = {
310 'ipsec_site_connection': {'name': name,
311 'peer_address': peer_address,
312 'peer_id': peer_id,
313 'peer_cidrs': peer_cidrs,
314 'mtu': mtu,
315 'psk': psk,
316 'initiator': initiator,
317 'dpd': {
318 'action': dpd_action,
319 'interval': dpd_interval,
320 'timeout': dpd_timeout,
321 },
322 'vpnservice_id': vpnservice_id,
323 'ikepolicy_id': ikepolicy_id,
324 'ipsecpolicy_id': ipsecpolicy_id,
325 'admin_state_up': admin_state_up,
326 'tenant_id': self._project_id,
327 'local_ep_group_id': local_ep_group_id,
328 'peer_ep_group_id': peer_ep_group_id}
329 }
330 if kwargs.get('description') is not None:
331 data['ipsec_site_connection'][
332 'description'] = kwargs['description']
333
334 ipsec_site_connection_req = self.new_create_request(
335 'ipsec-site-connections', data, fmt
336 )
337 ipsec_site_connection_res = ipsec_site_connection_req.get_response(
338 self.ext_api
339 )
340 if expected_res_status:
341 self.assertEqual(
342 ipsec_site_connection_res.status_int, expected_res_status
343 )
344
345 return ipsec_site_connection_res
346
347 @contextlib.contextmanager
348 def ipsec_site_connection(self, fmt=None, name='ipsec_site_connection1',
349 peer_address='192.168.1.10',
350 peer_id='192.168.1.10',
351 peer_cidrs=None,
352 mtu=1500,
353 psk='abcdefg',
354 initiator='bi-directional',
355 dpd_action='hold',
356 dpd_interval=30,
357 dpd_timeout=120,
358 vpnservice=None,
359 ikepolicy=None,
360 ipsecpolicy=None,
361 admin_state_up=True, do_delete=True,
362 local_ep_group_id=None,
363 peer_ep_group_id=None,
364 **kwargs):
365 if not fmt:
366 fmt = self.fmt
367 with test_db_plugin.optional_ctx(vpnservice, self.vpnservice
368 ) as tmp_vpnservice, \
369 test_db_plugin.optional_ctx(ikepolicy, self.ikepolicy
370 ) as tmp_ikepolicy, \
371 test_db_plugin.optional_ctx(ipsecpolicy, self.ipsecpolicy
372 ) as tmp_ipsecpolicy:
373 vpnservice_id = tmp_vpnservice['vpnservice']['id']
374 ikepolicy_id = tmp_ikepolicy['ikepolicy']['id']
375 ipsecpolicy_id = tmp_ipsecpolicy['ipsecpolicy']['id']
376 if not peer_cidrs and not local_ep_group_id:
377 # Must be legacy usage - pick default to use
378 peer_cidrs = ['10.0.0.0/24']
379 res = self._create_ipsec_site_connection(fmt,
380 name,
381 peer_address,
382 peer_id,
383 peer_cidrs,
384 mtu,
385 psk,
386 initiator,
387 dpd_action,
388 dpd_interval,
389 dpd_timeout,
390 vpnservice_id,
391 ikepolicy_id,
392 ipsecpolicy_id,
393 admin_state_up,
394 local_ep_group_id,
395 peer_ep_group_id,
396 **kwargs)
397 if res.status_int >= 400:
398 raise webob.exc.HTTPClientError(code=res.status_int)
399
400 ipsec_site_connection = self.deserialize(
401 fmt or self.fmt, res
402 )
403 yield ipsec_site_connection
404
405 if do_delete:
406 self._delete(
407 'ipsec-site-connections',
408 ipsec_site_connection[
409 'ipsec_site_connection']['id']
410 )
411
412 def _check_ipsec_site_connection(self, ipsec_site_connection, keys, dpd):
413 self.assertEqual(
414 keys,
415 {k: v for k, v
416 in ipsec_site_connection.items()
417 if k in keys})
418 self.assertEqual(
419 dpd,
420 {k: v for k, v
421 in ipsec_site_connection['dpd'].items()
422 if k in dpd})
423
424 def _set_active(self, model, resource_id):
425 service_plugin = directory.get_plugin(nconstants.VPN)
426 adminContext = context.get_admin_context()
427 with db_api.CONTEXT_WRITER.using(adminContext):
428 resource_db = service_plugin._get_resource(
429 adminContext,
430 model,
431 resource_id)
432 resource_db.status = lib_constants.ACTIVE
433
434
435 class VPNPluginDbTestCase(VPNTestMixin,
436 test_l3_plugin.L3NatTestCaseMixin,
437 base.NeutronDbPluginV2TestCase):
438 def setUp(self, core_plugin=None, vpnaas_plugin=DB_VPN_PLUGIN_KLASS,
439 vpnaas_provider=None):
440 if not vpnaas_provider:
441 vpnaas_provider = (
442 nconstants.VPN +
443 ':vpnaas:neutron_vpnaas.services.vpn.'
444 'service_drivers.ipsec.IPsecVPNDriver:default')
445 bits = vpnaas_provider.split(':')
446 vpnaas_provider = {
447 'service_type': bits[0],
448 'name': bits[1],
449 'driver': bits[2]
450 }
451 if len(bits) == 4:
452 vpnaas_provider['default'] = True
453 # override the default service provider
454 self.service_providers = (
455 mock.patch.object(sdb.ServiceTypeManager,
456 'get_service_providers').start())
457 self.service_providers.return_value = [vpnaas_provider]
458 # force service type manager to reload configuration:
459 sdb.ServiceTypeManager._instance = None
460 service_plugins = {
461 'vpnaas_plugin': vpnaas_plugin,
462 'flavors_plugin': FLAVOR_PLUGIN_KLASS}
463 plugin_str = ('neutron_vpnaas.tests.unit.db.vpn.'
464 'test_vpn_db.TestVpnCorePlugin')
465
466 super().setUp(plugin_str, service_plugins=service_plugins)
467 self._subnet_id = _uuid()
468 self.core_plugin = TestVpnCorePlugin()
469 self.plugin = vpn_plugin.VPNPlugin()
470 ext_mgr = api_extensions.PluginAwareExtensionManager(
471 extensions_path,
472 {nconstants.CORE: self.core_plugin,
473 nconstants.VPN: self.plugin}
474 )
475 app = config.load_paste_app('extensions_test_app')
476 self.ext_api = api_extensions.ExtensionMiddleware(app, ext_mgr=ext_mgr)
477
478
479 class TestVpnaas(VPNPluginDbTestCase):
480
481 def setUp(self, **kwargs):
482 # TODO(armax): this is far from being a unit test case, as it tests
483 # that multiple parties (core + vpn) are integrated properly and
484 # should be replaced by API test that do not rely on so much mocking.
485 # NOTE(armax): make sure that the callbacks needed by this test are
486 # registered, as they may get wiped out depending by the order in
487 # which imports, subscriptions and mocks occur.
488 super().setUp(**kwargs)
489 vpn_db.subscribe()
490
491 def _check_policy(self, policy, keys, lifetime):
492 for k, v in keys:
493 self.assertEqual(policy[k], v)
494 for k, v in lifetime.items():
495 self.assertEqual(policy['lifetime'][k], v)
496
497 def test_create_ikepolicy(self):
498 """Test case to create an ikepolicy."""
499 name = "ikepolicy1"
500 description = 'ipsec-ikepolicy'
501 keys = [('name', name),
502 ('description', 'ipsec-ikepolicy'),
503 ('auth_algorithm', 'sha1'),
504 ('encryption_algorithm', 'aes-128'),
505 ('phase1_negotiation_mode', 'main'),
506 ('ike_version', 'v1'),
507 ('pfs', 'group5'),
508 ('tenant_id', self._project_id)]
509 lifetime = {
510 'units': 'seconds',
511 'value': 3600}
512 with self.ikepolicy(name=name, description=description) as ikepolicy:
513 self._check_policy(ikepolicy['ikepolicy'], keys, lifetime)
514
515 def test_create_ikepolicy_with_every_pfs(self):
516 """Test case to create ikepolicies with different pfs."""
517 name = "ikepolicy1"
518 for group in vpn_models.PFS_ENUM_VALUES:
519 self.ikepolicy(name=name, pfs=group, expected_res_status=201)
520
521 def test_create_ikepolicy_with_aggressive_mode(self):
522 """Test case to create an ikepolicy with aggressive mode."""
523 name = "ikepolicy1"
524 description = 'ipsec-ikepolicy'
525 mode = 'aggressive'
526 keys = [('name', name),
527 ('description', 'ipsec-ikepolicy'),
528 ('auth_algorithm', 'sha1'),
529 ('encryption_algorithm', 'aes-128'),
530 ('phase1_negotiation_mode', 'aggressive'),
531 ('ike_version', 'v1'),
532 ('pfs', 'group5'),
533 ('tenant_id', self._project_id)]
534 lifetime = {
535 'units': 'seconds',
536 'value': 3600}
537 with self.ikepolicy(name=name, description=description,
538 phase1_negotiation_mode=mode) as ikepolicy:
539 self._check_policy(ikepolicy['ikepolicy'], keys, lifetime)
540
541 def test_delete_ikepolicy(self):
542 """Test case to delete an ikepolicy."""
543 with self.ikepolicy(do_delete=False) as ikepolicy:
544 req = self.new_delete_request('ikepolicies',
545 ikepolicy['ikepolicy']['id'])
546 res = req.get_response(self.ext_api)
547 self.assertEqual(res.status_int, 204)
548
549 def test_show_ikepolicy(self):
550 """Test case to show or get an ikepolicy."""
551 name = "ikepolicy1"
552 description = 'ipsec-ikepolicy'
553 keys = [('name', name),
554 ('auth_algorithm', 'sha1'),
555 ('encryption_algorithm', 'aes-128'),
556 ('phase1_negotiation_mode', 'main'),
557 ('ike_version', 'v1'),
558 ('pfs', 'group5'),
559 ('tenant_id', self._project_id)]
560 lifetime = {
561 'units': 'seconds',
562 'value': 3600}
563 with self.ikepolicy(name=name, description=description) as ikepolicy:
564 req = self.new_show_request('ikepolicies',
565 ikepolicy['ikepolicy']['id'],
566 fmt=self.fmt,
567 as_admin=True)
568 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
569 self._check_policy(res['ikepolicy'], keys, lifetime)
570
571 def test_list_ikepolicies(self):
572 """Test case to list all ikepolicies."""
573 name = "ikepolicy_list"
574 keys = [('name', name),
575 ('auth_algorithm', 'sha1'),
576 ('encryption_algorithm', 'aes-128'),
577 ('phase1_negotiation_mode', 'main'),
578 ('ike_version', 'v1'),
579 ('pfs', 'group5'),
580 ('tenant_id', self._project_id)]
581 lifetime = {
582 'units': 'seconds',
583 'value': 3600}
584 with self.ikepolicy(name=name) as ikepolicy:
585 keys.append(('id', ikepolicy['ikepolicy']['id']))
586 req = self.new_list_request('ikepolicies', as_admin=True)
587 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
588 self.assertEqual(len(res), 1)
589 for k, v in keys:
590 self.assertEqual(res['ikepolicies'][0][k], v)
591 for k, v in lifetime.items():
592 self.assertEqual(res['ikepolicies'][0]['lifetime'][k], v)
593
594 def test_list_ikepolicies_with_sort_emulated(self):
595 """Test case to list all ikepolicies."""
596 with self.ikepolicy(name='ikepolicy1') as ikepolicy1, \
597 self.ikepolicy(name='ikepolicy2') as ikepolicy2, \
598 self.ikepolicy(name='ikepolicy3') as ikepolicy3:
599 self._test_list_with_sort('ikepolicy', (ikepolicy3,
600 ikepolicy2,
601 ikepolicy1),
602 [('name', 'desc')],
603 'ikepolicies',
604 as_admin=True)
605
606 def test_list_ikepolicies_with_pagination_emulated(self):
607 """Test case to list all ikepolicies with pagination."""
608 with self.ikepolicy(name='ikepolicy1') as ikepolicy1, \
609 self.ikepolicy(name='ikepolicy2') as ikepolicy2, \
610 self.ikepolicy(name='ikepolicy3') as ikepolicy3:
611 self._test_list_with_pagination('ikepolicy',
612 (ikepolicy1,
613 ikepolicy2,
614 ikepolicy3),
615 ('name', 'asc'), 2, 2,
616 'ikepolicies',
617 as_admin=True)
618
619 def test_list_ikepolicies_with_pagination_reverse_emulated(self):
620 """Test case to list all ikepolicies with reverse pagination."""
621 with self.ikepolicy(name='ikepolicy1') as ikepolicy1, \
622 self.ikepolicy(name='ikepolicy2') as ikepolicy2, \
623 self.ikepolicy(name='ikepolicy3') as ikepolicy3:
624 self._test_list_with_pagination_reverse('ikepolicy',
625 (ikepolicy1,
626 ikepolicy2,
627 ikepolicy3),
628 ('name', 'asc'), 2, 2,
629 'ikepolicies',
630 as_admin=True)
631
632 def test_update_ikepolicy(self):
633 """Test case to update an ikepolicy."""
634 name = "new_ikepolicy1"
635 keys = [('name', name),
636 ('auth_algorithm', 'sha1'),
637 ('encryption_algorithm', 'aes-128'),
638 ('phase1_negotiation_mode', 'main'),
639 ('ike_version', 'v1'),
640 ('pfs', 'group5'),
641 ('tenant_id', self._project_id),
642 ('lifetime', {'units': 'seconds',
643 'value': 60})]
644 with self.ikepolicy(name=name) as ikepolicy:
645 data = {'ikepolicy': {'name': name,
646 'lifetime': {'units': 'seconds',
647 'value': 60}}}
648 req = self.new_update_request("ikepolicies",
649 data,
650 ikepolicy['ikepolicy']['id'])
651 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
652 for k, v in keys:
653 self.assertEqual(res['ikepolicy'][k], v)
654
655 def test_update_ikepolicy_with_aggressive_mode(self):
656 """Test case to update an ikepolicy with aggressive mode."""
657 name = "new_ikepolicy1"
658 keys = [('name', name),
659 ('auth_algorithm', 'sha1'),
660 ('encryption_algorithm', 'aes-128'),
661 ('phase1_negotiation_mode', 'aggressive'),
662 ('ike_version', 'v1'),
663 ('pfs', 'group5'),
664 ('tenant_id', self._project_id),
665 ('lifetime', {'units': 'seconds',
666 'value': 60})]
667 with self.ikepolicy(name=name) as ikepolicy:
668 data = {'ikepolicy': {'name': name,
669 'phase1_negotiation_mode': 'aggressive',
670 'lifetime': {'units': 'seconds',
671 'value': 60}}}
672 req = self.new_update_request("ikepolicies",
673 data,
674 ikepolicy['ikepolicy']['id'])
675 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
676 for k, v in keys:
677 self.assertEqual(res['ikepolicy'][k], v)
678
679 def test_create_ikepolicy_with_invalid_values(self):
680 """Test case to test invalid values."""
681 name = 'ikepolicy1'
682 self._create_ikepolicy(name=name,
683 fmt=self.fmt,
684 auth_algorithm='md5',
685 expected_res_status=400)
686 self._create_ikepolicy(name=name,
687 fmt=self.fmt,
688 auth_algorithm=200,
689 expected_res_status=400)
690 self._create_ikepolicy(name=name,
691 fmt=self.fmt,
692 encryption_algorithm='des',
693 expected_res_status=400)
694 self._create_ikepolicy(name=name,
695 fmt=self.fmt,
696 encryption_algorithm=100,
697 expected_res_status=400)
698 self._create_ikepolicy(name=name,
699 fmt=self.fmt,
700 phase1_negotiation_mode='unsupported',
701 expected_res_status=400)
702 self._create_ikepolicy(name=name,
703 fmt=self.fmt,
704 phase1_negotiation_mode=-100,
705 expected_res_status=400)
706 self._create_ikepolicy(name=name,
707 fmt=self.fmt,
708 ike_version='v6',
709 expected_res_status=400)
710 self._create_ikepolicy(name=name,
711 fmt=self.fmt,
712 ike_version=500,
713 expected_res_status=400)
714 self._create_ikepolicy(name=name,
715 fmt=self.fmt,
716 pfs='group1',
717 expected_res_status=400)
718 self._create_ikepolicy(name=name,
719 fmt=self.fmt,
720 pfs=120,
721 expected_res_status=400)
722 self._create_ikepolicy(name=name,
723 fmt=self.fmt,
724 lifetime_units='Megabytes',
725 expected_res_status=400)
726 self._create_ikepolicy(name=name,
727 fmt=self.fmt,
728 lifetime_units=20000,
729 expected_res_status=400)
730 self._create_ikepolicy(name=name,
731 fmt=self.fmt,
732 lifetime_value=-20,
733 expected_res_status=400)
734 self._create_ikepolicy(name=name,
735 fmt=self.fmt,
736 lifetime_value='Megabytes',
737 expected_res_status=400)
738
739 def test_create_ipsecpolicy(self):
740 """Test case to create an ipsecpolicy."""
741 name = "ipsecpolicy1"
742 description = 'my-ipsecpolicy'
743 keys = [('name', name),
744 ('description', 'my-ipsecpolicy'),
745 ('auth_algorithm', 'sha1'),
746 ('encryption_algorithm', 'aes-128'),
747 ('encapsulation_mode', 'tunnel'),
748 ('transform_protocol', 'esp'),
749 ('pfs', 'group5'),
750 ('tenant_id', self._project_id)]
751 lifetime = {
752 'units': 'seconds',
753 'value': 3600}
754 with self.ipsecpolicy(name=name,
755 description=description) as ipsecpolicy:
756 self._check_policy(ipsecpolicy['ipsecpolicy'], keys, lifetime)
757
758 def test_create_ipsecpolicies_with_every_pfs(self):
759 """Test case to create ipsecpolicies with different pfs."""
760 name = "ipsecpolicy1"
761 for group in vpn_models.PFS_ENUM_VALUES:
762 self.ipsecpolicy(name=name, pfs=group, expected_res_status=201)
763
764 def test_delete_ipsecpolicy(self):
765 """Test case to delete an ipsecpolicy."""
766 with self.ipsecpolicy(do_delete=False) as ipsecpolicy:
767 req = self.new_delete_request('ipsecpolicies',
768 ipsecpolicy['ipsecpolicy']['id'])
769 res = req.get_response(self.ext_api)
770 self.assertEqual(res.status_int, 204)
771
772 def test_show_ipsecpolicy(self):
773 """Test case to show or get an ipsecpolicy."""
774 name = "ipsecpolicy1"
775 keys = [('name', name),
776 ('auth_algorithm', 'sha1'),
777 ('encryption_algorithm', 'aes-128'),
778 ('encapsulation_mode', 'tunnel'),
779 ('transform_protocol', 'esp'),
780 ('pfs', 'group5'),
781 ('tenant_id', self._project_id)]
782 lifetime = {
783 'units': 'seconds',
784 'value': 3600}
785 with self.ipsecpolicy(name=name) as ipsecpolicy:
786 req = self.new_show_request('ipsecpolicies',
787 ipsecpolicy['ipsecpolicy']['id'],
788 fmt=self.fmt,
789 as_admin=True)
790 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
791 self._check_policy(res['ipsecpolicy'], keys, lifetime)
792
793 def test_list_ipsecpolicies(self):
794 """Test case to list all ipsecpolicies."""
795 name = "ipsecpolicy_list"
796 keys = [('name', name),
797 ('auth_algorithm', 'sha1'),
798 ('encryption_algorithm', 'aes-128'),
799 ('encapsulation_mode', 'tunnel'),
800 ('transform_protocol', 'esp'),
801 ('pfs', 'group5'),
802 ('tenant_id', self._project_id)]
803 lifetime = {
804 'units': 'seconds',
805 'value': 3600}
806 with self.ipsecpolicy(name=name) as ipsecpolicy:
807 keys.append(('id', ipsecpolicy['ipsecpolicy']['id']))
808 req = self.new_list_request('ipsecpolicies', as_admin=True)
809 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
810 self.assertEqual(len(res), 1)
811 self._check_policy(res['ipsecpolicies'][0], keys, lifetime)
812
813 def test_list_ipsecpolicies_with_sort_emulated(self):
814 """Test case to list all ipsecpolicies."""
815 with self.ipsecpolicy(name='ipsecpolicy1') as ipsecpolicy1, \
816 self.ipsecpolicy(name='ipsecpolicy2') as ipsecpolicy2, \
817 self.ipsecpolicy(name='ipsecpolicy3') as ipsecpolicy3:
818 self._test_list_with_sort('ipsecpolicy', (ipsecpolicy3,
819 ipsecpolicy2,
820 ipsecpolicy1),
821 [('name', 'desc')],
822 'ipsecpolicies',
823 as_admin=True)
824
825 def test_list_ipsecpolicies_with_pagination_emulated(self):
826 """Test case to list all ipsecpolicies with pagination."""
827 with self.ipsecpolicy(name='ipsecpolicy1') as ipsecpolicy1, \
828 self.ipsecpolicy(name='ipsecpolicy2') as ipsecpolicy2, \
829 self.ipsecpolicy(name='ipsecpolicy3') as ipsecpolicy3:
830 self._test_list_with_pagination('ipsecpolicy',
831 (ipsecpolicy1,
832 ipsecpolicy2,
833 ipsecpolicy3),
834 ('name', 'asc'), 2, 2,
835 'ipsecpolicies',
836 as_admin=True)
837
838 def test_list_ipsecpolicies_with_pagination_reverse_emulated(self):
839 """Test case to list all ipsecpolicies with reverse pagination."""
840 with self.ipsecpolicy(name='ipsecpolicy1') as ipsecpolicy1, \
841 self.ipsecpolicy(name='ipsecpolicy2') as ipsecpolicy2, \
842 self.ipsecpolicy(name='ipsecpolicy3') as ipsecpolicy3:
843 self._test_list_with_pagination_reverse('ipsecpolicy',
844 (ipsecpolicy1,
845 ipsecpolicy2,
846 ipsecpolicy3),
847 ('name', 'asc'), 2, 2,
848 'ipsecpolicies',
849 as_admin=True)
850
851 def test_update_ipsecpolicy(self):
852 """Test case to update an ipsecpolicy."""
853 name = "new_ipsecpolicy1"
854 keys = [('name', name),
855 ('auth_algorithm', 'sha1'),
856 ('encryption_algorithm', 'aes-128'),
857 ('encapsulation_mode', 'tunnel'),
858 ('transform_protocol', 'esp'),
859 ('pfs', 'group5'),
860 ('tenant_id', self._project_id),
861 ('lifetime', {'units': 'seconds',
862 'value': 60})]
863 with self.ipsecpolicy(name=name) as ipsecpolicy:
864 data = {'ipsecpolicy': {'name': name,
865 'lifetime': {'units': 'seconds',
866 'value': 60}}}
867 req = self.new_update_request("ipsecpolicies",
868 data,
869 ipsecpolicy['ipsecpolicy']['id'])
870 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
871 for k, v in keys:
872 self.assertEqual(res['ipsecpolicy'][k], v)
873
874 def test_update_ipsecpolicy_lifetime(self):
875 with self.ipsecpolicy() as ipsecpolicy:
876 data = {'ipsecpolicy': {'lifetime': {'units': 'seconds'}}}
877 req = self.new_update_request("ipsecpolicies",
878 data,
879 ipsecpolicy['ipsecpolicy']['id'])
880 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
881 self.assertEqual(res['ipsecpolicy']['lifetime']['units'],
882 'seconds')
883
884 data = {'ipsecpolicy': {'lifetime': {'value': 60}}}
885 req = self.new_update_request("ipsecpolicies",
886 data,
887 ipsecpolicy['ipsecpolicy']['id'])
888 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
889 self.assertEqual(res['ipsecpolicy']['lifetime']['value'], 60)
890
891 def test_create_ipsecpolicy_with_invalid_values(self):
892 """Test case to test invalid values."""
893 name = 'ipsecpolicy1'
894
895 self._create_ipsecpolicy(
896 fmt=self.fmt,
897 name=name, auth_algorithm='md5', expected_res_status=400)
898 self._create_ipsecpolicy(
899 fmt=self.fmt,
900 name=name, auth_algorithm=100, expected_res_status=400)
901
902 self._create_ipsecpolicy(
903 fmt=self.fmt,
904 name=name, encryption_algorithm='des', expected_res_status=400)
905 self._create_ipsecpolicy(
906 fmt=self.fmt,
907 name=name, encryption_algorithm=200, expected_res_status=400)
908
909 self._create_ipsecpolicy(
910 fmt=self.fmt,
911 name=name, transform_protocol='abcd', expected_res_status=400)
912 self._create_ipsecpolicy(
913 fmt=self.fmt,
914 name=name, transform_protocol=500, expected_res_status=400)
915
916 self._create_ipsecpolicy(
917 fmt=self.fmt,
918 name=name,
919 encapsulation_mode='unsupported', expected_res_status=400)
920 self._create_ipsecpolicy(name=name,
921 fmt=self.fmt,
922 encapsulation_mode=100,
923 expected_res_status=400)
924
925 self._create_ipsecpolicy(name=name,
926 fmt=self.fmt,
927 pfs='group9', expected_res_status=400)
928 self._create_ipsecpolicy(
929 fmt=self.fmt, name=name, pfs=-1, expected_res_status=400)
930
931 self._create_ipsecpolicy(
932 fmt=self.fmt, name=name, lifetime_units='minutes',
933 expected_res_status=400)
934
935 self._create_ipsecpolicy(fmt=self.fmt, name=name, lifetime_units=100,
936 expected_res_status=400)
937
938 self._create_ipsecpolicy(fmt=self.fmt, name=name,
939 lifetime_value=-800, expected_res_status=400)
940 self._create_ipsecpolicy(fmt=self.fmt, name=name,
941 lifetime_value='Megabytes',
942 expected_res_status=400)
943
944 def test_create_vpnservice(self, **extras):
945 """Test case to create a vpnservice."""
946 description = 'my-vpn-service'
947 expected = {'name': 'vpnservice1',
948 'description': 'my-vpn-service',
949 'admin_state_up': True,
950 'status': 'PENDING_CREATE',
951 'tenant_id': self._project_id, }
952
953 expected.update(extras)
954 with self.subnet(cidr='10.2.0.0/24') as subnet:
955 with self.router() as router:
956 expected['router_id'] = router['router']['id']
957 expected['subnet_id'] = subnet['subnet']['id']
958 name = expected['name']
959 with self.vpnservice(name=name,
960 subnet=subnet,
961 router=router,
962 description=description,
963 **extras) as vpnservice:
964 self.assertEqual({k: v for k, v in
965 vpnservice['vpnservice'].items()
966 if k in expected},
967 expected)
968
969 def test_delete_router_interface_in_use_by_vpnservice(self):
970 """Test delete router interface in use by vpn service."""
971 with self.subnet(cidr='10.2.0.0/24') as subnet:
972 with self.router() as router:
973 with self.vpnservice(subnet=subnet,
974 router=router):
975 self._router_interface_action('remove',
976 router['router']['id'],
977 subnet['subnet']['id'],
978 None,
979 expected_code=webob.exc.
980 HTTPConflict.code)
981
982 def test_delete_router_interface_not_in_use_by_vpnservice(self):
983 """Test delete router interface not in use by vpn service."""
984 with self.subnet(cidr='10.2.0.0/24') as subnet, \
985 self.router() as router1, self.router() as router2, \
986 self.vpnservice(subnet=subnet, router=router1), \
987 self.port(subnet=subnet) as port:
988 self._router_interface_action('add',
989 router2['router']['id'],
990 None,
991 port['port']['id'],
992 expected_code=webob.exc.
993 HTTPOk.code)
994 self._router_interface_action('remove',
995 router1['router']['id'],
996 subnet['subnet']['id'],
997 None,
998 expected_code=webob.exc.
999 HTTPConflict.code)
1000 self._router_interface_action('remove',
1001 router2['router']['id'],
1002 None,
1003 port['port']['id'],
1004 expected_code=webob.exc.
1005 HTTPOk.code)
1006
1007 def test_delete_external_gateway_interface_in_use_by_vpnservice(self):
1008 """Test delete external gateway interface in use by vpn service."""
1009 with self.subnet(cidr='10.2.0.0/24') as subnet:
1010 with self.router() as router:
1011 with self.subnet(cidr='11.0.0.0/24') as public_sub:
1012 self._set_net_external(
1013 public_sub['subnet']['network_id'])
1014 self._add_external_gateway_to_router(
1015 router['router']['id'],
1016 public_sub['subnet']['network_id'])
1017 with self.vpnservice(subnet=subnet,
1018 router=router):
1019 self._remove_external_gateway_from_router(
1020 router['router']['id'],
1021 public_sub['subnet']['network_id'],
1022 expected_code=webob.exc.HTTPConflict.code)
1023
1024 def test_router_update_after_ipsec_site_connection(self):
1025 """Test case to update router after vpn connection."""
1026 rname1 = "router_one"
1027 rname2 = "router_two"
1028 with self.subnet(cidr='10.2.0.0/24') as subnet:
1029 with self.router(name=rname1) as r:
1030 with self.vpnservice(subnet=subnet,
1031 router=r
1032 ) as vpnservice:
1033 self.ipsec_site_connection(
1034 name='connection1', vpnservice=vpnservice
1035 )
1036 body = self._show('routers', r['router']['id'])
1037 self.assertEqual(body['router']['name'], rname1)
1038 body = self._update('routers', r['router']['id'],
1039 {'router': {'name': rname2}})
1040 body = self._show('routers', r['router']['id'])
1041 self.assertEqual(body['router']['name'], rname2)
1042
1043 def test_update_vpnservice(self):
1044 """Test case to update a vpnservice."""
1045 name = 'new_vpnservice1'
1046 keys = [('name', name)]
1047 with self.subnet(cidr='10.2.0.0/24') as subnet, \
1048 self.router() as router:
1049 with self.vpnservice(name=name,
1050 subnet=subnet,
1051 router=router) as vpnservice:
1052 keys.append(('subnet_id',
1053 vpnservice['vpnservice']['subnet_id']))
1054 keys.append(('router_id',
1055 vpnservice['vpnservice']['router_id']))
1056 data = {'vpnservice': {'name': name}}
1057 self._set_active(vpn_models.VPNService,
1058 vpnservice['vpnservice']['id'])
1059 req = self.new_update_request(
1060 'vpnservices',
1061 data,
1062 vpnservice['vpnservice']['id'])
1063 res = self.deserialize(self.fmt,
1064 req.get_response(self.ext_api))
1065 for k, v in keys:
1066 self.assertEqual(res['vpnservice'][k], v)
1067
1068 def test_update_vpnservice_with_invalid_state(self):
1069 """Test case to update a vpnservice in invalid state ."""
1070 name = 'new_vpnservice1'
1071 keys = [('name', name)]
1072 with self.subnet(cidr='10.2.0.0/24') as subnet, \
1073 self.router() as router:
1074 with self.vpnservice(name=name,
1075 subnet=subnet,
1076 router=router) as vpnservice:
1077 keys.append(('subnet_id',
1078 vpnservice['vpnservice']['subnet_id']))
1079 keys.append(('router_id',
1080 vpnservice['vpnservice']['router_id']))
1081 data = {'vpnservice': {'name': name}}
1082 req = self.new_update_request(
1083 'vpnservices',
1084 data,
1085 vpnservice['vpnservice']['id'])
1086 res = req.get_response(self.ext_api)
1087 self.assertEqual(400, res.status_int)
1088 res = self.deserialize(self.fmt, res)
1089 self.assertIn(vpnservice['vpnservice']['id'],
1090 res['NeutronError']['message'])
1091
1092 def test_delete_vpnservice(self):
1093 """Test case to delete a vpnservice."""
1094 with self.vpnservice(name='vpnserver',
1095 do_delete=False) as vpnservice:
1096 req = self.new_delete_request('vpnservices',
1097 vpnservice['vpnservice']['id'])
1098 res = req.get_response(self.ext_api)
1099 self.assertEqual(res.status_int, 204)
1100
1101 def test_show_vpnservice(self):
1102 """Test case to show or get a vpnservice."""
1103 name = "vpnservice1"
1104 keys = [('name', name),
1105 ('description', ''),
1106 ('admin_state_up', True),
1107 ('status', 'PENDING_CREATE')]
1108 with self.vpnservice(name=name) as vpnservice:
1109 req = self.new_show_request('vpnservices',
1110 vpnservice['vpnservice']['id'],
1111 as_admin=True)
1112 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
1113 for k, v in keys:
1114 self.assertEqual(res['vpnservice'][k], v)
1115
1116 def test_list_vpnservices(self):
1117 """Test case to list all vpnservices."""
1118 name = "vpnservice_list"
1119 keys = [('name', name),
1120 ('description', ''),
1121 ('admin_state_up', True),
1122 ('status', 'PENDING_CREATE')]
1123 with self.vpnservice(name=name) as vpnservice:
1124 keys.append(('subnet_id', vpnservice['vpnservice']['subnet_id']))
1125 keys.append(('router_id', vpnservice['vpnservice']['router_id']))
1126 req = self.new_list_request('vpnservices', as_admin=True)
1127 res = self.deserialize(self.fmt, req.get_response(self.ext_api))
1128 self.assertEqual(len(res), 1)
1129 for k, v in keys:
1130 self.assertEqual(res['vpnservices'][0][k], v)
1131
1132 def test_list_vpnservices_with_sort_emulated(self):
1133 """Test case to list all vpnservices with sorting."""
1134 with self.subnet() as subnet:
1135 with self.router() as router:
1136 with self.vpnservice(name='vpnservice1',
1137 subnet=subnet,
1138 router=router,
1139 external_subnet_cidr='192.168.10.0/24'
1140 ) as vpnservice1, \
1141 self.vpnservice(name='vpnservice2',
1142 subnet=subnet,
1143 router=router,
1144 plug_subnet=False,
1145 external_router=False,
1146 external_subnet_cidr='192.168.11.0/24'
1147 ) as vpnservice2, \
1148 self.vpnservice(name='vpnservice3',
1149 subnet=subnet,
1150 router=router,
1151 plug_subnet=False,
1152 external_router=False,
1153 external_subnet_cidr='192.168.13.0/24'
1154 ) as vpnservice3:
1155 self._test_list_with_sort('vpnservice', (vpnservice3,
1156 vpnservice2,
1157 vpnservice1),
1158 [('name', 'desc')],
1159 as_admin=True)
1160
1161 def test_list_vpnservice_with_pagination_emulated(self):
1162 """Test case to list all vpnservices with pagination."""
1163 with self.subnet() as subnet:
1164 with self.router() as router:
1165 with self.vpnservice(name='vpnservice1',
1166 subnet=subnet,
1167 router=router,
1168 external_subnet_cidr='192.168.10.0/24'
1169 ) as vpnservice1, \
1170 self.vpnservice(name='vpnservice2',
1171 subnet=subnet,
1172 router=router,
1173 plug_subnet=False,
1174 external_subnet_cidr='192.168.20.0/24',
1175 external_router=False
1176 ) as vpnservice2, \
1177 self.vpnservice(name='vpnservice3',
1178 subnet=subnet,
1179 router=router,
1180 plug_subnet=False,
1181 external_subnet_cidr='192.168.30.0/24',
1182 external_router=False
1183 ) as vpnservice3:
1184 self._test_list_with_pagination('vpnservice',
1185 (vpnservice1,
1186 vpnservice2,
1187 vpnservice3),
1188 ('name', 'asc'), 2, 2,
1189 as_admin=True)
1190
1191 def test_list_vpnservice_with_pagination_reverse_emulated(self):
1192 """Test case to list all vpnservices with reverse pagination."""
1193 with self.subnet() as subnet:
1194 with self.router() as router:
1195 with self.vpnservice(name='vpnservice1',
1196 subnet=subnet,
1197 router=router,
1198 external_subnet_cidr='192.168.10.0/24'
1199 ) as vpnservice1, \
1200 self.vpnservice(name='vpnservice2',
1201 subnet=subnet,
1202 router=router,
1203 plug_subnet=False,
1204 external_subnet_cidr='192.168.11.0/24',
1205 external_router=False
1206 ) as vpnservice2, \
1207 self.vpnservice(name='vpnservice3',
1208 subnet=subnet,
1209 router=router,
1210 plug_subnet=False,
1211 external_subnet_cidr='192.168.12.0/24',
1212 external_router=False
1213 ) as vpnservice3:
1214 self._test_list_with_pagination_reverse('vpnservice',
1215 (vpnservice1,
1216 vpnservice2,
1217 vpnservice3),
1218 ('name', 'asc'),
1219 2, 2,
1220 as_admin=True)
1221
1222 def test_create_ipsec_site_connection_with_invalid_values(self):
1223 """Test case to create an ipsec_site_connection with invalid values."""
1224 name = 'connection1'
1225 self._create_ipsec_site_connection(
1226 fmt=self.fmt,
1227 name=name, peer_cidrs='myname', expected_status_int=400)
1228 self._create_ipsec_site_connection(
1229 fmt=self.fmt,
1230 name=name, mtu=-100, expected_status_int=400)
1231 self._create_ipsec_site_connection(
1232 fmt=self.fmt,
1233 name=name, dpd_action='unsupported', expected_status_int=400)
1234 self._create_ipsec_site_connection(
1235 fmt=self.fmt,
1236 name=name, dpd_interval=-1, expected_status_int=400)
1237 self._create_ipsec_site_connection(
1238 fmt=self.fmt,
1239 name=name, dpd_timeout=-200, expected_status_int=400)
1240 self._create_ipsec_site_connection(
1241 fmt=self.fmt,
1242 name=name, initiator='unsupported', expected_status_int=400)
1243
1244 def _test_create_ipsec_site_connection(self, key_overrides=None,
1245 setup_overrides=None,
1246 expected_status_int=200):
1247 """Create ipsec_site_connection and check results."""
1248 params = {'ikename': 'ikepolicy1',
1249 'ipsecname': 'ipsecpolicy1',
1250 'vpnsname': 'vpnservice1',
1251 'subnet_cidr': '10.2.0.0/24',
1252 'subnet_version': 4}
1253 if setup_overrides is not None:
1254 params.update(setup_overrides)
1255 keys = {'name': 'connection1',
1256 'description': 'my-ipsec-connection',
1257 'peer_address': '192.168.1.10',
1258 'peer_id': '192.168.1.10',
1259 'peer_cidrs': ['192.168.2.0/24', '192.168.3.0/24'],
1260 'initiator': 'bi-directional',
1261 'mtu': 1500,
1262 'tenant_id': self._project_id,
1263 'psk': 'abcd',
1264 'status': 'PENDING_CREATE',
1265 'admin_state_up': True}
1266 if key_overrides is not None:
1267 keys.update(key_overrides)
1268 dpd = {'action': 'hold',
1269 'interval': 40,
1270 'timeout': 120}
1271 with self.ikepolicy(name=params['ikename']) as ikepolicy, \
1272 self.ipsecpolicy(name=params['ipsecname']) as ipsecpolicy, \
1273 self.subnet(cidr=params['subnet_cidr'],
1274 ip_version=params['subnet_version']) as subnet, \
1275 self.router() as router:
1276 with self.vpnservice(name=params['vpnsname'], subnet=subnet,
1277 router=router) as vpnservice1:
1278 keys['ikepolicy_id'] = ikepolicy['ikepolicy']['id']
1279 keys['ipsecpolicy_id'] = ipsecpolicy['ipsecpolicy']['id']
1280 keys['vpnservice_id'] = vpnservice1['vpnservice']['id']
1281 try:
1282 with self.ipsec_site_connection(
1283 self.fmt,
1284 keys['name'],
1285 keys['peer_address'],
1286 keys['peer_id'],
1287 keys['peer_cidrs'],
1288 keys['mtu'],
1289 keys['psk'],
1290 keys['initiator'],
1291 dpd['action'],
1292 dpd['interval'],
1293 dpd['timeout'],
1294 vpnservice1,
1295 ikepolicy,
1296 ipsecpolicy,
1297 keys['admin_state_up'],
1298 description=keys['description']
1299 ) as ipsec_site_connection:
1300 if expected_status_int != 200:
1301 self.fail("Expected failure on create")
1302 self._check_ipsec_site_connection(
1303 ipsec_site_connection['ipsec_site_connection'],
1304 keys,
1305 dpd)
1306 except webob.exc.HTTPClientError as ce:
1307 self.assertEqual(ce.code, expected_status_int)
1308 self._delete('subnets', subnet['subnet']['id'])
1309
1310 def test_create_ipsec_site_connection(self, **extras):
1311 """Test case to create an ipsec_site_connection."""
1312 self._test_create_ipsec_site_connection(key_overrides=extras)
1313
1314 def test_delete_ipsec_site_connection(self):
1315 """Test case to delete a ipsec_site_connection."""
1316 with self.ipsec_site_connection(
1317 do_delete=False) as ipsec_site_connection:
1318 req = self.new_delete_request(
1319 'ipsec-site-connections',
1320 ipsec_site_connection['ipsec_site_connection']['id']
1321 )
1322 res = req.get_response(self.ext_api)
1323 self.assertEqual(res.status_int, 204)
1324
1325 def test_update_ipsec_site_connection(self):
1326 """Test case for valid updates to IPSec site connection."""
1327 dpd = {'action': 'hold',
1328 'interval': 40,
1329 'timeout': 120}
1330 self._test_update_ipsec_site_connection(update={'dpd': dpd})
1331 self._test_update_ipsec_site_connection(update={'mtu': 2000})
1332 ipv6_settings = {
1333 'peer_address': 'fe80::c0a8:10a',
1334 'peer_id': 'fe80::c0a8:10a',
1335 'peer_cidrs': ['fe80::c0a8:200/120', 'fe80::c0a8:300/120'],
1336 'subnet_cidr': 'fe80::a02:0/120',
1337 'subnet_version': 6}
1338 self._test_update_ipsec_site_connection(update={'mtu': 2000},
1339 overrides=ipv6_settings)
1340
1341 def test_update_ipsec_site_connection_with_invalid_state(self):
1342 """Test updating an ipsec_site_connection in invalid state."""
1343 self._test_update_ipsec_site_connection(
1344 overrides={'make_active': False},
1345 expected_status_int=400)
1346
1347 def test_update_ipsec_site_connection_peer_cidrs(self):
1348 """Test updating an ipsec_site_connection for peer_cidrs."""
1349 new_peers = {'peer_cidrs': ['192.168.4.0/24',
1350 '192.168.5.0/24']}
1351 self._test_update_ipsec_site_connection(
1352 update=new_peers)
1353
1354 def _test_update_ipsec_site_connection(self,
1355 update={'name': 'new name'},
1356 overrides=None,
1357 expected_status_int=200):
1358 """Creates and then updates ipsec_site_connection."""
1359 keys = {'name': 'new_ipsec_site_connection',
1360 'ikename': 'ikepolicy1',
1361 'ipsecname': 'ipsecpolicy1',
1362 'vpnsname': 'vpnservice1',
1363 'description': 'my-ipsec-connection',
1364 'peer_address': '192.168.1.10',
1365 'peer_id': '192.168.1.10',
1366 'peer_cidrs': ['192.168.2.0/24', '192.168.3.0/24'],
1367 'initiator': 'bi-directional',
1368 'mtu': 1500,
1369 'tenant_id': self._project_id,
1370 'psk': 'abcd',
1371 'status': 'ACTIVE',
1372 'admin_state_up': True,
1373 'action': 'hold',
1374 'interval': 40,
1375 'timeout': 120,
1376 'subnet_cidr': '10.2.0.0/24',
1377 'subnet_version': 4,
1378 'make_active': True}
1379 if overrides is not None:
1380 keys.update(overrides)
1381
1382 with self.ikepolicy(name=keys['ikename']) as ikepolicy, \
1383 self.ipsecpolicy(name=keys['ipsecname']) as ipsecpolicy, \
1384 self.subnet(cidr=keys['subnet_cidr'],
1385 ip_version=keys['subnet_version']) as subnet, \
1386 self.router() as router:
1387 with self.vpnservice(name=keys['vpnsname'], subnet=subnet,
1388 router=router) as vpnservice1:
1389 ext_gw = router['router']['external_gateway_info']
1390 if ext_gw:
1391 self._create_subnet(self.fmt,
1392 net_id=ext_gw['network_id'],
1393 ip_version=6, cidr='2001:db8::/32')
1394 keys['vpnservice_id'] = vpnservice1['vpnservice']['id']
1395 keys['ikepolicy_id'] = ikepolicy['ikepolicy']['id']
1396 keys['ipsecpolicy_id'] = ipsecpolicy['ipsecpolicy']['id']
1397 with self.ipsec_site_connection(
1398 self.fmt,
1399 keys['name'],
1400 keys['peer_address'],
1401 keys['peer_id'],
1402 keys['peer_cidrs'],
1403 keys['mtu'],
1404 keys['psk'],
1405 keys['initiator'],
1406 keys['action'],
1407 keys['interval'],
1408 keys['timeout'],
1409 vpnservice1,
1410 ikepolicy,
1411 ipsecpolicy,
1412 keys['admin_state_up'],
1413 description=keys['description']
1414 ) as ipsec_site_connection:
1415 data = {'ipsec_site_connection': update}
1416 if keys.get('make_active', None):
1417 self._set_active(
1418 vpn_models.IPsecSiteConnection,
1419 (ipsec_site_connection['ipsec_site_connection']
1420 ['id']))
1421 req = self.new_update_request(
1422 'ipsec-site-connections',
1423 data,
1424 ipsec_site_connection['ipsec_site_connection']['id'])
1425 res = req.get_response(self.ext_api)
1426 self.assertEqual(expected_status_int, res.status_int)
1427 if expected_status_int == 200:
1428 res_dict = self.deserialize(self.fmt, res)
1429 actual = res_dict['ipsec_site_connection']
1430 for k, v in update.items():
1431 # Sort lists before checking equality
1432 if isinstance(actual[k], list):
1433 self.assertEqual(v, sorted(actual[k]))
1434 else:
1435 self.assertEqual(v, actual[k])
1436 self._delete('networks', subnet['subnet']['network_id'])
1437
1438 def test_show_ipsec_site_connection(self):
1439 """Test case to show a ipsec_site_connection."""
1440 ikename = "ikepolicy1"
1441 ipsecname = "ipsecpolicy1"
1442 vpnsname = "vpnservice1"
1443 name = "connection1"
1444 description = "my-ipsec-connection"
1445 keys = {'name': name,
1446 'description': "my-ipsec-connection",
1447 'peer_address': '192.168.1.10',
1448 'peer_id': '192.168.1.10',
1449 'peer_cidrs': ['192.168.2.0/24', '192.168.3.0/24'],
1450 'initiator': 'bi-directional',
1451 'mtu': 1500,
1452 'tenant_id': self._project_id,
1453 'psk': 'abcd',
1454 'status': 'PENDING_CREATE',
1455 'admin_state_up': True}
1456 dpd = {'action': 'hold',
1457 'interval': 40,
1458 'timeout': 120}
1459 with self.ikepolicy(name=ikename) as ikepolicy, \
1460 self.ipsecpolicy(name=ipsecname) as ipsecpolicy, \
1461 self.subnet() as subnet, \
1462 self.router() as router:
1463 with self.vpnservice(name=vpnsname, subnet=subnet,
1464 router=router) as vpnservice1:
1465 keys['ikepolicy_id'] = ikepolicy['ikepolicy']['id']
1466 keys['ipsecpolicy_id'] = ipsecpolicy['ipsecpolicy']['id']
1467 keys['vpnservice_id'] = vpnservice1['vpnservice']['id']
1468 with self.ipsec_site_connection(
1469 self.fmt,
1470 name,
1471 keys['peer_address'],
1472 keys['peer_id'],
1473 keys['peer_cidrs'],
1474 keys['mtu'],
1475 keys['psk'],
1476 keys['initiator'],
1477 dpd['action'],
1478 dpd['interval'],
1479 dpd['timeout'],
1480 vpnservice1,
1481 ikepolicy,
1482 ipsecpolicy,
1483 keys['admin_state_up'],
1484 description=description,
1485 ) as ipsec_site_connection:
1486
1487 req = self.new_show_request(
1488 'ipsec-site-connections',
1489 ipsec_site_connection[
1490 'ipsec_site_connection']['id'],
1491 fmt=self.fmt,
1492 as_admin=True
1493 )
1494 res = self.deserialize(
1495 self.fmt,
1496 req.get_response(self.ext_api)
1497 )
1498
1499 self._check_ipsec_site_connection(
1500 res['ipsec_site_connection'],
1501 keys,
1502 dpd)
1503
1504 def test_list_ipsec_site_connections_with_sort_emulated(self):
1505 """Test case to list all ipsec_site_connections with sort."""
1506 with self.subnet(cidr='10.2.0.0/24') as subnet:
1507 with self.router() as router:
1508 with self.vpnservice(subnet=subnet,
1509 router=router
1510 ) as vpnservice:
1511 with self.ipsec_site_connection(name='connection1',
1512 vpnservice=vpnservice
1513 ) as conn1, \
1514 self.ipsec_site_connection(name='connection2',
1515 vpnservice=vpnservice
1516 ) as conn2, \
1517 self.ipsec_site_connection(name='connection3',
1518 vpnservice=vpnservice
1519 ) as conn3:
1520 self._test_list_with_sort('ipsec-site-connection',
1521 (conn3, conn2, conn1),
1522 [('name', 'desc')],
1523 as_admin=True)
1524
1525 def test_list_ipsec_site_connections_with_pagination_emulated(self):
1526 """Test case to list all ipsec_site_connections with pagination."""
1527 with self.subnet(cidr='10.2.0.0/24') as subnet:
1528 with self.router() as router:
1529 with self.vpnservice(subnet=subnet,
1530 router=router
1531 ) as vpnservice:
1532 with self.ipsec_site_connection(
1533 name='ipsec_site_connection1',
1534 vpnservice=vpnservice) as conn1, \
1535 self.ipsec_site_connection(
1536 name='ipsec_site_connection1',
1537 vpnservice=vpnservice) as conn2, \
1538 self.ipsec_site_connection(
1539 name='ipsec_site_connection1',
1540 vpnservice=vpnservice) as conn3:
1541 self._test_list_with_pagination(
1542 'ipsec-site-connection',
1543 (conn1, conn2, conn3),
1544 ('name', 'asc'), 2, 2,
1545 as_admin=True)
1546
1547 def test_list_ipsec_site_conns_with_pagination_reverse_emulated(self):
1548 """Test to list all ipsec_site_connections with reverse pagination."""
1549 with self.subnet(cidr='10.2.0.0/24') as subnet:
1550 with self.router() as router:
1551 with self.vpnservice(subnet=subnet,
1552 router=router
1553 ) as vpnservice:
1554 with self.ipsec_site_connection(name='connection1',
1555 vpnservice=vpnservice
1556 ) as conn1, \
1557 self.ipsec_site_connection(name='connection2',
1558 vpnservice=vpnservice
1559 ) as conn2, \
1560 self.ipsec_site_connection(name='connection3',
1561 vpnservice=vpnservice
1562 ) as conn3:
1563 self._test_list_with_pagination_reverse(
1564 'ipsec-site-connection',
1565 (conn1, conn2, conn3),
1566 ('name', 'asc'), 2, 2,
1567 as_admin=True
1568 )
1569
1570 def test_create_vpn(self):
1571 """Test case to create a vpn."""
1572 vpns_name = "vpnservice1"
1573 ike_name = "ikepolicy1"
1574 ipsec_name = "ipsecpolicy1"
1575 name1 = "ipsec_site_connection1"
1576 with self.ikepolicy(name=ike_name) as ikepolicy, \
1577 self.ipsecpolicy(name=ipsec_name) as ipsecpolicy, \
1578 self.vpnservice(name=vpns_name) as vpnservice:
1579 vpnservice_id = vpnservice['vpnservice']['id']
1580 ikepolicy_id = ikepolicy['ikepolicy']['id']
1581 ipsecpolicy_id = ipsecpolicy['ipsecpolicy']['id']
1582 with self.ipsec_site_connection(
1583 self.fmt,
1584 name1,
1585 '192.168.1.10',
1586 '192.168.1.10',
1587 ['192.168.2.0/24',
1588 '192.168.3.0/24'],
1589 1500,
1590 'abcdef',
1591 'bi-directional',
1592 'hold',
1593 30,
1594 120,
1595 vpnservice,
1596 ikepolicy,
1597 ipsecpolicy,
1598 True
1599 ) as vpnconn1:
1600
1601 vpnservice_req = self.new_show_request(
1602 'vpnservices',
1603 vpnservice_id,
1604 fmt=self.fmt,
1605 as_admin=True)
1606 vpnservice_updated = self.deserialize(
1607 self.fmt,
1608 vpnservice_req.get_response(self.ext_api)
1609 )
1610 self.assertEqual(
1611 vpnservice_updated['vpnservice']['id'],
1612 vpnconn1['ipsec_site_connection']['vpnservice_id']
1613 )
1614 ikepolicy_req = self.new_show_request('ikepolicies',
1615 ikepolicy_id,
1616 fmt=self.fmt,
1617 as_admin=True)
1618 ikepolicy_res = self.deserialize(
1619 self.fmt,
1620 ikepolicy_req.get_response(self.ext_api)
1621 )
1622 self.assertEqual(
1623 ikepolicy_res['ikepolicy']['id'],
1624 vpnconn1['ipsec_site_connection']['ikepolicy_id'])
1625 ipsecpolicy_req = self.new_show_request(
1626 'ipsecpolicies',
1627 ipsecpolicy_id,
1628 fmt=self.fmt,
1629 as_admin=True)
1630 ipsecpolicy_res = self.deserialize(
1631 self.fmt,
1632 ipsecpolicy_req.get_response(self.ext_api)
1633 )
1634 self.assertEqual(
1635 ipsecpolicy_res['ipsecpolicy']['id'],
1636 vpnconn1['ipsec_site_connection']['ipsecpolicy_id']
1637 )
1638
1639 def test_delete_ikepolicy_inuse(self):
1640 """Test case to delete an ikepolicy, that is in use."""
1641 vpns_name = "vpnservice1"
1642 ike_name = "ikepolicy1"
1643 ipsec_name = "ipsecpolicy1"
1644 name1 = "ipsec_site_connection1"
1645 with self.ikepolicy(name=ike_name) as ikepolicy:
1646 with self.ipsecpolicy(name=ipsec_name) as ipsecpolicy:
1647 with self.vpnservice(name=vpns_name) as vpnservice:
1648 with self.ipsec_site_connection(
1649 self.fmt,
1650 name1,
1651 '192.168.1.10',
1652 '192.168.1.10',
1653 ['192.168.2.0/24',
1654 '192.168.3.0/24'],
1655 1500,
1656 'abcdef',
1657 'bi-directional',
1658 'hold',
1659 30,
1660 120,
1661 vpnservice,
1662 ikepolicy,
1663 ipsecpolicy,
1664 True
1665 ):
1666 delete_req = self.new_delete_request(
1667 'ikepolicies',
1668 ikepolicy['ikepolicy']['id']
1669 )
1670 delete_res = delete_req.get_response(self.ext_api)
1671 self.assertEqual(409, delete_res.status_int)
1672
1673 def test_delete_ipsecpolicy_inuse(self):
1674 """Test case to delete an ipsecpolicy, that is in use."""
1675 vpns_name = "vpnservice1"
1676 ike_name = "ikepolicy1"
1677 ipsec_name = "ipsecpolicy1"
1678 name1 = "ipsec_site_connection1"
1679 with self.ikepolicy(name=ike_name) as ikepolicy:
1680 with self.ipsecpolicy(name=ipsec_name) as ipsecpolicy:
1681 with self.vpnservice(name=vpns_name) as vpnservice:
1682 with self.ipsec_site_connection(
1683 self.fmt,
1684 name1,
1685 '192.168.1.10',
1686 '192.168.1.10',
1687 ['192.168.2.0/24',
1688 '192.168.3.0/24'],
1689 1500,
1690 'abcdef',
1691 'bi-directional',
1692 'hold',
1693 30,
1694 120,
1695 vpnservice,
1696 ikepolicy,
1697 ipsecpolicy,
1698 True
1699 ):
1700
1701 delete_req = self.new_delete_request(
1702 'ipsecpolicies',
1703 ipsecpolicy['ipsecpolicy']['id']
1704 )
1705 delete_res = delete_req.get_response(self.ext_api)
1706 self.assertEqual(409, delete_res.status_int)
1707
1708 def test_router_in_use_by_vpnaas(self):
1709 """Check that exception raised, if router in use by VPNaaS."""
1710 with self.subnet(cidr='10.2.0.0/24') as subnet, \
1711 self.router() as router:
1712 with self.vpnservice(subnet=subnet,
1713 router=router):
1714 self.assertRaises(l3_exception.RouterInUse,
1715 self.plugin.check_router_in_use,
1716 context.get_admin_context(),
1717 router['router']['id'])
1718
1719 def test_subnet_in_use_by_vpnaas(self):
1720 """Check that exception raised, if subnet in use by VPNaaS."""
1721 with self.subnet(cidr='10.2.0.0/24') as subnet, \
1722 self.router() as router:
1723 with self.vpnservice(subnet=subnet,
1724 router=router):
1725 self.assertRaises(vpn_exception.SubnetInUseByVPNService,
1726 self.plugin.check_subnet_in_use,
1727 context.get_admin_context(),
1728 subnet['subnet']['id'],
1729 router['router']['id'])
1730
1731 def test_check_router_has_no_vpn(self):
1732 vpn_plugin = mock.Mock()
1733 directory.add_plugin('VPN', vpn_plugin)
1734 payload = events.DBEventPayload(
1735 context=mock.ANY,
1736 states=({'id': 'foo_id'},))
1737 self.assertTrue(vpn_db.migration_callback(
1738 mock.ANY, mock.ANY, mock.ANY, payload))
1739 vpn_plugin.check_router_in_use.assert_called_once_with(
1740 mock.ANY, 'foo_id')
1741
1742
1743 # Note: Below are new database related tests that only exercise the database
1744 # instead of going through the client API. The intent here is to (eventually)
1745 # convert all the database tests to this method, for faster, more granular
1746 # tests.
1747
1748 # TODO(pcm): Put helpers in another module for sharing
1749 class NeutronResourcesMixin:
1750
1751 def create_network(self, overrides=None):
1752 """Create database entry for network."""
1753 network_info = {'network': {'name': 'my-net',
1754 'project_id': self.project_id,
1755 'admin_state_up': True,
1756 'shared': False}}
1757 if overrides:
1758 network_info['network'].update(overrides)
1759 return self.core_plugin.create_network(self.context, network_info)
1760
1761 def create_subnet(self, overrides=None):
1762 """Create database entry for subnet."""
1763 subnet_info = {'subnet': {'name': 'my-subnet',
1764 'project_id': self.project_id,
1765 'ip_version': 4,
1766 'enable_dhcp': True,
1767 'dns_nameservers': None,
1768 'host_routes': None,
1769 'allocation_pools': None}}
1770 if overrides:
1771 subnet_info['subnet'].update(overrides)
1772 return self.core_plugin.create_subnet(self.context, subnet_info)
1773
1774 def create_router(self, overrides=None, gw=None):
1775 """Create database entry for router with optional gateway."""
1776 router_info = {
1777 'router': {
1778 'name': 'my-router',
1779 'project_id': self.project_id,
1780 'admin_state_up': True,
1781 }
1782 }
1783 if overrides:
1784 router_info['router'].update(overrides)
1785 if gw:
1786 gw_info = {
1787 'external_gateway_info': {
1788 'network_id': gw['net_id'],
1789 'external_fixed_ips': [{'subnet_id': gw['subnet_id'],
1790 'ip_address': gw['ip']}],
1791 }
1792 }
1793 router_info['router'].update(gw_info)
1794 return self.l3_plugin.create_router(self.context, router_info)
1795
1796 def create_router_port_for_subnet(self, router, subnet):
1797 """Creates port on router for subnet specified."""
1798 port = {'port': {
1799 'project_id': self.project_id,
1800 'network_id': subnet['network_id'],
1801 'fixed_ips': [
1802 {'ip_address': subnet['gateway_ip'],
1803 'subnet_id': subnet['id']}
1804 ],
1805 'mac_address': lib_constants.ATTR_NOT_SPECIFIED,
1806 'admin_state_up': True,
1807 'device_id': router['id'],
1808 'device_owner': lib_constants.DEVICE_OWNER_ROUTER_INTF,
1809 'name': ''
1810 }}
1811 return self.core_plugin.create_port(self.context, port)
1812
1813 def create_basic_topology(self, create_router_port=True):
1814 """Setup networks, subnets, and a router for testing VPN."""
1815
1816 public_net = self.create_network(overrides={'name': 'public',
1817 'router:external': True})
1818 private_net = self.create_network(overrides={'name': 'private'})
1819 overrides = {'name': 'private-subnet',
1820 'cidr': '10.2.0.0/24',
1821 'gateway_ip': '10.2.0.1',
1822 'network_id': private_net['id']}
1823 private_subnet = self.create_subnet(overrides=overrides)
1824 overrides = {'name': 'public-subnet',
1825 'cidr': '192.168.100.0/24',
1826 'gateway_ip': '192.168.100.1',
1827 'allocation_pools': [{'start': '192.168.100.2',
1828 'end': '192.168.100.254'}],
1829 'network_id': public_net['id']}
1830 public_subnet = self.create_subnet(overrides=overrides)
1831 gw_info = {'net_id': public_net['id'],
1832 'subnet_id': public_subnet['id'],
1833 'ip': '192.168.100.5'}
1834 router = self.create_router(gw=gw_info)
1835 if create_router_port:
1836 self.create_router_port_for_subnet(router, private_subnet)
1837 return (private_subnet, router)
1838
1839
1840 class TestVpnDatabase(base.NeutronDbPluginV2TestCase, NeutronResourcesMixin):
1841
1842 def setUp(self):
1843 # Setup the core plugin
1844 self.plugin_str = ('neutron_vpnaas.tests.unit.db.vpn.'
1845 'test_vpn_db.TestVpnCorePlugin')
1846 super().setUp(self.plugin_str)
1847 # Get the plugins
1848 self.core_plugin = directory.get_plugin()
1849 self.l3_plugin = directory.get_plugin(nconstants.L3)
1850
1851 # Create VPN database instance
1852 self.plugin = vpn_db.VPNPluginDb()
1853 self.project_id = _uuid()
1854 self.context = context.get_admin_context()
1855
1856 def prepare_service_info(self, private_subnet, router):
1857 subnet_id = private_subnet['id'] if private_subnet else None
1858 return {'vpnservice': {'tenant_id': self.project_id,
1859 'name': 'my-service',
1860 'description': 'new service',
1861 'subnet_id': subnet_id,
1862 'router_id': router['id'],
1863 'flavor_id': None,
1864 'admin_state_up': True}}
1865
1866 def test_create_vpnservice(self):
1867 private_subnet, router = self.create_basic_topology()
1868 info = self.prepare_service_info(private_subnet, router)
1869 expected = {'admin_state_up': True,
1870 'external_v4_ip': None,
1871 'external_v6_ip': None,
1872 'status': 'PENDING_CREATE'}
1873 expected.update(info['vpnservice'])
1874 new_service = self.plugin.create_vpnservice(self.context, info)
1875 self.assertDictSupersetOf(expected, new_service)
1876
1877 def test_create_vpn_service_without_subnet(self):
1878 """Create service w/o subnet (will use endpoint groups for conn)."""
1879 private_subnet, router = self.create_basic_topology()
1880 info = self.prepare_service_info(private_subnet=None, router=router)
1881 expected = {'admin_state_up': True,
1882 'external_v4_ip': None,
1883 'external_v6_ip': None,
1884 'status': 'PENDING_CREATE'}
1885 expected.update(info['vpnservice'])
1886
1887 new_service = self.plugin.create_vpnservice(self.context, info)
1888 self.assertDictSupersetOf(expected, new_service)
1889
1890 def test_update_external_tunnel_ips(self):
1891 """Verify that external tunnel IPs can be set."""
1892 private_subnet, router = self.create_basic_topology()
1893 info = self.prepare_service_info(private_subnet, router)
1894 expected = {'admin_state_up': True,
1895 'external_v4_ip': None,
1896 'external_v6_ip': None,
1897 'status': 'PENDING_CREATE'}
1898 expected.update(info['vpnservice'])
1899 new_service = self.plugin.create_vpnservice(self.context, info)
1900 self.assertDictSupersetOf(expected, new_service)
1901
1902 external_v4_ip = '192.168.100.5'
1903 external_v6_ip = 'fd00:1000::4'
1904 expected.update({'external_v4_ip': external_v4_ip,
1905 'external_v6_ip': external_v6_ip})
1906 mod_service = self.plugin.set_external_tunnel_ips(self.context,
1907 new_service['id'],
1908 v4_ip=external_v4_ip,
1909 v6_ip=external_v6_ip)
1910 self.assertDictSupersetOf(expected, mod_service)
1911
1912 def prepare_endpoint_info(self, group_type, endpoints):
1913 return {'endpoint_group': {'tenant_id': self.project_id,
1914 'name': 'my endpoint group',
1915 'description': 'my description',
1916 'type': group_type,
1917 'endpoints': endpoints}}
1918
1919 def test_endpoint_group_create_with_cidrs(self):
1920 """Verify create endpoint group using CIDRs."""
1921 info = self.prepare_endpoint_info(constants.CIDR_ENDPOINT,
1922 ['10.10.10.0/24', '20.20.20.0/24'])
1923 expected = info['endpoint_group']
1924 new_endpoint_group = self.plugin.create_endpoint_group(self.context,
1925 info)
1926 self._compare_groups(expected, new_endpoint_group)
1927
1928 def test_endpoint_group_create_with_subnets(self):
1929 """Verify create endpoint group using subnets."""
1930 # Skip validation for subnets, as validation is checked in other tests
1931 mock.patch.object(self.l3_plugin, "get_subnet").start()
1932 private_subnet, router = self.create_basic_topology()
1933 private_net2 = self.create_network(overrides={'name': 'private2'})
1934 overrides = {'name': 'private-subnet2',
1935 'cidr': '10.1.0.0/24',
1936 'gateway_ip': '10.1.0.1',
1937 'network_id': private_net2['id']}
1938 private_subnet2 = self.create_subnet(overrides=overrides)
1939 self.create_router_port_for_subnet(router, private_subnet2)
1940
1941 info = self.prepare_endpoint_info(constants.SUBNET_ENDPOINT,
1942 [private_subnet['id'],
1943 private_subnet2['id']])
1944 expected = info['endpoint_group']
1945 new_endpoint_group = self.plugin.create_endpoint_group(self.context,
1946 info)
1947 self._compare_groups(expected, new_endpoint_group)
1948
1949 def test_endpoint_group_create_with_vlans(self):
1950 """Verify endpoint group using VLANs."""
1951 info = self.prepare_endpoint_info(constants.VLAN_ENDPOINT,
1952 ['100', '200', '300'])
1953 expected = info['endpoint_group']
1954 new_endpoint_group = self.plugin.create_endpoint_group(self.context,
1955 info)
1956 self._compare_groups(expected, new_endpoint_group)
1957
1958 def _compare_groups(self, expected_group, actual_group):
1959 # Callers may want to reuse passed dicts later
1960 expected_group = copy.deepcopy(expected_group)
1961 actual_group = copy.deepcopy(actual_group)
1962
1963 # We need to compare endpoints separately because their order is
1964 # not defined
1965 check_endpoints = 'endpoints' in expected_group
1966 expected_endpoints = set(expected_group.pop('endpoints', []))
1967 actual_endpoints = set(actual_group.pop('endpoints', []))
1968
1969 self.assertDictSupersetOf(expected_group, actual_group)
1970 if check_endpoints:
1971 self.assertEqual(expected_endpoints, actual_endpoints)
1972
1973 def helper_create_endpoint_group(self, info):
1974 """Create endpoint group database entry and verify OK."""
1975 group = info['endpoint_group']
1976 try:
1977 actual = self.plugin.create_endpoint_group(self.context, info)
1978 except db_exc.DBError as e:
1979 self.fail("Endpoint create in prep for test failed: %s" % e)
1980 self._compare_groups(group, actual)
1981 self.assertIn('id', actual)
1982 return actual['id']
1983
1984 def check_endpoint_group_entry(self, endpoint_group_id, expected_info,
1985 should_exist=True):
1986 try:
1987 endpoint_group = self.plugin.get_endpoint_group(self.context,
1988 endpoint_group_id)
1989 is_found = True
1990 except vpn_exception.VPNEndpointGroupNotFound:
1991 is_found = False
1992 except Exception as e:
1993 self.fail("Unexpected exception getting endpoint group: %s" % e)
1994
1995 if should_exist != is_found:
1996 self.fail("Endpoint group should%(expected)s exist, but "
1997 "did%(actual)s exist" %
1998 {'expected': '' if should_exist else ' not',
1999 'actual': '' if is_found else ' not'})
2000 if is_found:
2001 self._compare_groups(expected_info, endpoint_group)
2002
2003 def test_delete_endpoint_group(self):
2004 """Test that endpoint group is deleted."""
2005 info = self.prepare_endpoint_info(constants.CIDR_ENDPOINT,
2006 ['10.10.10.0/24', '20.20.20.0/24'])
2007 expected = info['endpoint_group']
2008 group_id = self.helper_create_endpoint_group(info)
2009 self.check_endpoint_group_entry(group_id, expected, should_exist=True)
2010
2011 self.plugin.delete_endpoint_group(self.context, group_id)
2012 self.check_endpoint_group_entry(group_id, expected, should_exist=False)
2013
2014 self.assertRaises(vpn_exception.VPNEndpointGroupNotFound,
2015 self.plugin.delete_endpoint_group,
2016 self.context, group_id)
2017
2018 def test_show_endpoint_group(self):
2019 """Test showing a single endpoint group."""
2020 info = self.prepare_endpoint_info(constants.CIDR_ENDPOINT,
2021 ['10.10.10.0/24', '20.20.20.0/24'])
2022 expected = info['endpoint_group']
2023 group_id = self.helper_create_endpoint_group(info)
2024 self.check_endpoint_group_entry(group_id, expected, should_exist=True)
2025
2026 actual = self.plugin.get_endpoint_group(self.context, group_id)
2027 self._compare_groups(expected, actual)
2028
2029 def test_fail_showing_non_existent_endpoint_group(self):
2030 """Test failure to show non-existent endpoint group."""
2031 self.assertRaises(vpn_exception.VPNEndpointGroupNotFound,
2032 self.plugin.get_endpoint_group,
2033 self.context, uuidutils.generate_uuid())
2034
2035 def test_list_endpoint_groups(self):
2036 """Test listing multiple endpoint groups."""
2037 # Skip validation for subnets, as validation is checked in other tests
2038 mock.patch.object(self.l3_plugin, "get_subnet").start()
2039 info1 = self.prepare_endpoint_info(constants.CIDR_ENDPOINT,
2040 ['10.10.10.0/24', '20.20.20.0/24'])
2041 expected1 = info1['endpoint_group']
2042 group_id1 = self.helper_create_endpoint_group(info1)
2043 self.check_endpoint_group_entry(group_id1, expected1,
2044 should_exist=True)
2045
2046 info2 = self.prepare_endpoint_info(constants.SUBNET_ENDPOINT,
2047 [uuidutils.generate_uuid(),
2048 uuidutils.generate_uuid()])
2049 expected2 = info2['endpoint_group']
2050 group_id2 = self.helper_create_endpoint_group(info2)
2051 self.check_endpoint_group_entry(group_id2, expected2,
2052 should_exist=True)
2053 expected1.update({'id': group_id1})
2054 expected2.update({'id': group_id2})
2055 expected_groups = [expected1, expected2]
2056 actual_groups = self.plugin.get_endpoint_groups(
2057 self.context,
2058 fields=('type', 'tenant_id', 'endpoints',
2059 'name', 'description', 'id'))
2060 for expected_group, actual_group in zip(expected_groups,
2061 actual_groups):
2062 self._compare_groups(expected_group, actual_group)
2063
2064 def test_update_endpoint_group(self):
2065 """Test updating endpoint group information."""
2066 info = self.prepare_endpoint_info(constants.CIDR_ENDPOINT,
2067 ['10.10.10.0/24', '20.20.20.0/24'])
2068 expected = info['endpoint_group']
2069 group_id = self.helper_create_endpoint_group(info)
2070 self.check_endpoint_group_entry(group_id, expected, should_exist=True)
2071
2072 group_updates = {'endpoint_group': {'name': 'new name',
2073 'description': 'new description'}}
2074 updated_group = self.plugin.update_endpoint_group(self.context,
2075 group_id,
2076 group_updates)
2077
2078 # Check what was returned, and what is stored in database
2079 self._compare_groups(group_updates['endpoint_group'], updated_group)
2080 expected.update(group_updates['endpoint_group'])
2081 self.check_endpoint_group_entry(group_id, expected,
2082 should_exist=True)
2083
2084 def test_fail_updating_non_existent_group(self):
2085 """Test fail updating a non-existent group."""
2086 group_updates = {'endpoint_group': {'name': 'new name'}}
2087 self.assertRaises(
2088 vpn_exception.VPNEndpointGroupNotFound,
2089 self.plugin.update_endpoint_group,
2090 self.context, _uuid(), group_updates)
2091
2092 def prepare_ike_policy_info(self):
2093 return {'ikepolicy': {'tenant_id': self.project_id,
2094 'name': 'ike policy',
2095 'description': 'my ike policy',
2096 'auth_algorithm': 'sha1',
2097 'encryption_algorithm': 'aes-128',
2098 'phase1_negotiation_mode': 'main',
2099 'lifetime': {'units': 'seconds', 'value': 3600},
2100 'ike_version': 'v1',
2101 'pfs': 'group5'}}
2102
2103 def test_create_ike_policy(self):
2104 """Create IKE policy with all settings specified."""
2105 info = self.prepare_ike_policy_info()
2106 expected = info['ikepolicy']
2107 new_ike_policy = self.plugin.create_ikepolicy(self.context, info)
2108 self.assertDictSupersetOf(expected, new_ike_policy)
2109
2110 def prepare_ipsec_policy_info(self):
2111 return {'ipsecpolicy': {'tenant_id': self.project_id,
2112 'name': 'ipsec policy',
2113 'description': 'my ipsec policy',
2114 'auth_algorithm': 'sha1',
2115 'encryption_algorithm': 'aes-128',
2116 'encapsulation_mode': 'tunnel',
2117 'transform_protocol': 'esp',
2118 'lifetime': {'units': 'seconds',
2119 'value': 3600},
2120 'pfs': 'group5'}}
2121
2122 def test_create_ipsec_policy(self):
2123 """Create IPsec policy with all settings specified."""
2124 info = self.prepare_ipsec_policy_info()
2125 expected = info['ipsecpolicy']
2126 new_ipsec_policy = self.plugin.create_ipsecpolicy(self.context, info)
2127 self.assertDictSupersetOf(expected, new_ipsec_policy)
2128
2129 def create_vpn_service(self, with_subnet=True):
2130 private_subnet, router = self.create_basic_topology()
2131 if not with_subnet:
2132 private_subnet = None
2133 info = self.prepare_service_info(private_subnet, router)
2134 return self.plugin.create_vpnservice(self.context, info)
2135
2136 def create_ike_policy(self):
2137 info = self.prepare_ike_policy_info()
2138 return self.plugin.create_ikepolicy(self.context, info)
2139
2140 def create_ipsec_policy(self):
2141 info = self.prepare_ipsec_policy_info()
2142 return self.plugin.create_ipsecpolicy(self.context, info)
2143
2144 def create_endpoint_group(self, group_type, endpoints):
2145 info = self.prepare_endpoint_info(group_type=group_type,
2146 endpoints=endpoints)
2147 return self.plugin.create_endpoint_group(self.context, info)
2148
2149 def prepare_connection_info(self, service_id, ike_policy_id,
2150 ipsec_policy_id, local_id=''):
2151 """Creates connection request dictionary.
2152
2153 The peer_cidrs, local_ep_group_id, and peer_ep_group_id are set to
2154 defaults. Caller must then fill in either CIDRs or endpoints, before
2155 creating a connection.
2156 """
2157
2158 return {'ipsec_site_connection': {'name': 'my connection',
2159 'description': 'my description',
2160 'peer_id': '192.168.1.10',
2161 'peer_address': '192.168.1.10',
2162 'peer_cidrs': [],
2163 'local_id': local_id,
2164 'mtu': 1500,
2165 'psk': 'shhhh!!!',
2166 'initiator': 'bi-directional',
2167 'dpd_action': 'hold',
2168 'dpd_interval': 30,
2169 'dpd_timeout': 120,
2170 'vpnservice_id': service_id,
2171 'ikepolicy_id': ike_policy_id,
2172 'ipsecpolicy_id': ipsec_policy_id,
2173 'admin_state_up': True,
2174 'tenant_id': self._project_id,
2175 'local_ep_group_id': None,
2176 'peer_ep_group_id': None}}
2177
2178 def build_expected_connection_result(self, info):
2179 """Create the expected IPsec connection dict from the request info.
2180
2181 The DPD information is stored and converted to a nested dict, instead
2182 of individual fields.
2183 """
2184
2185 expected = copy.copy(info['ipsec_site_connection'])
2186 expected['dpd'] = {'action': expected['dpd_action'],
2187 'interval': expected['dpd_interval'],
2188 'timeout': expected['dpd_timeout']}
2189 del expected['dpd_action']
2190 del expected['dpd_interval']
2191 del expected['dpd_timeout']
2192 expected['status'] = 'PENDING_CREATE'
2193 return expected
2194
2195 def prepare_for_ipsec_connection_create(self, with_subnet=True):
2196 service = self.create_vpn_service(with_subnet)
2197 ike_policy = self.create_ike_policy()
2198 ipsec_policy = self.create_ipsec_policy()
2199 return self.prepare_connection_info(service['id'],
2200 ike_policy['id'],
2201 ipsec_policy['id'])
2202
2203 def test_create_ipsec_site_connection_with_peer_cidrs(self):
2204 """Create connection using old API with peer CIDRs specified."""
2205 info = self.prepare_for_ipsec_connection_create()
2206 info['ipsec_site_connection']['peer_cidrs'] = ['10.1.0.0/24',
2207 '10.2.0.0/24']
2208 expected = self.build_expected_connection_result(info)
2209
2210 new_conn = self.plugin.create_ipsec_site_connection(self.context,
2211 info)
2212 self.assertDictSupersetOf(expected, new_conn)
2213
2214 def test_create_ipsec_site_connection_with_endpoint_groups(self):
2215 """Create connection using new API with endpoint groups."""
2216 # Skip validation, which is tested separately
2217 mock.patch.object(self.plugin, '_get_validator').start()
2218 local_net = self.create_network(overrides={'name': 'local'})
2219 overrides = {'name': 'local-subnet',
2220 'cidr': '30.0.0.0/24',
2221 'gateway_ip': '30.0.0.1',
2222 'network_id': local_net['id']}
2223 local_subnet = self.create_subnet(overrides=overrides)
2224
2225 info = self.prepare_for_ipsec_connection_create(with_subnet=False)
2226 local_ep_group = self.create_endpoint_group(
2227 group_type='subnet', endpoints=[local_subnet['id']])
2228 peer_ep_group = self.create_endpoint_group(
2229 group_type='cidr', endpoints=['20.1.0.0/24', '20.2.0.0/24'])
2230 info['ipsec_site_connection'].update(
2231 {'local_ep_group_id': local_ep_group['id'],
2232 'peer_ep_group_id': peer_ep_group['id']})
2233 expected = self.build_expected_connection_result(info)
2234
2235 new_conn = self.plugin.create_ipsec_site_connection(self.context,
2236 info)
2237 self.assertDictSupersetOf(expected, new_conn)
2238
2239 def test_fail_endpoint_group_delete_when_in_use_by_ipsec_conn(self):
2240 """Ensure endpoint group is not deleted from under IPSec connection."""
2241 # Skip validation, which is tested separately
2242 mock.patch.object(self.plugin, '_get_validator').start()
2243 local_net = self.create_network(overrides={'name': 'local'})
2244 overrides = {'name': 'local-subnet',
2245 'cidr': '30.0.0.0/24',
2246 'gateway_ip': '30.0.0.1',
2247 'network_id': local_net['id']}
2248 local_subnet = self.create_subnet(overrides=overrides)
2249
2250 info = self.prepare_for_ipsec_connection_create(with_subnet=False)
2251 local_ep_group = self.create_endpoint_group(
2252 group_type='subnet', endpoints=[local_subnet['id']])
2253 peer_ep_group = self.create_endpoint_group(
2254 group_type='cidr', endpoints=['20.1.0.0/24', '20.2.0.0/24'])
2255 info['ipsec_site_connection'].update(
2256 {'local_ep_group_id': local_ep_group['id'],
2257 'peer_ep_group_id': peer_ep_group['id']})
2258 self.plugin.create_ipsec_site_connection(self.context, info)
2259 self.assertRaises(vpn_exception.EndpointGroupInUse,
2260 self.plugin.delete_endpoint_group,
2261 self.context,
2262 local_ep_group['id'])
2263 self.assertRaises(vpn_exception.EndpointGroupInUse,
2264 self.plugin.delete_endpoint_group,
2265 self.context,
2266 peer_ep_group['id'])
2267 unused_ep_group = self.create_endpoint_group(
2268 group_type=constants.CIDR_ENDPOINT, endpoints=['30.0.0.0/24'])
2269 self.plugin.delete_endpoint_group(self.context, unused_ep_group['id'])
2270
2271 def test_fail_subnet_delete_when_in_use_by_endpoint_group(self):
2272 """Ensure don't delete subnet from under endpoint group."""
2273 # mock.patch.object(self.plugin, '_get_validator').start()
2274 local_net = self.create_network(overrides={'name': 'local'})
2275 overrides = {'name': 'local-subnet',
2276 'cidr': '30.0.0.0/24',
2277 'gateway_ip': '30.0.0.1',
2278 'network_id': local_net['id']}
2279 local_subnet = self.create_subnet(overrides=overrides)
2280 self.create_endpoint_group(group_type='subnet',
2281 endpoints=[local_subnet['id']])
2282 self.assertRaises(vpn_exception.SubnetInUseByEndpointGroup,
2283 self.plugin.check_subnet_in_use_by_endpoint_group,
2284 self.context, local_subnet['id'])
2285
2286 def test_subnet_in_use_by_ipsec_site_connection(self):
2287 mock.patch.object(self.plugin, '_get_validator').start()
2288
2289 private_subnet, router = self.create_basic_topology(
2290 create_router_port=False)
2291 self.l3_plugin.add_router_interface(
2292 self.context,
2293 router['id'],
2294 {'subnet_id': private_subnet['id']})
2295 vpn_service_info = self.prepare_service_info(private_subnet=None,
2296 router=router)
2297 vpn_service = self.plugin.create_vpnservice(self.context,
2298 vpn_service_info)
2299
2300 ike_policy = self.create_ike_policy()
2301 ipsec_policy = self.create_ipsec_policy()
2302 ipsec_site_connection = self.prepare_connection_info(
2303 vpn_service['id'],
2304 ike_policy['id'],
2305 ipsec_policy['id'])
2306
2307 local_ep_group = self.create_endpoint_group(
2308 group_type='subnet', endpoints=[private_subnet['id']])
2309 peer_ep_group = self.create_endpoint_group(
2310 group_type='cidr', endpoints=['20.1.0.0/24', '20.2.0.0/24'])
2311 ipsec_site_connection['ipsec_site_connection'].update(
2312 {'local_ep_group_id': local_ep_group['id'],
2313 'peer_ep_group_id': peer_ep_group['id']})
2314 self.plugin.create_ipsec_site_connection(self.context,
2315 ipsec_site_connection)
2316
2317 self.assertRaises(vpn_exception.SubnetInUseByIPsecSiteConnection,
2318 self.plugin.check_subnet_in_use,
2319 self.context,
2320 private_subnet['id'],
2321 router['id'])
2322
2323 def _setup_ipsec_site_connections_with_ep_groups(self, peer_cidr_lists):
2324 private_subnet, router = self.create_basic_topology()
2325 vpn_service_info = self.prepare_service_info(private_subnet=None,
2326 router=router)
2327 vpn_service = self.plugin.create_vpnservice(self.context,
2328 vpn_service_info)
2329
2330 ike_policy = self.create_ike_policy()
2331 ipsec_policy = self.create_ipsec_policy()
2332 ipsec_site_connection = self.prepare_connection_info(
2333 vpn_service['id'],
2334 ike_policy['id'],
2335 ipsec_policy['id'])
2336
2337 local_ep_group = self.create_endpoint_group(
2338 group_type='subnet', endpoints=[private_subnet['id']])
2339 for peer_cidrs in peer_cidr_lists:
2340 peer_ep_group = self.create_endpoint_group(
2341 group_type='cidr', endpoints=peer_cidrs)
2342 ipsec_site_connection['ipsec_site_connection'].update(
2343 {'local_ep_group_id': local_ep_group['id'],
2344 'peer_ep_group_id': peer_ep_group['id']})
2345 self.plugin.create_ipsec_site_connection(self.context,
2346 ipsec_site_connection)
2347 return private_subnet, router
2348
2349 def _setup_ipsec_site_connections_without_ep_groups(self, peer_cidr_lists):
2350 private_subnet, router = self.create_basic_topology()
2351 vpn_service_info = \
2352 self.prepare_service_info(private_subnet=private_subnet,
2353 router=router)
2354 vpn_service = self.plugin.create_vpnservice(self.context,
2355 vpn_service_info)
2356
2357 ike_policy = self.create_ike_policy()
2358 ipsec_policy = self.create_ipsec_policy()
2359 ipsec_site_connection = self.prepare_connection_info(
2360 vpn_service['id'],
2361 ike_policy['id'],
2362 ipsec_policy['id'])
2363
2364 for peer_cidrs in peer_cidr_lists:
2365 ipsec_site_connection['ipsec_site_connection'].update(
2366 {'peer_cidrs': peer_cidrs})
2367 self.plugin.create_ipsec_site_connection(self.context,
2368 ipsec_site_connection)
2369 return private_subnet, router
2370
2371 def _test_get_peer_cidrs_for_router(self, setup_func):
2372 mock.patch.object(self.plugin, '_get_validator').start()
2373
2374 # create 1st setup with two connections
2375 peer_cidrs = [
2376 ['20.1.0.0/24', '20.2.0.0/24'],
2377 ['20.3.0.0/24']
2378 ]
2379 private_subnet, router = setup_func(peer_cidrs)
2380
2381 # create a 2nd setup for a different router
2382 setup_func([['10.1.0.0/24', '10.2.0.0/24']])
2383
2384 returned_cidrs = self.plugin.get_peer_cidrs_for_router(self.context,
2385 router['id'])
2386 expected = ['20.1.0.0/24', '20.2.0.0/24', '20.3.0.0/24']
2387 self.assertEqual(sorted(expected), sorted(returned_cidrs))
2388
2389 def test_get_peer_cidrs_for_router_with_ep_groups(self):
2390 self._test_get_peer_cidrs_for_router(
2391 self._setup_ipsec_site_connections_with_ep_groups)
2392
2393 def test_get_peer_cidrs_for_router_without_ep_groups(self):
2394 self._test_get_peer_cidrs_for_router(
2395 self._setup_ipsec_site_connections_without_ep_groups)