Skip to content

Commit e78113a

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Fix mutable default arguments in tests"
2 parents 98a0fba + 09c20b2 commit e78113a

5 files changed

Lines changed: 85 additions & 56 deletions

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ def create_one_aggregate(attrs=None):
100100
:return:
101101
A FakeResource object, with id and other attributes
102102
"""
103-
if attrs is None:
104-
attrs = {}
103+
attrs = attrs or {}
105104

106105
# Set default attribute
107106
aggregate_info = {
@@ -220,14 +219,16 @@ class FakeHypervisor(object):
220219
"""Fake one or more hypervisor."""
221220

222221
@staticmethod
223-
def create_one_hypervisor(attrs={}):
222+
def create_one_hypervisor(attrs=None):
224223
"""Create a fake hypervisor.
225224
226225
:param Dictionary attrs:
227226
A dictionary with all attributes
228227
:return:
229228
A FakeResource object, with id, hypervisor_hostname, and so on
230229
"""
230+
attrs = attrs or {}
231+
231232
# Set default attributes.
232233
hypervisor_info = {
233234
'id': 'hypervisor-id-' + uuid.uuid4().hex,
@@ -266,7 +267,7 @@ def create_one_hypervisor(attrs={}):
266267
return hypervisor
267268

268269
@staticmethod
269-
def create_hypervisors(attrs={}, count=2):
270+
def create_hypervisors(attrs=None, count=2):
270271
"""Create multiple fake hypervisors.
271272
272273
:param Dictionary attrs:
@@ -287,14 +288,16 @@ class FakeHypervisorStats(object):
287288
"""Fake one or more hypervisor stats."""
288289

