Skip to content

Commit 7d64003

Browse files
committed
tests: Stop returning FakeResource in compute tests
This was still being used in places where we have our own API bindings because SDK does not support the API. Those bindings should be returning dicts, not FakeResource objects. Correct this, and in doing so fix the bug this highlights in our cell-down output. Change-Id: I6647d94fcf5ada8186edbf64c03abd3d8ae7ca56 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent abed52f commit 7d64003

5 files changed

Lines changed: 63 additions & 110 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3004,7 +3004,7 @@ def take_action(self, parsed_args):
30043004
# infrastructure failure situations.
30053005
# For those servers with partial constructs we just skip the
30063006
# processing of the image and flavor information.
3007-
if not hasattr(s, 'image') or not hasattr(s, 'flavor'):
3007+
if getattr(s, 'status') == 'UNKNOWN':
30083008
continue
30093009

30103010
if 'id' in s.image and s.image.id is not None:

openstackclient/tests/unit/compute/v2/fakes.py

Lines changed: 56 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# under the License.
1414
#
1515

16-
import copy
1716
import random
1817
import re
1918
from unittest import mock
@@ -32,7 +31,6 @@
3231
from openstack.compute.v2 import server_migration as _server_migration
3332
from openstack.compute.v2 import volume_attachment as _volume_attachment
3433

35-
from openstackclient.tests.unit import fakes
3634
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
3735
from openstackclient.tests.unit.image.v2 import fakes as image_fakes
3836
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
@@ -79,16 +77,14 @@ class TestComputev2(
7977
def create_one_agent(attrs=None):
8078
"""Create a fake agent.
8179
82-
:param dict attrs:
83-
A dictionary with all attributes
84-
:return:
85-
A FakeResource object, with agent_id, os, and so on
80+
:param dict attrs: A dictionary with all attributes
81+
:return: A dicionarty faking the agent
8682
"""
8783

8884
attrs = attrs or {}
8985

9086
# set default attributes.
91-
agent_info = {
87+
agent_attrs = {
9288
'agent_id': 'agent-id-' + uuid.uuid4().hex,
9389
'os': 'agent-os-' + uuid.uuid4().hex,
9490
'architecture': 'agent-architecture',
@@ -98,22 +94,20 @@ def create_one_agent(attrs=None):
9894
'hypervisor': 'hypervisor',
9995
}
10096

97+
assert not set(attrs) - set(agent_attrs), 'unknown keys'
98+
10199
# Overwrite default attributes.
102-
agent_info.update(attrs)
100+
agent_attrs.update(attrs)
103101

104-
agent = fakes.FakeResource(info=copy.deepcopy(agent_info), loaded=True)
105-
return agent
102+
return agent_attrs
106103

107104

108105
def create_agents(attrs=None, count=2):
109106
"""Create multiple fake agents.
110107
111-
:param dict attrs:
112-
A dictionary with all attributes
113-
:param int count:
114-
The number of agents to fake
115-
:return:
116-
A list of FakeResource objects faking the agents
108+
:param dict attrs: A dictionary with all attributes
109+
:param int count: The number of agents to fake
110+
:return: A list of dictionaries faking the agents
117111
"""
118112
agents = []
119113
for i in range(0, count):
@@ -158,10 +152,8 @@ def create_one_extension(attrs=None):
158152
def create_one_security_group(attrs=None):
159153
"""Create a fake security group.
160154
161-
:param dict attrs:
162-
A dictionary with all attributes
163-
:return:
164-
A FakeResource object, with id, name, etc.
155+
:param dict attrs: A dictionary with all attributes
156+
:return: A dictionary faking the security group
165157
"""
166158
attrs = attrs or {}
167159

@@ -174,6 +166,8 @@ def create_one_security_group(attrs=None):
174166
'rules': [],
175167
}
176168

169+
assert not set(attrs) - set(security_group_attrs), 'unknown keys'
170+
177171
# Overwrite default attributes.
178172
security_group_attrs.update(attrs)
179173
return security_group_attrs
@@ -182,12 +176,9 @@ def create_one_security_group(attrs=None):
182176
def create_security_groups(attrs=None, count=2):
183177
"""Create multiple fake security groups.
184178
185-
:param dict attrs:
186-
A dictionary with all attributes
187-
:param int count:
188-
The number of security groups to fake
189-
:return:
190-
A list of FakeResource objects faking the security groups
179+
:param dict attrs: A dictionary with all attributes
180+
:param int count: The number of security groups to fake
181+
:return: A list of dictionaries faking the security groups
191182
"""
192183
security_groups = []
193184
for i in range(0, count):
@@ -199,10 +190,8 @@ def create_security_groups(attrs=None, count=2):
199190
def create_one_security_group_rule(attrs=None):
200191
"""Create a fake security group rule.
201192
202-
:param dict attrs:
203-
A dictionary with all attributes
204-
:return:
205-
A FakeResource object, with id, etc.
193+
:param dict attrs: A dictionary with all attributes
194+
:return: A dictionary faking the security group rule
206195
"""
207196
attrs = attrs or {}
208197

@@ -217,6 +206,8 @@ def create_one_security_group_rule(attrs=None):
217206
'to_port': 0,
218207
}
219208

209+
assert not set(attrs) - set(security_group_rule_attrs), 'unknown keys'
210+
220211
# Overwrite default attributes.
221212
security_group_rule_attrs.update(attrs)
222213

@@ -226,12 +217,9 @@ def create_one_security_group_rule(attrs=None):
226217
def create_security_group_rules(attrs=None, count=2):
227218
"""Create multiple fake security group rules.
228219
229-
:param dict attrs:
230-
A dictionary with all attributes
231-
:param int count:
232-
The number of security group rules to fake
233-
:return:
234-
A list of FakeResource objects faking the security group rules
220+
:param dict attrs: A dictionary with all attributes
221+
:param int count: The number of security group rules to fake
222+
:return: A list of dictionaries faking the security group rules
235223
"""
236224
security_group_rules = []
237225
for i in range(0, count):
@@ -379,10 +367,8 @@ def create_flavors(attrs=None, count=2):
379367
def create_one_flavor_access(attrs=None):
380368
"""Create a fake flavor access.
381369
382-
:param dict attrs:
383-
A dictionary with all attributes
384-
:return:
385-
A FakeResource object, with flavor_id, tenat_id
370+
:param dict attrs: A dictionary with all attributes
371+
:return: A dictionary faking the flavor access
386372
"""
387373
attrs = attrs or {}
388374

@@ -392,14 +378,12 @@ def create_one_flavor_access(attrs=None):
392378
'tenant_id': 'tenant-id-' + uuid.uuid4().hex,
393379
}
394380

381+
assert not set(attrs) - set(flavor_access_info), 'unknown keys'
382+
395383
# Overwrite default attributes.
396384
flavor_access_info.update(attrs)
397385

398-
flavor_access = fakes.FakeResource(
399-
info=copy.deepcopy(flavor_access_info), loaded=True
400-
)
401-
402-
return flavor_access
386+
return flavor_access_info
403387

404388

405389
def create_one_availability_zone(attrs=None):
@@ -454,12 +438,10 @@ def create_availability_zones(attrs=None, count=2):
454438

455439

456440
def create_one_floating_ip(attrs=None):
457-
"""Create a fake floating ip.
441+
"""Create a fake floating IP.
458442
459-
:param dict attrs:
460-
A dictionary with all attributes
461-
:return:
462-
A FakeResource object, with id, ip, and so on
443+
:param dict attrs: A dictionary with all attributes
444+
:return: A dictionary faking the floating IP
463445
"""
464446
attrs = attrs or {}
465447

@@ -472,54 +454,32 @@ def create_one_floating_ip(attrs=None):
472454
'pool': 'public',
473455
}
474456

457+
assert not set(attrs) - set(floating_ip_attrs), 'unknown keys'
458+
475459
# Overwrite default attributes.
476460
floating_ip_attrs.update(attrs)
477461

478462
return floating_ip_attrs
479463

480464

481465
def create_floating_ips(attrs=None, count=2):
482-
"""Create multiple fake floating ips.
483-
484-
:param dict attrs:
485-
A dictionary with all attributes
486-
:param int count:
487-
The number of floating ips to fake
488-
:return:
489-
A list of FakeResource objects faking the floating ips
466+
"""Create multiple fake floating IPs.
467+
468+
:param dict attrs: A dictionary with all attributes
469+
:param int count: The number of floating IPs to fake
470+
:return: A list of dictionaries faking the floating IPs
490471
"""
491472
floating_ips = []
492473
for i in range(0, count):
493474
floating_ips.append(create_one_floating_ip(attrs))
494475
return floating_ips
495476

496477

497-
def get_floating_ips(floating_ips=None, count=2):
498-
"""Get an iterable MagicMock object with a list of faked floating ips.
499-
500-
If floating_ips list is provided, then initialize the Mock object
501-
with the list. Otherwise create one.
502-
503-
:param List floating_ips:
504-
A list of FakeResource objects faking floating ips
505-
:param int count:
506-
The number of floating ips to fake
507-
:return:
508-
An iterable Mock object with side_effect set to a list of faked
509-
floating ips
510-
"""
511-
if floating_ips is None:
512-
floating_ips = create_floating_ips(count)
513-
return mock.Mock(side_effect=floating_ips)
514-
515-
516478
def create_one_floating_ip_pool(attrs=None):
517-
"""Create a fake floating ip pool.
479+
"""Create a fake floating IP pool.
518480
519-
:param dict attrs:
520-
A dictionary with all attributes
521-
:return:
522-
A FakeResource object, with name, etc
481+
:param dict attrs: A dictionary with all attributes
482+
:return: A dictionary faking the floating IP pool
523483
"""
524484
if attrs is None:
525485
attrs = {}
@@ -529,21 +489,20 @@ def create_one_floating_ip_pool(attrs=None):
529489
'name': 'floating-ip-pool-name-' + uuid.uuid4().hex,
530490
}
531491

492+
assert not set(attrs) - set(floating_ip_pool_attrs), 'unknown keys'
493+
532494
# Overwrite default attributes.
533495
floating_ip_pool_attrs.update(attrs)
534496

535497
return floating_ip_pool_attrs
536498

537499

538500
def create_floating_ip_pools(attrs=None, count=2):
539-
"""Create multiple fake floating ip pools.
540-
541-
:param dict attrs:
542-
A dictionary with all attributes
543-
:param int count:
544-
The number of floating ip pools to fake
545-
:return:
546-
A list of FakeResource objects faking the floating ip pools
501+
"""Create multiple fake floating IP pools.
502+
503+
:param dict attrs: A dictionary with all attributes
504+
:param int count: The number of floating IP pools to fake
505+
:return: A list of dictionaries faking the floating IP pools
547506
"""
548507
floating_ip_pools = []
549508
for i in range(0, count):
@@ -554,10 +513,8 @@ def create_floating_ip_pools(attrs=None, count=2):
554513
def create_one_network(attrs=None):
555514
"""Create a fake network.
556515
557-
:param dict attrs:
558-
A dictionary with all attributes
559-
:return:
560-
A FakeResource object, with id, label, cidr and so on
516+
:param dict attrs: A dictionary with all attributes
517+
:return: A dictionary faking the network
561518
"""
562519
attrs = attrs or {}
563520

@@ -597,6 +554,8 @@ def create_one_network(attrs=None):
597554
'vpn_public_port': None,
598555
}
599556

557+
assert not set(attrs) - set(network_attrs), 'unknown keys'
558+
600559
# Overwrite default attributes.
601560
network_attrs.update(attrs)
602561

@@ -606,12 +565,9 @@ def create_one_network(attrs=None):
606565
def create_networks(attrs=None, count=2):
607566
"""Create multiple fake networks.
608567
609-
:param dict attrs:
610-
A dictionary with all attributes
611-
:param int count:
612-
The number of networks to fake
613-
:return:
614-
A list of FakeResource objects faking the networks
568+
:param dict attrs: A dictionary with all attributes
569+
:param int count: The number of networks to fake
570+
:return: A list of dictionaries faking the networks
615571
"""
616572
networks = []
617573
for i in range(0, count):

openstackclient/tests/unit/compute/v2/test_flavor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ def test_private_flavor_show(self):
10521052
data_with_project = (
10531053
private_flavor.is_disabled,
10541054
private_flavor.ephemeral,
1055-
[self.flavor_access.tenant_id],
1055+
[self.flavor_access['tenant_id']],
10561056
private_flavor.description,
10571057
private_flavor.disk,
10581058
private_flavor.id,

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import uuid
2222

2323
import iso8601
24+
from openstack.compute.v2 import server as _server
2425
from openstack.compute.v2 import server_group as _server_group
2526
from openstack import exceptions as sdk_exceptions
2627
from openstack.test import fakes as sdk_fakes
@@ -5487,9 +5488,7 @@ def test_server_list_v269_with_partial_constructs(self):
54875488
],
54885489
"networks": {},
54895490
}
5490-
fake_server = compute_fakes.fakes.FakeResource(
5491-
info=server_dict,
5492-
)
5491+
fake_server = _server.Server(**server_dict)
54935492
self.servers.append(fake_server)
54945493
columns, data = self.cmd.take_action(parsed_args)
54955494
# get the first three servers out since our interest is in the partial
@@ -5500,9 +5499,9 @@ def test_server_list_v269_with_partial_constructs(self):
55005499
partial_server = next(data)
55015500
expected_row = (
55025501
'server-id-95a56bfc4xxxxxx28d7e418bfd97813a',
5503-
'',
5502+
None,
55045503
'UNKNOWN',
5505-
server.AddressesColumn(''),
5504+
server.AddressesColumn(None),
55065505
'',
55075506
'',
55085507
)

openstackclient/tests/unit/network/v2/test_security_group_rule_compute.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ class TestListSecurityGroupRuleCompute(compute_fakes.TestComputev2):
383383
_security_group_rule_tcp = compute_fakes.create_one_security_group_rule(
384384
{
385385
'ip_protocol': 'tcp',
386-
'ethertype': 'IPv4',
387386
'from_port': 80,
388387
'to_port': 80,
389388
'group': {'name': _security_group['name']},
@@ -392,7 +391,6 @@ class TestListSecurityGroupRuleCompute(compute_fakes.TestComputev2):
392391
_security_group_rule_icmp = compute_fakes.create_one_security_group_rule(
393392
{
394393
'ip_protocol': 'icmp',
395-
'ethertype': 'IPv4',
396394
'from_port': -1,
397395
'to_port': -1,
398396
'ip_range': {'cidr': '10.0.2.0/24'},
@@ -426,7 +424,7 @@ class TestListSecurityGroupRuleCompute(compute_fakes.TestComputev2):
426424
expected_rule_with_group = (
427425
rule['id'],
428426
rule['ip_protocol'],
429-
rule['ethertype'],
427+
'', # ethertype is a neutron-only thing
430428
rule['ip_range'],
431429
rule['port_range'],
432430
rule['remote_security_group'],

0 commit comments

Comments
 (0)