Skip to content

Commit 31ae635

Browse files
committed
tests: Use SDK objects where expected
We had not migrated a number of tests to use SDK objects instead of fake novaclient-like objects when migrating the commands themselves. Address this now. Change-Id: Ib0da07fd9d793968b111986bd36a6d4311469d4e Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
1 parent 19be070 commit 31ae635

File tree

6 files changed

+133
-150
lines changed

6 files changed

+133
-150
lines changed

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

Lines changed: 59 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
import uuid
2020

2121
from novaclient import api_versions
22+
from openstack.compute.v2 import aggregate as _aggregate
2223
from openstack.compute.v2 import flavor as _flavor
2324
from openstack.compute.v2 import hypervisor as _hypervisor
25+
from openstack.compute.v2 import keypair as _keypair
2426
from openstack.compute.v2 import migration as _migration
2527
from openstack.compute.v2 import server as _server
2628
from openstack.compute.v2 import server_action as _server_action
2729
from openstack.compute.v2 import server_group as _server_group
2830
from openstack.compute.v2 import server_interface as _server_interface
2931
from openstack.compute.v2 import server_migration as _server_migration
30-
from openstack.compute.v2 import service
31-
from openstack.compute.v2 import volume_attachment
32+
from openstack.compute.v2 import service as _service
33+
from openstack.compute.v2 import usage as _usage
34+
from openstack.compute.v2 import volume_attachment as _volume_attachment
3235

3336
from openstackclient.api import compute_v2
3437
from openstackclient.tests.unit import fakes
@@ -187,10 +190,8 @@ def setUp(self):
187190
def create_one_aggregate(attrs=None):
188191
"""Create a fake aggregate.
189192
190-
:param dict attrs:
191-
A dictionary with all attributes
192-
:return:
193-
A FakeResource object, with id and other attributes
193+
:param dict attrs: A dictionary with all attributes
194+
:return: A fake openstack.compute.v2.aggregate.Aggregate object
194195
"""
195196
attrs = attrs or {}
196197

@@ -209,21 +210,16 @@ def create_one_aggregate(attrs=None):
209210
# Overwrite default attributes.
210211
aggregate_info.update(attrs)
211212

212-
aggregate = fakes.FakeResource(
213-
info=copy.deepcopy(aggregate_info), loaded=True
214-
)
213+
aggregate = _aggregate.Aggregate(**aggregate_info)
215214
return aggregate
216215

217216

218217
def create_aggregates(attrs=None, count=2):
219218
"""Create multiple fake aggregates.
220219
221-
:param dict attrs:
222-
A dictionary with all attributes
223-
:param int count:
224-
The number of aggregates to fake
225-
:return:
226-
A list of FakeResource objects faking the aggregates
220+
:param dict attrs: A dictionary with all attributes
221+
:param int count: The number of aggregates to fake
222+
:return: A list of fake openstack.compute.v2.aggregate.Aggregate objects
227223
"""
228224
aggregates = []
229225
for i in range(0, count):
@@ -238,12 +234,9 @@ def get_aggregates(aggregates=None, count=2):
238234
If aggregates list is provided, then initialize the Mock object
239235
with the list. Otherwise create one.
240236
241-
:param List aggregates:
242-
A list of FakeResource objects faking aggregates
243-
:param int count:
244-
The number of aggregates to fake
245-
:return:
246-
An iterable Mock object with side_effect set to a list of faked
237+
:return: A list of fake openstack.compute.v2.aggregate.Aggregate objects
238+
:param int count: The number of aggregates to fake
239+
:return: An iterable Mock object with side_effect set to a list of faked
247240
aggregates
248241
"""
249242
if aggregates is None:
@@ -491,19 +484,13 @@ def create_servers(attrs=None, methods=None, count=2):
491484
return servers
492485

493486

494-
def create_one_sdk_server(attrs=None, methods=None):
487+
def create_one_sdk_server(attrs=None):
495488
"""Create a fake server for testing migration to sdk
496489
497-
:param dict attrs:
498-
A dictionary with all attributes
499-
:param dict methods:
500-
A dictionary with all methods
501-
:return:
502-
A openstack.compute.v2.server.Server object,
503-
with id, name, metadata, and so on
490+
:param dict attrs: A dictionary with all attributes
491+
:return: A fake openstack.compute.v2.server.Server object,
504492
"""
505493
attrs = attrs or {}
506-
methods = methods or {}
507494

508495
# Set default attributes.
509496
server_info = {
@@ -529,22 +516,16 @@ def create_one_sdk_server(attrs=None, methods=None):
529516
return server
530517

531518

532-
def create_sdk_servers(attrs=None, methods=None, count=2):
519+
def create_sdk_servers(attrs=None, count=2):
533520
"""Create multiple fake servers for testing migration to sdk
534521
535-
:param dict attrs:
536-
A dictionary with all attributes
537-
:param dict methods:
538-
A dictionary with all methods
539-
:param int count:
540-
The number of servers to fake
541-
:return:
542-
A list of openstack.compute.v2.server.Server objects
543-
faking the servers
522+
:param dict attrs: A dictionary with all attributes
523+
:param int count: The number of servers to fake
524+
:return: A list of fake openstack.compute.v2.server.Server objects
544525
"""
545526
servers = []
546527
for i in range(0, count):
547-
servers.append(create_one_sdk_server(attrs, methods))
528+
servers.append(create_one_sdk_server(attrs))
548529

549530
return servers
550531

@@ -555,12 +536,11 @@ def get_servers(servers=None, count=2):
555536
If servers list is provided, then initialize the Mock object with the
556537
list. Otherwise create one.
557538
558-
:param List servers:
559-
A list of FakeResource objects faking servers
539+
:param list servers: A list of fake openstack.compute.v2.server.Server
540+
objects
560541
:param int count:
561542
The number of servers to fake
562-
:return:
563-
An iterable Mock object with side_effect set to a list of faked
543+
:return: An iterable Mock object with side_effect set to a list of faked
564544
servers
565545
"""
566546
if servers is None:
@@ -614,10 +594,8 @@ def create_one_server_action(attrs=None):
614594
def create_one_service(attrs=None):
615595
"""Create a fake service.
616596
617-
:param dict attrs:
618-
A dictionary with all attributes
619-
:return:
620-
A fake Service object, with id, host, binary, and so on
597+
:param dict attrs: A dictionary with all attributes
598+
:return: A fake openstack.compute.v2.service.Service object
621599
"""
622600
attrs = attrs or {}
623601

