def testEjecutarDeberiaInvocarAlCorregirCuandoHayCorreccionesPendientes(self): corrector = AutomaticCorrectionRunner() automatic_correction = Mock() return_value = Mock() automatic_correction.delivery.practice.get_script.return_value = return_value automatic_correction.script = "test_script.sh" selection_strategy_mock = Mock() selection_strategy_mock.get_automatic_corrections.return_value = (automatic_correction, ) setup_enviroment_mock = Mock() script_result = ScriptResult() script_result.exit_value = 0 script_result.captured_stdout = "some stdout" execution_command_mock = Mock() execution_command_mock.execute.return_value = script_result publication_visitor = Mock() corrector.selection_strategy = selection_strategy_mock corrector.setup_enviroment = setup_enviroment_mock corrector.run_script_command = execution_command_mock corrector.publish_result_visitors = (publication_visitor, ) corrector.run() selection_strategy_mock.get_automatic_corrections.assert_called() setup_enviroment_mock.setup_enviroment.assert_called() execution_command_mock.execute.assert_called() publication_visitor.visit.assert_called()
def testEjecutarDeberiaInvocarAlCorregirCuandoHayCorreccionesPendientes( self): corrector = AutomaticCorrectionRunner() automatic_correction = Mock() return_value = Mock() automatic_correction.delivery.practice.get_script.return_value = return_value automatic_correction.script = "test_script.sh" selection_strategy_mock = Mock() selection_strategy_mock.get_automatic_corrections.return_value = ( automatic_correction, ) setup_enviroment_mock = Mock() script_result = ScriptResult() script_result.exit_value = 0 script_result.captured_stdout = "some stdout" execution_command_mock = Mock() execution_command_mock.execute.return_value = script_result publication_visitor = Mock() corrector.selection_strategy = selection_strategy_mock corrector.setup_enviroment = setup_enviroment_mock corrector.run_script_command = execution_command_mock corrector.publish_result_visitors = (publication_visitor, ) corrector.run() selection_strategy_mock.get_automatic_corrections.assert_called() setup_enviroment_mock.setup_enviroment.assert_called() execution_command_mock.execute.assert_called() publication_visitor.visit.assert_called()
def execute(self): if (self.script is None): self.log.error( "attempt to run the correction process but the script to be run is not defined." ) raise IllegalStateException( reason="In order to execute the script, you must set it first." ) # now we may call the script self.log.debug("launching correction process...") # FIXME: this is plain shit. We should try to use 'with' automatic_correction_tmp_dir = os.path.dirname( self.script ) # managepath.get_instance().get_automatic_correction_tmp_dir() current_dir = os.getcwd() self.log.debug("current dir: %s", current_dir) self.log.debug("script dir : %s", automatic_correction_tmp_dir) os.chdir(automatic_correction_tmp_dir) process = subprocess.Popen([self.script], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) os.chdir(current_dir) # fin FIXME!!! # must ensure that the process won't run forever process_timer = ProcessTimeout(process, RunScriptCommand.TIME_OUT) process_timer.start_timer() self.log.debug("Process timeout timer launched") # finally, we must capture all results so the can be published script_result = ScriptResult() self.log.debug("waiting for process to finish...") script_result.exit_value = process.wait() self.log.debug("process finished with exit value %d", script_result.exit_value) #if the result has been obtained, the is no point on keeping the timer alive if process_timer.ran: self.log.info( "Execution has been terminated for exceding the timeout limit." ) else: process_timer.cancel_timer() self.log.debug( "Process finished correctly without exceding timeout limit.") output = process.communicate() script_result.captured_stdout = output[0] self.log.debug("stdout captured.") self.log.debug("excecution completed.") return script_result
def run(self): """Runs the corresponding script for every Delivery which has not yet been run.""" results = { AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY: 0, AutomaticCorrectionRunner.FAILED_RESULTS_KEY: 0 } pending_automatic_corrections = self.selection_strategy.get_automatic_corrections( ) for pending_automatic_correction in pending_automatic_corrections: try: self.setup_enviroment.run(pending_automatic_correction, AutomaticCorrectionRunner.TMP_DIR) self.run_script_command.set_script( os.path.join( AutomaticCorrectionRunner.TMP_DIR, os.path.basename(pending_automatic_correction.script))) script_result = self.run_script_command.execute() except Exception, e: script_result = ScriptResult() script_result.captured_stdout = "An error has occurred when running the automatic correction process. Error information: " + str( e) script_result.exit_value = 2 script_result.automatic_correction = pending_automatic_correction for visitor in self.publish_result_visitors: script_result.accept(visitor) self.clean_up_tmp_dir() if (script_result.exit_value == 0): results[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY] += 1 else: results[AutomaticCorrectionRunner.FAILED_RESULTS_KEY] += 1
def execute(self): if(self.script is None): self.log.error("attempt to run the correction process but the script to be run is not defined.") raise IllegalStateException(reason="In order to execute the script, you must set it first.") # now we may call the script self.log.debug("launching correction process...") # FIXME: this is plain shit. We should try to use 'with' automatic_correction_tmp_dir = os.path.dirname(self.script) # managepath.get_instance().get_automatic_correction_tmp_dir() current_dir = os.getcwd() self.log.debug("current dir: %s", current_dir) self.log.debug("script dir : %s", automatic_correction_tmp_dir) os.chdir(automatic_correction_tmp_dir) process = subprocess.Popen([self.script], shell=False, stdout = subprocess.PIPE, stderr=subprocess.PIPE) os.chdir(current_dir) # fin FIXME!!! # must ensure that the process won't run forever process_timer = ProcessTimeout(process, RunScriptCommand.TIME_OUT) process_timer.start_timer() self.log.debug("Process timeout timer launched") # finally, we must capture all results so the can be published script_result = ScriptResult() self.log.debug("waiting for process to finish...") script_result.exit_value = process.wait() self.log.debug("process finished with exit value %d", script_result.exit_value) #if the result has been obtained, the is no point on keeping the timer alive if process_timer.ran: self.log.info("Execution has been terminated for exceding the timeout limit.") else: process_timer.cancel_timer() self.log.debug("Process finished correctly without exceding timeout limit.") output = process.communicate() script_result.captured_stdout = output[0] self.log.debug("stdout captured.") self.log.debug("excecution completed.") return script_result
def run(self): """Runs the corresponding script for every Delivery which has not yet been run.""" results = {AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY : 0, AutomaticCorrectionRunner.FAILED_RESULTS_KEY : 0} pending_automatic_corrections = self.selection_strategy.get_automatic_corrections() for pending_automatic_correction in pending_automatic_corrections: try: self.setup_enviroment.run(pending_automatic_correction, AutomaticCorrectionRunner.TMP_DIR) self.run_script_command.set_script(os.path.join(AutomaticCorrectionRunner.TMP_DIR, os.path.basename(pending_automatic_correction.script))) script_result = self.run_script_command.execute() except Exception, e: script_result = ScriptResult() script_result.captured_stdout = "An error has occurred when running the automatic correction process. Error information: " + str(e) script_result.exit_value = 2 script_result.automatic_correction = pending_automatic_correction for visitor in self.publish_result_visitors: script_result.accept(visitor) self.clean_up_tmp_dir() if(script_result.exit_value == 0): results[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY] += 1 else : results[AutomaticCorrectionRunner.FAILED_RESULTS_KEY] += 1
class PublishResultVisitorTest(TestCase): student = None practice = None course = None script = None delivery = None automatic_correction = None course_name = "2012-2" student_name = "student" student_email = "*****@*****.**" practice_deadline = '2012-12-31' practice_filepath = "practice_filepath" practice_uid = "practice_uid" script_file = "/home/anibal/workspace/python-aptana-wkspace/seal/daemon/feature_test/data/successfull_test_script.sh" delivery_filepath = "/home/anibal/workspace/python-aptana-wkspace/seal/daemon/feature_test/data/delivery.zip" delivery_date = '2012-12-15' stdout = "some generated stdout" exit_value = 0 status = 0 def setUp(self): self.automatic_correction = Mock(spec=AutomaticCorrection) self.script_result = ScriptResult() self.script_result.automatic_correction = self.automatic_correction self.script_result.exit_value = self.exit_value self.script_result.captured_stdout = self.stdout def tearDown(self): pass def testTheVisitorShouldSaveTheModificationsToTheDatabaseSoThatTheResultsWillBeReflectedOnTheWebsite( self): user = Mock() password = Mock() publish_result_visitor_web = PublishResultsVisitorWeb(user, password) rest_api_helper = Mock() publish_result_visitor_web.rest_api_helper = rest_api_helper self.script_result.accept(publish_result_visitor_web) self.assertEqual(self.automatic_correction.exit_value, 0) self.assertEqual(self.automatic_correction.captured_stdout, self.stdout) self.assertEqual(self.automatic_correction.status, 1) self.automatic_correction.save.assert_called() rest_api_helper.save_automatic_correction.assert_called_with( self.automatic_correction) def testTheVisitorShouldInvokeTheEmailSendingProcessForTheVisitedResult( self): user = Mock() password = Mock() publish_result_visitor_mail = PublishResultsVisitorMail(user, password) rest_api_helper = Mock() publish_result_visitor_mail.rest_api_helper = rest_api_helper self.script_result.accept(publish_result_visitor_mail) rest_api_helper.save_mail.assert_called() def testTheVisitorShouldConvertTheStatusCodeToTheCorrectString(self): auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) string_status = visitor_mail.get_status(0) self.assertEqual("pending", string_status) string_status = visitor_mail.get_status(1) self.assertEqual("successfull", string_status) string_status = visitor_mail.get_status(-1) self.assertEqual("failed", string_status) string_status = visitor_mail.get_status() self.assertEquals("unknown status", string_status) def testTheVisitorShouldBuildTheSuccessMailToSendProperly(self): result = Mock() result.automatic_correction = Mock() result.automatic_correction.user_mail = "mail" result.exit_value = 0 auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) mail = visitor_mail.build_mail(result) self.assertEquals("Resultado de la correccion automatica", mail.subject) self.assertEquals(result.automatic_correction.user_mail, mail.recipient) self.assertEquals("Ejecucion exitosa, trabajo aprobado.\n\n", mail.body) def testTheVisitorShouldBuildTheFailureMailToSendProperly(self): result = Mock() result.automatic_correction = Mock() result.automatic_correction.user_mail = "mail" result.exit_value = 1 auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) mail = visitor_mail.build_mail(result) self.assertEquals("Resultado de la correccion automatica", mail.subject) self.assertEquals(result.automatic_correction.user_mail, mail.recipient) self.assertEquals( "Ejecucion fallida, trabajo no aprobado. Puede corregirlo y volver a intentarlo.\n\n", mail.body)
def setUp(self): self.automatic_correction = Mock(spec=AutomaticCorrection) self.script_result = ScriptResult() self.script_result.automatic_correction = self.automatic_correction self.script_result.exit_value = self.exit_value self.script_result.captured_stdout = self.stdout
class PublishResultVisitorTest(TestCase): student = None practice = None course = None script = None delivery = None automatic_correction = None course_name = "2012-2" student_name = "student" student_email = "*****@*****.**" practice_deadline = "2012-12-31" practice_filepath = "practice_filepath" practice_uid = "practice_uid" script_file = ( "/home/anibal/workspace/python-aptana-wkspace/seal/daemon/feature_test/data/successfull_test_script.sh" ) delivery_filepath = "/home/anibal/workspace/python-aptana-wkspace/seal/daemon/feature_test/data/delivery.zip" delivery_date = "2012-12-15" stdout = "some generated stdout" exit_value = 0 status = 0 def setUp(self): self.automatic_correction = Mock(spec=AutomaticCorrection) self.script_result = ScriptResult() self.script_result.automatic_correction = self.automatic_correction self.script_result.exit_value = self.exit_value self.script_result.captured_stdout = self.stdout def tearDown(self): pass def testTheVisitorShouldSaveTheModificationsToTheDatabaseSoThatTheResultsWillBeReflectedOnTheWebsite(self): user = Mock() password = Mock() publish_result_visitor_web = PublishResultsVisitorWeb(user, password) rest_api_helper = Mock() publish_result_visitor_web.rest_api_helper = rest_api_helper self.script_result.accept(publish_result_visitor_web) self.assertEqual(self.automatic_correction.exit_value, 0) self.assertEqual(self.automatic_correction.captured_stdout, self.stdout) self.assertEqual(self.automatic_correction.status, 1) self.automatic_correction.save.assert_called() rest_api_helper.save_automatic_correction.assert_called_with(self.automatic_correction) def testTheVisitorShouldInvokeTheEmailSendingProcessForTheVisitedResult(self): user = Mock() password = Mock() publish_result_visitor_mail = PublishResultsVisitorMail(user, password) rest_api_helper = Mock() publish_result_visitor_mail.rest_api_helper = rest_api_helper self.script_result.accept(publish_result_visitor_mail) rest_api_helper.save_mail.assert_called() def testTheVisitorShouldConvertTheStatusCodeToTheCorrectString(self): auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) string_status = visitor_mail.get_status(0) self.assertEqual("pending", string_status) string_status = visitor_mail.get_status(1) self.assertEqual("successfull", string_status) string_status = visitor_mail.get_status(-1) self.assertEqual("failed", string_status) string_status = visitor_mail.get_status() self.assertEquals("unknown status", string_status) def testTheVisitorShouldBuildTheSuccessMailToSendProperly(self): result = Mock() result.automatic_correction = Mock() result.automatic_correction.user_mail = "mail" result.exit_value = 0 auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) mail = visitor_mail.build_mail(result) self.assertEquals("Resultado de la correccion automatica", mail.subject) self.assertEquals(result.automatic_correction.user_mail, mail.recipient) self.assertEquals("Ejecucion exitosa, trabajo aprobado.\n\n", mail.body) def testTheVisitorShouldBuildTheFailureMailToSendProperly(self): result = Mock() result.automatic_correction = Mock() result.automatic_correction.user_mail = "mail" result.exit_value = 1 auth_user = Mock() auth_pass = Mock() visitor_mail = PublishResultsVisitorMail(auth_user=auth_user, auth_pass=auth_pass) mail = visitor_mail.build_mail(result) self.assertEquals("Resultado de la correccion automatica", mail.subject) self.assertEquals(result.automatic_correction.user_mail, mail.recipient) self.assertEquals( "Ejecucion fallida, trabajo no aprobado. Puede corregirlo y volver a intentarlo.\n\n", mail.body )