Fix mutable default arguments in tests

Python’s default arguments are evaluated only once
when the function is defined, not each time the
function is called. This means that if you use a
mutable default argument (like list and dict) and
mutate it, you will and have mutated that object
for all future calls to the function as well.

More details about this wrong usage here:
http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments

In unit tests, most FakeXXX classes' methods take
mutable arguments with default values [] or {}.
We should change them to None.

Change-Id: Iea833b66aa1379829511ad5c6d4432b72f3488e2
Closed-bug: #1550320
This commit is contained in:
Tang Chen 2016-04-15 15:46:19 +08:00
parent 4639148b1d
commit 09c20b2b5c
5 changed files with 85 additions and 56 deletions
openstackclient/tests
compute/v2
fakes.py
image/v2
network/v2
volume/v2

@ -100,8 +100,7 @@ class FakeAggregate(object):
:return: :return:
A FakeResource object, with id and other attributes A FakeResource object, with id and other attributes
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attribute # Set default attribute
aggregate_info = { aggregate_info = {
@ -217,7 +216,7 @@ class FakeHypervisor(object):
"""Fake one or more hypervisor.""" """Fake one or more hypervisor."""
@staticmethod @staticmethod
def create_one_hypervisor(attrs={}): def create_one_hypervisor(attrs=None):
"""Create a fake hypervisor. """Create a fake hypervisor.
:param Dictionary attrs: :param Dictionary attrs:
@ -225,6 +224,8 @@ class FakeHypervisor(object):
:return: :return:
A FakeResource object, with id, hypervisor_hostname, and so on A FakeResource object, with id, hypervisor_hostname, and so on
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
hypervisor_info = { hypervisor_info = {
'id': 'hypervisor-id-' + uuid.uuid4().hex, 'id': 'hypervisor-id-' + uuid.uuid4().hex,
@ -263,7 +264,7 @@ class FakeHypervisor(object):
return hypervisor return hypervisor
@staticmethod @staticmethod
def create_hypervisors(attrs={}, count=2): def create_hypervisors(attrs=None, count=2):
"""Create multiple fake hypervisors. """Create multiple fake hypervisors.
:param Dictionary attrs: :param Dictionary attrs:
@ -284,7 +285,7 @@ class FakeHypervisorStats(object):
"""Fake one or more hypervisor stats.""" """Fake one or more hypervisor stats."""
@staticmethod @staticmethod
def create_one_hypervisor_stats(attrs={}): def create_one_hypervisor_stats(attrs=None):
"""Create a fake hypervisor stats. """Create a fake hypervisor stats.
:param Dictionary attrs: :param Dictionary attrs:
@ -292,6 +293,8 @@ class FakeHypervisorStats(object):
:return: :return:
A FakeResource object, with id, hypervisor_hostname, and so on A FakeResource object, with id, hypervisor_hostname, and so on
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
stats_info = { stats_info = {
'count': 2, 'count': 2,
@ -319,7 +322,7 @@ class FakeHypervisorStats(object):
return hypervisor_stats return hypervisor_stats
@staticmethod @staticmethod
def create_hypervisors_stats(attrs={}, count=2): def create_hypervisors_stats(attrs=None, count=2):
"""Create multiple fake hypervisors stats. """Create multiple fake hypervisors stats.
:param Dictionary attrs: :param Dictionary attrs:
@ -349,8 +352,7 @@ class FakeSecurityGroup(object):
:return: :return:
A FakeResource object, with id, name, etc. A FakeResource object, with id, name, etc.
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
security_group_attrs = { security_group_attrs = {
@ -400,8 +402,7 @@ class FakeSecurityGroupRule(object):
:return: :return:
A FakeResource object, with id, etc. A FakeResource object, with id, etc.
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
security_group_rule_attrs = { security_group_rule_attrs = {
@ -445,7 +446,7 @@ class FakeServer(object):
"""Fake one or more compute servers.""" """Fake one or more compute servers."""
@staticmethod @staticmethod
def create_one_server(attrs={}, methods={}): def create_one_server(attrs=None, methods=None):
"""Create a fake server. """Create a fake server.
:param Dictionary attrs: :param Dictionary attrs:
@ -455,6 +456,9 @@ class FakeServer(object):
:return: :return:
A FakeResource object, with id, name, metadata A FakeResource object, with id, name, metadata
""" """
attrs = attrs or {}
methods = methods or {}
# Set default attributes. # Set default attributes.
server_info = { server_info = {
'id': 'server-id-' + uuid.uuid4().hex, 'id': 'server-id-' + uuid.uuid4().hex,
@ -477,7 +481,7 @@ class FakeServer(object):
return server return server
@staticmethod @staticmethod
def create_servers(attrs={}, methods={}, count=2): def create_servers(attrs=None, methods=None, count=2):
"""Create multiple fake servers. """Create multiple fake servers.
:param Dictionary attrs: :param Dictionary attrs:
@ -527,8 +531,7 @@ class FakeFlavor(object):
:return: :return:
A FakeResource object, with id, name, ram, vcpus, properties A FakeResource object, with id, name, ram, vcpus, properties
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
flavor_info = { flavor_info = {
@ -566,7 +569,7 @@ class FakeFlavor(object):
return flavor return flavor
@staticmethod @staticmethod
def create_flavors(attrs={}, count=2): def create_flavors(attrs=None, count=2):
"""Create multiple fake flavors. """Create multiple fake flavors.
:param Dictionary attrs: :param Dictionary attrs:
@ -614,10 +617,9 @@ class FakeKeypair(object):
:return: :return:
A FakeResource A FakeResource
""" """
# Set default attributes. attrs = attrs or {}
if attrs is None:
attrs = {}
# Set default attributes.
keypair_info = { keypair_info = {
'name': 'keypair-name-' + uuid.uuid4().hex, 'name': 'keypair-name-' + uuid.uuid4().hex,
'fingerprint': 'dummy', 'fingerprint': 'dummy',
@ -658,7 +660,7 @@ class FakeAvailabilityZone(object):
"""Fake one or more compute availability zones (AZs).""" """Fake one or more compute availability zones (AZs)."""
@staticmethod @staticmethod
def create_one_availability_zone(attrs={}): def create_one_availability_zone(attrs=None):
"""Create a fake AZ. """Create a fake AZ.
:param Dictionary attrs: :param Dictionary attrs:
@ -666,6 +668,8 @@ class FakeAvailabilityZone(object):
:return: :return:
A FakeResource object with zoneName, zoneState, etc. A FakeResource object with zoneName, zoneState, etc.
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
host_name = uuid.uuid4().hex host_name = uuid.uuid4().hex
service_name = uuid.uuid4().hex service_name = uuid.uuid4().hex
@ -689,7 +693,7 @@ class FakeAvailabilityZone(object):
return availability_zone return availability_zone
@staticmethod @staticmethod
def create_availability_zones(attrs={}, count=2): def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs. """Create multiple fake AZs.
:param Dictionary attrs: :param Dictionary attrs:
@ -712,7 +716,7 @@ class FakeFloatingIP(object):
"""Fake one or more floating ip.""" """Fake one or more floating ip."""
@staticmethod @staticmethod
def create_one_floating_ip(attrs={}): def create_one_floating_ip(attrs=None):
"""Create a fake floating ip. """Create a fake floating ip.
:param Dictionary attrs: :param Dictionary attrs:
@ -720,6 +724,8 @@ class FakeFloatingIP(object):
:return: :return:
A FakeResource object, with id, ip, and so on A FakeResource object, with id, ip, and so on
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
floating_ip_attrs = { floating_ip_attrs = {
'id': 'floating-ip-id-' + uuid.uuid4().hex, 'id': 'floating-ip-id-' + uuid.uuid4().hex,
@ -739,7 +745,7 @@ class FakeFloatingIP(object):
return floating_ip return floating_ip
@staticmethod @staticmethod
def create_floating_ips(attrs={}, count=2): def create_floating_ips(attrs=None, count=2):
"""Create multiple fake floating ips. """Create multiple fake floating ips.
:param Dictionary attrs: :param Dictionary attrs:
@ -778,7 +784,7 @@ class FakeNetwork(object):
"""Fake one or more networks.""" """Fake one or more networks."""
@staticmethod @staticmethod
def create_one_network(attrs={}): def create_one_network(attrs=None):
"""Create a fake network. """Create a fake network.
:param Dictionary attrs: :param Dictionary attrs:
@ -786,6 +792,8 @@ class FakeNetwork(object):
:return: :return:
A FakeResource object, with id, label, cidr and so on A FakeResource object, with id, label, cidr and so on
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
network_attrs = { network_attrs = {
'bridge': 'br100', 'bridge': 'br100',
@ -831,7 +839,7 @@ class FakeNetwork(object):
return network return network
@staticmethod @staticmethod
def create_networks(attrs={}, count=2): def create_networks(attrs=None, count=2):
"""Create multiple fake networks. """Create multiple fake networks.
:param Dictionary attrs: :param Dictionary attrs:
@ -860,8 +868,7 @@ class FakeHost(object):
:return: :return:
A FakeResource object, with id and other attributes A FakeResource object, with id and other attributes
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
host_info = { host_info = {

@ -142,7 +142,7 @@ class FakeModule(object):
class FakeResource(object): class FakeResource(object):
def __init__(self, manager=None, info={}, loaded=False, methods={}): def __init__(self, manager=None, info=None, loaded=False, methods=None):
"""Set attributes and methods for a resource. """Set attributes and methods for a resource.
:param manager: :param manager:
@ -154,6 +154,9 @@ class FakeResource(object):
:param Dictionary methods: :param Dictionary methods:
A dictionary with all methods A dictionary with all methods
""" """
info = info or {}
methods = methods or {}
self.__name__ = type(self).__name__ self.__name__ = type(self).__name__
self.manager = manager self.manager = manager
self._info = info self._info = info
@ -189,9 +192,12 @@ class FakeResource(object):
class FakeResponse(requests.Response): class FakeResponse(requests.Response):
def __init__(self, headers={}, status_code=200, data=None, encoding=None): def __init__(self, headers=None, status_code=200,
data=None, encoding=None):
super(FakeResponse, self).__init__() super(FakeResponse, self).__init__()
headers = headers or {}
self.status_code = status_code self.status_code = status_code
self.headers.update(headers) self.headers.update(headers)

@ -181,7 +181,7 @@ class FakeImage(object):
""" """
@staticmethod @staticmethod
def create_one_image(attrs={}): def create_one_image(attrs=None):
"""Create a fake image. """Create a fake image.
:param Dictionary attrs: :param Dictionary attrs:
@ -190,6 +190,8 @@ class FakeImage(object):
A FakeResource object with id, name, owner, protected, A FakeResource object with id, name, owner, protected,
visibility and tags attrs visibility and tags attrs
""" """
attrs = attrs or {}
# Set default attribute # Set default attribute
image_info = { image_info = {
'id': 'image-id' + uuid.uuid4().hex, 'id': 'image-id' + uuid.uuid4().hex,
@ -210,7 +212,7 @@ class FakeImage(object):
return image return image
@staticmethod @staticmethod
def create_images(attrs={}, count=2): def create_images(attrs=None, count=2):
"""Create multiple fake images. """Create multiple fake images.
:param Dictionary attrs: :param Dictionary attrs:

@ -83,8 +83,7 @@ class FakeAddressScope(object):
:return: :return:
A FakeResource object with name, id, etc. A FakeResource object with name, id, etc.
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
address_scope_attrs = { address_scope_attrs = {
@ -112,7 +111,7 @@ class FakeAvailabilityZone(object):
"""Fake one or more network availability zones (AZs).""" """Fake one or more network availability zones (AZs)."""
@staticmethod @staticmethod
def create_one_availability_zone(attrs={}): def create_one_availability_zone(attrs=None):
"""Create a fake AZ. """Create a fake AZ.
:param Dictionary attrs: :param Dictionary attrs:
@ -120,6 +119,8 @@ class FakeAvailabilityZone(object):
:return: :return:
A FakeResource object with name, state, etc. A FakeResource object with name, state, etc.
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
availability_zone = { availability_zone = {
'name': uuid.uuid4().hex, 'name': uuid.uuid4().hex,
@ -136,7 +137,7 @@ class FakeAvailabilityZone(object):
return availability_zone return availability_zone
@staticmethod @staticmethod
def create_availability_zones(attrs={}, count=2): def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs. """Create multiple fake AZs.
:param Dictionary attrs: :param Dictionary attrs:
@ -159,7 +160,7 @@ class FakeNetwork(object):
"""Fake one or more networks.""" """Fake one or more networks."""
@staticmethod @staticmethod
def create_one_network(attrs={}): def create_one_network(attrs=None):
"""Create a fake network. """Create a fake network.
:param Dictionary attrs: :param Dictionary attrs:
@ -168,6 +169,8 @@ class FakeNetwork(object):
A FakeResource object, with id, name, admin_state_up, A FakeResource object, with id, name, admin_state_up,
router_external, status, subnets, tenant_id router_external, status, subnets, tenant_id
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
network_attrs = { network_attrs = {
'id': 'network-id-' + uuid.uuid4().hex, 'id': 'network-id-' + uuid.uuid4().hex,
@ -196,7 +199,7 @@ class FakeNetwork(object):
return network return network
@staticmethod @staticmethod
def create_networks(attrs={}, count=2): def create_networks(attrs=None, count=2):
"""Create multiple fake networks. """Create multiple fake networks.
:param Dictionary attrs: :param Dictionary attrs:
@ -236,16 +239,16 @@ class FakePort(object):
"""Fake one or more ports.""" """Fake one or more ports."""
@staticmethod @staticmethod
def create_one_port(attrs={}): def create_one_port(attrs=None):
"""Create a fake port. """Create a fake port.
:param Dictionary attrs: :param Dictionary attrs:
A dictionary with all attributes A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return: :return:
A FakeResource object, with id, name, etc. A FakeResource object, with id, name, etc.
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
port_attrs = { port_attrs = {
'admin_state_up': True, 'admin_state_up': True,
@ -288,7 +291,7 @@ class FakePort(object):
return port return port
@staticmethod @staticmethod
def create_ports(attrs={}, count=2): def create_ports(attrs=None, count=2):
"""Create multiple fake ports. """Create multiple fake ports.
:param Dictionary attrs: :param Dictionary attrs:
@ -328,7 +331,7 @@ class FakeRouter(object):
"""Fake one or more routers.""" """Fake one or more routers."""
@staticmethod @staticmethod
def create_one_router(attrs={}): def create_one_router(attrs=None):
"""Create a fake router. """Create a fake router.
:param Dictionary attrs: :param Dictionary attrs:
@ -337,6 +340,8 @@ class FakeRouter(object):
A FakeResource object, with id, name, admin_state_up, A FakeResource object, with id, name, admin_state_up,
status, tenant_id status, tenant_id
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
router_attrs = { router_attrs = {
'id': 'router-id-' + uuid.uuid4().hex, 'id': 'router-id-' + uuid.uuid4().hex,
@ -364,7 +369,7 @@ class FakeRouter(object):
return router return router
@staticmethod @staticmethod
def create_routers(attrs={}, count=2): def create_routers(attrs=None, count=2):
"""Create multiple fake routers. """Create multiple fake routers.
:param Dictionary attrs: :param Dictionary attrs:
@ -412,8 +417,7 @@ class FakeSecurityGroup(object):
:return: :return:
A FakeResource object, with id, name, etc. A FakeResource object, with id, name, etc.
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
security_group_attrs = { security_group_attrs = {
@ -467,8 +471,7 @@ class FakeSecurityGroupRule(object):
:return: :return:
A FakeResource object, with id, etc. A FakeResource object, with id, etc.
""" """
if attrs is None: attrs = attrs or {}
attrs = {}
# Set default attributes. # Set default attributes.
security_group_rule_attrs = { security_group_rule_attrs = {
@ -519,7 +522,7 @@ class FakeSubnet(object):
"""Fake one or more subnets.""" """Fake one or more subnets."""
@staticmethod @staticmethod
def create_one_subnet(attrs={}): def create_one_subnet(attrs=None):
"""Create a fake subnet. """Create a fake subnet.
:param Dictionary attrs: :param Dictionary attrs:
@ -527,6 +530,8 @@ class FakeSubnet(object):
:return: :return:
A FakeResource object faking the subnet A FakeResource object faking the subnet
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
project_id = 'project-id-' + uuid.uuid4().hex project_id = 'project-id-' + uuid.uuid4().hex
subnet_attrs = { subnet_attrs = {
@ -551,13 +556,14 @@ class FakeSubnet(object):
subnet = fakes.FakeResource(info=copy.deepcopy(subnet_attrs), subnet = fakes.FakeResource(info=copy.deepcopy(subnet_attrs),
loaded=True) loaded=True)
# Set attributes with special mappings in OpenStack SDK. # Set attributes with special mappings in OpenStack SDK.
subnet.project_id = subnet_attrs['tenant_id'] subnet.project_id = subnet_attrs['tenant_id']
return subnet return subnet
@staticmethod @staticmethod
def create_subnets(attrs={}, count=2): def create_subnets(attrs=None, count=2):
"""Create multiple fake subnets. """Create multiple fake subnets.
:param Dictionary attrs: :param Dictionary attrs:
@ -578,7 +584,7 @@ class FakeFloatingIP(object):
"""Fake one or more floating ip.""" """Fake one or more floating ip."""
@staticmethod @staticmethod
def create_one_floating_ip(attrs={}): def create_one_floating_ip(attrs=None):
"""Create a fake floating ip. """Create a fake floating ip.
:param Dictionary attrs: :param Dictionary attrs:
@ -586,6 +592,8 @@ class FakeFloatingIP(object):
:return: :return:
A FakeResource object, with id, ip, and so on A FakeResource object, with id, ip, and so on
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
floating_ip_attrs = { floating_ip_attrs = {
'id': 'floating-ip-id-' + uuid.uuid4().hex, 'id': 'floating-ip-id-' + uuid.uuid4().hex,
@ -614,7 +622,7 @@ class FakeFloatingIP(object):
return floating_ip return floating_ip
@staticmethod @staticmethod
def create_floating_ips(attrs={}, count=2): def create_floating_ips(attrs=None, count=2):
"""Create multiple fake floating ips. """Create multiple fake floating ips.
:param Dictionary attrs: :param Dictionary attrs:
@ -653,7 +661,7 @@ class FakeSubnetPool(object):
"""Fake one or more subnet pools.""" """Fake one or more subnet pools."""
@staticmethod @staticmethod
def create_one_subnet_pool(attrs={}): def create_one_subnet_pool(attrs=None):
"""Create a fake subnet pool. """Create a fake subnet pool.
:param Dictionary attrs: :param Dictionary attrs:
@ -661,6 +669,8 @@ class FakeSubnetPool(object):
:return: :return:
A FakeResource object faking the subnet pool A FakeResource object faking the subnet pool
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
subnet_pool_attrs = { subnet_pool_attrs = {
'id': 'subnet-pool-id-' + uuid.uuid4().hex, 'id': 'subnet-pool-id-' + uuid.uuid4().hex,
@ -691,7 +701,7 @@ class FakeSubnetPool(object):
return subnet_pool return subnet_pool
@staticmethod @staticmethod
def create_subnet_pools(attrs={}, count=2): def create_subnet_pools(attrs=None, count=2):
"""Create multiple fake subnet pools. """Create multiple fake subnet pools.
:param Dictionary attrs: :param Dictionary attrs:

@ -281,7 +281,7 @@ class FakeVolume(object):
""" """
@staticmethod @staticmethod
def create_one_volume(attrs={}): def create_one_volume(attrs=None):
"""Create a fake volume. """Create a fake volume.
:param Dictionary attrs: :param Dictionary attrs:
@ -289,6 +289,8 @@ class FakeVolume(object):
:retrun: :retrun:
A FakeResource object with id, name, status, etc. A FakeResource object with id, name, status, etc.
""" """
attrs = attrs or {}
# Set default attribute # Set default attribute
volume_info = { volume_info = {
'id': 'volume-id' + uuid.uuid4().hex, 'id': 'volume-id' + uuid.uuid4().hex,
@ -320,7 +322,7 @@ class FakeVolume(object):
return volume return volume
@staticmethod @staticmethod
def create_volumes(attrs={}, count=2): def create_volumes(attrs=None, count=2):
"""Create multiple fake volumes. """Create multiple fake volumes.
:param Dictionary attrs: :param Dictionary attrs:
@ -361,7 +363,7 @@ class FakeAvailabilityZone(object):
"""Fake one or more volume availability zones (AZs).""" """Fake one or more volume availability zones (AZs)."""
@staticmethod @staticmethod
def create_one_availability_zone(attrs={}): def create_one_availability_zone(attrs=None):
"""Create a fake AZ. """Create a fake AZ.
:param Dictionary attrs: :param Dictionary attrs:
@ -369,6 +371,8 @@ class FakeAvailabilityZone(object):
:return: :return:
A FakeResource object with zoneName, zoneState, etc. A FakeResource object with zoneName, zoneState, etc.
""" """
attrs = attrs or {}
# Set default attributes. # Set default attributes.
availability_zone = { availability_zone = {
'zoneName': uuid.uuid4().hex, 'zoneName': uuid.uuid4().hex,
@ -384,7 +388,7 @@ class FakeAvailabilityZone(object):
return availability_zone return availability_zone
@staticmethod @staticmethod
def create_availability_zones(attrs={}, count=2): def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs. """Create multiple fake AZs.
:param Dictionary attrs: :param Dictionary attrs: