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
This commit is contained in:
Ihar Hrachyshka 2015-02-27 15:19:47 +01:00
parent 1404f33b50
commit 1349891fce
7 changed files with 13 additions and 39 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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)

View File

@ -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:

View File

@ -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'

View File

@ -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']

View File

@ -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)