Fakes¶
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.
- openstack.test.fakes.generate_fake_resource(resource_type, **attrs)¶
Generate a fake resource
- Parameters:
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(...)
- Parameters:
resource_type (type) – Object class
attrs (dict) – Optional attributes to be set on resource
- Returns:
Instance of
resource_type
class populated with fake values of expected types- Raises:
NotImplementedError – If a resource attribute specifies a
type
orlist_type
that cannot be automatically generated
- openstack.test.fakes.generate_fake_resources(resource_type, count=1, attrs=None)¶
Generate a given number of fake resource entities
- Parameters:
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>
- Parameters:
resource_type (type) – Object class
count (int) – Number of objects to return
attrs (dict) – Attribute values to set into each instance
- Returns:
Generator of
resource_type
class instances populated with fake values of expected types.
- openstack.test.fakes.generate_fake_proxy(service, api_version=None)¶
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'?
- Parameters:
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 providedservice
. This is only required if openstacksdk supports multiple API versions for the given service.
- Raises:
ValueError – if the
service
is not a validServiceDescription
or ifapi_version
is not supported- Returns:
An autospecced mock of the
Proxy
implementation for the specified service type and API version