diff --git a/openstackclient/tests/volume/v2/fakes.py b/openstackclient/tests/volume/v2/fakes.py index fc45e47b9d..c4155c69be 100644 --- a/openstackclient/tests/volume/v2/fakes.py +++ b/openstackclient/tests/volume/v2/fakes.py @@ -498,3 +498,57 @@ class FakeAvailabilityZone(object): availability_zones.append(availability_zone) return availability_zones + + +class FakeBackup(object): + """Fake one or more backup.""" + + @staticmethod + def create_one_backup(attrs=None): + """Create a fake backup. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object with id, name, volume_id, etc. + """ + attrs = attrs or {} + + # Set default attributes. + backup_info = { + "id": 'backup-id-' + uuid.uuid4().hex, + "name": 'backup-name-' + uuid.uuid4().hex, + "volume_id": 'volume-id-' + uuid.uuid4().hex, + "description": 'description-' + uuid.uuid4().hex, + "object_count": None, + "container": 'container-' + uuid.uuid4().hex, + "size": random.randint(1, 20), + "status": "error", + "availability_zone": 'zone' + uuid.uuid4().hex, + } + + # Overwrite default attributes. + backup_info.update(attrs) + + backup = fakes.FakeResource( + info=copy.deepcopy(backup_info), + loaded=True) + return backup + + @staticmethod + def create_backups(attrs=None, count=2): + """Create multiple fake backups. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of backups to fake + :return: + A list of FakeResource objects faking the backups + """ + backups = [] + for i in range(0, count): + backup = FakeBackup.create_one_backup(attrs) + backups.append(backup) + + return backups diff --git a/openstackclient/tests/volume/v2/test_backup.py b/openstackclient/tests/volume/v2/test_backup.py index 0e906e7bf1..8a151a9108 100644 --- a/openstackclient/tests/volume/v2/test_backup.py +++ b/openstackclient/tests/volume/v2/test_backup.py @@ -12,9 +12,6 @@ # under the License. # -import copy - -from openstackclient.tests import fakes from openstackclient.tests.volume.v2 import fakes as volume_fakes from openstackclient.volume.v2 import backup @@ -34,83 +31,101 @@ class TestBackup(volume_fakes.TestVolume): class TestBackupCreate(TestBackup): + volume = volume_fakes.FakeVolume.create_one_volume() + new_backup = volume_fakes.FakeBackup.create_one_backup( + attrs={'volume_id': volume.id}) + + columns = ( + 'availability_zone', + 'container', + 'description', + 'id', + 'name', + 'object_count', + 'size', + 'status', + 'volume_id', + ) + data = ( + new_backup.availability_zone, + new_backup.container, + new_backup.description, + new_backup.id, + new_backup.name, + new_backup.object_count, + new_backup.size, + new_backup.status, + new_backup.volume_id, + ) + def setUp(self): super(TestBackupCreate, self).setUp() - self.volumes_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.VOLUME), - loaded=True - ) + self.volumes_mock.get.return_value = self.volume + self.backups_mock.create.return_value = self.new_backup - self.backups_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.BACKUP), - loaded=True - ) # Get the command object to test self.cmd = backup.CreateBackup(self.app, None) def test_backup_create(self): arglist = [ - volume_fakes.volume_id, - "--name", volume_fakes.backup_name, - "--description", volume_fakes.backup_description, - "--container", volume_fakes.backup_name + "--name", self.new_backup.name, + "--description", self.new_backup.description, + "--container", self.new_backup.container, + self.new_backup.volume_id, ] verifylist = [ - ("volume", volume_fakes.volume_id), - ("name", volume_fakes.backup_name), - ("description", volume_fakes.backup_description), - ("container", volume_fakes.backup_name) + ("name", self.new_backup.name), + ("description", self.new_backup.description), + ("container", self.new_backup.container), + ("volume", self.new_backup.volume_id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.backups_mock.create.assert_called_with( - volume_fakes.volume_id, - container=volume_fakes.backup_name, - name=volume_fakes.backup_name, - description=volume_fakes.backup_description + self.new_backup.volume_id, + container=self.new_backup.container, + name=self.new_backup.name, + description=self.new_backup.description ) - self.assertEqual(columns, volume_fakes.BACKUP_columns) - self.assertEqual(data, volume_fakes.BACKUP_data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) def test_backup_create_without_name(self): arglist = [ - volume_fakes.volume_id, - "--description", volume_fakes.backup_description, - "--container", volume_fakes.backup_name + "--description", self.new_backup.description, + "--container", self.new_backup.container, + self.new_backup.volume_id, ] verifylist = [ - ("volume", volume_fakes.volume_id), - ("description", volume_fakes.backup_description), - ("container", volume_fakes.backup_name) + ("description", self.new_backup.description), + ("container", self.new_backup.container), + ("volume", self.new_backup.volume_id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.backups_mock.create.assert_called_with( - volume_fakes.volume_id, - container=volume_fakes.backup_name, + self.new_backup.volume_id, + container=self.new_backup.container, name=None, - description=volume_fakes.backup_description + description=self.new_backup.description ) - self.assertEqual(columns, volume_fakes.BACKUP_columns) - self.assertEqual(data, volume_fakes.BACKUP_data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) class TestBackupDelete(TestBackup): + backup = volume_fakes.FakeBackup.create_one_backup() + def setUp(self): super(TestBackupDelete, self).setUp() - self.backups_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.BACKUP), - loaded=True) + self.backups_mock.get.return_value = self.backup self.backups_mock.delete.return_value = None # Get the command object to mock @@ -118,21 +133,25 @@ class TestBackupDelete(TestBackup): def test_backup_delete(self): arglist = [ - volume_fakes.backup_id + self.backup.id ] verifylist = [ - ("backups", [volume_fakes.backup_id]) + ("backups", [self.backup.id]) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - self.backups_mock.delete.assert_called_with(volume_fakes.backup_id) + self.backups_mock.delete.assert_called_with(self.backup.id) self.assertIsNone(result) class TestBackupList(TestBackup): + volume = volume_fakes.FakeVolume.create_one_volume() + backups = volume_fakes.FakeBackup.create_backups( + attrs={'volume_id': volume.name}, count=3) + columns = [ 'ID', 'Name', @@ -140,33 +159,39 @@ class TestBackupList(TestBackup): 'Status', 'Size', ] - datalist = ( - ( - volume_fakes.backup_id, - volume_fakes.backup_name, - volume_fakes.backup_description, - volume_fakes.backup_status, - volume_fakes.backup_size - ), - ) + columns_long = columns + [ + 'Availability Zone', + 'Volume', + 'Container', + ] + + data = [] + for b in backups: + data.append(( + b.id, + b.name, + b.description, + b.status, + b.size, + )) + data_long = [] + for b in backups: + data_long.append(( + b.id, + b.name, + b.description, + b.status, + b.size, + b.availability_zone, + b.volume_id, + b.container, + )) def setUp(self): super(TestBackupList, self).setUp() - self.volumes_mock.list.return_value = [ - fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.VOLUME), - loaded=True - ) - ] - self.backups_mock.list.return_value = [ - fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.BACKUP), - loaded=True - ) - ] + self.volumes_mock.list.return_value = [self.volume] + self.backups_mock.list.return_value = self.backups # Get the command to test self.cmd = backup.ListBackup(self.app, None) @@ -178,7 +203,7 @@ class TestBackupList(TestBackup): columns, data = self.cmd.take_action(parsed_args) self.assertEqual(self.columns, columns) - self.assertEqual(self.datalist, tuple(data)) + self.assertEqual(self.data, list(data)) def test_backup_list_with_options(self): arglist = ["--long"] @@ -187,86 +212,87 @@ class TestBackupList(TestBackup): parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) - columns = self.columns + [ - 'Availability Zone', - 'Volume', - 'Container', - ] - - self.assertEqual(columns, columns) - - datalist = (( - volume_fakes.backup_id, - volume_fakes.backup_name, - volume_fakes.backup_description, - volume_fakes.backup_status, - volume_fakes.backup_size, - volume_fakes.volume_availability_zone, - volume_fakes.backup_volume_id, - volume_fakes.backup_container - ),) - self.assertEqual(datalist, tuple(data)) + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, list(data)) class TestBackupRestore(TestBackup): + volume = volume_fakes.FakeVolume.create_one_volume() + backup = volume_fakes.FakeBackup.create_one_backup( + attrs={'volume_id': volume.id}) + def setUp(self): super(TestBackupRestore, self).setUp() - self.backups_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.BACKUP), - loaded=True - ) - self.volumes_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.VOLUME), - loaded=True - ) + self.backups_mock.get.return_value = self.backup + self.volumes_mock.get.return_value = self.volume self.restores_mock.restore.return_value = None # Get the command object to mock self.cmd = backup.RestoreBackup(self.app, None) def test_backup_restore(self): arglist = [ - volume_fakes.backup_id, - volume_fakes.volume_id + self.backup.id, + self.backup.volume_id ] verifylist = [ - ("backup", volume_fakes.backup_id), - ("volume", volume_fakes.volume_id) + ("backup", self.backup.id), + ("volume", self.backup.volume_id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - self.restores_mock.restore.assert_called_with(volume_fakes.backup_id, - volume_fakes.volume_id) + self.restores_mock.restore.assert_called_with(self.backup.id, + self.backup.volume_id) self.assertIsNone(result) class TestBackupShow(TestBackup): + backup = volume_fakes.FakeBackup.create_one_backup() + + columns = ( + 'availability_zone', + 'container', + 'description', + 'id', + 'name', + 'object_count', + 'size', + 'status', + 'volume_id', + ) + data = ( + backup.availability_zone, + backup.container, + backup.description, + backup.id, + backup.name, + backup.object_count, + backup.size, + backup.status, + backup.volume_id, + ) + def setUp(self): super(TestBackupShow, self).setUp() - self.backups_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.BACKUP), - loaded=True) + self.backups_mock.get.return_value = self.backup # Get the command object to test self.cmd = backup.ShowBackup(self.app, None) def test_backup_show(self): arglist = [ - volume_fakes.backup_id + self.backup.id ] verifylist = [ - ("backup", volume_fakes.backup_id) + ("backup", self.backup.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) - self.backups_mock.get.assert_called_with(volume_fakes.backup_id) + self.backups_mock.get.assert_called_with(self.backup.id) - self.assertEqual(volume_fakes.BACKUP_columns, columns) - self.assertEqual(volume_fakes.BACKUP_data, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data)