289290
@staticmethod
290-
def create_one_hypervisor_stats(attrs={}):
291+
def create_one_hypervisor_stats(attrs=None):
291292
"""Create a fake hypervisor stats.
292293
293294
:param Dictionary attrs:
294295
A dictionary with all attributes
295296
:return:
296297
A FakeResource object, with id, hypervisor_hostname, and so on
297298
"""
299+
attrs = attrs or {}
300+
298301
# Set default attributes.
299302
stats_info = {
300303
'count': 2,
@@ -322,7 +325,7 @@ def create_one_hypervisor_stats(attrs={}):
322325
return hypervisor_stats
323326

324327
@staticmethod
325-
def create_hypervisors_stats(attrs={}, count=2):
328+
def create_hypervisors_stats(attrs=None, count=2):
326329
"""Create multiple fake hypervisors stats.
327330
328331
:param Dictionary attrs:
@@ -352,8 +355,7 @@ def create_one_security_group(attrs=None):
352355
:return:
353356
A FakeResource object, with id, name, etc.
354357
"""
355-
if attrs is None:
356-
attrs = {}
358+
attrs = attrs or {}
357359

358360
# Set default attributes.
359361
security_group_attrs = {
@@ -403,8 +405,7 @@ def create_one_security_group_rule(attrs=None):
403405
:return:
404406
A FakeResource object, with id, etc.
405407
"""
406-
if attrs is None:
407-
attrs = {}
408+
attrs = attrs or {}
408409

409410
# Set default attributes.
410411
security_group_rule_attrs = {
@@ -448,7 +449,7 @@ class FakeServer(object):
448449
"""Fake one or more compute servers."""
449450

450451
@staticmethod
451-
def create_one_server(attrs={}, methods={}):
452+
def create_one_server(attrs=None, methods=None):
452453
"""Create a fake server.
453454
454455
:param Dictionary attrs:
@@ -458,6 +459,9 @@ def create_one_server(attrs={}, methods={}):
458459
:return:
459460
A FakeResource object, with id, name, metadata
460461
"""
462+
attrs = attrs or {}
463+
methods = methods or {}
464+
461465
# Set default attributes.
462466
server_info = {
463467
'id': 'server-id-' + uuid.uuid4().hex,
@@ -480,7 +484,7 @@ def create_one_server(attrs={}, methods={}):
480484
return server
481485

482486
@staticmethod
483-
def create_servers(attrs={}, methods={}, count=2):
487+
def create_servers(attrs=None, methods=None, count=2):
484488
"""Create multiple fake servers.
485489
486490
:param Dictionary attrs:
@@ -530,8 +534,7 @@ def create_one_flavor(attrs=None):
530534
:return:
531535
A FakeResource object, with id, name, ram, vcpus, properties
532536
"""
533-
if attrs is None:
534-
attrs = {}
537+
attrs = attrs or {}
535538

536539
# Set default attributes.
537540
flavor_info = {
@@ -569,7 +572,7 @@ def create_one_flavor(attrs=None):
569572
return flavor
570573

571574
@staticmethod
572-
def create_flavors(attrs={}, count=2):
575+
def create_flavors(attrs=None, count=2):
573576
"""Create multiple fake flavors.
574577
575578
:param Dictionary attrs:
@@ -617,10 +620,9 @@ def create_one_keypair(attrs=None, no_pri=False):
617620
:return:
618621
A FakeResource
619622
"""
620-
# Set default attributes.
621-
if attrs is None:
622-
attrs = {}
623+
attrs = attrs or {}
623624

625+
# Set default attributes.
624626
keypair_info = {
625627
'name': 'keypair-name-' + uuid.uuid4().hex,
626628
'fingerprint': 'dummy',
@@ -661,14 +663,16 @@ class FakeAvailabilityZone(object):
661663
"""Fake one or more compute availability zones (AZs)."""
662664

663665
@staticmethod
664-
def create_one_availability_zone(attrs={}):
666+
def create_one_availability_zone(attrs=None):
665667
"""Create a fake AZ.
666668
667669
:param Dictionary attrs:
668670
A dictionary with all attributes
669671
:return:
670672
A FakeResource object with zoneName, zoneState, etc.
671673
"""
674+
attrs = attrs or {}
675+
672676
# Set default attributes.
673677
host_name = uuid.uuid4().hex
674678
service_name = uuid.uuid4().hex
@@ -692,7 +696,7 @@ def create_one_availability_zone(attrs={}):
692696
return availability_zone
693697

694698
@staticmethod
695-
def create_availability_zones(attrs={}, count=2):
699+
def create_availability_zones(attrs=None, count=2):
696700
"""Create multiple fake AZs.
697701
698702
:param Dictionary attrs:
@@ -715,14 +719,16 @@ class FakeFloatingIP(object):
715719
"""Fake one or more floating ip."""
716720

717721
@staticmethod
718-
def create_one_floating_ip(attrs={}):
722+
def create_one_floating_ip(attrs=None):
719723
"""Create a fake floating ip.
720724
721725
:param Dictionary attrs:
722726
A dictionary with all attributes
723727
:return:
724728
A FakeResource object, with id, ip, and so on
725729
"""
730+
attrs = attrs or {}
731+
726732
# Set default attributes.
727733
floating_ip_attrs = {
728734
'id': 'floating-ip-id-' + uuid.uuid4().hex,
@@ -742,7 +748,7 @@ def create_one_floating_ip(attrs={}):
742748
return floating_ip
743749

744750
@staticmethod
745-
def create_floating_ips(attrs={}, count=2):
751+
def create_floating_ips(attrs=None, count=2):
746752
"""Create multiple fake floating ips.
747753
748754
:param Dictionary attrs:
@@ -781,14 +787,16 @@ class FakeNetwork(object):
781787
"""Fake one or more networks."""
782788

783789
@staticmethod
784-
def create_one_network(attrs={}):
790+
def create_one_network(attrs=None):
785791
"""Create a fake network.
786792
787793
:param Dictionary attrs:
788794
A dictionary with all attributes
789795
:return:
790796
A FakeResource object, with id, label, cidr and so on
791797
"""
798+
attrs = attrs or {}
799+
792800
# Set default attributes.
793801
network_attrs = {
794802
'bridge': 'br100',
@@ -834,7 +842,7 @@ def create_one_network(attrs={}):
834842
return network
835843

836844
@staticmethod
837-
def create_networks(attrs={}, count=2):
845+
def create_networks(attrs=None, count=2):
838846
"""Create multiple fake networks.
839847
840848
:param Dictionary attrs:
@@ -863,8 +871,7 @@ def create_one_host(attrs=None):
863871
:return:
864872
A FakeResource object, with id and other attributes
865873
"""
866-
if attrs is None:
867-
attrs = {}
874+
attrs = attrs or {}
868875

869876
# Set default attributes.
870877
host_info = {

openstackclient/tests/fakes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __init__(self, name, version):
142142

143143
class FakeResource(object):
144144

145-
def __init__(self, manager=None, info={}, loaded=False, methods={}):
145+
def __init__(self, manager=None, info=None, loaded=False, methods=None):
146146
"""Set attributes and methods for a resource.
147147
148148
:param manager:
@@ -154,6 +154,9 @@ def __init__(self, manager=None, info={}, loaded=False, methods={}):
154154
:param Dictionary methods:
155155
A dictionary with all methods
156156
"""
157+
info = info or {}
158+
methods = methods or {}
159+
157160
self.__name__ = type(self).__name__
158161
self.manager = manager
159162
self._info = info
@@ -189,9 +192,12 @@ def keys(self):
189192

190193
class FakeResponse(requests.Response):
191194

192-
def __init__(self, headers={}, status_code=200, data=None, encoding=None):
195+
def __init__(self, headers=None, status_code=200,
196+
data=None, encoding=None):
193197
super(FakeResponse, self).__init__()
194198

199+
headers = headers or {}
200+
195201
self.status_code = status_code
196202

197203
self.headers.update(headers)

openstackclient/tests/image/v2/fakes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class FakeImage(object):
181181
"""
182182

183183
@staticmethod
184-
def create_one_image(attrs={}):
184+
def create_one_image(attrs=None):
185185
"""Create a fake image.
186186
187187
:param Dictionary attrs:
@@ -190,6 +190,8 @@ def create_one_image(attrs={}):
190190
A FakeResource object with id, name, owner, protected,
191191
visibility and tags attrs
192192
"""
193+
attrs = attrs or {}
194+
193195
# Set default attribute
194196
image_info = {
195197
'id': 'image-id' + uuid.uuid4().hex,
@@ -210,7 +212,7 @@ def create_one_image(attrs={}):
210212
return image
211213

212214
@staticmethod
213-
def create_images(attrs={}, count=2):
215+
def create_images(attrs=None, count=2):
214216
"""Create multiple fake images.
215217
216218
:param Dictionary attrs:

0 commit comments

Comments
 (0)