return
def _create_container(self, context, backup):
- backup.container = self.update_container_name(backup, backup.container)
+ # Container's name will be decided by the driver (returned by method
+ # update_container_name), if no change is required by the driver then
+ # we'll use the one the backup object already has, but if it doesn't
+ # have one backup_default_container will be used.
+ new_container = self.update_container_name(backup, backup.container)
+ if new_container:
+ # If the driver is not really changing the name we don't want to
+ # dirty the field in the object and save it to the DB with the same
+ # value.
+ if new_container != backup.container:
+ backup.container = new_container
+ elif backup.container is None:
+ backup.container = self.backup_default_container
+
LOG.debug('_create_container started, container: %(container)s,'
'backup: %(backup_id)s.',
{'container': backup.container, 'backup_id': backup.id})
- if backup.container is None:
- backup.container = self.backup_default_container
+
backup.save()
self.put_container(backup.container)
return backup.container
backup = objects.Backup.get_by_id(self.ctxt, 123)
service.backup(backup, self.volume_file)
- def test_backup_default_container(self):
+ @mock.patch.object(db, 'backup_update', wraps=db.backup_update)
+ def test_backup_default_container(self, backup_update_mock):
volume_id = '9552017f-c8b9-4e4e-a876-00000053349c'
self._create_backup_db_entry(volume_id=volume_id,
container=None)
service.backup(backup, self.volume_file)
backup = objects.Backup.get_by_id(self.ctxt, 123)
self.assertEqual('volumebackups', backup['container'])
+ self.assertEqual(3, backup_update_mock.call_count)
+
+ @mock.patch.object(db, 'backup_update', wraps=db.backup_update)
+ def test_backup_db_container(self, backup_update_mock):
+ volume_id = '9552017f-c8b9-4e4e-a876-00000053349c'
+ self._create_backup_db_entry(volume_id=volume_id,
+ container='existing_name')
+ service = swift_dr.SwiftBackupDriver(self.ctxt)
+ self.volume_file.seek(0)
+ backup = objects.Backup.get_by_id(self.ctxt, 123)
+
+ service.backup(backup, self.volume_file)
+ backup = objects.Backup.get_by_id(self.ctxt, 123)
+ self.assertEqual('existing_name', backup['container'])
+ # Make sure we are not making a DB update when we are using the same
+ # value that's already in the DB.
+ self.assertEqual(2, backup_update_mock.call_count)
+
+ @mock.patch.object(db, 'backup_update', wraps=db.backup_update)
+ def test_backup_driver_container(self, backup_update_mock):
+ volume_id = '9552017f-c8b9-4e4e-a876-00000053349c'
+ self._create_backup_db_entry(volume_id=volume_id,
+ container=None)
+ service = swift_dr.SwiftBackupDriver(self.ctxt)
+ self.volume_file.seek(0)
+ backup = objects.Backup.get_by_id(self.ctxt, 123)
+ with mock.patch.object(service, 'update_container_name',
+ return_value='driver_name'):
+ service.backup(backup, self.volume_file)
+ backup = objects.Backup.get_by_id(self.ctxt, 123)
+ self.assertEqual('driver_name', backup['container'])
+ self.assertEqual(3, backup_update_mock.call_count)
@mock.patch('cinder.backup.drivers.swift.SwiftBackupDriver.'
'_send_progress_end')