Skip to content

Commit 194e966

Browse files
committed
chore(neutron-understack): switch oslo_config overrides to a fixture
Rather than patching the global scoped oslo_config, switch to using a fixture which resets between tests to ensure that things are cleaned up appropriately. This will be necessary when we no longer initialize oslo_config in the global/import scope of the module. While moving this it pointed out an issue with one of the tests missing a mock which attempted to hit the DB, which now couldn't be initialized because we didn't setup our configs because the data had the wrong types which is being fixed in this as well.
1 parent f280009 commit 194e966

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

python/neutron-understack/neutron_understack/tests/conftest.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
from neutron_lib import constants as p_const
2323
from neutron_lib.api.definitions import portbindings
2424
from neutron_lib.callbacks.events import DBEventPayload
25+
from oslo_config import fixture as config_fixture
2526

27+
from neutron_understack import config as understack_config
2628
from neutron_understack.ironic import IronicClient
2729
from neutron_understack.neutron_understack_mech import UnderstackDriver
2830
from neutron_understack.tests.helpers import Ml2PluginNoInit
@@ -276,7 +278,7 @@ def ironic_client(mocker) -> IronicClient:
276278

277279

278280
@pytest.fixture
279-
def understack_driver(ironic_client) -> UnderstackDriver:
281+
def understack_driver(oslo_config, ironic_client) -> UnderstackDriver:
280282
driver = UnderstackDriver()
281283
driver.undersync = Undersync("auth_token", "api_url")
282284
driver.ironic_client = ironic_client
@@ -341,18 +343,29 @@ def port_db_payload(network) -> DBEventPayload:
341343

342344

343345
@pytest.fixture
344-
def ml2_understack_conf(mocker, ucvni_group_id) -> None:
345-
mocker.patch(
346-
"neutron_understack.neutron_understack_mech.cfg.CONF.ml2_understack.ucvni_group",
347-
str(ucvni_group_id),
346+
def oslo_config():
347+
"""CONF from oslo_config is global but we need to override it sometimes."""
348+
conf_fixture = config_fixture.Config()
349+
conf_fixture.setUp()
350+
# register the ml2_understack options
351+
understack_config.register_ml2_understack_opts(conf_fixture.conf)
352+
yield conf_fixture
353+
conf_fixture.cleanUp()
354+
355+
356+
@pytest.fixture
357+
def ml2_understack_conf(oslo_config, ucvni_group_id) -> None:
358+
oslo_config.config(
359+
ucvni_group=str(ucvni_group_id),
360+
group="ml2_understack",
348361
)
349-
mocker.patch(
350-
"neutron_understack.neutron_understack_mech.cfg.CONF.ml2_understack.network_node_switchport_uuid",
351-
"a27f7260-a7c5-4f0c-ac70-6258b026d368",
362+
oslo_config.config(
363+
network_node_switchport_uuid="a27f7260-a7c5-4f0c-ac70-6258b026d368",
364+
group="ml2_understack",
352365
)
353-
mocker.patch(
354-
"neutron_understack.neutron_understack_mech.cfg.CONF.ml2_understack.undersync_dry_run",
355-
False,
366+
oslo_config.config(
367+
undersync_dry_run=False,
368+
group="ml2_understack",
356369
)
357370

358371

python/neutron-understack/neutron_understack/tests/test_trunk.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,23 @@ class TestCheckSubportsSegmentationId:
336336
def test_when_trunk_id_is_network_node_trunk_id(
337337
self,
338338
mocker,
339+
oslo_config,
339340
understack_trunk_driver,
340341
trunk_id,
341342
):
342-
mocker.patch(
343-
"oslo_config.cfg.CONF.ml2_understack.network_node_trunk_uuid",
344-
trunk_id,
343+
oslo_config.config(
344+
network_node_trunk_uuid=str(trunk_id),
345+
group="ml2_understack",
346+
)
347+
# Mock to ensure the function returns early and doesn't call this
348+
allowed_ranges_mock = mocker.patch(
349+
"neutron_understack.utils.allowed_tenant_vlan_id_ranges"
350+
)
351+
result = understack_trunk_driver._check_subports_segmentation_id(
352+
[], str(trunk_id)
345353
)
346-
result = understack_trunk_driver._check_subports_segmentation_id([], trunk_id)
354+
# Should not call allowed_tenant_vlan_id_ranges because it returns early
355+
allowed_ranges_mock.assert_not_called()
347356
assert result is None
348357

349358
def test_when_segmentation_id_is_in_allowed_range(

python/neutron-understack/neutron_understack/tests/test_utils.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ class TestAllowedTenantVlanIdRanges:
189189
def test_multiple_non_overlapping_ranges(
190190
self,
191191
mocker,
192+
oslo_config,
192193
):
193-
mocker.patch(
194-
"oslo_config.cfg.CONF.ml2_understack.default_tenant_vlan_id_range",
195-
[1, 2000],
194+
oslo_config.config(
195+
default_tenant_vlan_id_range=[1, 2000],
196+
group="ml2_understack",
196197
)
197198
mocker.patch(
198199
"neutron_understack.utils.fetch_vlan_network_segment_ranges",
@@ -205,10 +206,11 @@ def test_multiple_non_overlapping_ranges(
205206
def test_multiple_overlapping_ranges(
206207
self,
207208
mocker,
209+
oslo_config,
208210
):
209-
mocker.patch(
210-
"oslo_config.cfg.CONF.ml2_understack.default_tenant_vlan_id_range",
211-
[1, 2000],
211+
oslo_config.config(
212+
default_tenant_vlan_id_range=[1, 2000],
213+
group="ml2_understack",
212214
)
213215
mocker.patch(
214216
"neutron_understack.utils.fetch_vlan_network_segment_ranges",
@@ -221,10 +223,11 @@ def test_multiple_overlapping_ranges(
221223
def test_single_range(
222224
self,
223225
mocker,
226+
oslo_config,
224227
):
225-
mocker.patch(
226-
"oslo_config.cfg.CONF.ml2_understack.default_tenant_vlan_id_range",
227-
[1, 2000],
228+
oslo_config.config(
229+
default_tenant_vlan_id_range=[1, 2000],
230+
group="ml2_understack",
228231
)
229232
mocker.patch(
230233
"neutron_understack.utils.fetch_vlan_network_segment_ranges",

0 commit comments

Comments
 (0)