def test_max_time_per_day_with_downtime_and_previous_activity(self):

        dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(self._logger)
        a_rule_handler = self.create_dummy_rule_handler(p_persistence=dummy_persistence,
                                                        p_create_complex_handlers=False)

        reference_time = datetime.datetime.utcnow()
        rule_set = TestRuleHandler.create_dummy_ruleset_config()

        activity_start = reference_time + datetime.timedelta(seconds=-2000)
        activity_end = reference_time + datetime.timedelta(seconds=-1700)

        stat_info = process_statistics.ProcessStatisticsInfo(p_username=USERNAME, p_reference_time=reference_time,
                                                             p_min_activity_duration=MIN_ACTIVITY_DURATION,
                                                             p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS)

        a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_pid=PID,
                                                  p_start_time=activity_start,
                                                  p_downtime=50)
        stat_info.add_process_start(p_process_info=a_process_info, p_start_time=activity_start)

        a_process_info.end_time = activity_end

        stat_info.add_process_end(p_process_info=a_process_info, p_end_time=activity_end)

        activity_start = reference_time + datetime.timedelta(seconds=-500)

        a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_pid=PID,
                                                  p_start_time=activity_start,
                                                  p_downtime=100)
        stat_info.add_process_start(p_process_info=a_process_info, p_start_time=activity_start)

        # Check that playing is allowed up to the maximum session time
        rule_set.max_time_per_day = 649
        rule_result_info = rule_handler.RuleResultInfo()

        a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info,
                                          p_rule_result_info=rule_result_info)

        self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY,
                         rule_handler.RULE_TIME_PER_DAY)  # TODO: test error on CircleCI

        rule_set.max_time_per_day = 651

        # Check that playing is allowed after the maximum session time
        rule_result_info = rule_handler.RuleResultInfo()

        a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info,
                                          p_rule_result_info=rule_result_info)

        self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY, 0)
Exemplo n.º 2
0
    def test_process_statistics_info_add_process_without_end_time(self):

        start_time = datetime.datetime.utcnow()
        #end_time = start_time + datetime.timedelta(seconds=60)
        psi = process_statistics.ProcessStatisticsInfo(
            p_username=USERNAME,
            p_reference_time=start_time,
            p_min_activity_duration=MIN_ACTIVITY_DURATION,
            p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS)

        self.assertEqual(psi.active_processes, 0)

        pi = process_info.ProcessInfo(p_hostname=HOSTNAME,
                                      p_username=USERNAME,
                                      p_processhandler=None,
                                      p_processname=PROCESS_NAME,
                                      p_pid=PID,
                                      p_start_time=start_time,
                                      p_end_time=None)

        psi.add_process_start(p_process_info=pi, p_start_time=pi.start_time)

        self.assertEqual(psi.active_processes, 1)

        psi.add_process_end(p_process_info=pi, p_end_time=pi.end_time)

        self.assertEqual(psi.active_processes, 0)
def create_process_info_from_event(p_event):
    return process_info.ProcessInfo(p_hostname=p_event.hostname,
                                    p_pid=p_event.pid,
                                    p_username=p_event.username,
                                    p_processhandler=p_event.processhandler,
                                    p_processname=p_event.processname,
                                    p_start_time=p_event.process_start_time)
Exemplo n.º 4
0
    def test_max_session_duration_with_downtime(self):

        dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(
            self._logger)
        a_rule_handler = self.create_dummy_rule_handler(
            p_persistence=dummy_persistence, p_create_complex_handlers=False)

        reference_time = datetime.datetime.utcnow()
        rule_set = TestRuleHandler.create_dummy_ruleset_config()

        activity_start = reference_time + datetime.timedelta(seconds=-1200)

        stat_info = process_statistics.ProcessStatisticsInfo(
            p_username=USERNAME,
            p_reference_time=reference_time,
            p_min_activity_duration=MIN_ACTIVITY_DURATION,
            p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS)

        a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME,
                                                  p_username=USERNAME,
                                                  p_pid=PID,
                                                  p_start_time=activity_start,
                                                  p_downtime=300)
        stat_info.add_process_start(p_process_info=a_process_info,
                                    p_start_time=activity_start)

        # Check that playing is allowed up to the maximum session time
        rule_set.max_activity_duration = 899
        rule_result_info = rule_handler.RuleResultInfo()

        a_rule_handler.check_activity_duration(
            p_rule_set=rule_set,
            p_stat_info=stat_info,
            p_rule_result_info=rule_result_info)

        self.assertEqual(
            rule_result_info.applying_rules
            & rule_handler.RULE_ACTIVITY_DURATION,
            rule_handler.RULE_ACTIVITY_DURATION)

        rule_set.max_activity_duration = 901

        # Check that playing is allowed after the maximum session time
        rule_result_info = rule_handler.RuleResultInfo()

        a_rule_handler.check_activity_duration(
            p_rule_set=rule_set,
            p_stat_info=stat_info,
            p_rule_result_info=rule_result_info)

        self.assertEqual(
            rule_result_info.applying_rules
            & rule_handler.RULE_ACTIVITY_DURATION, 0)
