Merge "Move keys() methods in each resource class to FakeResource"

This commit is contained in:
Jenkins 2016-04-11 15:07:33 +00:00 committed by Gerrit Code Review
commit 3be49a8abe
4 changed files with 63 additions and 166 deletions

View File

@ -183,6 +183,9 @@ class FakeResource(object):
info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys)
return "<%s %s>" % (self.__class__.__name__, info)
def keys(self):
return self._info.keys()
class FakeResponse(requests.Response):

View File

@ -75,13 +75,11 @@ class FakeAvailabilityZone(object):
"""Fake one or more network availability zones (AZs)."""
@staticmethod
def create_one_availability_zone(attrs={}, methods={}):
def create_one_availability_zone(attrs={}):
"""Create a fake AZ.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object with name, state, etc.
"""
@ -97,18 +95,15 @@ class FakeAvailabilityZone(object):
availability_zone = fakes.FakeResource(
info=copy.deepcopy(availability_zone),
methods=methods,
loaded=True)
return availability_zone
@staticmethod
def create_availability_zones(attrs={}, methods={}, count=2):
def create_availability_zones(attrs={}, count=2):
"""Create multiple fake AZs.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of AZs to fake
:return:
@ -117,8 +112,7 @@ class FakeAvailabilityZone(object):
availability_zones = []
for i in range(0, count):
availability_zone = \
FakeAvailabilityZone.create_one_availability_zone(
attrs, methods)
FakeAvailabilityZone.create_one_availability_zone(attrs)
availability_zones.append(availability_zone)
return availability_zones
@ -128,13 +122,11 @@ class FakeNetwork(object):
"""Fake one or more networks."""
@staticmethod
def create_one_network(attrs={}, methods={}):
def create_one_network(attrs={}):
"""Create a fake network.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, admin_state_up,
router_external, status, subnets, tenant_id
@ -158,18 +150,7 @@ class FakeNetwork(object):
# Overwrite default attributes.
network_attrs.update(attrs)
# Set default methods.
network_methods = {
'keys': ['id', 'name', 'admin_state_up', 'router_external',
'status', 'subnets', 'tenant_id', 'availability_zones',
'availability_zone_hints', 'is_default'],
}
# Overwrite default methods.
network_methods.update(methods)
network = fakes.FakeResource(info=copy.deepcopy(network_attrs),
methods=copy.deepcopy(network_methods),
loaded=True)
# Set attributes with special mapping in OpenStack SDK.
@ -178,13 +159,11 @@ class FakeNetwork(object):
return network
@staticmethod
def create_networks(attrs={}, methods={}, count=2):
def create_networks(attrs={}, count=2):
"""Create multiple fake networks.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of networks to fake
:return:
@ -192,7 +171,7 @@ class FakeNetwork(object):
"""
networks = []
for i in range(0, count):
networks.append(FakeNetwork.create_one_network(attrs, methods))
networks.append(FakeNetwork.create_one_network(attrs))
return networks
@ -220,7 +199,7 @@ class FakePort(object):
"""Fake one or more ports."""
@staticmethod
def create_one_port(attrs={}, methods={}):
def create_one_port(attrs={}):
"""Create a fake port.
:param Dictionary attrs:
@ -258,23 +237,7 @@ class FakePort(object):
# Overwrite default attributes.
port_attrs.update(attrs)
# Set default methods.
port_methods = {
'keys': ['admin_state_up', 'allowed_address_pairs',
'binding:host_id', 'binding:profile',
'binding:vif_details', 'binding:vif_type',
'binding:vnic_type', 'device_id', 'device_owner',
'dns_assignment', 'dns_name', 'extra_dhcp_opts',
'fixed_ips', 'id', 'mac_address', 'name',
'network_id', 'port_security_enabled',
'security_groups', 'status', 'tenant_id'],
}
# Overwrite default methods.
port_methods.update(methods)
port = fakes.FakeResource(info=copy.deepcopy(port_attrs),
methods=copy.deepcopy(port_methods),
loaded=True)
# Set attributes with special mappings in OpenStack SDK.
@ -288,13 +251,11 @@ class FakePort(object):
return port
@staticmethod
def create_ports(attrs={}, methods={}, count=2):
def create_ports(attrs={}, count=2):
"""Create multiple fake ports.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of ports to fake
:return:
@ -302,7 +263,7 @@ class FakePort(object):
"""
ports = []
for i in range(0, count):
ports.append(FakePort.create_one_port(attrs, methods))
ports.append(FakePort.create_one_port(attrs))
return ports
@ -330,13 +291,11 @@ class FakeRouter(object):
"""Fake one or more routers."""
@staticmethod
def create_one_router(attrs={}, methods={}):
def create_one_router(attrs={}):
"""Create a fake router.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, admin_state_up,
status, tenant_id
@ -359,17 +318,7 @@ class FakeRouter(object):
# Overwrite default attributes.
router_attrs.update(attrs)
# Set default methods.
router_methods = {
'keys': ['id', 'name', 'admin_state_up', 'distributed', 'ha',
'tenant_id'],
}
# Overwrite default methods.
router_methods.update(methods)
router = fakes.FakeResource(info=copy.deepcopy(router_attrs),
methods=copy.deepcopy(router_methods),
loaded=True)
# Set attributes with special mapping in OpenStack SDK.
@ -378,13 +327,11 @@ class FakeRouter(object):
return router
@staticmethod
def create_routers(attrs={}, methods={}, count=2):
def create_routers(attrs={}, count=2):
"""Create multiple fake routers.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of routers to fake
:return:
@ -392,7 +339,7 @@ class FakeRouter(object):
"""
routers = []
for i in range(0, count):
routers.append(FakeRouter.create_one_router(attrs, methods))
routers.append(FakeRouter.create_one_router(attrs))
return routers
@ -420,20 +367,16 @@ class FakeSecurityGroup(object):
"""Fake one or more security groups."""
@staticmethod
def create_one_security_group(attrs=None, methods=None):
def create_one_security_group(attrs=None):
"""Create a fake security group.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, etc.
"""
if attrs is None:
attrs = {}
if methods is None:
methods = {}
# Set default attributes.
security_group_attrs = {
@ -447,18 +390,8 @@ class FakeSecurityGroup(object):
# Overwrite default attributes.
security_group_attrs.update(attrs)
# Set default methods.
security_group_methods = {
'keys': ['id', 'name', 'description', 'tenant_id',
'security_group_rules'],
}
# Overwrite default methods.
security_group_methods.update(methods)
security_group = fakes.FakeResource(
info=copy.deepcopy(security_group_attrs),
methods=copy.deepcopy(security_group_methods),
loaded=True)
# Set attributes with special mapping in OpenStack SDK.
@ -467,13 +400,11 @@ class FakeSecurityGroup(object):
return security_group
@staticmethod
def create_security_groups(attrs=None, methods=None, count=2):
def create_security_groups(attrs=None, count=2):
"""Create multiple fake security groups.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of security groups to fake
:return:
@ -482,7 +413,7 @@ class FakeSecurityGroup(object):
security_groups = []
for i in range(0, count):
security_groups.append(
FakeSecurityGroup.create_one_security_group(attrs, methods))
FakeSecurityGroup.create_one_security_group(attrs))
return security_groups
@ -491,20 +422,16 @@ class FakeSecurityGroupRule(object):
"""Fake one or more security group rules."""
@staticmethod
def create_one_security_group_rule(attrs=None, methods=None):
def create_one_security_group_rule(attrs=None):
"""Create a fake security group rule.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, etc.
"""
if attrs is None:
attrs = {}
if methods is None:
methods = {}
# Set default attributes.
security_group_rule_attrs = {
@ -523,19 +450,8 @@ class FakeSecurityGroupRule(object):
# Overwrite default attributes.
security_group_rule_attrs.update(attrs)
# Set default methods.
security_group_rule_methods = {
'keys': ['direction', 'ethertype', 'id', 'port_range_max',
'port_range_min', 'protocol', 'remote_group_id',
'remote_ip_prefix', 'security_group_id', 'tenant_id'],
}
# Overwrite default methods.
security_group_rule_methods.update(methods)
security_group_rule = fakes.FakeResource(
info=copy.deepcopy(security_group_rule_attrs),
methods=copy.deepcopy(security_group_rule_methods),
loaded=True)
# Set attributes with special mapping in OpenStack SDK.
@ -544,13 +460,11 @@ class FakeSecurityGroupRule(object):
return security_group_rule
@staticmethod
def create_security_group_rules(attrs=None, methods=None, count=2):
def create_security_group_rules(attrs=None, count=2):
"""Create multiple fake security group rules.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of security group rules to fake
:return:
@ -559,8 +473,7 @@ class FakeSecurityGroupRule(object):
security_group_rules = []
for i in range(0, count):
security_group_rules.append(
FakeSecurityGroupRule.create_one_security_group_rule(
attrs, methods))
FakeSecurityGroupRule.create_one_security_group_rule(attrs))
return security_group_rules
@ -569,13 +482,11 @@ class FakeSubnet(object):
"""Fake one or more subnets."""
@staticmethod
def create_one_subnet(attrs={}, methods={}):
def create_one_subnet(attrs={}):
"""Create a fake subnet.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object faking the subnet
"""
@ -601,19 +512,7 @@ class FakeSubnet(object):
# Overwrite default attributes.
subnet_attrs.update(attrs)
# Set default methods.
subnet_methods = {
'keys': ['id', 'name', 'network_id', 'cidr', 'enable_dhcp',
'allocation_pools', 'dns_nameservers', 'gateway_ip',
'host_routes', 'ip_version', 'tenant_id',
'ipv6_address_mode', 'ipv6_ra_mode', 'subnetpool_id']
}
# Overwrite default methods.
subnet_methods.update(methods)
subnet = fakes.FakeResource(info=copy.deepcopy(subnet_attrs),
methods=copy.deepcopy(subnet_methods),
loaded=True)
# Set attributes with special mappings in OpenStack SDK.
subnet.project_id = subnet_attrs['tenant_id']
@ -621,13 +520,11 @@ class FakeSubnet(object):
return subnet
@staticmethod
def create_subnets(attrs={}, methods={}, count=2):
def create_subnets(attrs={}, count=2):
"""Create multiple fake subnets.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of subnets to fake
:return:
@ -635,7 +532,7 @@ class FakeSubnet(object):
"""
subnets = []
for i in range(0, count):
subnets.append(FakeSubnet.create_one_subnet(attrs, methods))
subnets.append(FakeSubnet.create_one_subnet(attrs))
return subnets
@ -644,13 +541,11 @@ class FakeFloatingIP(object):
"""Fake one or more floating ip."""
@staticmethod
def create_one_floating_ip(attrs={}, methods={}):
def create_one_floating_ip(attrs={}):
"""Create a fake floating ip.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, ip, and so on
"""
@ -671,19 +566,8 @@ class FakeFloatingIP(object):
# Overwrite default attributes.
floating_ip_attrs.update(attrs)
# Set default methods.
floating_ip_methods = {
'keys': ['id', 'floating_ip_address', 'fixed_ip_address',
'dns_domain', 'dns_name', 'status', 'router_id',
'floating_network_id', 'port_id', 'tenant_id']
}
# Overwrite default methods.
floating_ip_methods.update(methods)
floating_ip = fakes.FakeResource(
info=copy.deepcopy(floating_ip_attrs),
methods=copy.deepcopy(floating_ip_methods),
loaded=True
)
@ -693,13 +577,11 @@ class FakeFloatingIP(object):
return floating_ip
@staticmethod
def create_floating_ips(attrs={}, methods={}, count=2):
def create_floating_ips(attrs={}, count=2):
"""Create multiple fake floating ips.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of floating ips to fake
:return:
@ -707,10 +589,7 @@ class FakeFloatingIP(object):
"""
floating_ips = []
for i in range(0, count):
floating_ips.append(FakeFloatingIP.create_one_floating_ip(
attrs,
methods
))
floating_ips.append(FakeFloatingIP.create_one_floating_ip(attrs))
return floating_ips
@staticmethod
@ -737,13 +616,11 @@ class FakeSubnetPool(object):
"""Fake one or more subnet pools."""
@staticmethod
def create_one_subnet_pool(attrs={}, methods={}):
def create_one_subnet_pool(attrs={}):
"""Create a fake subnet pool.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object faking the subnet pool
"""
@ -766,20 +643,8 @@ class FakeSubnetPool(object):
# Overwrite default attributes.
subnet_pool_attrs.update(attrs)
# Set default methods.
subnet_pool_methods = {
'keys': ['id', 'name', 'prefixes', 'default_prefixlen',
'address_scope_id', 'tenant_id', 'is_default',
'shared', 'max_prefixlen', 'min_prefixlen',
'default_quota', 'ip_version']
}
# Overwrite default methods.
subnet_pool_methods.update(methods)
subnet_pool = fakes.FakeResource(
info=copy.deepcopy(subnet_pool_attrs),
methods=copy.deepcopy(subnet_pool_methods),
loaded=True
)
@ -789,13 +654,11 @@ class FakeSubnetPool(object):
return subnet_pool
@staticmethod
def create_subnet_pools(attrs={}, methods={}, count=2):
def create_subnet_pools(attrs={}, count=2):
"""Create multiple fake subnet pools.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of subnet pools to fake
:return:
@ -804,7 +667,7 @@ class FakeSubnetPool(object):
subnet_pools = []
for i in range(0, count):
subnet_pools.append(
FakeSubnetPool.create_one_subnet_pool(attrs, methods)
FakeSubnetPool.create_one_subnet_pool(attrs)
)
return subnet_pools

View File

@ -54,7 +54,9 @@ class TestCreateNetworkIdentityV3(TestNetwork):
'is_default',
'name',
'project_id',
'provider_network_type',
'router_external',
'shared',
'status',
'subnets',
)
@ -67,7 +69,9 @@ class TestCreateNetworkIdentityV3(TestNetwork):
_network.is_default,
_network.name,
_network.project_id,
_network.provider_network_type,
network._format_router_external(_network.router_external),
_network.shared,
_network.status,
utils.format_list(_network.subnets),
)
@ -219,7 +223,9 @@ class TestCreateNetworkIdentityV2(TestNetwork):
'is_default',
'name',
'project_id',
'provider_network_type',
'router_external',
'shared',
'status',
'subnets',
)
@ -232,7 +238,9 @@ class TestCreateNetworkIdentityV2(TestNetwork):
_network.is_default,
_network.name,
_network.project_id,
_network.provider_network_type,
network._format_router_external(_network.router_external),
_network.shared,
_network.status,
utils.format_list(_network.subnets),
)
@ -539,7 +547,9 @@ class TestShowNetwork(TestNetwork):
'is_default',
'name',
'project_id',
'provider_network_type',
'router_external',
'shared',
'status',
'subnets',
)
@ -552,7 +562,9 @@ class TestShowNetwork(TestNetwork):
_network.is_default,
_network.name,
_network.project_id,
_network.provider_network_type,
network._format_router_external(_network.router_external),
_network.shared,
_network.status,
utils.format_list(_network.subnets),
)

View File

@ -117,19 +117,29 @@ class TestCreateRouter(TestRouter):
columns = (
'admin_state_up',
'availability_zone_hints',
'availability_zones',
'distributed',
'external_gateway_info',
'ha',
'id',
'name',
'project_id',
'routes',
'status',
)
data = (
router._format_admin_state(new_router.admin_state_up),
osc_utils.format_list(new_router.availability_zone_hints),
osc_utils.format_list(new_router.availability_zones),
new_router.distributed,
router._format_external_gateway_info(new_router.external_gateway_info),
new_router.ha,
new_router.id,
new_router.name,
new_router.tenant_id,
new_router.routes,
new_router.status,
)
def setUp(self):
@ -541,20 +551,29 @@ class TestShowRouter(TestRouter):
columns = (
'admin_state_up',
'availability_zone_hints',
'availability_zones',
'distributed',
'external_gateway_info',
'ha',
'id',
'name',
'project_id',
'routes',
'status',
)
data = (
router._format_admin_state(_router.admin_state_up),
osc_utils.format_list(_router.availability_zone_hints),
osc_utils.format_list(_router.availability_zones),
_router.distributed,
router._format_external_gateway_info(_router.external_gateway_info),
_router.ha,
_router.id,
_router.name,
_router.project_id,
_router.tenant_id,
_router.routes,
_router.status,
)
def setUp(self):