def test_worker_loop(self): # test that a single worker can work two items # create a single worker saq.CONFIG['engine']['max_concurrent_remediation_count'] = '1' manager = initialize_remediation_system_manager() start_remediation_system_manager() remediation_id_1 = request_remediation( REMEDIATION_TYPE_TEST, '<message_id_1>', '<recipient_1@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id_1, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) remediation_id_2 = request_remediation( REMEDIATION_TYPE_TEST, '<message_id_2>', '<recipient_2@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id_2, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) saq.db.commit() self.assertEquals( len( saq.db.query(Remediation).filter( Remediation.id == remediation_id_1, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()), 1) self.assertEquals( len( saq.db.query(Remediation).filter( Remediation.id == remediation_id_2, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()), 1) stop_remediation_system_manager()
def test_remediation_execution(self): remediation_id = request_remediation(REMEDIATION_TYPE_EMAIL, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) self.assertTrue(isinstance(remediation_id, int)) remediation = saq.db.query(Remediation).filter( Remediation.id == remediation_id).one() self.assertIsNotNone(remediation) self.system.execute_request(remediation) remediation = saq.db.query(Remediation).filter( Remediation.id == remediation_id).one() self.assertIsNotNone(remediation) self.assertEquals(remediation.type, REMEDIATION_TYPE_EMAIL) self.assertEquals(remediation.action, REMEDIATION_ACTION_REMOVE) self.assertIsNotNone(remediation.insert_date) self.assertEquals(remediation.user_id, saq.test.UNITTEST_USER_ID) self.assertEquals(remediation.key, '<message_id>:<recipient@localhost>') self.assertIsNotNone(remediation.result) self.assertIsNone(remediation.comment) self.assertTrue(remediation.successful) self.assertEquals(remediation.company_id, saq.COMPANY_ID) self.assertIsNone(remediation.lock) self.assertIsNone(remediation.lock_time) self.assertEquals(remediation.status, REMEDIATION_STATUS_COMPLETED)
def test_automation_cleanup(self): # make sure a lock uuid is created start_remediation_system_manager() stop_remediation_system_manager() # insert a new work request remediation_id = request_remediation(REMEDIATION_TYPE_EMAIL, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) # pretend it started processing saq.db.execute(Remediation.__table__.update().values( lock=self.system.lock, lock_time=func.now(), status=REMEDIATION_STATUS_IN_PROGRESS).where( and_(Remediation.company_id == saq.COMPANY_ID, Remediation.lock == None, Remediation.status == REMEDIATION_STATUS_NEW))) saq.db.commit() # start up the system again start_remediation_system_manager() # and it should process that job wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) stop_remediation_system_manager()
def test_automation_queue(self): manager = initialize_remediation_system_manager() start_remediation_system_manager() remediation_id = request_remediation(REMEDIATION_TYPE_TEST, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) stop_remediation_system_manager() saq.db.commit() self.assertTrue(manager.systems['test'].remediation_executed.is_set()) self.assertEquals( len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()), 1)
def test_automation_failure(self): manager = initialize_remediation_system_manager() start_remediation_system_manager() remediation_id = request_remediation(REMEDIATION_TYPE_TEST, '<fail>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) stop_remediation_system_manager() saq.db.commit() self.assertFalse(manager.systems['test'].remediation_executed.is_set()) self.assertEquals( len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()), 1) self.assertEquals(log_count('unable to execute remediation item'), 1) saq.db.commit() r = saq.db.query(Remediation).filter( Remediation.id == remediation_id).one() self.assertFalse(r.successful) self.assertTrue('forced failure' in r.result)
def test_requests(self): initialize_remediation_system_manager() remediation_id = request_remediation(REMEDIATION_TYPE_TEST, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) self.assertTrue(isinstance(remediation_id, int)) r = saq.db.query(Remediation).filter( Remediation.id == remediation_id).one() self.assertIsNotNone(r) self.assertEquals(r.id, remediation_id) self.assertEquals(r.type, REMEDIATION_TYPE_TEST) self.assertEquals(r.action, REMEDIATION_ACTION_REMOVE) self.assertTrue(isinstance(r.insert_date, datetime.datetime)) self.assertEquals(r.user_id, saq.test.UNITTEST_USER_ID) self.assertEquals(r.key, '<message_id>:<recipient@localhost>') self.assertIsNone(r.result) self.assertIsNone(r.comment) self.assertIsNone(r.successful) self.assertEquals(r.company_id, saq.COMPANY_ID) self.assertIsNone(r.lock) self.assertIsNone(r.lock_time) self.assertEquals(r.status, REMEDIATION_STATUS_NEW) remediation_id = request_restoration(REMEDIATION_TYPE_TEST, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) self.assertTrue(isinstance(remediation_id, int)) r = saq.db.query(Remediation).filter( Remediation.id == remediation_id).one() self.assertIsNotNone(r) self.assertEquals(r.id, remediation_id) self.assertEquals(r.type, REMEDIATION_TYPE_TEST) self.assertEquals(r.action, REMEDIATION_ACTION_RESTORE) self.assertTrue(isinstance(r.insert_date, datetime.datetime)) self.assertEquals(r.user_id, saq.test.UNITTEST_USER_ID) self.assertEquals(r.key, '<message_id>:<recipient@localhost>') self.assertIsNone(r.result) self.assertIsNone(r.comment) self.assertIsNone(r.successful) self.assertEquals(r.company_id, saq.COMPANY_ID) self.assertIsNone(r.lock) self.assertIsNone(r.lock_time) self.assertEquals(r.status, REMEDIATION_STATUS_NEW)
def test_automation_queue(self): start_remediation_system_manager() remediation_id = request_remediation(REMEDIATION_TYPE_EMAIL, '<message_id>', '<recipient@localhost>', user_id=saq.test.UNITTEST_USER_ID, company_id=saq.COMPANY_ID) wait_for( lambda: len( saq.db.query(Remediation).filter( Remediation.id == remediation_id, Remediation.status == REMEDIATION_STATUS_COMPLETED).all()) > 0, 1, 5) stop_remediation_system_manager()