From 1349891fce528365e5d1abce95d019ce40275234 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 27 Feb 2015 15:19:47 +0100 Subject: [PATCH] tests: initialize policy in BaseTestCase This is needed to avoid test cases breaking policy file search code from oslo.config by mocking out stdlib functions from os module like os.path.isdir or os.path.exists. This also allows us to remove explicit policy setup from test_api_v2 and test_policy files. Note that for test_netns_cleanup, test_ovs_cleanup, and test_config, we removed test_setup_conf test cases. They test a function that is used in other test cases only, and hence do not belong to the suite. This allows us to avoid hacks around those test cases that do not play nice with global config-file options we set in BaseTestCase. Change-Id: If14a3c741837193ad104467f0cf4486a6a386e6d Closes-Bug: #1426369 --- neutron/tests/base.py | 4 ++++ neutron/tests/functional/api/test_policies.py | 1 - neutron/tests/unit/test_api_v2.py | 7 +++---- neutron/tests/unit/test_config.py | 4 ---- neutron/tests/unit/test_netns_cleanup.py | 10 ---------- neutron/tests/unit/test_ovs_cleanup.py | 10 ---------- neutron/tests/unit/test_policy.py | 16 ++++++---------- 7 files changed, 13 insertions(+), 39 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 740119581..6886af9e3 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -33,6 +33,7 @@ from oslo_messaging import conffixture as messaging_conffixture from neutron.common import config from neutron.common import rpc as n_rpc +from neutron import policy from neutron.tests import fake_notifier from neutron.tests import sub_base @@ -104,6 +105,9 @@ class BaseTestCase(sub_base.SubBaseTestCase): self.setup_rpc_mocks() self.setup_config() + policy.init() + self.addCleanup(policy.reset) + def get_new_temp_dir(self): """Create a new temporary directory. diff --git a/neutron/tests/functional/api/test_policies.py b/neutron/tests/functional/api/test_policies.py index 4a692394a..c9d4a99dc 100644 --- a/neutron/tests/functional/api/test_policies.py +++ b/neutron/tests/functional/api/test_policies.py @@ -94,7 +94,6 @@ class APIPolicyTestCase(base.BaseTestCase): True) def tearDown(self): - policy.reset() if self.ATTRIBUTE_MAP_COPY: attributes.RESOURCE_ATTRIBUTE_MAP = self.ATTRIBUTE_MAP_COPY super(APIPolicyTestCase, self).tearDown() diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index ecbbbcc97..75d2a26e2 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -113,6 +113,9 @@ class APIv2TestBase(base.BaseTestCase, testlib_plugin.PluginSetupHelper): cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', group='QUOTAS') + # APIRouter initialization resets policy module, re-initializing it + policy.init() + class _ArgMatcher(object): """An adapter to assist mock assertions, used to custom compare.""" @@ -518,8 +521,6 @@ class APIv2TestCase(APIv2TestBase): # Note: since all resources use the same controller and validation # logic, we actually get really good coverage from testing just networks. class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): - def setUp(self): - super(JSONV2TestCase, self).setUp() def _test_list(self, req_tenant_id, real_tenant_id): env = {} @@ -1027,8 +1028,6 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): def test_get_keystone_strip_admin_only_attribute(self): tenant_id = _uuid() # Inject rule in policy engine - policy.init() - self.addCleanup(policy.reset) rules = {'get_network:name': common_policy.parse_rule( "rule:admin_only")} policy.set_rules(rules, overwrite=False) diff --git a/neutron/tests/unit/test_config.py b/neutron/tests/unit/test_config.py index ec58a2522..52521ac9d 100644 --- a/neutron/tests/unit/test_config.py +++ b/neutron/tests/unit/test_config.py @@ -22,10 +22,6 @@ from neutron.tests import base class ConfigurationTest(base.BaseTestCase): - def setup_config(self): - # don't use default config - pass - def test_load_paste_app_not_found(self): self.config(api_paste_config='no_such_file.conf') with mock.patch.object(cfg.CONF, 'find_file', return_value=None) as ff: diff --git a/neutron/tests/unit/test_netns_cleanup.py b/neutron/tests/unit/test_netns_cleanup.py index 244e4a978..38894a017 100644 --- a/neutron/tests/unit/test_netns_cleanup.py +++ b/neutron/tests/unit/test_netns_cleanup.py @@ -15,22 +15,12 @@ import mock -from neutron.agent.linux import interface from neutron.cmd import netns_cleanup as util from neutron.tests import base class TestNetnsCleanup(base.BaseTestCase): - def setup_config(self): - # don't use default config - pass - - def test_setup_conf(self): - expected_opts = interface.OPTS - conf = util.setup_conf() - self.assertTrue(all([opt.name in conf for opt in expected_opts])) - def test_kill_dhcp(self, dhcp_active=True): conf = mock.Mock() conf.dhcp_driver = 'driver' diff --git a/neutron/tests/unit/test_ovs_cleanup.py b/neutron/tests/unit/test_ovs_cleanup.py index fb9a0eaad..591f9204c 100644 --- a/neutron/tests/unit/test_ovs_cleanup.py +++ b/neutron/tests/unit/test_ovs_cleanup.py @@ -26,16 +26,6 @@ from neutron.tests import base class TestOVSCleanup(base.BaseTestCase): - def setup_config(self): - # don't use default config - pass - - def test_setup_conf(self): - conf = util.setup_conf() - self.assertEqual(conf.external_network_bridge, 'br-ex') - self.assertEqual(conf.ovs_integration_bridge, 'br-int') - self.assertFalse(conf.ovs_all_ports) - def test_main(self): bridges = ['br-int', 'br-ex'] ports = ['p1', 'p2', 'p3'] diff --git a/neutron/tests/unit/test_policy.py b/neutron/tests/unit/test_policy.py index 17af7c44e..399f642dc 100644 --- a/neutron/tests/unit/test_policy.py +++ b/neutron/tests/unit/test_policy.py @@ -40,7 +40,6 @@ from neutron.tests import base class PolicyFileTestCase(base.BaseTestCase): def setUp(self): super(PolicyFileTestCase, self).setUp() - self.addCleanup(policy.reset) self.context = context.Context('fake', 'fake', is_admin=False) self.target = {'tenant_id': 'fake'} @@ -66,7 +65,6 @@ class PolicyFileTestCase(base.BaseTestCase): class PolicyTestCase(base.BaseTestCase): def setUp(self): super(PolicyTestCase, self).setUp() - self.addCleanup(policy.reset) # NOTE(vish): preload rules to circumvent reloading from file rules = { "true": '@', @@ -174,7 +172,6 @@ class DefaultPolicyTestCase(base.BaseTestCase): jsonutils.dump(self.rules, policyfile) cfg.CONF.set_override('policy_file', tmpfilename) policy.refresh() - self.addCleanup(policy.reset) self.context = context.Context('fake', 'fake') @@ -201,10 +198,13 @@ FAKE_RESOURCE = {"%ss" % FAKE_RESOURCE_NAME: class NeutronPolicyTestCase(base.BaseTestCase): + def fakepolicyinit(self, **kwargs): + enf = policy._ENFORCER + enf.set_rules(common_policy.Rules(self.rules)) + def setUp(self): super(NeutronPolicyTestCase, self).setUp() policy.refresh() - self.addCleanup(policy.reset) self.admin_only_legacy = "role:admin" self.admin_or_owner_legacy = "role:admin or tenant_id:%(tenant_id)s" # Add a Fake 'something' resource to RESOURCE_ATTRIBUTE_MAP @@ -245,16 +245,12 @@ class NeutronPolicyTestCase(base.BaseTestCase): "rule:shared" }.items()) - def fakepolicyinit(**kwargs): - enf = policy._ENFORCER - enf.set_rules(common_policy.Rules(self.rules)) - def remove_fake_resource(): del attributes.RESOURCE_ATTRIBUTE_MAP["%ss" % FAKE_RESOURCE_NAME] self.patcher = mock.patch.object(neutron.policy, 'init', - new=fakepolicyinit) + new=self.fakepolicyinit) self.patcher.start() self.addCleanup(remove_fake_resource) self.context = context.Context('fake', 'fake', roles=['user']) @@ -500,7 +496,7 @@ class NeutronPolicyTestCase(base.BaseTestCase): # Trigger a policy with rule admin_or_owner action = "create_network" target = {'tenant_id': 'fake'} - policy.init() + self.fakepolicyinit() self.assertRaises(exceptions.PolicyCheckError, policy.enforce, self.context, action, target) -- 2.45.2