|
13 | 13 | # under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import copy |
16 | 17 | import json |
17 | 18 | import mock |
18 | 19 | import six |
19 | 20 | import sys |
| 21 | +import uuid |
20 | 22 |
|
21 | 23 | from keystoneauth1 import fixture |
22 | 24 | import requests |
@@ -183,3 +185,71 @@ def __getattr__(self, key): |
183 | 185 | return self[key] |
184 | 186 | except KeyError: |
185 | 187 | 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