Skip to content

Commit 09c20b2

Browse files
author
Tang Chen
committed
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
1 parent 4639148 commit 09c20b2

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 = {
@@ -217,14 +216,16 @@ class FakeHypervisor(object):
217216
"""Fake one or more hypervisor."""
218217

219218
@staticmethod
220-
def create_one_hypervisor(attrs={}):
219+
def create_one_hypervisor(attrs=None):
221220
"""Create a fake hypervisor.
222221
223222
:param Dictionary attrs:
224223
A dictionary with all attributes
225224
:return:
226225
A FakeResource object, with id, hypervisor_hostname, and so on
227226
"""
227+
attrs = attrs or {}
228+
228229
# Set default attributes.
229230
hypervisor_info = {
230231
'id': 'hypervisor-id-' + uuid.uuid4().hex,
@@ -263,7 +264,7 @@ def create_one_hypervisor(attrs={}):
263264
return hypervisor
264265

265266
@staticmethod
266-
def create_hypervisors(attrs={}, count=2):
267+
def create_hypervisors(attrs=None, count=2):
267268
"""Create multiple fake hypervisors.
268269
269270
:param Dictionary attrs:
@@ -284,14 +285,16 @@ class FakeHypervisorStats(object):
284285
"""Fake one or more hypervisor stats."""
285286

286287
@staticmethod
287-
def create_one_hypervisor_stats(attrs={}):
288+
def create_one_hypervisor_stats(attrs=None):
288289
"""Create a fake hypervisor stats.
289290
290291
:param Dictionary attrs:
291292
A dictionary with all attributes
292293
:return:
293294
A FakeResource object, with id, hypervisor_hostname, and so on
294295
"""
296+
attrs = attrs or {}
297+
295298
# Set default attributes.
296299
stats_info = {
297300
'count': 2,
@@ -319,7 +322,7 @@ def create_one_hypervisor_stats(attrs={}):
319322
return hypervisor_stats
320323

321324
@staticmethod
322-
def create_hypervisors_stats(attrs={}, count=2):
325+
def create_hypervisors_stats(attrs=None, count=2):
323326
"""Create multiple fake hypervisors stats.
324327
325328
:param Dictionary attrs:
@@ -349,8 +352,7 @@ def create_one_security_group(attrs=None):
349352
:return:
350353
A FakeResource object, with id, name, etc.
351354
"""
352-
if attrs is None:
353-
attrs = {}
355+
attrs = attrs or {}
354356

355357
# Set default attributes.
356358
security_group_attrs = {
@@ -400,8 +402,7 @@ def create_one_security_group_rule(attrs=None):
400402
:return:
401403
A FakeResource object, with id, etc.
402404
"""
403-
if attrs is None:
404-
attrs = {}
405+
attrs = attrs or {}
405406

406407
# Set default attributes.
407408
security_group_rule_attrs = {
@@ -445,7 +446,7 @@ class FakeServer(object):
445446
"""Fake one or more compute servers."""
446447

447448
@staticmethod
448-
def create_one_server(attrs={}, methods={}):
449+
def create_one_server(attrs=None, methods=None):
449450
"""Create a fake server.
450451
451452
:param Dictionary attrs:
@@ -455,6 +456,9 @@ def create_one_server(attrs={}, methods={}):
455456
:return:
456457
A FakeResource object, with id, name, metadata
457458
"""
459+
attrs = attrs or {}
460+
methods = methods or {}
461+
458462
# Set default attributes.
459463
server_info = {
460464
'id': 'server-id-' + uuid.uuid4().hex,
@@ -477,7 +481,7 @@ def create_one_server(attrs={}, methods={}):
477481
return server
478482

479483
@staticmethod
480-
def create_servers(attrs={}, methods={}, count=2):
484+
def create_servers(attrs=None, methods=None, count=2):
481485
"""Create multiple fake servers.
482486
483487
:param Dictionary attrs:
@@ -527,8 +531,7 @@ def create_one_flavor(attrs=None):
527531
:return:
528532
A FakeResource object, with id, name, ram, vcpus, properties
529533
"""
530-
if attrs is None:
531-
attrs = {}
534+
attrs = attrs or {}
532535

533536
# Set default attributes.
534537
flavor_info = {
@@ -566,7 +569,7 @@ def create_one_flavor(attrs=None):
566569
return flavor
567570

568571
@staticmethod
569-
def create_flavors(attrs={}, count=2):
572+
def create_flavors(attrs=None, count=2):
570573
"""Create multiple fake flavors.
571574
572575
:param Dictionary attrs:
@@ -614,10 +617,9 @@ def create_one_keypair(attrs=None, no_pri=False):
614617
:return:
615618
A FakeResource
616619
"""
617-
# Set default attributes.
618-
if attrs is None:
619-
attrs = {}
620+
attrs = attrs or {}
620621

622+
# Set default attributes.
621623
keypair_info = {
622624
'name': 'keypair-name-' + uuid.uuid4().hex,
623625
'fingerprint': 'dummy',
@@ -658,14 +660,16 @@ class FakeAvailabilityZone(object):
658660
"""Fake one or more compute availability zones (AZs)."""
659661

660662
@staticmethod
661-
def create_one_availability_zone(attrs={}):
663+
def create_one_availability_zone(attrs=None):
662664
"""Create a fake AZ.
663665
664666
:param Dictionary attrs:
665667
A dictionary with all attributes
666668
:return:
667669
A FakeResource object with zoneName, zoneState, etc.
668670
"""
671+
attrs = attrs or {}
672+
669673
# Set default attributes.
670674
host_name = uuid.uuid4().hex
671675
service_name = uuid.uuid4().hex
@@ -689,7 +693,7 @@ def create_one_availability_zone(attrs={}):
689693
return availability_zone
690694

691695
@staticmethod
692-
def create_availability_zones(attrs={}, count=2):
696+
def create_availability_zones(attrs=None, count=2):
693697
"""Create multiple fake AZs.
694698
695699
:param Dictionary attrs:
@@ -712,14 +716,16 @@ class FakeFloatingIP(object):
712716
"""Fake one or more floating ip."""
713717

714718
@staticmethod
715-
def create_one_floating_ip(attrs={}):
719+
def create_one_floating_ip(attrs=None):
716720
"""Create a fake floating ip.
717721
718722
:param Dictionary attrs:
719723
A dictionary with all attributes
720724
:return:
721725
A FakeResource object, with id, ip, and so on
722726
"""
727+
attrs = attrs or {}
728+
723729
# Set default attributes.
724730
floating_ip_attrs = {
725731
'id': 'floating-ip-id-' + uuid.uuid4().hex,
@@ -739,7 +745,7 @@ def create_one_floating_ip(attrs={}):
739745
return floating_ip
740746

741747
@staticmethod
742-
def create_floating_ips(attrs={}, count=2):
748+
def create_floating_ips(attrs=None, count=2):
743749
"""Create multiple fake floating ips.
744750
745751
:param Dictionary attrs:
@@ -778,14 +784,16 @@ class FakeNetwork(object):
778784
"""Fake one or more networks."""
779785

780786
@staticmethod
781-
def create_one_network(attrs={}):
787+
def create_one_network(attrs=None):
782788
"""Create a fake network.
783789
784790
:param Dictionary attrs:
785791
A dictionary with all attributes
786792
:return:
787793
A FakeResource object, with id, label, cidr and so on
788794
"""
795+
attrs = attrs or {}
796+
789797
# Set default attributes.
790798
network_attrs = {
791799
'bridge': 'br100',
@@ -831,7 +839,7 @@ def create_one_network(attrs={}):
831839
return network
832840

833841
@staticmethod
834-
def create_networks(attrs={}, count=2):
842+
def create_networks(attrs=None, count=2):
835843
"""Create multiple fake networks.
836844
837845
:param Dictionary attrs:
@@ -860,8 +868,7 @@ def create_one_host(attrs=None):
860868
:return:
861869
A FakeResource object, with id and other attributes
862870
"""
863-
if attrs is None:
864-
attrs = {}
871+
attrs = attrs or {}
865872

866873
# Set default attributes.
867874
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)