Skip to content

Commit df8ef60

Browse files
author
Huanxuan Ao
committed
Add unit tests for backup commands in volume v1
There was not any unit tests for backup commands in volume v1 so that sometimes some small bugs maybe ignored, this patch add unit tests for them. Change-Id: Ic67c1b80243f7b3d15dabd25e4e4a1b1517a8b59
1 parent fd876e4 commit df8ef60

2 files changed

Lines changed: 435 additions & 0 deletions

File tree

openstackclient/tests/unit/volume/v1/fakes.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ def __init__(self, **kwargs):
451451
self.transfers.resource_class = fakes.FakeResource(None, {})
452452
self.volume_snapshots = mock.Mock()
453453
self.volume_snapshots.resource_class = fakes.FakeResource(None, {})
454+
self.backups = mock.Mock()
455+
self.backups.resource_class = fakes.FakeResource(None, {})
456+
self.restores = mock.Mock()
457+
self.restores.resource_class = fakes.FakeResource(None, {})
454458
self.auth_token = kwargs['token']
455459
self.management_url = kwargs['endpoint']
456460

@@ -624,3 +628,79 @@ def get_snapshots(snapshots=None, count=2):
624628
snapshots = FakeSnapshot.create_snapshots(count)
625629

626630
return mock.Mock(side_effect=snapshots)
631+
632+
633+
class FakeBackup(object):
634+
"""Fake one or more backup."""
635+
636+
@staticmethod
637+
def create_one_backup(attrs=None):
638+
"""Create a fake backup.
639+
640+
:param Dictionary attrs:
641+
A dictionary with all attributes
642+
:return:
643+
A FakeResource object with id, name, volume_id, etc.
644+
"""
645+
attrs = attrs or {}
646+
647+
# Set default attributes.
648+
backup_info = {
649+
"id": 'backup-id-' + uuid.uuid4().hex,
650+
"name": 'backup-name-' + uuid.uuid4().hex,
651+
"volume_id": 'volume-id-' + uuid.uuid4().hex,
652+
"snapshot_id": 'snapshot-id' + uuid.uuid4().hex,
653+
"description": 'description-' + uuid.uuid4().hex,
654+
"object_count": None,
655+
"container": 'container-' + uuid.uuid4().hex,
656+
"size": random.randint(1, 20),
657+
"status": "error",
658+
"availability_zone": 'zone' + uuid.uuid4().hex,
659+
"links": 'links-' + uuid.uuid4().hex,
660+
}
661+
662+
# Overwrite default attributes.
663+
backup_info.update(attrs)
664+
665+
backup = fakes.FakeResource(
666+
info=copy.deepcopy(backup_info),
667+
loaded=True)
668+
return backup
669+
670+
@staticmethod
671+
def create_backups(attrs=None, count=2):
672+
"""Create multiple fake backups.
673+
674+
:param Dictionary attrs:
675+
A dictionary with all attributes
676+
:param int count:
677+
The number of backups to fake
678+
:return:
679+
A list of FakeResource objects faking the backups
680+
"""
681+
backups = []
682+
for i in range(0, count):
683+
backup = FakeBackup.create_one_backup(attrs)
684+
backups.append(backup)
685+
686+
return backups
687+
688+
@staticmethod
689+
def get_backups(backups=None, count=2):
690+
"""Get an iterable MagicMock object with a list of faked backups.
691+
692+
If backups list is provided, then initialize the Mock object with the
693+
list. Otherwise create one.
694+
695+
:param List volumes:
696+
A list of FakeResource objects faking backups
697+
:param Integer count:
698+
The number of backups to be faked
699+
:return
700+
An iterable Mock object with side_effect set to a list of faked
701+
backups
702+
"""
703+
if backups is None:
704+
backups = FakeBackup.create_backups(count)
705+
706+
return mock.Mock(side_effect=backups)

0 commit comments

Comments
 (0)