| 1 |
# Copyright 2023 SysEleven GmbH |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from neutron.api import extensions |
| 16 |
from neutron.tests.unit.api import test_extensions |
| 17 |
from neutron.tests.unit.extensions import test_l3 as test_l3_plugin |
| 18 |
from neutron_lib.callbacks import events |
| 19 |
from neutron_lib.callbacks import exceptions as cb_exc |
| 20 |
from neutron_lib.callbacks import registry |
| 21 |
from neutron_lib.callbacks import resources |
| 22 |
from neutron_lib import constants as lib_constants |
| 23 |
from neutron_lib import context |
| 24 |
from neutron_lib.plugins import constants as nconstants |
| 25 |
from neutron_lib.plugins import directory |
| 26 |
from neutron_vpnaas.db.vpn.vpn_ext_gw_db import VPNExtGWPlugin_db |
| 27 |
from neutron_vpnaas.tests import base |
| 28 |
from neutron_vpnaas.tests.unit.db.vpn import test_vpn_db |
| 29 |
|
| 30 |
|
| 31 |
OVN_VPN_PLUGIN_KLASS = "neutron_vpnaas.services.vpn.ovn_plugin.VPNOVNPlugin" |
| 32 |
|
| 33 |
|
| 34 |
class VPNOVNPluginDbTestCase(test_l3_plugin.L3NatTestCaseMixin, |
| 35 |
base.NeutronDbPluginV2TestCase): |
| 36 |
def setUp(self, core_plugin=None, vpnaas_plugin=OVN_VPN_PLUGIN_KLASS, |
| 37 |
vpnaas_provider=None): |
| 38 |
|
| 39 |
service_plugins = {'vpnaas_plugin': vpnaas_plugin} |
| 40 |
plugin_str = 'neutron.tests.unit.extensions.test_l3.TestL3NatIntPlugin' |
| 41 |
super().setUp(plugin_str, service_plugins=service_plugins) |
| 42 |
ext_mgr = extensions.PluginAwareExtensionManager.get_instance() |
| 43 |
self.ext_api = test_extensions.setup_extensions_middleware(ext_mgr) |
| 44 |
self.core_plugin = directory.get_plugin() |
| 45 |
self.project_id = 'project1' |
| 46 |
|
| 47 |
|
| 48 |
class TestVPNExtGw(VPNOVNPluginDbTestCase): |
| 49 |
def _pre_port_delete(self, admin_context, port_id): |
| 50 |
registry.publish( |
| 51 |
resources.PORT, events.BEFORE_DELETE, self, |
| 52 |
payload=events.DBEventPayload( |
| 53 |
admin_context, |
| 54 |
metadata={'port_check': True}, |
| 55 |
resource_id=port_id)) |
| 56 |
|
| 57 |
def _pre_subnet_delete(self, admin_context, subnet_id): |
| 58 |
registry.publish(resources.SUBNET, events.BEFORE_DELETE, self, |
| 59 |
payload=events.DBEventPayload(admin_context, |
| 60 |
resource_id=subnet_id)) |
| 61 |
|
| 62 |
def _pre_network_delete(self, admin_context, network_id): |
| 63 |
registry.publish(resources.NETWORK, events.BEFORE_DELETE, self, |
| 64 |
payload=events.DBEventPayload(admin_context, |
| 65 |
resource_id=network_id)) |
| 66 |
|
| 67 |
def _test_prevent_vpn_port_deletion(self, device_owner, gw_key): |
| 68 |
plugin = directory.get_plugin(nconstants.VPN) |
| 69 |
with self.router() as router, \ |
| 70 |
self.port(device_owner=device_owner) as port: |
| 71 |
gateway = {'gateway': { |
| 72 |
'router_id': router['router']['id'], |
| 73 |
gw_key: port['port']['id'], |
| 74 |
'tenant_id': self.project_id |
| 75 |
}} |
| 76 |
admin_context = context.get_admin_context() |
| 77 |
plugin.create_gateway(admin_context, gateway) |
| 78 |
self.assertRaises( |
| 79 |
cb_exc.CallbackFailure, |
| 80 |
self._pre_port_delete, admin_context, port['port']['id']) |
| 81 |
|
| 82 |
def test_prevent_vpn_port_deletion_gw_port(self): |
| 83 |
self._test_prevent_vpn_port_deletion( |
| 84 |
lib_constants.DEVICE_OWNER_VPN_ROUTER_GW, 'gw_port_id') |
| 85 |
|
| 86 |
def test_prevent_vpn_port_deletion_transit_port(self): |
| 87 |
self._test_prevent_vpn_port_deletion( |
| 88 |
lib_constants.DEVICE_OWNER_VPN_TRANSIT_NETWORK, 'transit_port_id') |
| 89 |
|
| 90 |
def test_prevent_vpn_port_deletion_other_device_owner(self): |
| 91 |
plugin = directory.get_plugin(nconstants.VPN) |
| 92 |
device_owner = lib_constants.DEVICE_OWNER_VPN_TRANSIT_NETWORK |
| 93 |
with self.router() as router, \ |
| 94 |
self.port(device_owner=device_owner) as transit_port, \ |
| 95 |
self.port(device_owner='other-device-owner') as other_port: |
| 96 |
gateway = {'gateway': { |
| 97 |
'router_id': router['router']['id'], |
| 98 |
'transit_port_id': transit_port['port']['id'], |
| 99 |
'tenant_id': self.project_id |
| 100 |
}} |
| 101 |
admin_context = context.get_admin_context() |
| 102 |
plugin.create_gateway(admin_context, gateway) |
| 103 |
# BEFORE_DELETE event for other_port should not raise an exception |
| 104 |
self._pre_port_delete(admin_context, other_port['port']['id']) |
| 105 |
|
| 106 |
def test_prevent_vpn_subnet_deletion(self): |
| 107 |
plugin = directory.get_plugin(nconstants.VPN) |
| 108 |
with self.router() as router, self.subnet() as subnet: |
| 109 |
gateway = {'gateway': { |
| 110 |
'router_id': router['router']['id'], |
| 111 |
'transit_subnet_id': subnet['subnet']['id'], |
| 112 |
'tenant_id': self.project_id |
| 113 |
}} |
| 114 |
admin_context = context.get_admin_context() |
| 115 |
plugin.create_gateway(admin_context, gateway) |
| 116 |
self.assertRaises( |
| 117 |
cb_exc.CallbackFailure, |
| 118 |
self._pre_subnet_delete, admin_context, subnet['subnet']['id']) |
| 119 |
# should not raise an exception for other subnet id |
| 120 |
self._pre_subnet_delete(admin_context, "other-id") |
| 121 |
|
| 122 |
def test_prevent_vpn_network_deletion(self): |
| 123 |
plugin = directory.get_plugin(nconstants.VPN) |
| 124 |
with self.router() as router, self.network() as network: |
| 125 |
gateway = {'gateway': { |
| 126 |
'router_id': router['router']['id'], |
| 127 |
'transit_network_id': network['network']['id'], |
| 128 |
'tenant_id': self.project_id |
| 129 |
}} |
| 130 |
admin_context = context.get_admin_context() |
| 131 |
plugin.create_gateway(admin_context, gateway) |
| 132 |
self.assertRaises( |
| 133 |
cb_exc.CallbackFailure, |
| 134 |
self._pre_network_delete, admin_context, |
| 135 |
network['network']['id']) |
| 136 |
# should not raise an exception for other network id |
| 137 |
self._pre_network_delete(admin_context, "other-id") |
| 138 |
|
| 139 |
|
| 140 |
class TestVPNExtGwDB(base.NeutronDbPluginV2TestCase, |
| 141 |
test_vpn_db.NeutronResourcesMixin): |
| 142 |
def setUp(self): |
| 143 |
plugin_str = 'neutron.tests.unit.extensions.test_l3.TestL3NatIntPlugin' |
| 144 |
super().setUp(plugin_str) |
| 145 |
|
| 146 |
self.core_plugin = directory.get_plugin() |
| 147 |
self.l3_plugin = directory.get_plugin(nconstants.L3) |
| 148 |
self.project_id = 'project1' |
| 149 |
self.context = context.get_admin_context() |
| 150 |
|
| 151 |
def _create_gw_port(self, router): |
| 152 |
port = {'port': { |
| 153 |
'project_id': self.project_id, |
| 154 |
'network_id': router['external_gateway_info']['network_id'], |
| 155 |
'fixed_ips': lib_constants.ATTR_NOT_SPECIFIED, |
| 156 |
'mac_address': lib_constants.ATTR_NOT_SPECIFIED, |
| 157 |
'admin_state_up': True, |
| 158 |
'device_id': router['id'], |
| 159 |
'device_owner': lib_constants.DEVICE_OWNER_VPN_ROUTER_GW, |
| 160 |
'name': '' |
| 161 |
}} |
| 162 |
return self.core_plugin.create_port(self.context, port) |
| 163 |
|
| 164 |
def test_create_gateway(self): |
| 165 |
private_subnet, router = self.create_basic_topology() |
| 166 |
gateway = {'gateway': { |
| 167 |
'router_id': router['id'], |
| 168 |
'tenant_id': self.project_id |
| 169 |
}} |
| 170 |
gwdb = VPNExtGWPlugin_db() |
| 171 |
new_gateway = gwdb.create_gateway(self.context, gateway) |
| 172 |
expected = {**gateway['gateway'], |
| 173 |
'status': lib_constants.PENDING_CREATE} |
| 174 |
self.assertDictSupersetOf(expected, new_gateway) |
| 175 |
|
| 176 |
def test_update_gateway_with_external_port(self): |
| 177 |
private_subnet, router = self.create_basic_topology() |
| 178 |
gwdb = VPNExtGWPlugin_db() |
| 179 |
# create gateway |
| 180 |
gateway = {'gateway': { |
| 181 |
'router_id': router['id'], |
| 182 |
'tenant_id': self.project_id |
| 183 |
}} |
| 184 |
new_gateway = gwdb.create_gateway(self.context, gateway) |
| 185 |
|
| 186 |
# create external port and update gateway with the port id |
| 187 |
gw_port = self._create_gw_port(router) |
| 188 |
gateway_update = {'gateway': { |
| 189 |
'gw_port_id': gw_port['id'] |
| 190 |
}} |
| 191 |
gwdb.update_gateway(self.context, new_gateway['id'], gateway_update) |
| 192 |
|
| 193 |
# check that get_vpn_gw_dict_by_router_id includes external_fixed_ips |
| 194 |
found_gateway = gwdb.get_vpn_gw_dict_by_router_id(self.context, |
| 195 |
router['id']) |
| 196 |
self.assertIn('external_fixed_ips', found_gateway) |
| 197 |
expected = sorted(gw_port['fixed_ips']) |
| 198 |
returned = sorted(found_gateway['external_fixed_ips']) |
| 199 |
self.assertEqual(returned, expected) |
| 200 |
|
| 201 |
def test_delete_gateway(self): |
| 202 |
private_subnet, router = self.create_basic_topology() |
| 203 |
gwdb = VPNExtGWPlugin_db() |
| 204 |
# create gateway |
| 205 |
gateway = {'gateway': { |
| 206 |
'router_id': router['id'], |
| 207 |
'tenant_id': self.project_id |
| 208 |
}} |
| 209 |
new_gateway = gwdb.create_gateway(self.context, gateway) |
| 210 |
self.assertIsNotNone(new_gateway) |
| 211 |
deleted = gwdb.delete_gateway(self.context, new_gateway['id']) |
| 212 |
self.assertEqual(deleted, 1) |
| 213 |
deleted = gwdb.delete_gateway(self.context, new_gateway['id']) |
| 214 |
self.assertEqual(deleted, 0) |
| 215 |
found_gateway = gwdb.get_vpn_gw_dict_by_router_id(self.context, |
| 216 |
router['id']) |
| 217 |
self.assertIsNone(found_gateway) |