@@ -638,7 +616,7 @@ def create_one_service(attrs=None):
638616
# Overwrite default attributes.
639617
service_info.update(attrs)
640618

641-
return service.Service(**service_info)
619+
return _service.Service(**service_info)
642620

643621

644622
def create_services(attrs=None, count=2):
@@ -661,10 +639,8 @@ def create_services(attrs=None, count=2):
661639
def create_one_flavor(attrs=None):
662640
"""Create a fake flavor.
663641
664-
:param dict attrs:
665-
A dictionary with all attributes
666-
:return:
667-
A FakeResource object, with id, name, ram, vcpus, and so on
642+
:param dict attrs: A dictionary with all attributes
643+
:return: A fake openstack.compute.v2.flavor.Flavor object
668644
"""
669645
attrs = attrs or {}
670646

@@ -695,12 +671,9 @@ def create_one_flavor(attrs=None):
695671
def create_flavors(attrs=None, count=2):
696672
"""Create multiple fake flavors.
697673
698-
:param dict attrs:
699-
A dictionary with all attributes
700-
:param int count:
701-
The number of flavors to fake
702-
:return:
703-
A list of FakeResource objects faking the flavors
674+
:param dict attrs: A dictionary with all attributes
675+
:param int count: The number of flavors to fake
676+
:return: A list of fake openstack.compute.v2.flavor.Flavor objects
704677
"""
705678
flavors = []
706679
for i in range(0, count):
@@ -715,12 +688,10 @@ def get_flavors(flavors=None, count=2):
715688
If flavors list is provided, then initialize the Mock object with the
716689
list. Otherwise create one.
717690
718-
:param List flavors:
719-
A list of FakeResource objects faking flavors
720-
:param int count:
721-
The number of flavors to fake
722-
:return:
723-
An iterable Mock object with side_effect set to a list of faked
691+
:param list flavors: A list of fake openstack.compute.v2.flavor.Flavor
692+
objects
693+
:param int count: The number of flavors to fake
694+
:return: An iterable Mock object with side_effect set to a list of faked
724695
flavors
725696
"""
726697
if flavors is None:
@@ -757,10 +728,8 @@ def create_one_flavor_access(attrs=None):
757728
def create_one_keypair(attrs=None):
758729
"""Create a fake keypair
759730
760-
:param dict attrs:
761-
A dictionary with all attributes
762-
:return:
763-
A FakeResource object, name, fingerprint, and so on
731+
:param dict attrs: A dictionary with all attributes
732+
:return: A fake openstack.compute.v2.keypair.Keypair object
764733
"""
765734
attrs = attrs or {}
766735

@@ -776,20 +745,15 @@ def create_one_keypair(attrs=None):
776745
# Overwrite default attributes.
777746
keypair_info.update(attrs)
778747

779-
keypair = fakes.FakeResource(info=copy.deepcopy(keypair_info), loaded=True)
780-
781-
return keypair
748+
return _keypair.Keypair(**keypair_info)
782749

783750

784751
def create_keypairs(attrs=None, count=2):
785752
"""Create multiple fake keypairs.
786753
787-
:param dict attrs:
788-
A dictionary with all attributes
789-
:param int count:
790-
The number of keypairs to fake
791-
:return:
792-
A list of FakeResource objects faking the keypairs
754+
:param dict attrs: A dictionary with all attributes
755+
:param int count: The number of keypairs to fake
756+
:return: A list of fake openstack.compute.v2.keypair.Keypair objects
793757
"""
794758

795759
keypairs = []
@@ -805,12 +769,10 @@ def get_keypairs(keypairs=None, count=2):
805769
If keypairs list is provided, then initialize the Mock object with the
806770
list. Otherwise create one.
807771
808-
:param List keypairs:
809-
A list of FakeResource objects faking keypairs
810-
:param int count:
811-
The number of keypairs to fake
812-
:return:
813-
An iterable Mock object with side_effect set to a list of faked
772+
:param list keypairs: A list of fake openstack.compute.v2.keypair.Keypair
773+
objects
774+
:param int count: The number of keypairs to fake
775+
:return: An iterable Mock object with side_effect set to a list of faked
814776
keypairs
815777
"""
816778
if keypairs is None:
@@ -1122,7 +1084,7 @@ def create_one_usage(attrs=None):
11221084

