Skip to content

Commit 1e28e5f

Browse files
committed
Remove redundant check - port update can't cause native vlan clash
The parent port is not allowed to be updated after the trunk has been created so this extra check will never catch any problems.
1 parent 1a6d117 commit 1e28e5f

2 files changed

Lines changed: 27 additions & 141 deletions

File tree

‎python/neutron-understack/neutron_understack/tests/test_understack_mech.py‎

Lines changed: 27 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -24,100 +24,6 @@ def _port_update_context(original, current):
2424

2525

2626
class TestUpdatePortPreCommit:
27-
def test_rejects_parent_native_vlan_used_by_existing_subport(
28-
self, mocker, understack_driver, understack_trunk_driver
29-
):
30-
trunk_details = {
31-
"trunk_id": "trunk-a",
32-
"sub_ports": [
33-
{
34-
"port_id": "subport-a",
35-
"segmentation_id": 2222,
36-
}
37-
],
38-
}
39-
original = {
40-
"id": "parent-a",
41-
"network_id": "network-a",
42-
"device_owner": "",
43-
portbindings.PROFILE: {"physical_network": "physnet-a"},
44-
"trunk_details": trunk_details,
45-
}
46-
current = {
47-
**original,
48-
portbindings.PROFILE: {"physical_network": "physnet-b"},
49-
}
50-
native_segment = mocker.Mock(segmentation_id=2222)
51-
find_segment = mocker.patch(
52-
f"{MECH_UTILS}.network_segment_by_physnet",
53-
return_value=native_segment,
54-
)
55-
mocker.patch(
56-
"neutron_understack.utils.fetch_network_node_trunk_id",
57-
return_value="network-node-trunk",
58-
)
59-
understack_driver.trunk_driver = understack_trunk_driver
60-
61-
with pytest.raises(trunk.SubportSegmentationIDError, match="used as both"):
62-
understack_driver.update_port_precommit(
63-
_port_update_context(original, current)
64-
)
65-
66-
find_segment.assert_called_once_with(
67-
network_id="network-a", physnet="physnet-b"
68-
)
69-
70-
def test_defers_parent_native_vlan_check_until_binding_when_segment_missing(
71-
self, mocker, understack_driver, understack_trunk_driver
72-
):
73-
trunk_details = {
74-
"trunk_id": "trunk-a",
75-
"sub_ports": [
76-
{
77-
"port_id": "subport-a",
78-
"segmentation_id": 2222,
79-
}
80-
],
81-
}
82-
original = {
83-
"id": "parent-a",
84-
"network_id": "network-a",
85-
"device_owner": "",
86-
portbindings.PROFILE: {"physical_network": "physnet-a"},
87-
"trunk_details": trunk_details,
88-
}
89-
current = {
90-
**original,
91-
portbindings.PROFILE: {"physical_network": "physnet-b"},
92-
}
93-
mocker.patch(f"{MECH_UTILS}.network_segment_by_physnet", return_value=None)
94-
understack_driver.trunk_driver = understack_trunk_driver
95-
check_native_vlan = mocker.patch.object(
96-
understack_driver.trunk_driver, "check_trunk_native_vlan"
97-
)
98-
99-
understack_driver.update_port_precommit(_port_update_context(original, current))
100-
101-
check_native_vlan.assert_not_called()
102-
103-
def test_skips_native_vlan_check_when_parent_physnet_is_unchanged(
104-
self, mocker, understack_driver, understack_trunk_driver
105-
):
106-
port = {
107-
"id": "parent-a",
108-
"network_id": "network-a",
109-
"device_owner": "",
110-
portbindings.PROFILE: {"physical_network": "physnet-a"},
111-
"trunk_details": {"trunk_id": "trunk-a", "sub_ports": []},
112-
}
113-
current = {**port, "name": "new-name"}
114-
find_segment = mocker.patch(f"{MECH_UTILS}.network_segment_by_physnet")
115-
understack_driver.trunk_driver = understack_trunk_driver
116-
117-
understack_driver.update_port_precommit(_port_update_context(port, current))
118-
119-
find_segment.assert_not_called()
120-
12127
def test_validates_when_port_becomes_router_interface(
12228
self, mocker, understack_driver
12329
):
@@ -436,19 +342,24 @@ def test_releases_allocated_segment_when_trunk_check_fails(
436342
mocker,
437343
port_context,
438344
understack_driver,
345+
understack_trunk_driver,
439346
vlan_network_segment,
440347
):
441348
"""A native VLAN that collides with a subport must not leak its segment."""
349+
port_context.current["trunk_details"] = {"trunk_id": "trunk-a"}
442350
mocker.patch.object(
443351
port_context, "allocate_dynamic_segment", return_value=vlan_network_segment
444352
)
445353
mocker.patch(f"{MECH_UTILS}.ports_bound_to_segment", return_value=[])
446-
understack_driver.trunk_driver.configure_trunk.side_effect = (
447-
trunk.SubportSegmentationIDError(
354+
understack_driver.trunk_driver = understack_trunk_driver
355+
mocker.patch.object(
356+
understack_driver.trunk_driver,
357+
"configure_trunk",
358+
side_effect=trunk.SubportSegmentationIDError(
448359
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
449360
subport_id="subport-a",
450361
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
451-
)
362+
),
452363
)
453364
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
454365
port_context._prepare_to_bind(port_context.network.network_segments)
@@ -470,21 +381,26 @@ def test_keeps_allocated_segment_when_other_ports_are_bound(
470381
mocker,
471382
port_context,
472383
understack_driver,
384+
understack_trunk_driver,
473385
vlan_network_segment,
474386
):
475387
"""allocate_dynamic_segment can return a segment another port is using."""
388+
port_context.current["trunk_details"] = {"trunk_id": "trunk-a"}
476389
mocker.patch.object(
477390
port_context, "allocate_dynamic_segment", return_value=vlan_network_segment
478391
)
479392
mocker.patch(
480393
f"{MECH_UTILS}.ports_bound_to_segment", return_value=["other-port"]
481394
)
482-
understack_driver.trunk_driver.configure_trunk.side_effect = (
483-
trunk.SubportSegmentationIDError(
395+
understack_driver.trunk_driver = understack_trunk_driver
396+
mocker.patch.object(
397+
understack_driver.trunk_driver,
398+
"configure_trunk",
399+
side_effect=trunk.SubportSegmentationIDError(
484400
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
485401
subport_id="subport-a",
486402
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
487-
)
403+
),
488404
)
489405
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
490406
port_context._prepare_to_bind(port_context.network.network_segments)
@@ -506,19 +422,24 @@ def test_keeps_reused_segment_when_trunk_check_fails(
506422
mocker,
507423
port_context,
508424
understack_driver,
425+
understack_trunk_driver,
509426
vlan_network_segment,
510427
):
511428
"""A segment that already existed is not ours to release."""
429+
port_context.current["trunk_details"] = {"trunk_id": "trunk-a"}
512430
mocker.patch(
513431
f"{MECH_UTILS}.vlan_segment_for_physnet",
514432
return_value=vlan_network_segment,
515433
)
516-
understack_driver.trunk_driver.configure_trunk.side_effect = (
517-
trunk.SubportSegmentationIDError(
434+
understack_driver.trunk_driver = understack_trunk_driver
435+
mocker.patch.object(
436+
understack_driver.trunk_driver,
437+
"configure_trunk",
438+
side_effect=trunk.SubportSegmentationIDError(
518439
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
519440
subport_id="subport-a",
520441
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
521-
)
442+
),
522443
)
523444
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
524445
port_context._prepare_to_bind(port_context.network.network_segments)
@@ -601,12 +522,15 @@ def test_rejects_new_native_vlan_used_by_existing_subport_before_binding(
601522
"neutron_understack.utils.fetch_network_node_trunk_id",
602523
return_value="network-node-trunk",
603524
)
525+
mocker.patch(f"{MECH_UTILS}.ports_bound_to_segment", return_value=[])
526+
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
604527
understack_driver.trunk_driver = understack_trunk_driver
605528

606529
with pytest.raises(trunk.SubportSegmentationIDError, match="used as both"):
607530
understack_driver.bind_port(port_context)
608531

609532
port_context.continue_binding.assert_not_called()
533+
release.assert_called_once()
610534

611535

612536
class TestCreateNetworkPostCommit:

‎python/neutron-understack/neutron_understack/understack_mech.py‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ def create_port_postcommit(self, context: PortContext) -> None:
189189
raise
190190

191191
def update_port_precommit(self, context: PortContext):
192-
self._check_trunk_native_vlan(context)
193-
194192
if not utils.is_router_interface(context):
195193
return
196194

@@ -248,42 +246,6 @@ def update_port_precommit(self, context: PortContext):
248246
},
249247
)
250248

251-
def _check_trunk_native_vlan(self, context: PortContext) -> None:
252-
"""Reject a parent-port update that would collide with a subport tag."""
253-
port = context.current
254-
trunk_details = port.get("trunk_details")
255-
if not trunk_details:
256-
return
257-
258-
physical_network = (port.get(portbindings.PROFILE) or {}).get(
259-
"physical_network"
260-
)
261-
if not physical_network:
262-
return
263-
264-
original_profile = context.original.get(portbindings.PROFILE) or {}
265-
original_physical_network = original_profile.get("physical_network")
266-
if physical_network == original_physical_network:
267-
# The native VLAN for a (network, physnet) pair is stable. If a
268-
# rebind has to allocate a replacement segment, the binding-path
269-
# candidate check below remains authoritative.
270-
return
271-
272-
native_segment = utils.network_segment_by_physnet(
273-
network_id=port["network_id"], physnet=physical_network
274-
)
275-
if native_segment is None:
276-
# A first binding to this (network, physnet) allocates the segment
277-
# later. _bind_port_segment validates that candidate before doing
278-
# any trunk configuration.
279-
return
280-
281-
self.trunk_driver.check_trunk_native_vlan(
282-
trunk_details=trunk_details,
283-
physical_network=physical_network,
284-
native_vlan_id=native_segment.segmentation_id,
285-
)
286-
287249
def update_port_postcommit(self, context: PortContext) -> None:
288250
if utils.is_baremetal_port(context) and utils.is_port_unbinding(context):
289251
self._tenant_network_port_cleanup(context)

0 commit comments

Comments
 (0)