Introduce random server faking mechanism.

This patch introduces a new server faking mechanism to support
multiple servers faking.

Server names and ids can be generated randomly, and use APIs in
class FakeServer to get one or more servers.

Change-Id: Ic54f3bf7c77294dc7dfb9acdbf4a721eb5eef6af
Implements: blueprint osc-unit-test-framework-improvement
This commit is contained in:
Tang Chen 2015-11-12 11:25:59 +08:00
parent 20bf1ef675
commit b1cc7fb4f6

View File

@ -13,10 +13,12 @@
# under the License.
#
import copy
import json
import mock
import six
import sys
import uuid
from keystoneauth1 import fixture
import requests
@ -183,3 +185,71 @@ class FakeModel(dict):
return self[key]
except KeyError:
raise AttributeError(key)
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 = 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)