11231085
# Set default attributes.
11241086
usage_info = {
1125-
'tenant_id': 'usage-tenant-id-' + uuid.uuid4().hex,
1087+
'project_id': 'usage-tenant-id-' + uuid.uuid4().hex,
11261088
'total_memory_mb_usage': 512.0,
11271089
'total_vcpus_usage': 1.0,
11281090
'total_local_gb_usage': 1.0,
@@ -1145,9 +1107,7 @@ def create_one_usage(attrs=None):
11451107
# Overwrite default attributes.
11461108
usage_info.update(attrs)
11471109

1148-
usage = fakes.FakeResource(info=copy.deepcopy(usage_info), loaded=True)
1149-
1150-
return usage
1110+
return _usage.Usage(**usage_info)
11511111

11521112

11531113
def create_usages(attrs=None, count=2):
@@ -1511,7 +1471,7 @@ def create_one_volume_attachment(attrs=None):
15111471
# Overwrite default attributes.
15121472
volume_attachment_info.update(attrs)
15131473

1514-
return volume_attachment.VolumeAttachment(**volume_attachment_info)
1474+
return _volume_attachment.VolumeAttachment(**volume_attachment_info)
15151475

15161476

15171477
def create_volume_attachments(attrs=None, count=2):
@@ -1532,10 +1492,8 @@ def create_volume_attachments(attrs=None, count=2):
15321492
def create_one_hypervisor(attrs=None):
15331493
"""Create a fake hypervisor.
15341494
1535-
:param dict attrs:
1536-
A dictionary with all attributes
1537-
:return:
1538-
A FakeResource object, with id, hypervisor_hostname, and so on
1495+
:param dict attrs: A dictionary with all attributes
1496+
:return: A fake openstack.compute.v2.hypervisor.Hypervisor object
15391497
"""
15401498
attrs = attrs or {}
15411499

@@ -1579,12 +1537,9 @@ def create_one_hypervisor(attrs=None):
15791537
def create_hypervisors(attrs=None, count=2):
15801538
"""Create multiple fake hypervisors.
15811539
1582-
:param dict attrs:
1583-
A dictionary with all attributes
1584-
:param int count:
1585-
The number of hypervisors to fake
1586-
:return:
1587-
A list of FakeResource objects faking the hypervisors
1540+
:param dict attrs: A dictionary with all attributes
1541+
:param int count: The number of hypervisors to fake
1542+
:return: A list of fake openstack.compute.v2.hypervisor.Hypervisor objects
15881543
"""
15891544
hypervisors = []
15901545
for i in range(0, count):
@@ -1596,10 +1551,8 @@ def create_hypervisors(attrs=None, count=2):
15961551
def create_one_server_group(attrs=None):
15971552
"""Create a fake server group
15981553
1599-
:param dict attrs:
1600-
A dictionary with all attributes
1601-
:return:
1602-
A fake ServerGroup object, with id and other attributes
1554+
:param dict attrs: A dictionary with all attributes
1555+
:return: A fake openstack.compute.v2.server_group.ServerGroup object
16031556
"""
16041557
if attrs is None:
16051558
attrs = {}
@@ -1626,7 +1579,8 @@ def create_one_server_interface(attrs=None):
16261579
16271580
:param dict attrs: A dictionary with all attributes
16281581
:param dict methods: A dictionary with all methods
1629-
:return: A fake ServerInterface object with various attributes set
1582+
:return: A fake openstack.compute.v2.server_interface.ServerInterface
1583+
object
16301584
"""
16311585
attrs = attrs or {}
16321586

0 commit comments

Comments
 (0)