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 6262from nova import exception
6363from nova import objects
6464from nova .objects import base as objects_base
65+ from nova .pci import request
6566from nova import quota
6667from nova .scheduler .client import report
6768from nova .scheduler import utils as scheduler_utils
@@ -190,6 +191,10 @@ def setUp(self):
190191 self .useFixture (
191192 nova_fixtures .PropagateTestCaseIdToChildEventlets (self .id ()))
192193
194+ # Ensure that the pci alias is reset between test cases running in
195+ # the same process
196+ request .get_alias_from_config .cache_clear ()
197+
193198 # How many of which service we've started. {$service-name: $count}
194199 self ._service_fixture_count = collections .defaultdict (int )
195200
@@ -426,6 +431,10 @@ def flags(self, **kw):
426431 group = kw .pop ('group' , None )
427432 for k , v in kw .items ():
428433 CONF .set_override (k , v , group )
434+ # loading and validating alias is cached so if it is reconfigured
435+ # we need to reset the cache
436+ if k == 'alias' and group == 'pci' :
437+ request .get_alias_from_config .cache_clear ()
429438
430439 def reset_flags (self , * k , ** kw ):
431440 """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