Skip to content

Commit cddf4c3

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Introduce random server faking mechanism."
2 parents 4776bf6 + b1cc7fb commit cddf4c3

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

openstackclient/tests/fakes.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
# under the License.
1414
#
1515

16+
import copy
1617
import json
1718
import mock
1819
import six
1920
import sys
21+
import uuid
2022

2123
from keystoneauth1 import fixture
2224
import requests
@@ -183,3 +185,71 @@ def __getattr__(self, key):
183185
return self[key]
184186
except KeyError:
185187
raise AttributeError(key)
188+
189+
190+
class FakeServer(object):
191+
"""Fake one or more compute servers."""
192+
193+
@staticmethod
194+
def create_one_server(attrs={}, methods={}):
195+
"""Create a fake server.
196+
197+
:param Dictionary attrs:
198+
A dictionary with all attributes
199+
:param Dictionary methods:
200+
A dictionary with all methods
201+
:return:
202+
A FakeResource object, with id, name, metadata
203+
"""
204+
# Set default attributes.
205+
server_info = {
206+
'id': 'server-id-' + uuid.uuid4().hex,
207+
'name': 'server-name-' + uuid.uuid4().hex,
208+
'metadata': {},
209+
}
210+
211+
# Overwrite default attributes.
212+
server_info.update(attrs)
213+
214+
server = FakeResource(info=copy.deepcopy(server_info),
215+
methods=methods,
216+
loaded=True)
217+
return server
218+
219+
@staticmethod
220+
def create_servers(attrs={}, methods={}, count=2):
221+
"""Create multiple fake servers.
222+
223+
:param Dictionary attrs:
224+
A dictionary with all attributes
225+
:param Dictionary methods:
226+
A dictionary with all methods
227+
:param int count:
228+
The number of servers to fake
229+
:return:
230+
A list of FakeResource objects faking the servers
231+
"""
232+
servers = []
233+
for i in range(0, count):
234+
servers.append(FakeServer.create_one_server(attrs, methods))
235+
236+
return servers
237+
238+
@staticmethod
239+
def get_servers(servers=None, count=2):
240+
"""Get an iterable MagicMock object with a list of faked servers.
241+
242+
If servers list is provided, then initialize the Mock object with the
243+
list. Otherwise create one.
244+
245+
:param List servers:
246+
A list of FakeResource objects faking servers
247+
:param int count:
248+
The number of servers to fake
249+
:return:
250+
An iterable Mock object with side_effect set to a list of faked
251+
servers
252+
"""
253+
if servers is None:
254+
servers = FakeServer.create_servers(count)
255+
return mock.MagicMock(side_effect=servers)

0 commit comments

Comments
 (0)