From 868e443b09b397637c17ecf92537d78fa3a52fd0 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Sun, 9 Sep 2018 11:36:48 -0600 Subject: [PATCH] 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 --- placement/policy.py | 4 +--- placement/tests/unit/policy_fixture.py | 12 +++++++----- placement/tests/unit/test_policy.py | 8 +++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/placement/policy.py b/placement/policy.py index c9d323283..1f223f78a 100644 --- a/placement/policy.py +++ b/placement/policy.py @@ -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 diff --git a/placement/tests/unit/policy_fixture.py b/placement/tests/unit/policy_fixture.py index e005e5c81..5e2aa9478 100644 --- a/placement/tests/unit/policy_fixture.py +++ b/placement/tests/unit/policy_fixture.py @@ -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) diff --git a/placement/tests/unit/test_policy.py b/placement/tests/unit/test_policy.py index 5a98c6f00..5edd3f48d 100644 --- a/placement/tests/unit/test_policy.py +++ b/placement/tests/unit/test_policy.py @@ -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.