Example #1
0
 def testAutomaticCorrectionRunnerMustCreateDirectoryToRunScript(self):
     runner = AutomaticCorrectionRunner()
     runner.setup_enviroment(self.delivery, self.script)
     self.assertTrue(os.path.isfile(AutomaticCorrectionRunner.TMP_DIR + "/" + os.path.basename(self.script.file.name)))
     zipfile = ZipFile(self.delivery.file.name)
     for name in zipfile.namelist():
         self.assertTrue(os.path.isfile(AutomaticCorrectionRunner.TMP_DIR + "/" + name))
Example #2
0
 def testEjecutarDeberiaConsultarCorreccionesPendientes(self):
     corrector = AutomaticCorrectionRunner()
     selection_strategy_mock = Mock()
     selection_strategy_mock.get_automatic_corrections.return_value = list()
     corrector.selection_strategy = selection_strategy_mock
     
     corrector.run()
     
     corrector.selection_strategy.get_automatic_corrections.assert_called()
Example #3
0
    def testEjecutarDeberiaConsultarCorreccionesPendientes(self):
        corrector = AutomaticCorrectionRunner()
        selection_strategy_mock = Mock()
        selection_strategy_mock.get_automatic_corrections.return_value = list()
        corrector.selection_strategy = selection_strategy_mock

        corrector.run()

        corrector.selection_strategy.get_automatic_corrections.assert_called()
Example #4
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("loop runner")
     self.loop_interval = LoopRunner.LOOP_INTERVAL  # in seconds
     self.stdin_path = '/dev/null'
     self.stdout_path = '/dev/tty'
     self.stderr_path = '/dev/tty'
     self.pidfile_path = '/tmp/foo.pid'
     self.pidfile_timeout = 5
     self.automatic_correction_runner = AutomaticCorrectionRunner()
     self.administrator_mail = AdministratorMail()
Example #5
0
    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()
Example #6
0
 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()
Example #7
0
class LoopRunner():

    LOOP_INTERVAL = 65

    def __init__(self):
        logger_manager = LoggerManager()
        self.log = logger_manager.get_new_logger("loop runner")
        self.loop_interval = LoopRunner.LOOP_INTERVAL  # in seconds
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = '/tmp/foo.pid'
        self.pidfile_timeout = 5
        self.automatic_correction_runner = AutomaticCorrectionRunner()
        self.administrator_mail = AdministratorMail()

    def stall_loop(self, start_timestamp, finish_timestamp):
        delta = finish_timestamp - start_timestamp
        time_to_wait = self.loop_interval - delta.seconds  # if the process took less than 30 seconds, we will wait
        if (time_to_wait > 0):
            time.sleep(time_to_wait)
        else:
            print("not sleeping... delta: " + str(delta))
        start_timestamp = datetime.today()

    def print_result(self, result):
        # TODO: pass to a log entry
        print str(datetime.today()) + " | " + str(result)

    def run(self):
        self.log = LoggerManager().get_new_logger("daemon control")
        self.log.info("Daemon's game loop started.")
        while True:
            start_timestamp = datetime.today()

            try:
                result = self.automatic_correction_runner.run()
                self.log.info(
                    "Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d",
                    result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                    result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
            except:
                self.log.exception(
                    "The automatic correction process failed. Have in mind that is required that the web service must be online."
                )

            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception(
                    "The mail sending process failed. Have in mind that is required that the web service must be online."
                )

            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
Example #8
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("loop runner")
     self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds
     self.stdin_path = '/dev/null'
     self.stdout_path = '/dev/tty'
     self.stderr_path = '/dev/tty'
     self.pidfile_path =  '/tmp/foo.pid'
     self.pidfile_timeout = 5
     self.automatic_correction_runner = AutomaticCorrectionRunner()
     self.administrator_mail = AdministratorMail()
Example #9
0
class LoopRunner():
    
    LOOP_INTERVAL = 65
    
    def __init__(self):
        logger_manager = LoggerManager()
        self.log = logger_manager.get_new_logger("loop runner")
        self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
        self.automatic_correction_runner = AutomaticCorrectionRunner()
        self.administrator_mail = AdministratorMail()
    
    def stall_loop(self, start_timestamp, finish_timestamp):
        delta = finish_timestamp - start_timestamp
        time_to_wait = self.loop_interval - delta.seconds # if the process took less than 30 seconds, we will wait
        if (time_to_wait > 0):
            time.sleep(time_to_wait)
        else:
            print("not sleeping... delta: " + str(delta))
        start_timestamp = datetime.today()
    
    def print_result(self, result):
        # TODO: pass to a log entry
        print str(datetime.today()) + " | " + str(result)
    
    def run(self):
        self.log = LoggerManager().get_new_logger("daemon control")
        self.log.info("Daemon's game loop started.")
        while True:
            start_timestamp = datetime.today()
            
            try:
                result = self.automatic_correction_runner.run()
                self.log.info("Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d", 
                              result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                              result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
            except:
                self.log.exception("The automatic correction process failed. Have in mind that is required that the web service must be online.")
            
            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception("The mail sending process failed. Have in mind that is required that the web service must be online.")
            
            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
Example #10
0
 def testRunScriptMustTurnPendingAutomaticCorrectionToSuccessfull(self):
     runner = AutomaticCorrectionRunner()
     runner.setup_enviroment(self.delivery, self.script)
     runner.run_script(self.automatic_correction, self.script)
     runner.clean_up_tmp_dir()
     self.assertEquals(self.automatic_correction.status, 1)
Example #11
0
 def testAutomaticCorrectionRunnerMustCleanUpDirectoryAfterRunningScript(self):
     runner = AutomaticCorrectionRunner()
     runner.clean_up_tmp_dir()
     self.assertFalse(os.path.isfile(AutomaticCorrectionRunner.TMP_DIR + "/" + os.path.basename(self.script.file.name)))
Example #12
0
 def testTheMethodMustReturnOnlyOneElementInTheList(self):
     runner = AutomaticCorrectionRunner()
     pending_automatic_corrections = runner.get_pending_automatic_corrections()
     self.assertEquals(len(pending_automatic_corrections), 1)