Exemplo n.º 5
0
    def test_constructor(self):
        some_process_handler = object()
        some_start_time = datetime.datetime.now()
        some_end_time = some_start_time + datetime.timedelta(seconds=5)

        pi = process_info.ProcessInfo(p_hostname=HOSTNAME,
                                      p_username=USERNAME,
                                      p_processhandler=some_process_handler,
                                      p_processname=PROCESS_NAME,
                                      p_pid=PID,
                                      p_start_time=some_start_time,
                                      p_end_time=some_end_time)

        self.assertEqual(pi.hostname, HOSTNAME)
Exemplo n.º 6
0
    def create_pinfo(p_age_in_days, p_include_end_time=False):

        start_time = datetime.datetime.utcnow() + datetime.timedelta(
            days=-p_age_in_days)

        if p_include_end_time:
            end_time = start_time + datetime.timedelta(minutes=5)

        else:
            end_time = None

        return process_info.ProcessInfo(p_username=test_data.USER_1,
                                        p_processname=test_data.PROCESS_NAME_1,
                                        p_pid=test_data.PID_1,
                                        p_start_time=start_time,
                                        p_end_time=end_time)
Exemplo n.º 7
0
    def load_historic_process_infos(self):

        pinfos = self._persistence.load_process_infos(
            p_lookback_in_days=self._config.process_lookback_in_days + 1)

        counter_open_end_time = 0

        for pinfo in pinfos:

            if pinfo.end_time is None:
                end_time = None

            else:
                end_time = pinfo.end_time

            new_pinfo = process_info.ProcessInfo(
                p_hostname=pinfo.hostname,
                p_pid=pinfo.pid,
                p_processhandler=pinfo.processhandler,
                p_username=pinfo.username,
                p_processname=pinfo.processname,
                p_start_time=pinfo.start_time,
                p_end_time=end_time)

            if not new_pinfo.is_valid():
                fmt = "Loaded {process_info} is invalid -> ignore"
                self._logger.warning(fmt.format(process_info=str(new_pinfo)))
                continue

            if new_pinfo.end_time is None:
                counter_open_end_time = counter_open_end_time + 1

            process_handler = self.get_process_handler(
                p_id=new_pinfo.processhandler, p_raise_exception=False)

            if process_handler is not None:
                process_handler.add_historic_process(p_process_info=new_pinfo)

        fmt = "Loaded %d historic process infos from database looking back %s days (%d of which had no end time)" % (
            len(pinfos), self._config.process_lookback_in_days,
            counter_open_end_time)
        self._logger.info(fmt)
Exemplo n.º 8
0
def get_active_processes(p_start_time, p_end_time=None):
    return [
        process_info.ProcessInfo(p_username=USER_1, p_processname=PROCESS_NAME_1,
                                 p_pid=PID_1, p_start_time=p_start_time, p_end_time=p_end_time)
    ]
Exemplo n.º 9
0
MAX_TIME_PER_DAY_1 = 3600
MAX_TIME_PER_DAY_2 = 7200
MIN_TIME_OF_DAY_1 = datetime.time(hour=10)
MAX_TIME_OF_DAY_1 = datetime.time(hour=22)

MIN_BREAK_1 = 1800
FREEPLAY_1 = True

LOGIN_MAPPING = login_mapping.LoginMapping(p_default_server_group=login_mapping.DEFAULT_SERVER_GROUP)
LOGIN_UID_MAPPING_ENTRY = p_login_uid_mapping_entry=login_mapping.LoginUidMappingEntry(USER_1, UID_1)
LOGIN_MAPPING.add_entry(p_server_group=login_mapping.DEFAULT_SERVER_GROUP,
                        p_login_uid_mapping_entry=LOGIN_UID_MAPPING_ENTRY)

PROCESS_REGEX_MAP_1 = {USER_1: re.compile(PROCESS_NAME_1)}

PINFO_1 = process_info.ProcessInfo(p_username=USER_1, p_processname=PROCESS_NAME_1,
                                   p_pid=PID_1, p_start_time=START_TIME_1)
PINFO_2 = process_info.ProcessInfo(p_username=USER_1, p_processname=PROCESS_NAME_1,
                                   p_pid=PID_1, p_start_time=START_TIME_1, p_end_time=END_TIME_1)

START_TIME_NOW = datetime.datetime.utcnow()
END_TIME_NOW = START_TIME_NOW + datetime.timedelta(minutes=5)

PINFO_3 = process_info.ProcessInfo(p_username=USER_1, p_processname=PROCESS_NAME_1,
                                   p_pid=PID_1, p_start_time=START_TIME_NOW, p_end_time=END_TIME_NOW)

RULESET_CONFIG_USER1_NO_RESTRICTIONS = rule_handler.RuleSetConfigModel()
RULESET_CONFIG_USER1_NO_RESTRICTIONS.username = USER_1
RULESET_CONFIG_USER1_NO_RESTRICTIONS.process_name_pattern = PROCESS_NAME_1

RULESET_CONFIGS_USER1_NO_RESTRICTIONS = [RULESET_CONFIG_USER1_NO_RESTRICTIONS]