|
13 | 13 | # under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import copy |
16 | 17 | import mock |
| 18 | +import random |
| 19 | +import uuid |
17 | 20 |
|
18 | 21 | from openstackclient.tests.unit import fakes |
19 | 22 | from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes |
@@ -234,6 +237,159 @@ def get_services(services=None, count=2): |
234 | 237 | return mock.MagicMock(side_effect=services) |
235 | 238 |
|
236 | 239 |
|
| 240 | +class FakeQos(object): |
| 241 | + """Fake one or more Qos specification.""" |
| 242 | + |
| 243 | + @staticmethod |
| 244 | + def create_one_qos(attrs=None): |
| 245 | + """Create a fake Qos specification. |
| 246 | +
|
| 247 | + :param Dictionary attrs: |
| 248 | + A dictionary with all attributes |
| 249 | + :return: |
| 250 | + A FakeResource object with id, name, consumer, etc. |
| 251 | + """ |
| 252 | + attrs = attrs or {} |
| 253 | + |
| 254 | + # Set default attributes. |
| 255 | + qos_info = { |
| 256 | + "id": 'qos-id-' + uuid.uuid4().hex, |
| 257 | + "name": 'qos-name-' + uuid.uuid4().hex, |
| 258 | + "consumer": 'front-end', |
| 259 | + "specs": {"foo": "bar", "iops": "9001"}, |
| 260 | + } |
| 261 | + |
| 262 | + # Overwrite default attributes. |
| 263 | + qos_info.update(attrs) |
| 264 | + |
| 265 | + qos = fakes.FakeResource( |
| 266 | + info=copy.deepcopy(qos_info), |
| 267 | + loaded=True) |
| 268 | + return qos |
| 269 | + |
| 270 | + @staticmethod |
| 271 | + def create_qoses(attrs=None, count=2): |
| 272 | + """Create multiple fake Qos specifications. |
| 273 | +
|
| 274 | + :param Dictionary attrs: |
| 275 | + A dictionary with all attributes |
| 276 | + :param int count: |
| 277 | + The number of Qos specifications to fake |
| 278 | + :return: |
| 279 | + A list of FakeResource objects faking the Qos specifications |
| 280 | + """ |
| 281 | + qoses = [] |
| 282 | + for i in range(0, count): |
| 283 | + qos = FakeQos.create_one_qos(attrs) |
| 284 | + qoses.append(qos) |
| 285 | + |
| 286 | + return qoses |
| 287 | + |
| 288 | + @staticmethod |
| 289 | + def get_qoses(qoses=None, count=2): |
| 290 | + """Get an iterable MagicMock object with a list of faked qoses. |
| 291 | +
|
| 292 | + If qoses list is provided, then initialize the Mock object with the |
| 293 | + list. Otherwise create one. |
| 294 | +
|
| 295 | + :param List volumes: |
| 296 | + A list of FakeResource objects faking qoses |
| 297 | + :param Integer count: |
| 298 | + The number of qoses to be faked |
| 299 | + :return |
| 300 | + An iterable Mock object with side_effect set to a list of faked |
| 301 | + qoses |
| 302 | + """ |
| 303 | + if qoses is None: |
| 304 | + qoses = FakeQos.create_qoses(count) |
| 305 | + |
| 306 | + return mock.MagicMock(side_effect=qoses) |
| 307 | + |
| 308 | + |
| 309 | +class FakeVolume(object): |
| 310 | + """Fake one or more volumes.""" |
| 311 | + |
| 312 | + @staticmethod |
| 313 | + def create_one_volume(attrs=None): |
| 314 | + """Create a fake volume. |
| 315 | +
|
| 316 | + :param Dictionary attrs: |
| 317 | + A dictionary with all attributes of volume |
| 318 | + :return: |
| 319 | + A FakeResource object with id, name, status, etc. |
| 320 | + """ |
| 321 | + attrs = attrs or {} |
| 322 | + |
| 323 | + # Set default attribute |
| 324 | + volume_info = { |
| 325 | + 'id': 'volume-id' + uuid.uuid4().hex, |
| 326 | + 'name': 'volume-name' + uuid.uuid4().hex, |
| 327 | + 'description': 'description' + uuid.uuid4().hex, |
| 328 | + 'status': random.choice(['available', 'in_use']), |
| 329 | + 'size': random.randint(1, 20), |
| 330 | + 'volume_type': |
| 331 | + random.choice(['fake_lvmdriver-1', 'fake_lvmdriver-2']), |
| 332 | + 'bootable': |
| 333 | + random.randint(0, 1), |
| 334 | + 'metadata': { |
| 335 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex, |
| 336 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex, |
| 337 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex}, |
| 338 | + 'snapshot_id': random.randint(1, 5), |
| 339 | + 'availability_zone': 'zone' + uuid.uuid4().hex, |
| 340 | + 'attachments': [{ |
| 341 | + 'device': '/dev/' + uuid.uuid4().hex, |
| 342 | + 'server_id': uuid.uuid4().hex, |
| 343 | + }, ], |
| 344 | + } |
| 345 | + |
| 346 | + # Overwrite default attributes if there are some attributes set |
| 347 | + volume_info.update(attrs) |
| 348 | + |
| 349 | + volume = fakes.FakeResource( |
| 350 | + None, |
| 351 | + volume_info, |
| 352 | + loaded=True) |
| 353 | + return volume |
| 354 | + |
| 355 | + @staticmethod |
| 356 | + def create_volumes(attrs=None, count=2): |
| 357 | + """Create multiple fake volumes. |
| 358 | +
|
| 359 | + :param Dictionary attrs: |
| 360 | + A dictionary with all attributes of volume |
| 361 | + :param Integer count: |
| 362 | + The number of volumes to be faked |
| 363 | + :return: |
| 364 | + A list of FakeResource objects |
| 365 | + """ |
| 366 | + volumes = [] |
| 367 | + for n in range(0, count): |
| 368 | + volumes.append(FakeVolume.create_one_volume(attrs)) |
| 369 | + |
| 370 | + return volumes |
| 371 | + |
| 372 | + @staticmethod |
| 373 | + def get_volumes(volumes=None, count=2): |
| 374 | + """Get an iterable MagicMock object with a list of faked volumes. |
| 375 | +
|
| 376 | + If volumes list is provided, then initialize the Mock object with the |
| 377 | + list. Otherwise create one. |
| 378 | +
|
| 379 | + :param List volumes: |
| 380 | + A list of FakeResource objects faking volumes |
| 381 | + :param Integer count: |
| 382 | + The number of volumes to be faked |
| 383 | + :return |
| 384 | + An iterable Mock object with side_effect set to a list of faked |
| 385 | + volumes |
| 386 | + """ |
| 387 | + if volumes is None: |
| 388 | + volumes = FakeVolume.create_volumes(count) |
| 389 | + |
| 390 | + return mock.MagicMock(side_effect=volumes) |
| 391 | + |
| 392 | + |
237 | 393 | class FakeImagev1Client(object): |
238 | 394 |
|
239 | 395 | def __init__(self, **kwargs): |
|
0 commit comments