The fakes
module exists to help application developers
using the OpenStack SDK to unit test their applications. It provides a number
of helper utilities to generate fake Resource
and
Proxy
instances. These fakes do not require an
established connection and allow you to validate that your application using
valid attributes and methods for both Resource
and
Proxy
instances.
Generate a fake resource
resource_type (type) – Object class
attrs (dict) – Optional attributes to be set on resource
Example usage:
>>> from openstack.compute.v2 import server
>>> from openstack.test import fakes
>>> fakes.generate_fake_resource(server.Server)
openstack.compute.v2.server.Server(...)
resource_type (type) – Object class
attrs (dict) – Optional attributes to be set on resource
Instance of resource_type
class populated with fake
values of expected types
NotImplementedError – If a resource attribute specifies a type
or list_type
that cannot be automatically generated
Generate a given number of fake resource entities
resource_type (type) – Object class
count (int) – Number of objects to return
attrs (dict) – Attribute values to set into each instance
Example usage:
>>> from openstack.compute.v2 import server
>>> from openstack.test import fakes
>>> fakes.generate_fake_resources(server.Server, count=3)
<generator object generate_fake_resources at 0x7f075dc65040>
resource_type (type) – Object class
count (int) – Number of objects to return
attrs (dict) – Attribute values to set into each instance
Generator of resource_type
class instances populated with fake
values of expected types.
Generate a fake proxy for the given service type
Example usage:
>>> import functools
>>> from openstack.compute import compute_service
>>> from openstack.compute.v2 import server
>>> from openstack.test import fakes
>>> # create the fake proxy
>>> fake_compute_proxy = fakes.generate_fake_proxy(
... compute_service.ComputeService,
... )
>>> # configure return values for various proxy APIs
>>> # note that this will generate new fake resources on each invocation
>>> fake_compute_proxy.get_server.side_effect = functools.partial(
... fakes.generate_fake_resource,
... server.Server,
... )
>>> fake_compute_proxy.servers.side_effect = functools.partial(
... fakes.generate_fake_resources,
... server.Server,
... )
>>> fake_compute_proxy.servers()
<generator object generate_fake_resources at 0x7f92768dc040>
>>> fake_compute_proxy.serverssss()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.11/unittest/mock.py", line 653, in __getattr__
raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'serverssss'. Did you mean: 'server_ips'?
service (ServiceDescription
) – The service to generate the fake proxy for.
api_version (int or None) – The API version to generate the fake proxy for.
This should be a major version must be supported by openstacksdk, as
specified in the supported_versions
attribute of the provided
service
. This is only required if openstacksdk supports multiple
API versions for the given service.
ValueError – if the service
is not a valid
ServiceDescription
or if
api_version
is not supported
An autospecced mock of the Proxy
implementation for the specified service type and API version
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.