[test][unit]creating resources support set project_id

To write a new unit test, need to set "project_id", and then
use the discarded "tenant_id" is not appropriate.

this patch updated creating resources method, both "project_id"
and "tenant_id" are acceptable. of course, "project_id" priority.

Closes-bug: #1966354

Change-Id: Ic24f03da169dd3d1549b05b35ec77d3e9a25f17b
This commit is contained in:
zhouhenglc 2022-03-23 21:11:21 +08:00 committed by ZhouHeng
parent 1b68aebaba
commit cc50b45ca3
6 changed files with 38 additions and 8 deletions

View File

@ -108,6 +108,8 @@ class MeteringPluginDbTestCaseMixin(object):
@contextlib.contextmanager @contextlib.contextmanager
def metering_label(self, name='label', description='desc', def metering_label(self, name='label', description='desc',
fmt=None, **kwargs): fmt=None, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
if not fmt: if not fmt:
fmt = self.fmt fmt = self.fmt
metering_label = self._make_metering_label(fmt, name, metering_label = self._make_metering_label(fmt, name,
@ -117,6 +119,8 @@ class MeteringPluginDbTestCaseMixin(object):
@contextlib.contextmanager @contextlib.contextmanager
def metering_label_rule(self, metering_label_id=None, direction='ingress', def metering_label_rule(self, metering_label_id=None, direction='ingress',
excluded=False, fmt=None, **kwargs): excluded=False, fmt=None, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
if not fmt: if not fmt:
fmt = self.fmt fmt = self.fmt
metering_label_rule = self._make_metering_label_rule(fmt, metering_label_rule = self._make_metering_label_rule(fmt,

View File

@ -746,6 +746,8 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
admin_state_up=True, admin_state_up=True,
fmt=None, fmt=None,
**kwargs): **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
network = self._make_network(fmt or self.fmt, name, network = self._make_network(fmt or self.fmt, name,
admin_state_up, **kwargs) admin_state_up, **kwargs)
yield network yield network
@ -766,9 +768,11 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
ipv6_ra_mode=None, ipv6_ra_mode=None,
ipv6_address_mode=None, ipv6_address_mode=None,
tenant_id=None, tenant_id=None,
project_id=None,
service_types=None, service_types=None,
set_context=False): set_context=False):
if project_id:
tenant_id = project_id
cidr = netaddr.IPNetwork(cidr) if cidr else None cidr = netaddr.IPNetwork(cidr) if cidr else None
if (gateway_ip is not None and if (gateway_ip is not None and
gateway_ip != constants.ATTR_NOT_SPECIFIED): gateway_ip != constants.ATTR_NOT_SPECIFIED):
@ -797,6 +801,8 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
@contextlib.contextmanager @contextlib.contextmanager
def subnetpool(self, prefixes, admin=False, **kwargs): def subnetpool(self, prefixes, admin=False, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
subnetpool = self._make_subnetpool(self.fmt, subnetpool = self._make_subnetpool(self.fmt,
prefixes, prefixes,
admin, admin,
@ -804,8 +810,10 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
yield subnetpool yield subnetpool
@contextlib.contextmanager @contextlib.contextmanager
def port(self, subnet=None, fmt=None, set_context=False, tenant_id=None, def port(self, subnet=None, fmt=None, set_context=False, project_id=None,
**kwargs): **kwargs):
tenant_id = project_id if project_id else kwargs.pop(
'tenant_id', None)
with optional_ctx( with optional_ctx(
subnet, self.subnet, subnet, self.subnet,
set_context=set_context, tenant_id=tenant_id) as subnet_to_use: set_context=set_context, tenant_id=tenant_id) as subnet_to_use:

View File

@ -78,6 +78,8 @@ class AddressScopeTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
@contextlib.contextmanager @contextlib.contextmanager
def address_scope(self, ip_version=constants.IP_VERSION_4, def address_scope(self, ip_version=constants.IP_VERSION_4,
admin=False, **kwargs): admin=False, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
addr_scope = self._make_address_scope(self.fmt, ip_version, addr_scope = self._make_address_scope(self.fmt, ip_version,
admin, **kwargs) admin, **kwargs)
yield addr_scope yield addr_scope

View File

@ -458,9 +458,11 @@ class L3NatTestCaseMixin(object):
@contextlib.contextmanager @contextlib.contextmanager
def router(self, name='router1', admin_state_up=True, def router(self, name='router1', admin_state_up=True,
fmt=None, tenant_id=None, fmt=None, project_id=None,
external_gateway_info=None, set_context=False, external_gateway_info=None, set_context=False,
**kwargs): **kwargs):
tenant_id = project_id if project_id else kwargs.pop(
'tenant_id', None)
router = self._make_router(fmt or self.fmt, tenant_id, name, router = self._make_router(fmt or self.fmt, tenant_id, name,
admin_state_up, external_gateway_info, admin_state_up, external_gateway_info,
set_context, **kwargs) set_context, **kwargs)
@ -520,7 +522,9 @@ class L3NatTestCaseMixin(object):
@contextlib.contextmanager @contextlib.contextmanager
def floatingip_with_assoc(self, port_id=None, fmt=None, fixed_ip=None, def floatingip_with_assoc(self, port_id=None, fmt=None, fixed_ip=None,
public_cidr='11.0.0.0/24', set_context=False, public_cidr='11.0.0.0/24', set_context=False,
tenant_id=None, flavor_id=None, **kwargs): project_id=None, flavor_id=None, **kwargs):
tenant_id = project_id if project_id else kwargs.pop(
'tenant_id', None)
with self.subnet(cidr=public_cidr, with self.subnet(cidr=public_cidr,
set_context=set_context, set_context=set_context,
tenant_id=tenant_id) as public_sub: tenant_id=tenant_id) as public_sub:
@ -566,6 +570,8 @@ class L3NatTestCaseMixin(object):
def floatingip_no_assoc_with_public_sub(self, private_sub, fmt=None, def floatingip_no_assoc_with_public_sub(self, private_sub, fmt=None,
set_context=False, public_sub=None, set_context=False, public_sub=None,
flavor_id=None, **kwargs): flavor_id=None, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
self._set_net_external(public_sub['subnet']['network_id']) self._set_net_external(public_sub['subnet']['network_id'])
args_list = {} args_list = {}
if flavor_id: if flavor_id:
@ -598,6 +604,8 @@ class L3NatTestCaseMixin(object):
@contextlib.contextmanager @contextlib.contextmanager
def floatingip_no_assoc(self, private_sub, fmt=None, def floatingip_no_assoc(self, private_sub, fmt=None,
set_context=False, flavor_id=None, **kwargs): set_context=False, flavor_id=None, **kwargs):
if 'project_id' in kwargs:
kwargs['tenant_id'] = kwargs['project_id']
with self.subnet(cidr='12.0.0.0/24') as public_sub: with self.subnet(cidr='12.0.0.0/24') as public_sub:
with self.floatingip_no_assoc_with_public_sub( with self.floatingip_no_assoc_with_public_sub(
private_sub, fmt, set_context, public_sub, private_sub, fmt, set_context, public_sub,

View File

@ -33,7 +33,9 @@ class SubnetOnboardTestsBase(object):
@contextlib.contextmanager @contextlib.contextmanager
def address_scope(self, ip_version, prefixes=None, shared=False, def address_scope(self, ip_version, prefixes=None, shared=False,
admin=True, name='test-scope', is_default_pool=False, admin=True, name='test-scope', is_default_pool=False,
tenant_id=None, **kwargs): project_id=None, **kwargs):
tenant_id = project_id if project_id else kwargs.get(
'tenant_id', None)
if not tenant_id: if not tenant_id:
tenant_id = _uuid() tenant_id = _uuid()
@ -46,8 +48,10 @@ class SubnetOnboardTestsBase(object):
@contextlib.contextmanager @contextlib.contextmanager
def subnetpool(self, ip_version, prefixes=None, shared=False, admin=True, def subnetpool(self, ip_version, prefixes=None, shared=False, admin=True,
name='test-pool', is_default_pool=False, tenant_id=None, name='test-pool', is_default_pool=False, project_id=None,
address_scope_id=None, **kwargs): address_scope_id=None, **kwargs):
tenant_id = project_id if project_id else kwargs.get(
'tenant_id', None)
if not tenant_id: if not tenant_id:
tenant_id = _uuid() tenant_id = _uuid()
pool_data = {'tenant_id': tenant_id, 'shared': shared, 'name': name, pool_data = {'tenant_id': tenant_id, 'shared': shared, 'name': name,

View File

@ -32,7 +32,9 @@ class SubnetpoolPrefixOpsTestBase(object):
@contextlib.contextmanager @contextlib.contextmanager
def address_scope(self, ip_version, prefixes=None, shared=False, def address_scope(self, ip_version, prefixes=None, shared=False,
admin=True, name='test-scope', is_default_pool=False, admin=True, name='test-scope', is_default_pool=False,
tenant_id=None, **kwargs): project_id=None, **kwargs):
tenant_id = project_id if project_id else kwargs.get(
'tenant_id', None)
if not tenant_id: if not tenant_id:
tenant_id = _uuid() tenant_id = _uuid()
@ -45,8 +47,10 @@ class SubnetpoolPrefixOpsTestBase(object):
@contextlib.contextmanager @contextlib.contextmanager
def subnetpool(self, ip_version, prefixes=None, shared=False, admin=True, def subnetpool(self, ip_version, prefixes=None, shared=False, admin=True,
name='test-pool', is_default_pool=False, tenant_id=None, name='test-pool', is_default_pool=False, project_id=None,
address_scope_id=None, **kwargs): address_scope_id=None, **kwargs):
tenant_id = project_id if project_id else kwargs.get(
'tenant_id', None)
if not tenant_id: if not tenant_id:
tenant_id = _uuid() tenant_id = _uuid()
pool_data = {'tenant_id': tenant_id, 'shared': shared, 'name': name, pool_data = {'tenant_id': tenant_id, 'shared': shared, 'name': name,