def test_scan_cancel(self): scanid = scan_ctrl.new(self.scanid, self.userid, 10) for code in IrmaScanStatus.label.keys(): scan_ctrl.error(scanid, code) with session_query() as session: scan = Scan.load(scanid, session) self.assertEqual(scan.status, code)
def info(job_id): with session_query() as session: job = Job.load(job_id, session) frontend_scan_id = job.scan.scan_id filename = job.filename probe = job.probename return (frontend_scan_id, filename, probe)
def get_quota(user_id): with session_query() as session: user = User.load(user_id, session) quota = user.quota remaining = None if quota is not None: remaining = user.remaining_quota(session) return (remaining, quota)
def test004_query_error(self, m_session): exception = IrmaDatabaseError with self.assertRaises(exception): with module.session_query(): raise exception m_session.commit.assert_not_called() m_session.rollback.assert_not_called() m_session.close.assert_not_called()
def test003_query(self, m_SQLDatabase): m_db_session = MagicMock() m_SQLDatabase.get_session.return_value = m_db_session with module.session_query(): pass m_db_session.commit.assert_not_called() m_db_session.rollback.assert_not_called() m_db_session.close.assert_not_called()
def test_scan_new_status(self): # test we have corrects fields scanid = scan_ctrl.new(self.scanid, self.userid, 10) with session_query() as session: scan = Scan.load(scanid, session) self.assertIsNotNone(scan.timestamp) self.assertEqual(scan.status, IrmaScanStatus.empty) self.assertEqual(scan.nb_files, 10) self.assertIsNotNone(scan.nb_jobs)
def test004_query_error(self, m_SQLDatabase): m_db_session = MagicMock() m_SQLDatabase.get_session.return_value = m_db_session exception = IrmaDatabaseError with self.assertRaises(exception): with module.session_query(): raise exception m_db_session.commit.assert_not_called() m_db_session.rollback.assert_not_called() m_db_session.close.assert_not_called()
def job_error(parent_taskid, file, probe): try: log.info("file:%s probe %s", file, probe) with session_query() as session: result = probe_ctrl.create_error_results(probe, "job error", session) celery_frontend.scan_result(file, probe, result) except Exception as e: log.exception(e) raise job_error.retry(countdown=5, max_retries=3, exc=e)
def test_scan_launched(self): scan_id = scan_ctrl.new(self.scanid, self.userid, 10) for i in xrange(0, 10): for probe in ['probe1', 'probe2']: job_ctrl.new(scan_id, "file-{0}".format(i), probe, 1) scan_ctrl.launched(scan_id) with session_query() as session: scan = Scan.load(scan_id, session) self.assertEqual(scan.status, IrmaScanStatus.launched) self.assertEqual(scan.nb_files, 10) self.assertEqual(scan.nb_jobs, 20)
def progress(scan_id): with session_query() as session: scan = Scan.load(scan_id, session) if IrmaScanStatus.is_error(scan.status): status_str = IrmaScanStatus.label[scan.status] raise IrmaValueError(status_str) status = IrmaScanStatus.label[scan.status] progress_details = None if scan.status == IrmaScanStatus.launched: (total, finished, success) = scan.progress() progress_details = {} progress_details['total'] = total progress_details['finished'] = finished progress_details['successful'] = success return (status, progress_details)
def test_user_quota_update(self): # test update quota (before, _) = user_ctrl.get_quota(self.userid) scanid = scan_ctrl.new(self.scanid, self.userid, 2) for i in xrange(0, 10): for probe in ['probe1', 'probe2']: job_ctrl.new(scanid, "file-{0}".format(i), probe, 1) scan_ctrl.launched(scanid) (after, _) = user_ctrl.get_quota(self.userid) self.assertEqual(before - after, 20) with session_query() as session: user = User.load(self.userid, session) scan_ids = [scan.id for scan in user.scans] self.assertNotEqual(scan_ids, []) self.assertTrue(scanid in scan_ids)
def test_scan_job_error(self): scan_id = scan_ctrl.new(self.scanid, self.userid, 10) job_ids = [] for i in xrange(0, 10): for probe in ['probe1', 'probe2']: job_ids.append(job_ctrl.new(scan_id, "file-{0}".format(i), probe, 1)) scan_ctrl.launched(scan_id) shuffle(job_ids) for job_id in job_ids: job_ctrl.error(job_id) self.assertTrue(scan_ctrl.check_finished(scan_id)) with session_query() as session: scan = Scan.load(scan_id, session) self.assertEqual(scan.status, IrmaScanStatus.processed) self.assertEqual(scan.nb_files, 10) self.assertEqual(scan.nb_jobs, 20)
def job_error(parent_taskid, frontend_scan_id, filename, probename): try: log.info("scanid %s: filename:%s probe %s", frontend_scan_id, filename, probename) with session_query() as session: probe = Probe.get_by_name(probename, session) result = {} result['status'] = -1 result['name'] = probe.display_name result['type'] = probe.category result['error'] = "job error" result['duration'] = None celery_frontend.scan_result(frontend_scan_id, filename, probe, result) except Exception as e: log.exception(e) raise job_error.retry(countdown=5, max_retries=3, exc=e)
def get_scan_id(frontend_scan_id, user_id): with session_query() as session: scan = Scan.get_scan(frontend_scan_id, user_id, session) return scan.id
def get_pending_jobs(scan_id): with session_query() as session: scan = Scan.load(scan_id, session) return scan.get_pending_jobs_taskid()
def get_nbjobs(scan_id): with session_query() as session: scan = Scan.load(scan_id, session) return scan.nb_jobs
def test003_query(self, m_session): with module.session_query(): pass m_session.commit.assert_not_called() m_session.rollback.assert_not_called() m_session.close.assert_not_called()
def duration(job_id): with session_query() as session: job = Job.load(job_id, session) return (job.ts_end - job.ts_start)
def get_userid(rmqvhost): with session_query() as session: user = User.get_by_rmqvhost(rmqvhost, session) return user.id
def get_ftpuser(user_id): with session_query() as session: user = User.load(user_id, session) return user.ftpuser