def test_backup(self): volume_id = '1234-5678-1234-8888' self._create_volume_db_entry(volume_id) backup_id1 = 123 backup_id2 = 456 self._create_backup_db_entry(backup_id1) self._create_backup_db_entry(backup_id2) volume_file = open('/dev/null', 'rw') # Create two backups of the volume backup1 = db.backup_get(self.ctxt, 123) self.driver.backup(backup1, volume_file) backup2 = db.backup_get(self.ctxt, 456) self.driver.backup(backup2, volume_file) # Create a backup that fails self._create_backup_db_entry(666) fail_back = db.backup_get(self.ctxt, 666) self.sim.error_injection('backup', 'fail') self.assertRaises(exception.InvalidBackup, self.driver.backup, fail_back, volume_file) # Try to restore one, then the other backup1 = db.backup_get(self.ctxt, 123) self.driver.restore(backup1, volume_id, volume_file) self.driver.restore(backup2, volume_id, volume_file) # Delete both backups self.driver.delete(backup2) self.driver.delete(backup1)
def test_backup_shafile(self): def _fake_generate_object_name_prefix(self, backup): az = 'az_fake' backup_name = '%s_backup_%s' % (az, backup['id']) volume = 'volume_%s' % (backup['volume_id']) prefix = volume + '_' + backup_name LOG.debug('_generate_object_name_prefix: %s', prefix) return prefix # Raise a pseudo exception.BackupDriverException. self.stubs.Set(nfs.NFSBackupDriver, '_generate_object_name_prefix', _fake_generate_object_name_prefix) container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '', 1) self._create_backup_db_entry(container=container_name) service = nfs.NFSBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file) backup = db.backup_get(self.ctxt, 123) self.assertEqual(backup['container'], container_name) # Verify sha contents content1 = service._read_sha256file(backup) self.assertEqual(128 * 1024 / content1['chunk_size'], len(content1['sha256s']))
def test_init_host(self): """Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called """ vol1_id = self._create_volume_db_entry() self._create_volume_attach(vol1_id) db.volume_update(self.ctxt, vol1_id, {'status': 'backing-up'}) vol2_id = self._create_volume_db_entry() self._create_volume_attach(vol2_id) db.volume_update(self.ctxt, vol2_id, {'status': 'restoring-backup'}) backup1 = self._create_backup_db_entry(status='creating') backup2 = self._create_backup_db_entry(status='restoring') backup3 = self._create_backup_db_entry(status='deleting') self.backup_mgr.init_host() vol1 = db.volume_get(self.ctxt, vol1_id) self.assertEqual(vol1['status'], 'available') vol2 = db.volume_get(self.ctxt, vol2_id) self.assertEqual(vol2['status'], 'error_restoring') backup1 = db.backup_get(self.ctxt, backup1.id) self.assertEqual(backup1['status'], 'error') backup2 = db.backup_get(self.ctxt, backup2.id) self.assertEqual(backup2['status'], 'available') self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3.id)
def test_backup_image(self): volume_id = '1234-5678-1234-7777' mode = 'image' self._create_volume_db_entry(volume_id) backup_id1 = 123 backup_id2 = 456 backup_id3 = 666 self._create_backup_db_entry(backup_id1, mode) self._create_backup_db_entry(backup_id2, mode) self._create_backup_db_entry(backup_id3, mode) with open(VOLUME_PATH, 'rw') as volume_file: # Create two backups of the volume backup1 = db.backup_get(self.ctxt, backup_id1) self.driver.backup(backup1, volume_file) backup2 = db.backup_get(self.ctxt, backup_id2) self.driver.backup(backup2, volume_file) # Create a backup that fails fail_back = db.backup_get(self.ctxt, backup_id3) self.sim.error_injection('backup', 'fail') self.assertRaises(exception.InvalidBackup, self.driver.backup, fail_back, volume_file) # Try to restore one, then the other self.driver.restore(backup1, volume_id, volume_file) self.driver.restore(backup2, volume_id, volume_file) # Delete both backups self.driver.delete(backup2) self.driver.delete(backup1)
def test_init_host(self): """Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called """ vol1_id = self._create_volume_db_entry() self._create_volume_attach(vol1_id) db.volume_update(self.ctxt, vol1_id, {"status": "backing-up"}) vol2_id = self._create_volume_db_entry() self._create_volume_attach(vol2_id) db.volume_update(self.ctxt, vol2_id, {"status": "restoring-backup"}) backup1_id = self._create_backup_db_entry(status="creating") backup2_id = self._create_backup_db_entry(status="restoring") backup3_id = self._create_backup_db_entry(status="deleting") self.backup_mgr.init_host() vol1 = db.volume_get(self.ctxt, vol1_id) self.assertEqual(vol1["status"], "available") vol2 = db.volume_get(self.ctxt, vol2_id) self.assertEqual(vol2["status"], "error_restoring") backup1 = db.backup_get(self.ctxt, backup1_id) self.assertEqual(backup1["status"], "error") backup2 = db.backup_get(self.ctxt, backup2_id) self.assertEqual(backup2["status"], "available") self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3_id)
def test_backup_file(self): volume_id = '1234-5678-1234-8888' mode = 'file' self.stubs.Set(os, 'stat', fake_stat_file) self._create_volume_db_entry(volume_id) backup_id1 = 123 backup_id2 = 456 self._create_backup_db_entry(backup_id1, mode) self._create_backup_db_entry(backup_id2, mode) with open(VOLUME_PATH, 'rw') as volume_file: # Create two backups of the volume backup1 = db.backup_get(self.ctxt, 123) self.driver.backup(backup1, volume_file) backup2 = db.backup_get(self.ctxt, 456) self.driver.backup(backup2, volume_file) # Create a backup that fails self._create_backup_db_entry(666, mode) fail_back = db.backup_get(self.ctxt, 666) self.sim.error_injection('backup', 'fail') self.assertRaises(exception.InvalidBackup, self.driver.backup, fail_back, volume_file) # Try to restore one, then the other self.driver.restore(backup1, volume_id, volume_file) self.driver.restore(backup2, volume_id, volume_file) # Delete both backups self.driver.delete(backup1) self.driver.delete(backup2)
def test_backup_default_container(self): self._create_backup_db_entry(container=None) service = SwiftBackupService(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file) backup = db.backup_get(self.ctxt, 123) self.assertEquals(backup['container'], 'volumebackups')
def test_backup_custom_container(self): container_name = 'fake99' self._create_backup_db_entry(container=container_name) service = SwiftBackupService(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file) backup = db.backup_get(self.ctxt, 123) self.assertEquals(backup['container'], container_name)
def test_backup_default_container(self): self._create_backup_db_entry(container=None, backup_id=FAKE_BACKUP_ID) service = nfs.NFSBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, FAKE_BACKUP_ID) service.backup(backup, self.volume_file) backup = db.backup_get(self.ctxt, FAKE_BACKUP_ID) self.assertEqual(backup['container'], UPDATED_CONTAINER_NAME)
def test_init_host(self, mock_delete_volume, mock_delete_snapshot): """Test stuck volumes and backups. Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called """ vol1_id = self._create_volume_db_entry() self._create_volume_attach(vol1_id) db.volume_update(self.ctxt, vol1_id, {"status": "backing-up"}) vol2_id = self._create_volume_db_entry() self._create_volume_attach(vol2_id) db.volume_update(self.ctxt, vol2_id, {"status": "restoring-backup"}) vol3_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol3_id, {"status": "available"}) vol4_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol4_id, {"status": "backing-up"}) temp_vol_id = self._create_volume_db_entry() db.volume_update(self.ctxt, temp_vol_id, {"status": "available"}) vol5_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol5_id, {"status": "backing-up"}) temp_snap = self._create_snapshot_db_entry() temp_snap.status = "available" temp_snap.save() vol6_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol6_id, {"status": "restoring-backup"}) backup1 = self._create_backup_db_entry(status="creating", volume_id=vol1_id) backup2 = self._create_backup_db_entry(status="restoring", volume_id=vol2_id) backup3 = self._create_backup_db_entry(status="deleting", volume_id=vol3_id) self._create_backup_db_entry(status="creating", volume_id=vol4_id, temp_volume_id=temp_vol_id) self._create_backup_db_entry(status="creating", volume_id=vol5_id, temp_snapshot_id=temp_snap.id) self.backup_mgr.init_host() vol1 = db.volume_get(self.ctxt, vol1_id) self.assertEqual("available", vol1["status"]) vol2 = db.volume_get(self.ctxt, vol2_id) self.assertEqual("error_restoring", vol2["status"]) vol3 = db.volume_get(self.ctxt, vol3_id) self.assertEqual("available", vol3["status"]) vol4 = db.volume_get(self.ctxt, vol4_id) self.assertEqual("available", vol4["status"]) vol5 = db.volume_get(self.ctxt, vol5_id) self.assertEqual("available", vol5["status"]) vol6 = db.volume_get(self.ctxt, vol6_id) self.assertEqual("error_restoring", vol6["status"]) backup1 = db.backup_get(self.ctxt, backup1.id) self.assertEqual("error", backup1["status"]) backup2 = db.backup_get(self.ctxt, backup2.id) self.assertEqual("available", backup2["status"]) self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3.id) self.assertTrue(mock_delete_volume.called) self.assertTrue(mock_delete_snapshot.called)
def test_backup_delta_two_objects_change(self): def _fake_generate_object_name_prefix(self, backup): az = 'az_fake' backup_name = '%s_backup_%s' % (az, backup['id']) volume = 'volume_%s' % (backup['volume_id']) prefix = volume + '_' + backup_name LOG.debug('_generate_object_name_prefix: %s', prefix) return prefix # Raise a pseudo exception.BackupDriverException. self.stubs.Set(swift_dr.SwiftBackupDriver, '_generate_object_name_prefix', _fake_generate_object_name_prefix) self.flags(backup_swift_object_size=8 * 1024) self.flags(backup_swift_block_size=1024) container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '', 1) self._create_backup_db_entry(container=container_name, backup_id=123) self.stubs.Set(swift, 'Connection', fake_swift_client2.FakeSwiftClient2.Connection) service = swift_dr.SwiftBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file) backup = db.backup_get(self.ctxt, 123) self.assertEqual(backup['container'], container_name) # Create incremental backup with no change to contents self.volume_file.seek(2 * 8 * 1024) self.volume_file.write(os.urandom(1024)) self.volume_file.seek(4 * 8 * 1024) self.volume_file.write(os.urandom(1024)) self._create_backup_db_entry(container=container_name, backup_id=124, parent_id=123) self.stubs.Set(swift, 'Connection', fake_swift_client2.FakeSwiftClient2.Connection) service = swift_dr.SwiftBackupDriver(self.ctxt) self.volume_file.seek(0) deltabackup = db.backup_get(self.ctxt, 124) service.backup(deltabackup, self.volume_file) deltabackup = db.backup_get(self.ctxt, 124) self.assertEqual(deltabackup['container'], container_name) content1 = service._read_sha256file(backup) content2 = service._read_sha256file(deltabackup) # Verify that two shas are changed at index 16 and 32 self.assertNotEqual(content1['sha256s'][16], content2['sha256s'][16]) self.assertNotEqual(content1['sha256s'][32], content2['sha256s'][32])
def test_init_host(self, mock_delete_volume, mock_delete_snapshot): """Test stuck volumes and backups. Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called """ vol1_id = self._create_volume_db_entry() self._create_volume_attach(vol1_id) db.volume_update(self.ctxt, vol1_id, {'status': 'backing-up'}) vol2_id = self._create_volume_db_entry() self._create_volume_attach(vol2_id) db.volume_update(self.ctxt, vol2_id, {'status': 'restoring-backup'}) vol3_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol3_id, {'status': 'available'}) vol4_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol4_id, {'status': 'backing-up'}) temp_vol_id = self._create_volume_db_entry() db.volume_update(self.ctxt, temp_vol_id, {'status': 'available'}) vol5_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol4_id, {'status': 'backing-up'}) temp_snap = self._create_snapshot_db_entry() temp_snap.status = 'available' temp_snap.save() backup1 = self._create_backup_db_entry(status='creating', volume_id=vol1_id) backup2 = self._create_backup_db_entry(status='restoring', volume_id=vol2_id) backup3 = self._create_backup_db_entry(status='deleting', volume_id=vol3_id) self._create_backup_db_entry(status='creating', volume_id=vol4_id, temp_volume_id=temp_vol_id) self._create_backup_db_entry(status='creating', volume_id=vol5_id, temp_snapshot_id=temp_snap.id) self.backup_mgr.init_host() vol1 = db.volume_get(self.ctxt, vol1_id) self.assertEqual(vol1['status'], 'available') vol2 = db.volume_get(self.ctxt, vol2_id) self.assertEqual(vol2['status'], 'error_restoring') backup1 = db.backup_get(self.ctxt, backup1.id) self.assertEqual(backup1['status'], 'error') backup2 = db.backup_get(self.ctxt, backup2.id) self.assertEqual(backup2['status'], 'available') self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3.id) self.assertTrue(mock_delete_volume.called) self.assertTrue(mock_delete_snapshot.called)
def test_restore_uncompressed(self): self._create_backup_db_entry() self.flags(backup_compression_algorithm='none') self.flags(backup_sha_block_size_bytes=32) service = nfs.NFSBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file) with tempfile.NamedTemporaryFile() as restored_file: backup = db.backup_get(self.ctxt, 123) service.restore(backup, '1234-5678-1234-8888', restored_file) self.assertTrue(filecmp.cmp(self.volume_file.name, restored_file.name))
def test_import_record_with_verify_not_implemented(self): """Test normal backup record import. Test the case when import succeeds for the case that the driver does not support verify. """ vol_size = 1 export = self._create_exported_record_entry(vol_size=vol_size) imported_record = self._create_export_record_db_entry() backup_hosts = [] backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) _mock_backup_verify_class = ('%s.%s.%s' % (backup_driver.__module__, backup_driver.__class__.__name__, 'verify')) with mock.patch(_mock_backup_verify_class) as _mock_record_verify: _mock_record_verify.side_effect = NotImplementedError() self.backup_mgr.import_record(self.ctxt, imported_record, export['backup_service'], export['backup_url'], backup_hosts) backup = db.backup_get(self.ctxt, imported_record) self.assertEqual(backup['status'], 'available') self.assertEqual(backup['size'], vol_size)
def test_restore_backup_with_bad_service(self): """Test error handling when attempting a restore of a backup with a different service to that used to create the backup """ vol_id = self._create_volume_db_entry(status='restoring-backup', size=1) backup_id = self._create_backup_db_entry(status='restoring', volume_id=vol_id) def fake_restore_backup(context, backup, volume, backup_service): pass self.stubs.Set(self.backup_mgr.driver, 'restore_backup', fake_restore_backup) service = 'cinder.tests.backup.bad_service' db.backup_update(self.ctxt, backup_id, {'service': service}) self.assertRaises(exception.InvalidBackup, self.backup_mgr.restore_backup, self.ctxt, backup_id, vol_id) vol = db.volume_get(self.ctxt, vol_id) self.assertEqual(vol['status'], 'error') backup = db.backup_get(self.ctxt, backup_id) self.assertEqual(backup['status'], 'available')
def test_backup_bz2(self): self._create_backup_db_entry() self.flags(backup_compression_algorithm='bz2') service = nfs.NFSBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file)
def test_try_delete_base_image_diff_format(self): # don't create volume db entry since it should not be required backup = db.backup_get(self.ctxt, self.backup_id) backup_name = self.service._get_backup_base_name(self.volume_id, diff_format=True) snap_name = self.service._get_new_snap_name(self.backup_id) snaps = [{'name': snap_name}] def rbd_list(*args): return [backup_name] def list_snaps(*args): return snaps def remove_snap(*args): snaps.pop() self.stubs.Set(self.service.rbd.Image, 'remove_snap', remove_snap) self.stubs.Set(self.service.rbd.Image, 'list_snaps', list_snaps) self.stubs.Set(self.service.rbd.RBD, 'list', rbd_list) def remove(inst, ioctx, name): self.called.append('remove') self.stubs.Set(self.service.rbd.RBD, 'remove', remove) self.service.delete(backup) self.assertEqual(self.called, ['remove'])
def test_import_record_with_verify_invalid_backup(self): """Test error handling. Test error handling when attempting an import of a backup record where the backup driver returns an exception. """ vol_size = 1 backup_id = uuid.uuid4() export = self._create_exported_record_entry( vol_size=vol_size, exported_id=backup_id) imported_record = self._create_export_record_db_entry( backup_id=backup_id) backup_hosts = [] backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) _mock_backup_verify_class = ('%s.%s.%s' % (backup_driver.__module__, backup_driver.__class__.__name__, 'verify')) with mock.patch(_mock_backup_verify_class) as _mock_record_verify: _mock_record_verify.side_effect = \ exception.InvalidBackup(reason='fake') self.assertRaises(exception.InvalidBackup, self.backup_mgr.import_record, self.ctxt, imported_record, export['backup_service'], export['backup_url'], backup_hosts) self.assertTrue(_mock_record_verify.called) backup = db.backup_get(self.ctxt, imported_record.id) self.assertEqual('error', backup['status'])
def test_backup_vol_length_0(self): self._set_common_backup_stubs(self.service) backup = db.backup_get(self.ctxt, self.backup_id) self._create_volume_db_entry(self.volume_id, 0) self.assertRaises(exception.InvalidParameterValue, self.service.backup, backup, self.volume_file)
def test_restore(self): self._create_volume_db_entry(self.volume_id, 1) backup = db.backup_get(self.ctxt, self.backup_id) self._set_common_restore_stubs(self.service) backup_name = self.service._get_backup_base_name(self.backup_id, diff_format=True) def rbd_list(inst, ioctx): return [backup_name] self.stubs.Set(self.service.rbd.RBD, 'list', rbd_list) self.stubs.Set(self.service, '_discard_bytes', lambda *args: None) with tempfile.NamedTemporaryFile() as test_file: self.volume_file.seek(0) def read_data(inst, offset, length): return self.volume_file.read(self.length) self.stubs.Set(self.service.rbd.Image, 'read', read_data) self.service.restore(backup, self.volume_id, test_file) checksum = hashlib.sha256() test_file.seek(0) for c in xrange(0, self.num_chunks): checksum.update(test_file.read(self.chunk_size)) # Ensure the files are equal self.assertEqual(checksum.digest(), self.checksum.digest())
def test_import_record_with_verify(self): """Test normal backup record import. Test the case when import succeeds for the case that the driver implements verify. """ vol_size = 1 backup_id = uuid.uuid4() export = self._create_exported_record_entry( vol_size=vol_size, exported_id=backup_id) imported_record = self._create_export_record_db_entry( backup_id=backup_id) backup_hosts = [] backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) _mock_backup_verify_class = ('%s.%s.%s' % (backup_driver.__module__, backup_driver.__class__.__name__, 'verify')) with mock.patch(_mock_backup_verify_class): self.backup_mgr.import_record(self.ctxt, imported_record, export['backup_service'], export['backup_url'], backup_hosts) backup = db.backup_get(self.ctxt, imported_record.id) self.assertEqual('available', backup['status']) self.assertEqual(vol_size, backup['size'])
def test_try_delete_base_image(self): # don't create volume db entry since it should not be required self._create_volume_db_entry(self.volume_id, 1) backup = db.backup_get(self.ctxt, self.backup_id) backup_name = self.service._get_backup_base_name(self.volume_id, self.backup_id) def rbd_list(inst, ioctx): return [backup_name] self.stubs.Set(self.service.rbd.RBD, 'list', rbd_list) # Must be something mutable remove_called = [] self.stubs.Set(self.service, 'get_backup_snaps', lambda *args, **kwargs: None) def remove(inst, ioctx, name): remove_called.append(True) self.stubs.Set(self.service.rbd.RBD, 'remove', remove) self.service.delete(backup) self.assertTrue(remove_called[0])
def test_backup_reset_status_to_available_invalid_backup(self): volume = db.volume_create(self.ctxt, {'status': 'available', 'host': 'test', 'provider_location': '', 'size': 1}) backup = self._create_backup_db_entry(status='error', volume_id=volume['id']) backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) _mock_backup_verify_class = ('%s.%s.%s' % (backup_driver.__module__, backup_driver.__class__.__name__, 'verify')) with mock.patch(_mock_backup_verify_class) as \ _mock_record_verify: _mock_record_verify.side_effect = \ exception.BackupVerifyUnsupportedDriver(reason='fake') self.assertRaises(exception.BackupVerifyUnsupportedDriver, self.backup_mgr.reset_status, self.ctxt, backup, 'available') backup = db.backup_get(self.ctxt, backup.id) self.assertEqual('error', backup['status'])
def test_import_record_with_invalid_backup(self): """Test error handling. Test error handling when attempting an import of a backup record where the backup driver returns an exception. """ export = self._create_exported_record_entry() backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) _mock_record_import_class = ('%s.%s.%s' % (backup_driver.__module__, backup_driver.__class__.__name__, 'import_record')) imported_record = self._create_export_record_db_entry() backup_hosts = [] with mock.patch(_mock_record_import_class) as _mock_record_import: _mock_record_import.side_effect = FakeBackupException('fake') self.assertRaises(exception.InvalidBackup, self.backup_mgr.import_record, self.ctxt, imported_record, export['backup_service'], export['backup_url'], backup_hosts) self.assertTrue(_mock_record_import.called) backup = db.backup_get(self.ctxt, imported_record.id) self.assertEqual('error', backup['status'])
def test_try_delete_base_image_busy(self): """This should induce retries then raise rbd.ImageBusy.""" # don't create volume db entry since it should not be required self._create_volume_db_entry(self.volume_id, 1) backup = db.backup_get(self.ctxt, self.backup_id) backup_name = self.service._get_backup_base_name(self.volume_id, self.backup_id) def rbd_list(inst, ioctx): return [backup_name] self.stubs.Set(self.service.rbd.RBD, 'list', rbd_list) self.stubs.Set(self.service, 'get_backup_snaps', lambda *args, **kwargs: None) def remove(inst, ioctx, name): raise self.service.rbd.ImageBusy("image busy") self.stubs.Set(self.service.rbd.RBD, 'remove', remove) self.assertRaises(self.service.rbd.ImageBusy, self.service._try_delete_base_image, backup['id'], backup['volume_id'])
def test_backup_backup_metadata_fail2(self): """Test of when an exception occurs in an exception handler. In backup(), after an exception occurs in self._backup_metadata(), we want to check the process when the second exception occurs in self.delete(). """ self._create_backup_db_entry() self.flags(backup_compression_algorithm='none') service = nfs.NFSBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) def fake_backup_metadata(self, backup, object_meta): raise exception.BackupDriverException(message=_('fake')) # Raise a pseudo exception.BackupDriverException. self.stubs.Set(nfs.NFSBackupDriver, '_backup_metadata', fake_backup_metadata) def fake_delete(self, backup): raise exception.BackupOperationError() # Raise a pseudo exception.BackupOperationError. self.stubs.Set(nfs.NFSBackupDriver, 'delete', fake_delete) # We expect that the second exception is notified. self.assertRaises(exception.BackupOperationError, service.backup, backup, self.volume_file)
def test_create_backup_put_object_wraps_socket_error(self): container_name = "socket_error_on_put" self._create_backup_db_entry(container=container_name) service = SwiftBackupService(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) self.assertRaises(exception.SwiftConnectionFailed, service.backup, backup, self.volume_file)
def test_backup_uncompressed(self): self._create_backup_db_entry() self.flags(backup_compression_algorithm='none') service = SwiftBackupDriver(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file)
def test_backup_zlib(self): self._create_backup_db_entry() self.flags(backup_compression_algorithm='zlib') service = SwiftBackupService(self.ctxt) self.volume_file.seek(0) backup = db.backup_get(self.ctxt, 123) service.backup(backup, self.volume_file)
def test_restore(self): self._create_backup_db_entry() service = SwiftBackupService(self.ctxt) with tempfile.NamedTemporaryFile() as volume_file: backup = db.backup_get(self.ctxt, 123) service.restore(backup, '1234-5678-1234-8888', volume_file)
def test_delete(self): self._create_backup_db_entry() service = SwiftBackupDriver(self.ctxt) backup = db.backup_get(self.ctxt, 123) service.delete(backup)
def _get_backup_attrib(backup_id, attrib_name): return db.backup_get(context.get_admin_context(), backup_id)[attrib_name]
def get_by_id(cls, context, id): db_backup = db.backup_get(context, id) return cls._from_db_object(context, cls(context), db_backup)
def get_by_id(cls, context, id, read_deleted=None, project_only=None): db_backup = db.backup_get(context, id, read_deleted=read_deleted, project_only=project_only) return cls._from_db_object(context, cls(context), db_backup)
def test_init_host(self, mock_delete_volume, mock_delete_snapshot): """Test stuck volumes and backups. Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called """ vol1_id = self._create_volume_db_entry() self._create_volume_attach(vol1_id) db.volume_update(self.ctxt, vol1_id, {'status': 'backing-up'}) vol2_id = self._create_volume_db_entry() self._create_volume_attach(vol2_id) db.volume_update(self.ctxt, vol2_id, {'status': 'restoring-backup'}) vol3_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol3_id, {'status': 'available'}) vol4_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol4_id, {'status': 'backing-up'}) temp_vol_id = self._create_volume_db_entry() db.volume_update(self.ctxt, temp_vol_id, {'status': 'available'}) vol5_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol5_id, {'status': 'backing-up'}) temp_snap = self._create_snapshot_db_entry() temp_snap.status = 'available' temp_snap.save() vol6_id = self._create_volume_db_entry() db.volume_update(self.ctxt, vol6_id, {'status': 'restoring-backup'}) backup1 = self._create_backup_db_entry(status='creating', volume_id=vol1_id) backup2 = self._create_backup_db_entry(status='restoring', volume_id=vol2_id) backup3 = self._create_backup_db_entry(status='deleting', volume_id=vol3_id) self._create_backup_db_entry(status='creating', volume_id=vol4_id, temp_volume_id=temp_vol_id) self._create_backup_db_entry(status='creating', volume_id=vol5_id, temp_snapshot_id=temp_snap.id) self.backup_mgr.init_host() vol1 = db.volume_get(self.ctxt, vol1_id) self.assertEqual('available', vol1['status']) vol2 = db.volume_get(self.ctxt, vol2_id) self.assertEqual('error_restoring', vol2['status']) vol3 = db.volume_get(self.ctxt, vol3_id) self.assertEqual('available', vol3['status']) vol4 = db.volume_get(self.ctxt, vol4_id) self.assertEqual('available', vol4['status']) vol5 = db.volume_get(self.ctxt, vol5_id) self.assertEqual('available', vol5['status']) vol6 = db.volume_get(self.ctxt, vol6_id) self.assertEqual('error_restoring', vol6['status']) backup1 = db.backup_get(self.ctxt, backup1.id) self.assertEqual('error', backup1['status']) backup2 = db.backup_get(self.ctxt, backup2.id) self.assertEqual('available', backup2['status']) self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3.id) self.assertTrue(mock_delete_volume.called) self.assertTrue(mock_delete_snapshot.called)
def test_backup_get(self): for backup in self.created: backup_get = db.backup_get(self.ctxt, backup['id']) self._assertEqualObjects(backup, backup_get)