diff options
| author | Jenkins <jenkins@review.openstack.org> | 2015-11-26 04:03:00 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2015-11-26 04:03:00 +0000 |
| commit | 487fff8cdbabc9629e070b50e9ce3a762e2b180c (patch) | |
| tree | c1698f8820f2453ac0a0ddf2d07d1e335d1a56f1 /openstackclient/tests/compute/v2/fakes.py | |
| parent | bf273985e9cf3f601a9e6aedd88b684c69558ec7 (diff) | |
| parent | b3c2668c341da42ce7f193b1035f7ab352538d95 (diff) | |
| download | python-openstackclient-487fff8cdbabc9629e070b50e9ce3a762e2b180c.tar.gz | |
Merge "Move FakeServer to tests.common.v2.compute.fakes"
Diffstat (limited to 'openstackclient/tests/compute/v2/fakes.py')
| -rw-r--r-- | openstackclient/tests/compute/v2/fakes.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 4ac6a20e..32161ddc 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -13,7 +13,9 @@ # under the License. # +import copy import mock +import uuid from openstackclient.tests import fakes from openstackclient.tests.identity.v2_0 import fakes as identity_fakes @@ -139,3 +141,71 @@ class TestComputev2(utils.TestCommand): endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN, ) + + +class FakeServer(object): + """Fake one or more compute servers.""" + + @staticmethod + def create_one_server(attrs={}, methods={}): + """Create a fake server. + + :param Dictionary attrs: + A dictionary with all attributes + :param Dictionary methods: + A dictionary with all methods + :return: + A FakeResource object, with id, name, metadata + """ + # Set default attributes. + server_info = { + 'id': 'server-id-' + uuid.uuid4().hex, + 'name': 'server-name-' + uuid.uuid4().hex, + 'metadata': {}, + } + + # Overwrite default attributes. + server_info.update(attrs) + + server = fakes.FakeResource(info=copy.deepcopy(server_info), + methods=methods, + loaded=True) + return server + + @staticmethod + def create_servers(attrs={}, methods={}, count=2): + """Create multiple fake servers. + + :param Dictionary attrs: + A dictionary with all attributes + :param Dictionary methods: + A dictionary with all methods + :param int count: + The number of servers to fake + :return: + A list of FakeResource objects faking the servers + """ + servers = [] + for i in range(0, count): + servers.append(FakeServer.create_one_server(attrs, methods)) + + return servers + + @staticmethod + def get_servers(servers=None, count=2): + """Get an iterable MagicMock object with a list of faked servers. + + If servers list is provided, then initialize the Mock object with the + list. Otherwise create one. + + :param List servers: + A list of FakeResource objects faking servers + :param int count: + The number of servers to fake + :return: + An iterable Mock object with side_effect set to a list of faked + servers + """ + if servers is None: + servers = FakeServer.create_servers(count) + return mock.MagicMock(side_effect=servers) |
