| 1 |
# Copyright 2015, Cisco Systems Inc. |
| 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 |
import socket |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from neutron.db import l3_db |
| 20 |
from neutron_lib import context as n_ctx |
| 21 |
from neutron_lib import exceptions as nexception |
| 22 |
from neutron_lib.exceptions import vpn as vpn_exception |
| 23 |
from neutron_lib.plugins import constants as nconstants |
| 24 |
from neutron_lib.plugins import directory |
| 25 |
from oslo_utils import uuidutils |
| 26 |
from sqlalchemy.orm import query |
| 27 |
|
| 28 |
from neutron_vpnaas.db.vpn import vpn_validator |
| 29 |
from neutron_vpnaas.services.vpn.common import constants as v_constants |
| 30 |
from neutron_vpnaas.tests import base |
| 31 |
|
| 32 |
_uuid = uuidutils.generate_uuid |
| 33 |
|
| 34 |
FAKE_ROUTER_ID = _uuid() |
| 35 |
FAKE_ROUTER = {l3_db.EXTERNAL_GW_INFO: FAKE_ROUTER_ID} |
| 36 |
FAKE_SUBNET_ID = _uuid() |
| 37 |
IPV4 = 4 |
| 38 |
IPV6 = 6 |
| 39 |
|
| 40 |
|
| 41 |
class TestVpnValidation(base.BaseTestCase): |
| 42 |
|
| 43 |
def setUp(self): |
| 44 |
super().setUp() |
| 45 |
self.l3_plugin = mock.Mock() |
| 46 |
self.core_plugin = mock.Mock() |
| 47 |
directory.add_plugin(nconstants.CORE, self.core_plugin) |
| 48 |
directory.add_plugin(nconstants.L3, self.l3_plugin) |
| 49 |
self.context = n_ctx.Context('some_user', 'some_tenant') |
| 50 |
self.validator = vpn_validator.VpnReferenceValidator() |
| 51 |
self.router = mock.Mock() |
| 52 |
self.router.gw_port = {'fixed_ips': [{'ip_address': '10.0.0.99'}]} |
| 53 |
|
| 54 |
def test_non_public_router_for_vpn_service(self): |
| 55 |
"""Failure test of service validate, when router missing ext. I/F.""" |
| 56 |
self.l3_plugin.get_router.return_value = {} # No external gateway |
| 57 |
vpnservice = {'router_id': 123, 'subnet_id': 456} |
| 58 |
self.assertRaises(vpn_exception.RouterIsNotExternal, |
| 59 |
self.validator.validate_vpnservice, |
| 60 |
self.context, vpnservice) |
| 61 |
|
| 62 |
def test_subnet_not_connected_for_vpn_service(self): |
| 63 |
"""Failure test of service validate, when subnet not on router.""" |
| 64 |
self.l3_plugin.get_router.return_value = FAKE_ROUTER |
| 65 |
self.core_plugin.get_ports.return_value = None |
| 66 |
vpnservice = {'router_id': FAKE_ROUTER_ID, 'subnet_id': FAKE_SUBNET_ID} |
| 67 |
self.assertRaises(vpn_exception.SubnetIsNotConnectedToRouter, |
| 68 |
self.validator.validate_vpnservice, |
| 69 |
self.context, vpnservice) |
| 70 |
|
| 71 |
def test_defaults_for_ipsec_site_connections_on_create(self): |
| 72 |
"""Check that defaults are applied correctly. |
| 73 |
|
| 74 |
MTU has a default and will always be present on create. |
| 75 |
However, the DPD settings do not have a default, so |
| 76 |
database create method will assign default values for any |
| 77 |
missing. In addition, the DPD dict will be flattened |
| 78 |
for storage into the database, so we'll do it as part of |
| 79 |
assigning defaults. |
| 80 |
""" |
| 81 |
ipsec_sitecon = {} |
| 82 |
self.validator.assign_sensible_ipsec_sitecon_defaults(ipsec_sitecon) |
| 83 |
expected = { |
| 84 |
'dpd_action': 'hold', |
| 85 |
'dpd_timeout': 120, |
| 86 |
'dpd_interval': 30 |
| 87 |
} |
| 88 |
self.assertEqual(expected, ipsec_sitecon) |
| 89 |
|
| 90 |
ipsec_sitecon = {'dpd': {'interval': 50}} |
| 91 |
self.validator.assign_sensible_ipsec_sitecon_defaults(ipsec_sitecon) |
| 92 |
expected = { |
| 93 |
'dpd': {'interval': 50}, |
| 94 |
'dpd_action': 'hold', |
| 95 |
'dpd_timeout': 120, |
| 96 |
'dpd_interval': 50 |
| 97 |
} |
| 98 |
self.assertEqual(expected, ipsec_sitecon) |
| 99 |
|
| 100 |
def test_resolve_peer_address_with_ipaddress(self): |
| 101 |
ipsec_sitecon = {'peer_address': '10.0.0.9'} |
| 102 |
self.validator._validate_peer_address = mock.Mock() |
| 103 |
self.validator.resolve_peer_address(ipsec_sitecon, self.router) |
| 104 |
self.assertEqual('10.0.0.9', ipsec_sitecon['peer_address']) |
| 105 |
self.validator._validate_peer_address.assert_called_once_with( |
| 106 |
IPV4, self.router) |
| 107 |
|
| 108 |
def test_resolve_peer_address_with_fqdn(self): |
| 109 |
with mock.patch.object(socket, 'getaddrinfo') as mock_getaddr_info: |
| 110 |
mock_getaddr_info.return_value = [(2, 1, 6, '', |
| 111 |
('10.0.0.9', 0))] |
| 112 |
ipsec_sitecon = {'peer_address': 'fqdn.peer.addr'} |
| 113 |
self.validator._validate_peer_address = mock.Mock() |
| 114 |
self.validator.resolve_peer_address(ipsec_sitecon, self.router) |
| 115 |
self.assertEqual('10.0.0.9', ipsec_sitecon['peer_address']) |
| 116 |
self.validator._validate_peer_address.assert_called_once_with( |
| 117 |
IPV4, self.router) |
| 118 |
|
| 119 |
def test_resolve_peer_address_with_invalid_fqdn(self): |
| 120 |
with mock.patch.object(socket, 'getaddrinfo') as mock_getaddr_info: |
| 121 |
def getaddr_info_failer(*args, **kwargs): |
| 122 |
raise socket.gaierror() |
| 123 |
mock_getaddr_info.side_effect = getaddr_info_failer |
| 124 |
ipsec_sitecon = {'peer_address': 'fqdn.invalid'} |
| 125 |
self.assertRaises(vpn_exception.VPNPeerAddressNotResolved, |
| 126 |
self.validator.resolve_peer_address, |
| 127 |
ipsec_sitecon, self.router) |
| 128 |
|
| 129 |
def helper_validate_peer_address(self, fixed_ips, ip_version, |
| 130 |
expected_exception=False): |
| 131 |
self.router.id = FAKE_ROUTER_ID |
| 132 |
self.router.gw_port = {'fixed_ips': fixed_ips} |
| 133 |
try: |
| 134 |
self.validator._validate_peer_address(ip_version, self.router) |
| 135 |
if expected_exception: |
| 136 |
self.fail("No exception raised for invalid peer address") |
| 137 |
except vpn_exception.ExternalNetworkHasNoSubnet: |
| 138 |
if not expected_exception: |
| 139 |
self.fail("exception for valid peer address raised") |
| 140 |
|
| 141 |
def test_validate_peer_address(self): |
| 142 |
# validate ipv4 peer_address with ipv4 gateway |
| 143 |
fixed_ips = [{'ip_address': '10.0.0.99'}] |
| 144 |
self.helper_validate_peer_address(fixed_ips, IPV4) |
| 145 |
|
| 146 |
# validate ipv6 peer_address with ipv6 gateway |
| 147 |
fixed_ips = [{'ip_address': '2001::1'}] |
| 148 |
self.helper_validate_peer_address(fixed_ips, IPV6) |
| 149 |
|
| 150 |
# validate ipv6 peer_address with both ipv4 and ipv6 gateways |
| 151 |
fixed_ips = [{'ip_address': '2001::1'}, {'ip_address': '10.0.0.99'}] |
| 152 |
self.helper_validate_peer_address(fixed_ips, IPV6) |
| 153 |
|
| 154 |
# validate ipv4 peer_address with both ipv4 and ipv6 gateways |
| 155 |
fixed_ips = [{'ip_address': '2001::1'}, {'ip_address': '10.0.0.99'}] |
| 156 |
self.helper_validate_peer_address(fixed_ips, IPV4) |
| 157 |
|
| 158 |
# validate ipv4 peer_address with ipv6 gateway |
| 159 |
fixed_ips = [{'ip_address': '2001::1'}] |
| 160 |
self.helper_validate_peer_address(fixed_ips, IPV4, |
| 161 |
expected_exception=True) |
| 162 |
|
| 163 |
# validate ipv6 peer_address with ipv4 gateway |
| 164 |
fixed_ips = [{'ip_address': '10.0.0.99'}] |
| 165 |
self.helper_validate_peer_address(fixed_ips, IPV6, |
| 166 |
expected_exception=True) |
| 167 |
|
| 168 |
def test_defaults_for_ipsec_site_connections_on_update(self): |
| 169 |
"""Check that defaults are used for any values not specified.""" |
| 170 |
ipsec_sitecon = {} |
| 171 |
prev_connection = {'peer_cidrs': [{'cidr': '10.0.0.0/24'}, |
| 172 |
{'cidr': '20.0.0.0/24'}], |
| 173 |
'local_ep_group_id': None, |
| 174 |
'peer_ep_group_id': None, |
| 175 |
'dpd_action': 'clear', |
| 176 |
'dpd_timeout': 500, |
| 177 |
'dpd_interval': 250} |
| 178 |
self.validator.assign_sensible_ipsec_sitecon_defaults(ipsec_sitecon, |
| 179 |
prev_connection) |
| 180 |
expected = { |
| 181 |
'peer_cidrs': ['10.0.0.0/24', '20.0.0.0/24'], |
| 182 |
'local_ep_group_id': None, |
| 183 |
'peer_ep_group_id': None, |
| 184 |
'dpd_action': 'clear', |
| 185 |
'dpd_timeout': 500, |
| 186 |
'dpd_interval': 250 |
| 187 |
} |
| 188 |
self.assertEqual(expected, ipsec_sitecon) |
| 189 |
|
| 190 |
ipsec_sitecon = {'dpd': {'timeout': 200}} |
| 191 |
local_epg_id = _uuid() |
| 192 |
peer_epg_id = _uuid() |
| 193 |
prev_connection = {'peer_cidrs': [], |
| 194 |
'local_ep_group_id': local_epg_id, |
| 195 |
'peer_ep_group_id': peer_epg_id, |
| 196 |
'dpd_action': 'clear', |
| 197 |
'dpd_timeout': 500, |
| 198 |
'dpd_interval': 100} |
| 199 |
self.validator.assign_sensible_ipsec_sitecon_defaults(ipsec_sitecon, |
| 200 |
prev_connection) |
| 201 |
expected = { |
| 202 |
'peer_cidrs': [], |
| 203 |
'local_ep_group_id': local_epg_id, |
| 204 |
'peer_ep_group_id': peer_epg_id, |
| 205 |
'dpd': {'timeout': 200}, |
| 206 |
'dpd_action': 'clear', |
| 207 |
'dpd_timeout': 200, |
| 208 |
'dpd_interval': 100 |
| 209 |
} |
| 210 |
self.assertEqual(expected, ipsec_sitecon) |
| 211 |
|
| 212 |
def test_bad_dpd_settings_on_create(self): |
| 213 |
"""Failure tests of DPD settings for IPSec conn during create.""" |
| 214 |
ipsec_sitecon = {'dpd_action': 'hold', 'dpd_interval': 100, |
| 215 |
'dpd_timeout': 100} |
| 216 |
self.assertRaises( |
| 217 |
vpn_exception.IPsecSiteConnectionDpdIntervalValueError, |
| 218 |
self.validator._check_dpd, ipsec_sitecon) |
| 219 |
ipsec_sitecon = {'dpd_action': 'hold', 'dpd_interval': 100, |
| 220 |
'dpd_timeout': 99} |
| 221 |
self.assertRaises( |
| 222 |
vpn_exception.IPsecSiteConnectionDpdIntervalValueError, |
| 223 |
self.validator._check_dpd, ipsec_sitecon) |
| 224 |
|
| 225 |
def test_bad_mtu_for_ipsec_connection(self): |
| 226 |
"""Failure test of invalid MTU values for IPSec conn create/update.""" |
| 227 |
ip_version_limits = self.validator.IP_MIN_MTU |
| 228 |
for version, limit in ip_version_limits.items(): |
| 229 |
ipsec_sitecon = {'mtu': limit - 1} |
| 230 |
self.assertRaises( |
| 231 |
vpn_exception.IPsecSiteConnectionMtuError, |
| 232 |
self.validator._check_mtu, |
| 233 |
self.context, ipsec_sitecon['mtu'], version) |
| 234 |
|
| 235 |
def test_endpoints_all_cidrs_in_endpoint_group(self): |
| 236 |
"""All endpoints in the endpoint group are valid CIDRs.""" |
| 237 |
endpoint_group = {'type': v_constants.CIDR_ENDPOINT, |
| 238 |
'endpoints': ['10.10.10.0/24', '20.20.20.0/24']} |
| 239 |
try: |
| 240 |
self.validator.validate_endpoint_group(self.context, |
| 241 |
endpoint_group) |
| 242 |
except Exception: |
| 243 |
self.fail("All CIDRs in endpoint_group should be valid") |
| 244 |
|
| 245 |
def test_endpoints_all_subnets_in_endpoint_group(self): |
| 246 |
"""All endpoints in the endpoint group are valid subnets.""" |
| 247 |
endpoint_group = {'type': v_constants.SUBNET_ENDPOINT, |
| 248 |
'endpoints': [_uuid(), _uuid()]} |
| 249 |
try: |
| 250 |
self.validator.validate_endpoint_group(self.context, |
| 251 |
endpoint_group) |
| 252 |
except Exception: |
| 253 |
self.fail("All subnets in endpoint_group should be valid") |
| 254 |
|
| 255 |
def test_mixed_endpoint_types_in_endpoint_group(self): |
| 256 |
"""Fail when mixing types of endpoints in endpoint group.""" |
| 257 |
endpoint_group = {'type': v_constants.CIDR_ENDPOINT, |
| 258 |
'endpoints': ['10.10.10.0/24', _uuid()]} |
| 259 |
self.assertRaises(vpn_exception.InvalidEndpointInEndpointGroup, |
| 260 |
self.validator.validate_endpoint_group, |
| 261 |
self.context, endpoint_group) |
| 262 |
endpoint_group = {'type': v_constants.SUBNET_ENDPOINT, |
| 263 |
'endpoints': [_uuid(), '10.10.10.0/24']} |
| 264 |
self.assertRaises(vpn_exception.InvalidEndpointInEndpointGroup, |
| 265 |
self.validator.validate_endpoint_group, |
| 266 |
self.context, endpoint_group) |
| 267 |
|
| 268 |
def test_missing_endpoints_for_endpoint_group(self): |
| 269 |
endpoint_group = {'type': v_constants.CIDR_ENDPOINT, |
| 270 |
'endpoints': []} |
| 271 |
self.assertRaises(vpn_exception.MissingEndpointForEndpointGroup, |
| 272 |
self.validator.validate_endpoint_group, |
| 273 |
self.context, endpoint_group) |
| 274 |
|
| 275 |
def test_fail_bad_cidr_in_endpoint_group(self): |
| 276 |
"""Testing catches bad CIDR. |
| 277 |
|
| 278 |
Just check one case, as CIDR validator used has good test coverage. |
| 279 |
""" |
| 280 |
endpoint_group = {'type': v_constants.CIDR_ENDPOINT, |
| 281 |
'endpoints': ['10.10.10.10/24', '20.20.20.1']} |
| 282 |
self.assertRaises(vpn_exception.InvalidEndpointInEndpointGroup, |
| 283 |
self.validator.validate_endpoint_group, |
| 284 |
self.context, endpoint_group) |
| 285 |
|
| 286 |
def test_unknown_subnet_in_endpoint_group(self): |
| 287 |
subnet_id = _uuid() |
| 288 |
self.core_plugin.get_subnet.side_effect = nexception.SubnetNotFound( |
| 289 |
subnet_id=subnet_id) |
| 290 |
endpoint_group = {'type': v_constants.SUBNET_ENDPOINT, |
| 291 |
'endpoints': [subnet_id]} |
| 292 |
self.assertRaises(vpn_exception.NonExistingSubnetInEndpointGroup, |
| 293 |
self.validator.validate_endpoint_group, |
| 294 |
self.context, endpoint_group) |
| 295 |
|
| 296 |
def test_fail_on_endpoints_not_unique(self): |
| 297 |
ep_cidrs = ["10.10.10.0/24", "20.20.20.0/24", "10.10.10.0/24"] |
| 298 |
endpoint_group = {'type': v_constants.CIDR_ENDPOINT, |
| 299 |
'endpoints': ep_cidrs} |
| 300 |
self.assertRaises(vpn_exception.InvalidEndpointInEndpointGroup, |
| 301 |
self.validator.validate_endpoint_group, |
| 302 |
self.context, endpoint_group) |
| 303 |
|
| 304 |
def test_fail_subnets_not_on_same_router_for_endpoint_group(self): |
| 305 |
"""Detect when local endpoints not on the same router.""" |
| 306 |
subnet1 = {'id': _uuid(), 'ip_version': 4} |
| 307 |
subnet2 = {'id': _uuid(), 'ip_version': 4} |
| 308 |
router = _uuid() |
| 309 |
multiple_subnets = [subnet1, subnet2] |
| 310 |
port_mock = mock.patch.object(self.core_plugin, "get_ports").start() |
| 311 |
port_mock.side_effect = ['dummy info', None] |
| 312 |
self.assertRaises(vpn_exception.SubnetIsNotConnectedToRouter, |
| 313 |
self.validator._check_local_subnets_on_router, |
| 314 |
self.context, router, multiple_subnets) |
| 315 |
|
| 316 |
def test_ipsec_conn_local_endpoints_same_ip_version(self): |
| 317 |
"""Check local endpoint subnets all have same IP version.""" |
| 318 |
endpoint_group_id = _uuid() |
| 319 |
subnet1 = {'ip_version': 4} |
| 320 |
subnet2 = {'ip_version': 4} |
| 321 |
single_subnet = [subnet1] |
| 322 |
version = self.validator._check_local_endpoint_ip_versions( |
| 323 |
endpoint_group_id, single_subnet) |
| 324 |
self.assertEqual(4, version) |
| 325 |
multiple_subnets = [subnet1, subnet2] |
| 326 |
version = self.validator._check_local_endpoint_ip_versions( |
| 327 |
endpoint_group_id, multiple_subnets) |
| 328 |
self.assertEqual(4, version) |
| 329 |
|
| 330 |
def test_fail_ipsec_conn_local_endpoints_mixed_ip_version(self): |
| 331 |
"""Fail when mixed IP versions in local endpoints.""" |
| 332 |
endpoint_group_id = _uuid() |
| 333 |
subnet1 = {'ip_version': 6} |
| 334 |
subnet2 = {'ip_version': 4} |
| 335 |
mixed_subnets = [subnet1, subnet2] |
| 336 |
self.assertRaises(vpn_exception.MixedIPVersionsForIPSecEndpoints, |
| 337 |
self.validator._check_local_endpoint_ip_versions, |
| 338 |
endpoint_group_id, mixed_subnets) |
| 339 |
|
| 340 |
def test_ipsec_conn_peer_endpoints_same_ip_version(self): |
| 341 |
"""Check all CIDRs have the same IP version.""" |
| 342 |
endpoint_group_id = _uuid() |
| 343 |
one_cidr = ['2002:0a00::/48'] |
| 344 |
version = self.validator._check_peer_endpoint_ip_versions( |
| 345 |
endpoint_group_id, one_cidr) |
| 346 |
self.assertEqual(6, version) |
| 347 |
multiple_cidr = ['10.10.10.0/24', '20.20.20.0/24'] |
| 348 |
version = self.validator._check_peer_endpoint_ip_versions( |
| 349 |
endpoint_group_id, multiple_cidr) |
| 350 |
self.assertEqual(4, version) |
| 351 |
|
| 352 |
def test_fail_ipsec_conn_peer_endpoints_mixed_ip_version(self): |
| 353 |
"""Fail when mixed IP versions in peer endpoints.""" |
| 354 |
endpoint_group_id = _uuid() |
| 355 |
mixed_cidrs = ['10.10.10.0/24', '2002:1400::/48'] |
| 356 |
self.assertRaises(vpn_exception.MixedIPVersionsForIPSecEndpoints, |
| 357 |
self.validator._check_peer_endpoint_ip_versions, |
| 358 |
endpoint_group_id, mixed_cidrs) |
| 359 |
|
| 360 |
def test_fail_ipsec_conn_locals_and_peers_different_ip_version(self): |
| 361 |
"""Ensure catch when local and peer IP versions are not the same.""" |
| 362 |
self.assertRaises(vpn_exception.MixedIPVersionsForIPSecConnection, |
| 363 |
self.validator._validate_compatible_ip_versions, |
| 364 |
4, 6) |
| 365 |
|
| 366 |
def test_fail_ipsec_conn_no_subnet_requiring_endpoint_groups(self): |
| 367 |
"""When no subnet, connection must use endpoints. |
| 368 |
|
| 369 |
This means both endpoint groups must be present, and peer_cidrs |
| 370 |
cannot be used. |
| 371 |
""" |
| 372 |
subnet = None |
| 373 |
ipsec_sitecon = {'peer_cidrs': ['10.0.0.0/24'], |
| 374 |
'local_ep_group_id': 'local-epg-id', |
| 375 |
'peer_ep_group_id': 'peer-epg-id'} |
| 376 |
self.assertRaises(vpn_exception.PeerCidrsInvalid, |
| 377 |
self.validator.validate_ipsec_conn_optional_args, |
| 378 |
ipsec_sitecon, subnet) |
| 379 |
|
| 380 |
ipsec_sitecon = {'peer_cidrs': [], |
| 381 |
'local_ep_group_id': None, |
| 382 |
'peer_ep_group_id': 'peer-epg-id'} |
| 383 |
self.assertRaises(vpn_exception.MissingRequiredEndpointGroup, |
| 384 |
self.validator.validate_ipsec_conn_optional_args, |
| 385 |
ipsec_sitecon, subnet) |
| 386 |
|
| 387 |
ipsec_sitecon = {'peer_cidrs': [], |
| 388 |
'local_ep_group_id': 'local-epg-id', |
| 389 |
'peer_ep_group_id': None} |
| 390 |
self.assertRaises(vpn_exception.MissingRequiredEndpointGroup, |
| 391 |
self.validator.validate_ipsec_conn_optional_args, |
| 392 |
ipsec_sitecon, subnet) |
| 393 |
|
| 394 |
ipsec_sitecon = {'peer_cidrs': [], |
| 395 |
'local_ep_group_id': None, |
| 396 |
'peer_ep_group_id': None} |
| 397 |
self.assertRaises(vpn_exception.MissingRequiredEndpointGroup, |
| 398 |
self.validator.validate_ipsec_conn_optional_args, |
| 399 |
ipsec_sitecon, subnet) |
| 400 |
|
| 401 |
def test_fail_ipsec_conn_subnet_requiring_peer_cidrs(self): |
| 402 |
"""When legacy mode, no endpoint groups. |
| 403 |
|
| 404 |
This means neither endpoint group can be specified, and the peer_cidrs |
| 405 |
must be present. |
| 406 |
""" |
| 407 |
subnet = {'id': FAKE_SUBNET_ID} |
| 408 |
ipsec_sitecon = {'peer_cidrs': [], |
| 409 |
'local_ep_group_id': None, |
| 410 |
'peer_ep_group_id': None} |
| 411 |
self.assertRaises(vpn_exception.MissingPeerCidrs, |
| 412 |
self.validator.validate_ipsec_conn_optional_args, |
| 413 |
ipsec_sitecon, subnet) |
| 414 |
|
| 415 |
ipsec_sitecon = {'peer_cidrs': ['10.0.0.0/24'], |
| 416 |
'local_ep_group_id': 'local-epg-id', |
| 417 |
'peer_ep_group_id': None} |
| 418 |
self.assertRaises(vpn_exception.InvalidEndpointGroup, |
| 419 |
self.validator.validate_ipsec_conn_optional_args, |
| 420 |
ipsec_sitecon, subnet) |
| 421 |
|
| 422 |
ipsec_sitecon = {'peer_cidrs': ['10.0.0.0/24'], |
| 423 |
'local_ep_group_id': None, |
| 424 |
'peer_ep_group_id': 'peer-epg-id'} |
| 425 |
self.assertRaises(vpn_exception.InvalidEndpointGroup, |
| 426 |
self.validator.validate_ipsec_conn_optional_args, |
| 427 |
ipsec_sitecon, subnet) |
| 428 |
|
| 429 |
ipsec_sitecon = {'peer_cidrs': ['10.0.0.0/24'], |
| 430 |
'local_ep_group_id': 'local-epg-id', |
| 431 |
'peer_ep_group_id': 'peer-epg-id'} |
| 432 |
self.assertRaises(vpn_exception.InvalidEndpointGroup, |
| 433 |
self.validator.validate_ipsec_conn_optional_args, |
| 434 |
ipsec_sitecon, subnet) |
| 435 |
|
| 436 |
def test_ipsec_conn_get_local_subnets(self): |
| 437 |
subnet1 = _uuid() |
| 438 |
subnet2 = _uuid() |
| 439 |
expected_subnets = [subnet1, subnet2] |
| 440 |
local_epg = {'id': _uuid(), |
| 441 |
'type': v_constants.SUBNET_ENDPOINT, |
| 442 |
'endpoints': expected_subnets} |
| 443 |
query_mock = mock.patch.object(query.Query, 'all').start() |
| 444 |
query_mock.return_value = expected_subnets |
| 445 |
subnets = self.validator._get_local_subnets(self.context, local_epg) |
| 446 |
self.assertEqual(expected_subnets, subnets) |
| 447 |
|
| 448 |
def test_ipsec_conn_get_peer_cidrs(self): |
| 449 |
expected_cidrs = ['10.10.10.10/24', '20.20.20.20/24'] |
| 450 |
peer_epg = {'id': 'should-be-cidrs', |
| 451 |
'type': v_constants.CIDR_ENDPOINT, |
| 452 |
'endpoints': expected_cidrs} |
| 453 |
cidrs = self.validator._get_peer_cidrs(peer_epg) |
| 454 |
self.assertEqual(expected_cidrs, cidrs) |
| 455 |
|
| 456 |
def test_ipsec_conn_check_peer_cidrs(self): |
| 457 |
peer_cidrs = ['10.10.10.0/24', '20.20.20.0/24'] |
| 458 |
try: |
| 459 |
self.validator._check_peer_cidrs(peer_cidrs) |
| 460 |
except Exception: |
| 461 |
self.fail("All peer cidrs format should be valid") |
| 462 |
|
| 463 |
def test_fail_ipsec_conn_peer_cidrs_with_invalid_format(self): |
| 464 |
peer_cidrs = ['invalid_cidr'] |
| 465 |
self.assertRaises(vpn_exception.IPsecSiteConnectionPeerCidrError, |
| 466 |
self.validator._check_peer_cidrs, |
| 467 |
peer_cidrs) |
| 468 |
peer_cidrs = ['192/24'] |
| 469 |
self.assertRaises(vpn_exception.IPsecSiteConnectionPeerCidrError, |
| 470 |
self.validator._check_peer_cidrs, |
| 471 |
peer_cidrs) |
| 472 |
|
| 473 |
def test_fail_ipsec_conn_endpoint_group_types(self): |
| 474 |
local_epg = {'id': 'should-be-subnets', |
| 475 |
'type': v_constants.CIDR_ENDPOINT, |
| 476 |
'endpoints': ['10.10.10.10/24', '20.20.20.20/24']} |
| 477 |
self.assertRaises(vpn_exception.WrongEndpointGroupType, |
| 478 |
self.validator._get_local_subnets, |
| 479 |
self.context, local_epg) |
| 480 |
peer_epg = {'id': 'should-be-cidrs', |
| 481 |
'type': v_constants.SUBNET_ENDPOINT, |
| 482 |
'endpoints': [_uuid(), _uuid()]} |
| 483 |
self.assertRaises(vpn_exception.WrongEndpointGroupType, |
| 484 |
self.validator._get_peer_cidrs, |
| 485 |
peer_epg) |
| 486 |
|
| 487 |
def test_validate_ipsec_conn_for_endpoints(self): |
| 488 |
"""Check upper-level validation method for endpoint groups. |
| 489 |
|
| 490 |
Tests the happy path for doing general validation of the IPSec |
| 491 |
connection, calling all the sub-checks for an endpoint group case. |
| 492 |
""" |
| 493 |
subnet1 = {'id': _uuid(), 'ip_version': 4} |
| 494 |
subnet2 = {'id': _uuid(), 'ip_version': 4} |
| 495 |
local_subnets = [subnet1, subnet2] |
| 496 |
local_epg_id = _uuid() |
| 497 |
local_epg = {'id': local_epg_id, |
| 498 |
'type': v_constants.SUBNET_ENDPOINT, |
| 499 |
'endpoints': local_subnets} |
| 500 |
# Mock getting the subnets from the IDs |
| 501 |
query_mock = mock.patch.object(query.Query, 'all').start() |
| 502 |
query_mock.return_value = local_subnets |
| 503 |
# Mock that subnet is on router |
| 504 |
port_mock = mock.patch.object(self.core_plugin, "get_ports").start() |
| 505 |
port_mock.side_effect = ['dummy info', 'more dummy info'] |
| 506 |
|
| 507 |
peer_epg_id = _uuid() |
| 508 |
peer_cidrs = ['10.10.10.10/24', '20.20.20.20/24'] |
| 509 |
peer_epg = {'id': peer_epg_id, |
| 510 |
'type': v_constants.CIDR_ENDPOINT, |
| 511 |
'endpoints': peer_cidrs} |
| 512 |
|
| 513 |
ipsec_sitecon = {'local_ep_group_id': local_epg_id, |
| 514 |
'local_epg_subnets': local_epg, |
| 515 |
'peer_ep_group_id': peer_epg_id, |
| 516 |
'peer_epg_cidrs': peer_epg, |
| 517 |
'mtu': 2000, |
| 518 |
'dpd_action': 'hold', |
| 519 |
'dpd_interval': 30, |
| 520 |
'dpd_timeout': 120} |
| 521 |
local_version = None |
| 522 |
vpnservice = {'router_id': _uuid()} |
| 523 |
self.validator.validate_ipsec_site_connection( |
| 524 |
self.context, ipsec_sitecon, local_version, vpnservice) |
| 525 |
|
| 526 |
# NOTE: Following are tests for the older API, providing some additional |
| 527 |
# coverage. |
| 528 |
|
| 529 |
def test_ipsec_conn_peer_cidrs_same_ip_version(self): |
| 530 |
"""Check legacy peer_cidrs have same IP version.""" |
| 531 |
one_cidr = ['2002:0a00::/48'] |
| 532 |
version = self.validator._check_peer_cidrs_ip_versions(one_cidr) |
| 533 |
self.assertEqual(6, version) |
| 534 |
multiple_cidrs = ['10.10.10.0/24', '20.20.20.0/24'] |
| 535 |
version = self.validator._check_peer_cidrs_ip_versions(multiple_cidrs) |
| 536 |
self.assertEqual(4, version) |
| 537 |
|
| 538 |
def test_fail_ipsec_conn_peer_cidrs_mixed_ip_version(self): |
| 539 |
mixed_cidrs = ['2002:0a00::/48', '20.20.20.0/24'] |
| 540 |
self.assertRaises(vpn_exception.MixedIPVersionsForPeerCidrs, |
| 541 |
self.validator._check_peer_cidrs_ip_versions, |
| 542 |
mixed_cidrs) |