From 53ad6251793b2662044a792564a9c789b25bbe04 Mon Sep 17 00:00:00 2001 From: Boden R Date: Tue, 31 Oct 2017 15:04:55 -0600 Subject: [PATCH] cleanup unit test usage of api extension maps We cleaned up most usage of global and extension resource attribute maps in neutron with I2586f0b11b107d7f57214a0d65bcf7c38a5f0ebb This patch follows suit in vmware-nsx by cleaning up modification of attribute maps in the tests and instead using the fixture in parent class setup method. Change-Id: Ia51af39ceacac26455f7f274f28eff598b9e1e19 --- vmware_nsx/plugins/nsx_v3/plugin.py | 3 +- .../tests/unit/extensions/test_maclearning.py | 18 ------ .../tests/unit/extensions/test_networkgw.py | 7 --- .../test_provider_security_groups.py | 14 +---- .../test_secgroup_rule_local_ip_prefix.py | 12 +--- .../extensions/test_security_group_policy.py | 13 ++--- vmware_nsx/tests/unit/nsx_mh/test_plugin.py | 37 ++---------- vmware_nsx/tests/unit/nsx_v/test_plugin.py | 56 ++++--------------- vmware_nsx/tests/unit/nsx_v3/test_plugin.py | 24 ++------ 9 files changed, 30 insertions(+), 154 deletions(-) diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index fa6280e545..f897959c3c 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -153,7 +153,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, extradhcpopt_db.ExtraDhcpOptMixin, dns_db.DNSDbMixin, mac_db.MacLearningDbMixin, - nsx_com_az.NSXAvailabilityZonesPluginCommon): + nsx_com_az.NSXAvailabilityZonesPluginCommon, + l3_attrs_db.ExtraAttributesMixin): __native_bulk_support = True __native_pagination_support = True diff --git a/vmware_nsx/tests/unit/extensions/test_maclearning.py b/vmware_nsx/tests/unit/extensions/test_maclearning.py index c750f8b354..78bfc9def4 100644 --- a/vmware_nsx/tests/unit/extensions/test_maclearning.py +++ b/vmware_nsx/tests/unit/extensions/test_maclearning.py @@ -14,12 +14,10 @@ # under the License. import mock -from neutron.api.v2 import attributes from neutron.extensions import agent from neutron.tests.unit.db import test_db_base_plugin_v2 as test_db_plugin from neutron_lib import context from oslo_config import cfg -import six from vmware_nsx.api_client import version from vmware_nsx.common import sync @@ -31,12 +29,6 @@ from vmware_nsx.tests.unit import test_utils class MacLearningExtensionManager(object): def get_resources(self): - # Add the resources to the global attribute map - # This is done here as the setup process won't - # initialize the main API router which extends - # the global attribute map - attributes.RESOURCE_ATTRIBUTE_MAP.update( - agent.RESOURCE_ATTRIBUTE_MAP) return agent.Agent.get_resources() def get_actions(self): @@ -52,11 +44,6 @@ class MacLearningDBTestCase(test_db_plugin.NeutronDbPluginV2TestCase): def setUp(self): test_utils.override_nsx_ini_full_test() cfg.CONF.set_override('api_extensions_path', vmware.NSXEXT_PATH) - # Save the original RESOURCE_ATTRIBUTE_MAP - self.saved_attr_map = {} - for resource, attrs in six.iteritems( - attributes.RESOURCE_ATTRIBUTE_MAP): - self.saved_attr_map[resource] = attrs.copy() ext_mgr = MacLearningExtensionManager() # mock api client self.fc = fake.FakeClient(vmware.STUBS_PATH) @@ -71,15 +58,10 @@ class MacLearningDBTestCase(test_db_plugin.NeutronDbPluginV2TestCase): instance.return_value.request.side_effect = self.fc.fake_request cfg.CONF.set_override('metadata_mode', None, 'NSX') self.addCleanup(self.fc.reset_all) - self.addCleanup(self.restore_resource_attribute_map) super(MacLearningDBTestCase, self).setUp(plugin=vmware.PLUGIN_NAME, ext_mgr=ext_mgr) self.adminContext = context.get_admin_context() - def restore_resource_attribute_map(self): - # Restore the original RESOURCE_ATTRIBUTE_MAP - attributes.RESOURCE_ATTRIBUTE_MAP = self.saved_attr_map - def test_create_with_mac_learning(self): with self.port(arg_list=('mac_learning_enabled',), mac_learning_enabled=True) as port: diff --git a/vmware_nsx/tests/unit/extensions/test_networkgw.py b/vmware_nsx/tests/unit/extensions/test_networkgw.py index ce380c3ff4..725ae6552b 100644 --- a/vmware_nsx/tests/unit/extensions/test_networkgw.py +++ b/vmware_nsx/tests/unit/extensions/test_networkgw.py @@ -16,7 +16,6 @@ import contextlib import mock from neutron.api import extensions -from neutron.api.v2 import attributes from neutron.db import api as db_api from neutron.db import db_base_plugin_v2 from neutron import quota @@ -48,12 +47,6 @@ _get_path = test_base._get_path class TestExtensionManager(object): def get_resources(self): - # Add the resources to the global attribute map - # This is done here as the setup process won't - # initialize the main API router which extends - # the global attribute map - attributes.RESOURCE_ATTRIBUTE_MAP.update( - networkgw.RESOURCE_ATTRIBUTE_MAP) return networkgw.Networkgw.get_resources() def get_actions(self): diff --git a/vmware_nsx/tests/unit/extensions/test_provider_security_groups.py b/vmware_nsx/tests/unit/extensions/test_provider_security_groups.py index ff1a92e72b..83742c6738 100644 --- a/vmware_nsx/tests/unit/extensions/test_provider_security_groups.py +++ b/vmware_nsx/tests/unit/extensions/test_provider_security_groups.py @@ -15,7 +15,6 @@ import mock import webob.exc -from neutron.api.v2 import attributes as attr from neutron.db import api as db_api from neutron.db import db_base_plugin_v2 from neutron.db import securitygroups_db @@ -133,17 +132,8 @@ class ProviderSecurityGroupExtTestCase( plugin=plugin, ext_mgr=ext_mgr) self._tenant_id = 'foobar' # add provider group attributes - ext_sg.RESOURCE_ATTRIBUTE_MAP['security_groups'].update( - provider_sg.EXTENDED_ATTRIBUTES_2_0['security_groups']) - - attr.RESOURCE_ATTRIBUTE_MAP['ports'].update( - provider_sg.EXTENDED_ATTRIBUTES_2_0['ports']) - - def tearDown(self): - # remove provider security group attributes - del ext_sg.RESOURCE_ATTRIBUTE_MAP['security_groups']['provider'] - del attr.RESOURCE_ATTRIBUTE_MAP['ports']['provider_security_groups'] - super(ProviderSecurityGroupExtTestCase, self).tearDown() + ext_sg.Securitygroup().update_attributes_map( + provider_sg.EXTENDED_ATTRIBUTES_2_0) def _create_provider_security_group(self): body = {'security_group': {'name': 'provider-deny', diff --git a/vmware_nsx/tests/unit/extensions/test_secgroup_rule_local_ip_prefix.py b/vmware_nsx/tests/unit/extensions/test_secgroup_rule_local_ip_prefix.py index b6db6b086c..5d24109977 100644 --- a/vmware_nsx/tests/unit/extensions/test_secgroup_rule_local_ip_prefix.py +++ b/vmware_nsx/tests/unit/extensions/test_secgroup_rule_local_ip_prefix.py @@ -61,16 +61,8 @@ class LocalIPPrefixExtTestCase(test_securitygroup.SecurityGroupDBTestCase): def setUp(self, plugin=PLUGIN_NAME, ext_mgr=None): super(LocalIPPrefixExtTestCase, self).setUp( plugin=plugin, ext_mgr=ext_mgr) - ext_sg.RESOURCE_ATTRIBUTE_MAP['security_group_rules'].update( - ext_loip.RESOURCE_ATTRIBUTE_MAP['security_group_rules']) - - def tearDown(self): - # Remove attributes which were written to global attr map, they may - # interfer with tests for other plugins which doesn't support this - # extension. - del ext_sg.RESOURCE_ATTRIBUTE_MAP[ - 'security_group_rules']['local_ip_prefix'] - super(LocalIPPrefixExtTestCase, self).tearDown() + ext_sg.Securitygroup().update_attributes_map( + ext_loip.RESOURCE_ATTRIBUTE_MAP) def _build_ingress_rule_with_local_ip_prefix(self, security_group_id, local_ip_prefix, diff --git a/vmware_nsx/tests/unit/extensions/test_security_group_policy.py b/vmware_nsx/tests/unit/extensions/test_security_group_policy.py index 10f41fe28e..8eabdf4854 100644 --- a/vmware_nsx/tests/unit/extensions/test_security_group_policy.py +++ b/vmware_nsx/tests/unit/extensions/test_security_group_policy.py @@ -46,15 +46,10 @@ class SecGroupPolicyExtensionTestCase( plugin=plugin, ext_mgr=ext_mgr) self._tenant_id = 'foobar' # add policy & logging security group attribute - ext_sg.RESOURCE_ATTRIBUTE_MAP['security_groups'].update( - ext_policy.RESOURCE_ATTRIBUTE_MAP['security_groups']) - ext_sg.RESOURCE_ATTRIBUTE_MAP['security_groups'].update( - ext_logging.RESOURCE_ATTRIBUTE_MAP['security_groups']) - - def tearDown(self): - # remove policy security group attribute - del ext_sg.RESOURCE_ATTRIBUTE_MAP['security_groups']['policy'] - super(SecGroupPolicyExtensionTestCase, self).tearDown() + ext_sg.Securitygroup().update_attributes_map( + ext_policy.RESOURCE_ATTRIBUTE_MAP) + ext_sg.Securitygroup().update_attributes_map( + ext_logging.RESOURCE_ATTRIBUTE_MAP) def _create_secgroup_with_policy(self, policy_id, description=None, logging=False): diff --git a/vmware_nsx/tests/unit/nsx_mh/test_plugin.py b/vmware_nsx/tests/unit/nsx_mh/test_plugin.py index 60ea13f979..5be1f60ea2 100644 --- a/vmware_nsx/tests/unit/nsx_mh/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_mh/test_plugin.py @@ -16,7 +16,6 @@ import copy import uuid import mock -from neutron.api.v2 import attributes from neutron.extensions import dvr from neutron.extensions import l3 from neutron.extensions import l3_ext_gw_mode @@ -39,7 +38,6 @@ from oslo_config import cfg from oslo_db import exception as db_exc from oslo_log import log from oslo_utils import uuidutils -import six from sqlalchemy import exc as sql_exc import webob.exc @@ -457,15 +455,10 @@ class TestL3ExtensionManager(object): def get_resources(self): # Simulate extension of L3 attribute map - # First apply attribute extensions - for key in l3.RESOURCE_ATTRIBUTE_MAP.keys(): - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - dvr.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - # Finally add l3 resources to the global attribute map - attributes.RESOURCE_ATTRIBUTE_MAP.update( - l3.RESOURCE_ATTRIBUTE_MAP) + l3.L3().update_attributes_map( + l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + dvr.EXTENDED_ATTRIBUTES_2_0) return l3.L3.get_resources() def get_actions(self): @@ -488,31 +481,11 @@ class TestL3SecGrpExtensionManager(TestL3ExtensionManager): return resources -def backup_l3_attribute_map(): - """Return a backup of the original l3 attribute map.""" - return dict((res, attrs.copy()) for - (res, attrs) in six.iteritems(l3.RESOURCE_ATTRIBUTE_MAP)) - - -def restore_l3_attribute_map(map_to_restore): - """Ensure changes made by fake ext mgrs are reverted.""" - l3.RESOURCE_ATTRIBUTE_MAP = map_to_restore - - class L3NatTest(test_l3_plugin.L3BaseForIntTests, NsxPluginV2TestCase): - def _restore_l3_attribute_map(self): - l3.RESOURCE_ATTRIBUTE_MAP = self._l3_attribute_map_bk - def setUp(self, plugin=vmware.PLUGIN_NAME, ext_mgr=None, service_plugins=None): - self._l3_attribute_map_bk = {} - for item in l3.RESOURCE_ATTRIBUTE_MAP: - self._l3_attribute_map_bk[item] = ( - l3.RESOURCE_ATTRIBUTE_MAP[item].copy()) cfg.CONF.set_override('api_extensions_path', vmware.NSXEXT_PATH) - l3_attribute_map_bk = backup_l3_attribute_map() - self.addCleanup(restore_l3_attribute_map, l3_attribute_map_bk) ext_mgr = ext_mgr or TestL3ExtensionManager() super(L3NatTest, self).setUp( plugin=plugin, ext_mgr=ext_mgr, service_plugins=service_plugins) @@ -923,8 +896,6 @@ class NeutronNsxOutOfSync(NsxPluginV2TestCase, ext_sg.SecurityGroupsTestCase): def setUp(self): - l3_attribute_map_bk = backup_l3_attribute_map() - self.addCleanup(restore_l3_attribute_map, l3_attribute_map_bk) super(NeutronNsxOutOfSync, self).setUp( ext_mgr=TestL3SecGrpExtensionManager()) diff --git a/vmware_nsx/tests/unit/nsx_v/test_plugin.py b/vmware_nsx/tests/unit/nsx_v/test_plugin.py index 52119e0b00..310b3589de 100644 --- a/vmware_nsx/tests/unit/nsx_v/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v/test_plugin.py @@ -19,7 +19,6 @@ import copy from eventlet import greenthread import mock import netaddr -from neutron.api.v2 import attributes from neutron.extensions import address_scope from neutron.extensions import dvr as dist_router from neutron.extensions import l3 @@ -39,7 +38,6 @@ import neutron.tests.unit.extensions.test_l3_ext_gw_mode as test_ext_gw_mode import neutron.tests.unit.extensions.test_portsecurity as test_psec import neutron.tests.unit.extensions.test_securitygroup as ext_sg from neutron.tests.unit import testlib_api -from neutron_lib.api.definitions import address_scope as addr_apidef from neutron_lib.api.definitions import allowedaddresspairs as addrp_apidef from neutron_lib.api.definitions import external_net as extnet_apidef from neutron_lib.api.definitions import extra_dhcp_opt as edo_ext @@ -70,7 +68,6 @@ from vmware_nsx.dvs import dvs from vmware_nsx.dvs import dvs_utils from vmware_nsx.extensions import routersize as router_size from vmware_nsx.extensions import routertype as router_type -from vmware_nsx.extensions import securitygrouplogging from vmware_nsx.extensions import vnicindex as ext_vnic_idx from vmware_nsx.plugins.nsx_v import availability_zones as nsx_az from vmware_nsx.plugins.nsx_v.drivers import ( @@ -2097,25 +2094,18 @@ class TestL3ExtensionManager(object): def get_resources(self): # Simulate extension of L3 attribute map - # First apply attribute extensions - for key in l3.RESOURCE_ATTRIBUTE_MAP.keys(): - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - dist_router.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - router_type.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - router_size.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - router_availability_zone.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - l3_flavors.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - # Finally add l3 resources to the global attribute map - attributes.RESOURCE_ATTRIBUTE_MAP.update( - l3.RESOURCE_ATTRIBUTE_MAP) - attributes.RESOURCE_ATTRIBUTE_MAP.update( - addr_apidef.RESOURCE_ATTRIBUTE_MAP) + l3.L3().update_attributes_map( + l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + dist_router.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + router_type.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + router_size.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + router_availability_zone.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + l3_flavors.EXTENDED_ATTRIBUTES_2_0) return (l3.L3.get_resources() + address_scope.Address_scope.get_resources()) @@ -2126,32 +2116,12 @@ class TestL3ExtensionManager(object): return [] -def backup_l3_attribute_map(): - """Return a backup of the original l3 attribute map.""" - return dict((res, attrs.copy()) for - (res, attrs) in six.iteritems(l3.RESOURCE_ATTRIBUTE_MAP)) - - -def restore_l3_attribute_map(map_to_restore): - """Ensure changes made by fake ext mgrs are reverted.""" - l3.RESOURCE_ATTRIBUTE_MAP = map_to_restore - - class L3NatTest(test_l3_plugin.L3BaseForIntTests, NsxVPluginV2TestCase): - def _restore_l3_attribute_map(self): - l3.RESOURCE_ATTRIBUTE_MAP = self._l3_attribute_map_bk - def setUp(self, plugin=PLUGIN_NAME, ext_mgr=None, service_plugins=None): - self._l3_attribute_map_bk = {} - for item in l3.RESOURCE_ATTRIBUTE_MAP: - self._l3_attribute_map_bk[item] = ( - l3.RESOURCE_ATTRIBUTE_MAP[item].copy()) cfg.CONF.set_override('task_status_check_interval', 200, group="nsxv") cfg.CONF.set_override('api_extensions_path', vmware.NSXEXT_PATH) - l3_attribute_map_bk = backup_l3_attribute_map() - self.addCleanup(restore_l3_attribute_map, l3_attribute_map_bk) ext_mgr = ext_mgr or TestL3ExtensionManager() super(L3NatTest, self).setUp( plugin=plugin, ext_mgr=ext_mgr, service_plugins=service_plugins) @@ -3850,8 +3820,6 @@ class NsxVSecurityGroupsTestCase(ext_sg.SecurityGroupDBTestCase): ext_mgr=None, service_plugins=None): test_utils.override_nsx_ini_test() - attributes.RESOURCE_ATTRIBUTE_MAP.update( - securitygrouplogging.RESOURCE_ATTRIBUTE_MAP) mock_vcns = mock.patch(vmware.VCNS_NAME, autospec=True) mock_vcns_instance = mock_vcns.start() self.fc2 = fake_vcns.FakeVcns() diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 6c0730bad7..0582f7cf8e 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -13,11 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - import mock import netaddr -from neutron.api.v2 import attributes from neutron.db import models_v2 from neutron.extensions import address_scope from neutron.extensions import l3 @@ -33,7 +30,6 @@ from neutron.tests.unit.extensions \ import test_l3_ext_gw_mode as test_ext_gw_mode from neutron.tests.unit.scheduler \ import test_dhcp_agent_scheduler as test_dhcpagent -from neutron_lib.api.definitions import address_scope as addr_apidef from neutron_lib.api.definitions import external_net as extnet_apidef from neutron_lib.api.definitions import extraroute as xroute_apidef from neutron_lib.api.definitions import port_security as psec @@ -958,17 +954,10 @@ class TestL3ExtensionManager(object): def get_resources(self): # Simulate extension of L3 attribute map - # First apply attribute extensions - for key in l3.RESOURCE_ATTRIBUTE_MAP.keys(): - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0.get(key, {})) - l3.RESOURCE_ATTRIBUTE_MAP[key].update( - xroute_apidef.RESOURCE_ATTRIBUTE_MAP.get(key, {})) - # Finally add l3 resources to the global attribute map - attributes.RESOURCE_ATTRIBUTE_MAP.update( - l3.RESOURCE_ATTRIBUTE_MAP) - attributes.RESOURCE_ATTRIBUTE_MAP.update( - addr_apidef.RESOURCE_ATTRIBUTE_MAP) + l3.L3().update_attributes_map( + l3_ext_gw_mode.EXTENDED_ATTRIBUTES_2_0) + l3.L3().update_attributes_map( + xroute_apidef.RESOURCE_ATTRIBUTE_MAP) return (l3.L3.get_resources() + address_scope.Address_scope.get_resources()) @@ -982,15 +971,10 @@ class TestL3ExtensionManager(object): class L3NatTest(test_l3_plugin.L3BaseForIntTests, NsxV3PluginTestCaseMixin, test_address_scope.AddressScopeTestCase): - def _restore(self): - l3.RESOURCE_ATTRIBUTE_MAP = self._backup - def setUp(self, plugin=PLUGIN_NAME, ext_mgr=None, service_plugins=None): - self._backup = copy.deepcopy(l3.RESOURCE_ATTRIBUTE_MAP) cfg.CONF.set_override('api_extensions_path', vmware.NSXEXT_PATH) cfg.CONF.set_default('max_routes', 3) - self.addCleanup(self._restore) ext_mgr = ext_mgr or TestL3ExtensionManager() mock_nsx_version = mock.patch.object(nsx_plugin.utils, 'is_nsx_version_2_0_0',