]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
tests: initialize policy in BaseTestCase
authorIhar Hrachyshka <ihrachys@redhat.com>
Fri, 27 Feb 2015 14:19:47 +0000 (15:19 +0100)
committerIhar Hrachyshka <ihrachys@redhat.com>
Fri, 27 Feb 2015 17:11:55 +0000 (18:11 +0100)
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
neutron/tests/functional/api/test_policies.py
neutron/tests/unit/test_api_v2.py
neutron/tests/unit/test_config.py
neutron/tests/unit/test_netns_cleanup.py
neutron/tests/unit/test_ovs_cleanup.py
neutron/tests/unit/test_policy.py

index 740119581e710e2d47e9fca6a187f72f5080f8a8..6886af9e3cf5f2c95c8c9a2842b141c596b15027 100644 (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.
 
index 4a692394a4d8c319a5db5de64e66831e5e8090a0..c9d4a99dcd848c1bc72e3613ced6b5ac2f1805bd 100644 (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()
index ecbbbcc975d702e6c279ea6d2e3a13c4b0e6c259..75d2a26e251292594325e6f9863fb59b252990b3 100644 (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)
index ec58a25224b8a1690d2996caea5e7b39e15f5d3f..52521ac9d818b4b9107d162b3aef4bdffbfbd647 100644 (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:
index 244e4a978d29f1b31cfccc7de5e703470464be5d..38894a017be0b5052d121c1707f922d2e0d7fd83 100644 (file)
 
 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'
index fb9a0eaad84bd18ee5f138ee77fd5efddab5cbda..591f9204ccd82de0cfe22d9631d89a31e86fc171 100644 (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']
index 17af7c44e8b964c43166f06130d28f5244dc9592..399f642dcfe9619cd23931eb00d1e492598250e3 100644 (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)