'Test stuck volumes and backups.
Make sure stuck volumes and backups are reset to correct
states when backup_manager.init_host() is called'

@mock.patch.object(cinder.tests.fake_driver.FakeLoggingVolumeDriver, 'set_initialized')
@mock.patch.object(cinder.tests.fake_driver.FakeLoggingVolumeDriver, 'do_setup')
@mock.patch.object(cinder.tests.fake_driver.FakeLoggingVolumeDriver, 'check_for_setup_error')
@mock.patch('cinder.context.get_admin_context')
def test_init_host(self, mock_get_admin_context, mock_check, mock_setup, mock_set_initialized):

    def get_admin_context():
        return self.ctxt
    self.override_config('backup_service_inithost_offload', False)
    self.override_config('periodic_interval', 0)
    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 = fields.SnapshotStatus.AVAILABLE
    temp_snap.save()
    backup1 = self._create_backup_db_entry(status=fields.BackupStatus.CREATING, volume_id=vol1_id)
    backup2 = self._create_backup_db_entry(status=fields.BackupStatus.RESTORING, restore_volume_id=vol2_id)
    backup3 = self._create_backup_db_entry(status=fields.BackupStatus.DELETING, volume_id=vol3_id)
    self._create_backup_db_entry(status=fields.BackupStatus.CREATING, volume_id=vol4_id, temp_volume_id=temp_vol_id)
    self._create_backup_db_entry(status=fields.BackupStatus.CREATING, volume_id=vol5_id, temp_snapshot_id=temp_snap.id)
    mock_get_admin_context.side_effect = get_admin_context
    self.volume = importutils.import_object(CONF.volume_manager)
    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'])
    backup1 = db.backup_get(self.ctxt, backup1.id)
    self.assertEqual(fields.BackupStatus.ERROR, backup1['status'])
    backup2 = db.backup_get(self.ctxt, backup2.id)
    self.assertEqual(fields.BackupStatus.AVAILABLE, backup2['status'])
    self.assertRaises(exception.BackupNotFound, db.backup_get, self.ctxt, backup3.id)
    temp_vol = objects.Volume.get_by_id(self.ctxt, temp_vol_id)
    self.volume_mocks['delete_volume'].assert_called_once_with(self.ctxt, temp_vol)
    self.assertTrue(self.volume_mocks['detach_volume'].called)
