Debian Codegraph @

neutron_fwaas/tests/functional/services/firewall/ovn/test_ovn_db_sync.py

raw symbols SCIP
1 # Copyright 2026 Red Hat, Inc.
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 unittest import mock
16
17 from neutron.common.ovn import constants as ovn_const
18 from neutron.common.ovn import utils as neutron_ovn_utils
19 from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import maintenance
20 from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import \
21 ovn_db_sync as neutron_ovn_db_sync
22 from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovsdb_monitor
23 from neutron.tests.functional import base
24 from neutron_lib.plugins import directory
25 from oslo_config import cfg
26
27 from neutron_fwaas.common import fwaas_constants
28 from neutron_fwaas.services.firewall.service_drivers.ovn import \
29 constants as ovn_fw_const
30 from neutron_fwaas.services.firewall.service_drivers.ovn import \
31 ovn_db_sync
32
33
34 class BaseTestOvnNbSync(base.TestOVNFunctionalBase):
35
36 def setUp(self):
37 self._mock_has_lock = mock.patch.object(
38 maintenance.DBInconsistenciesPeriodics, 'has_lock',
39 mock.PropertyMock(return_value=True))
40 self.mock_has_lock = self._mock_has_lock.start()
41 self._mock_set_lock = mock.patch.object(
42 ovsdb_monitor.BaseOvnIdl, 'set_lock')
43 self.mock_set_lock = self._mock_set_lock.start()
44
45 cfg.CONF.set_override(
46 'service_provider',
47 ['FIREWALL_V2:fwaas_db:neutron_fwaas.services.firewall.'
48 'service_drivers.ovn.firewall_l3_driver.OVNFwaasDriver:default'],
49 group='service_providers')
50 super().setUp(
51 maintenance_worker=True,
52 service_plugins={
53 fwaas_constants.FIREWALL_V2: (
54 'neutron_fwaas.services.firewall.'
55 'fwaas_plugin_v2.FirewallPluginV2')})
56 self.context.project_id = self._project_id
57 self.context.tenant_id = self._project_id
58 self.fwaas_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2)
59
60 def _create_basic_resources(self, number_of_networks):
61 """This method creates the basic resources for the tests.
62 """
63
64 name_prefix = 'test-resource'
65
66 router = self.l3_plugin.create_router(
67 self.context,
68 {'router': {
69 'name': f'{name_prefix}-router', 'admin_state_up': True,
70 'project_id': self._project_id}})
71
72 for i in range(number_of_networks):
73 res = self._create_network(
74 self.fmt, f'{name_prefix}-network-{i}', True)
75 network = self.deserialize(self.fmt, res)
76 res = self._create_subnet(self.fmt, network['network']['id'],
77 f'10.0.{i}.0/24')
78 subnet = self.deserialize(self.fmt, res)
79
80 self.l3_plugin.add_router_interface(
81 self.context, router['id'],
82 {'subnet_id': subnet['subnet']['id']})
83
84 res = self._list_ports(self.fmt, device_id=router['id'])
85 return [port['id'] for port in
86 self.deserialize(self.fmt, res)['ports']]
87
88 def _get_ovn_port_groups(self):
89 plugin_nb_ovn = self.mech_driver.nb_ovn
90 port_groups = {}
91 for pg in plugin_nb_ovn.db_list_rows('Port_Group').execute() or []:
92 fwaas_group_id = pg.external_ids.get(
93 ovn_fw_const.OVN_FWG_EXT_ID_KEY)
94 if not fwaas_group_id:
95 continue
96 port_groups[fwaas_group_id] = {
97 'name': pg.name,
98 'ports': pg.ports,
99 'acls': [],
100 }
101 for acl in plugin_nb_ovn.pg_acl_list(pg.name).execute() or []:
102 port_groups[fwaas_group_id]['acls'].append({
103 'match': acl.match,
104 'action': acl.action,
105 'priority': acl.priority,
106 'direction': acl.direction,
107 })
108 return port_groups
109
110 def _create_resources(self):
111 for i in range(2):
112 # There will be router with 2 subnets from 2 different networks
113 # plugged in
114 router_ports = self._create_basic_resources(number_of_networks=2)
115
116 firewall_ingress_rule = self.fwaas_plugin.create_firewall_rule(
117 self.context,
118 {'firewall_rule': {
119 'name': 'ingress-ssh-rule',
120 'protocol': 'tcp',
121 'ip_version': 4,
122 'source_ip_address': '',
123 'source_port': '',
124 'destination_ip_address': '10.0.0.2',
125 'destination_port': '22',
126 'action': 'allow',
127 'enabled': True,
128 'project_id': self._project_id,
129 'tenant_id': self._project_id,
130 'description': 'test firewall SSH ingress rule',
131 'shared': False,
132 }})
133 firewall_egress_rule = self.fwaas_plugin.create_firewall_rule(
134 self.context,
135 {'firewall_rule': {
136 'name': 'egress-icmp-rule',
137 'protocol': 'icmp',
138 'ip_version': 4,
139 'source_ip_address': '',
140 'source_port': '',
141 'destination_ip_address': '',
142 'destination_port': '',
143 'action': 'deny',
144 'enabled': True,
145 'project_id': self._project_id,
146 'tenant_id': self._project_id,
147 'description': 'test firewall ICMP egress rule',
148 'shared': False,
149 }})
150
151 firewall_ingress_policy = self.fwaas_plugin.create_firewall_policy(
152 self.context,
153 {'firewall_policy': {
154 'tenant_id': self._project_id,
155 'project_id': self._project_id,
156 'name': 'ingress-test-policy',
157 'description': 'test firewall ingresspolicy',
158 'shared': False,
159 'audited': False,
160 'firewall_rules': [firewall_ingress_rule['id']],
161 }})
162 firewall_egress_policy = self.fwaas_plugin.create_firewall_policy(
163 self.context,
164 {'firewall_policy': {
165 'tenant_id': self._project_id,
166 'project_id': self._project_id,
167 'name': 'ingress-test-policy',
168 'description': 'test firewall egresspolicy',
169 'shared': False,
170 'audited': False,
171 'firewall_rules': [firewall_egress_rule['id']],
172 }})
173
174 self.fwaas_plugin.create_firewall_group(
175 self.context,
176 {'firewall_group': {
177 'tenant_id': self._project_id,
178 'project_id': self._project_id,
179 'name': 'test-group',
180 'description': 'test firewall group',
181 'shared': False,
182 'admin_state_up': True,
183 'ingress_firewall_policy_id': firewall_ingress_policy['id'],
184 'egress_firewall_policy_id': firewall_egress_policy['id'],
185 'ports': router_ports,
186 }})
187
188 # Now, once all is configured, get the Port Groups from OVN DB - that
189 # will be used to validate ovn_db_sync if it can restore the same
190 # state later
191 self.expected_ovn_port_groups = self._get_ovn_port_groups()
192
193 def _assert_acl_lists_equal(self, list1, list2):
194 self.assertEqual(len(list1), len(list2))
195 for acl in list1:
196 self.assertIn(acl, list2)
197
198 def _validate_resources(self, should_match=True):
199 current_ovn_port_groups = self._get_ovn_port_groups()
200 if should_match:
201 self.assertEqual(
202 sorted(self.expected_ovn_port_groups.keys()),
203 sorted(current_ovn_port_groups.keys())
204 )
205 for fwg_id in self.expected_ovn_port_groups.keys():
206 self._assert_acl_lists_equal(
207 current_ovn_port_groups[fwg_id]['acls'],
208 self.expected_ovn_port_groups[fwg_id]['acls']
209 )
210 ports_in_current_ovn_port_groups = sorted([
211 p.external_ids.get(ovn_const.OVN_DEVID_EXT_ID_KEY)
212 for p in current_ovn_port_groups[fwg_id]['ports']])
213 ports_in_expected_ovn_port_groups = sorted([
214 p.external_ids.get(ovn_const.OVN_DEVID_EXT_ID_KEY)
215 for p in self.expected_ovn_port_groups[fwg_id]['ports']])
216 self.assertEqual(
217 ports_in_current_ovn_port_groups,
218 ports_in_expected_ovn_port_groups
219 )
220 self.assertEqual(
221 current_ovn_port_groups[fwg_id]['name'],
222 self.expected_ovn_port_groups[fwg_id]['name']
223 )
224 else:
225 self.assertNotEqual(
226 current_ovn_port_groups, self.expected_ovn_port_groups)
227
228 def _test_ovn_nb_sync_helper(self, resources_modifier=None,
229 restart_ovsdb_processes=False):
230 self._create_resources()
231 self._validate_resources()
232 if resources_modifier:
233 resources_modifier()
234 if restart_ovsdb_processes:
235 self.restart()
236 # After deleting the OVS DB sync of the neutron resources, such as
237 # networks, ports and routers is also needed
238 self._sync_neutron_resources(ovn_const.OVN_DB_SYNC_MODE_REPAIR)
239 if resources_modifier or restart_ovsdb_processes:
240 self._validate_resources(should_match=False)
241 self._sync_resources()
242 self._validate_resources(should_match=self.should_match_after_sync)
243
244 def _sync_resources(self):
245 nb_synchronizer = ovn_db_sync.OvnNbDbSync(
246 self.plugin, self.mech_driver, self.mode)
247 self.addCleanup(nb_synchronizer.stop)
248 nb_synchronizer.do_sync()
249
250 def _sync_neutron_resources(self, mode):
251 nb_synchronizer = neutron_ovn_db_sync.OvnNbSynchronizer(
252 self.plugin, self.mech_driver, mode)
253 self.addCleanup(nb_synchronizer.stop)
254 nb_synchronizer.do_sync()
255
256 def _test_ovn_db_sync_port_group_deleted(self, restart_ovsdb_processes):
257
258 def delete_port_group():
259 plugin_nb_ovn = self.mech_driver.nb_ovn
260 pg_to_delete = list(self.expected_ovn_port_groups.keys())[0]
261 plugin_nb_ovn.pg_del(
262 name=neutron_ovn_utils.ovn_port_group_name(pg_to_delete),
263 if_exists=True
264 ).execute(check_error=True)
265
266 self._test_ovn_nb_sync_helper(
267 resources_modifier=delete_port_group,
268 restart_ovsdb_processes=restart_ovsdb_processes)
269
270 def _test_ovn_nb_sync_port_group_modified(self, restart_ovsdb_processes):
271
272 def modify_port_group():
273 plugin_nb_ovn = self.mech_driver.nb_ovn
274 pg_to_modify = list(self.expected_ovn_port_groups.keys())[0]
275 pg_name = neutron_ovn_utils.ovn_port_group_name(pg_to_modify)
276 acl = self.expected_ovn_port_groups[pg_to_modify]['acls'][0]
277 port = self.expected_ovn_port_groups[pg_to_modify]['ports'][0]
278
279 with plugin_nb_ovn.transaction(check_error=True) as txn:
280 txn.add(plugin_nb_ovn.pg_acl_del(
281 pg_name, acl['direction'], acl['priority'], acl['match']))
282 txn.add(plugin_nb_ovn.pg_del_ports(
283 neutron_ovn_utils.ovn_port_group_name(pg_to_modify),
284 port, if_exists=True))
285
286 self._test_ovn_nb_sync_helper(
287 resources_modifier=modify_port_group,
288 restart_ovsdb_processes=restart_ovsdb_processes)
289
290
291 class TestOvnNbSyncRepair(BaseTestOvnNbSync):
292
293 mode = ovn_const.OVN_DB_SYNC_MODE_REPAIR
294 should_match_after_sync = True
295
296 def get_ovsdb_server_protocol(self):
297 return 'unix'
298
299 def test_ovn_db_sync_port_group_deleted_no_restart_ovsdb(self):
300 self._test_ovn_db_sync_port_group_deleted(
301 restart_ovsdb_processes=False)
302
303 def test_ovn_db_sync_port_group_deleted_restart_ovsdb(self):
304 self._test_ovn_db_sync_port_group_deleted(restart_ovsdb_processes=True)
305
306 def test_ovn_db_sync_port_group_modified_no_restart_ovsdb(self):
307 self._test_ovn_nb_sync_port_group_modified(
308 restart_ovsdb_processes=False)
309
310 def test_ovn_db_sync_port_group_modified_restart_ovsdb(self):
311 self._test_ovn_nb_sync_port_group_modified(
312 restart_ovsdb_processes=True)
313
314
315 class TestOvnNbSyncLog(BaseTestOvnNbSync):
316
317 mode = ovn_const.OVN_DB_SYNC_MODE_LOG
318 should_match_after_sync = False
319
320 def get_ovsdb_server_protocol(self):
321 return 'unix'
322
323 def test_ovn_db_sync_port_group_deleted_no_restart_ovsdb(self):
324 self._test_ovn_db_sync_port_group_deleted(
325 restart_ovsdb_processes=False)
326
327 def test_ovn_db_sync_port_group_deleted_restart_ovsdb(self):
328 self._test_ovn_db_sync_port_group_deleted(restart_ovsdb_processes=True)
329
330 def test_ovn_db_sync_port_group_modified_no_restart_ovsdb(self):
331 self._test_ovn_nb_sync_port_group_modified(
332 restart_ovsdb_processes=False)
333
334 def test_ovn_db_sync_port_group_modified_restart_ovsdb(self):
335 self._test_ovn_nb_sync_port_group_modified(
336 restart_ovsdb_processes=True)