summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
authorTang Chen <tangchen@cn.fujitsu.com>2015-11-12 11:25:59 +0800
committerTang Chen <tangchen@cn.fujitsu.com>2015-11-19 18:23:11 +0800
commitb1cc7fb4f6b6fd1844ead784978d9f5dae0e81f5 (patch)
treefb7df46d687338c8db49365ddb00493881b4f319 /openstackclient
parent20bf1ef6757258619678e5ee00b8a2c6d742e1f1 (diff)
downloadpython-openstackclient-b1cc7fb4f6b6fd1844ead784978d9f5dae0e81f5.tar.gz
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
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/tests/fakes.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/openstackclient/tests/fakes.py b/openstackclient/tests/fakes.py
index 9f4dcc50..85e65fb1 100644
--- a/openstackclient/tests/fakes.py
+++ b/openstackclient/tests/fakes.py
@@ -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)