oslo_config fixture in policy tests and 'placement' in policy

Use of the fixture is a  bit more clean and correct and 'placement'
is the policy we want.

Also, reset() the loaded policy between tests so that global
state is reset. This ought to avoid race conditions in tests.

Change-Id: Ibd0880e8d21e46002aa2a46a0300092d5fa0aac9
This commit is contained in:
Chris Dent 2018-09-09 11:36:48 -06:00
parent cb39c55f3d
commit 868e443b09
3 changed files with 13 additions and 11 deletions

View File

@ -52,9 +52,7 @@ def get_enforcer():
# This method is used by oslopolicy CLI scripts in order to generate policy
# files from overrides on disk and defaults in code. We can just pass an
# empty list and let oslo do the config lifting for us.
# TODO(mriedem): Change the project kwarg value to "placement" once
# this code is extracted from nova.
cfg.CONF([], project='nova')
cfg.CONF([], project='placement')
init()
return _ENFORCER_PLACEMENT

View File

@ -13,21 +13,23 @@
# under the License.
import fixtures
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_policy import policy as oslo_policy
import placement.conf
from placement.conf import paths
from placement import policy as placement_policy
CONF = placement.conf.CONF
class PolicyFixture(fixtures.Fixture):
"""Load the default placement policy for tests."""
def setUp(self):
super(PolicyFixture, self).setUp()
policy_file = paths.state_path_def('etc/nova/placement-policy.yaml')
CONF.set_override('policy_file', policy_file, group='placement')
self.conf_fixture = self.useFixture(config_fixture.Config(cfg.CONF))
policy_file = paths.state_path_def(
'etc/placement/placement-policy.yaml')
self.conf_fixture.config(group='placement', policy_file=policy_file)
placement_policy.reset()
placement_policy.init()
self.addCleanup(placement_policy.reset)

View File

@ -31,10 +31,12 @@ class PlacementPolicyTestCase(testtools.TestCase):
"""Tests interactions with placement policy."""
def setUp(self):
super(PlacementPolicyTestCase, self).setUp()
self.conf = self.useFixture(config_fixture.Config(CONF)).conf
self.conf_fixture = self.useFixture(config_fixture.Config(CONF))
self.ctxt = context.RequestContext(user_id='fake', project_id='fake')
self.target = {'user_id': 'fake', 'project_id': 'fake'}
CONF([], default_config_files=[])
policy.reset()
self.addCleanup(policy.reset)
def test_modified_policy_reloads(self):
"""Creates a temporary placement-policy.yaml file and tests
@ -44,8 +46,8 @@ class PlacementPolicyTestCase(testtools.TestCase):
with util.tempdir() as tmpdir:
tmpfilename = os.path.join(tmpdir, 'placement-policy.yaml')
self.conf.set_default(
'policy_file', tmpfilename, group='placement')
self.conf_fixture.config(
group='placement', policy_file=tmpfilename)
action = 'placement:test'
# Expect PolicyNotRegistered since defaults are not yet loaded.