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:
parent
20bf1ef675
commit
b1cc7fb4f6
@ -13,10 +13,12 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import copy
|
||||||
import json
|
import json
|
||||||
import mock
|
import mock
|
||||||
import six
|
import six
|
||||||
import sys
|
import sys
|
||||||
|
import uuid
|
||||||
|
|
||||||
from keystoneauth1 import fixture
|
from keystoneauth1 import fixture
|
||||||
import requests
|
import requests
|
||||||
@ -183,3 +185,71 @@ class FakeModel(dict):
|
|||||||
return self[key]
|
return self[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise AttributeError(key)
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user