File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3737 These two aliases define a device request meaning: vendor_id is "8086" and
3838 product_id is "0442" or "0443".
3939 """
40-
40+ import functools
4141import typing as ty
4242
4343import jsonschema
@@ -173,6 +173,7 @@ def _validate_aliases(aliases):
173173 _validate_required_ids (aliases )
174174
175175
176+ @functools .cache
176177def get_alias_from_config () -> Alias :
177178 """Parse and validate PCI aliases from the nova config.
178179
Original file line number Diff line number Diff line change 6161from nova import exception
6262from nova import objects
6363from nova .objects import base as objects_base
64+ from nova .pci import request
6465from nova import quota
6566from nova .scheduler .client import report
6667from nova .scheduler import utils as scheduler_utils
@@ -189,6 +190,10 @@ def setUp(self):
189190 self .useFixture (
190191 nova_fixtures .PropagateTestCaseIdToChildEventlets (self .id ()))
191192
193+ # Ensure that the pci alias is reset between test cases running in
194+ # the same process
195+ request .get_alias_from_config .cache_clear ()
196+
192197 # How many of which service we've started. {$service-name: $count}
193198 self ._service_fixture_count = collections .defaultdict (int )
194199
@@ -425,6 +430,10 @@ def flags(self, **kw):
425430 group = kw .pop ('group' , None )
426431 for k , v in kw .items ():
427432 CONF .set_override (k , v , group )
433+ # loading and validating alias is cached so if it is reconfigured
434+ # we need to reset the cache
435+ if k == 'alias' and group == 'pci' :
436+ request .get_alias_from_config .cache_clear ()
428437
429438 def reset_flags (self , * k , ** kw ):
430439 """Reset flag variables for a test."""
Original file line number Diff line number Diff line change @@ -342,6 +342,23 @@ def test_get_alias_from_config_missing_ids_or_rc_pci_in_placement(self):
342342 "product_id fields set or resource_class field set." ,
343343 str (ex ))
344344
345+ def test_get_alias_from_config_cached (self ):
346+ alias = jsonutils .dumps ({
347+ "name" : "a5" ,
348+ "vendor_id" : "4444" ,
349+ "product_id" : "4444" ,
350+ })
351+ self .flags (alias = [alias ], group = 'pci' )
352+
353+ origi_loads = jsonutils .loads
354+
355+ with mock .patch ('oslo_serialization.jsonutils.loads' ) as mock_loads :
356+ mock_loads .side_effect = origi_loads
357+ request .get_alias_from_config ()
358+ request .get_alias_from_config ()
359+
360+ mock_loads .assert_called_once ()
361+
345362 def _verify_result (self , expected , real ):
346363 exp_real = zip (expected , real )
347364 for exp , real in exp_real :
You can’t perform that action at this time.
0 commit comments