def test_activity_add_host_process(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)

        self.assertIsNotNone(a.host_process_counts)
        self.assertEqual(len(a.host_process_counts), 0)

        a.add_host_process(p_hostname=HOSTNAME)

        self.assertIsNotNone(a.host_process_counts)
        self.assertEqual(len(a.host_process_counts), 1)
        self.assertIn(HOSTNAME, a.host_process_counts)
        self.assertEqual(a.host_process_counts[HOSTNAME], 1)

        a.add_host_process(p_hostname=HOSTNAME)

        self.assertIsNotNone(a.host_process_counts)
        self.assertEqual(len(a.host_process_counts), 1)
        self.assertIn(HOSTNAME, a.host_process_counts)
        self.assertEqual(a.host_process_counts[HOSTNAME], 2)

        a.add_host_process(p_hostname=HOSTNAME2)

        self.assertIsNotNone(a.host_process_counts)
        self.assertEqual(len(a.host_process_counts), 2)
        self.assertIn(HOSTNAME2, a.host_process_counts)
        self.assertEqual(a.host_process_counts[HOSTNAME2], 1)
예제 #2
0
    def test_activity_init(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 0)
        self.assertEqual(a.start_time, reference_time)
        self.assertIsNone(a.end_time)
예제 #3
0
    def test_activity_host_infos(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)

        a.add_host_process(p_hostname=HOSTNAME)

        host_infos = a.host_infos

        self.assertIsNotNone(host_infos)
        self.assertIn(HOSTNAME, host_infos)
예제 #4
0
    def test_activity_str(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)
        a.end_time = reference_time + datetime.timedelta(seconds=DURATION)

        a_str = str(a)

        self.assertIsNotNone(a_str)
        self.assertIn(str(DURATION), a_str)
        self.assertIn('Activity', a_str)
예제 #5
0
    def test_activity_duration(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)

        self.assertIsNone(a.duration)

        a.end_time = reference_time + datetime.timedelta(seconds=DURATION)

        self.assertIsNotNone(a.duration)
        self.assertEqual(a.duration, DURATION)
예제 #6
0
    def test_activity_add_host_process(self):
        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 0)

        a.add_host_process(p_hostname=HOSTNAME, p_percent=25)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 1)
        self.assertIn(HOSTNAME, a.host_stats)
        host_stat = a.host_stats[HOSTNAME]
        self.assertEqual(host_stat.count, 1)
        self.assertEqual(host_stat.percent, 25)

        a.add_host_process(p_hostname=HOSTNAME, p_percent=50)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 1)
        self.assertIn(HOSTNAME, a.host_stats)
        host_stat = a.host_stats[HOSTNAME]
        self.assertEqual(host_stat.count, 2)
        self.assertEqual(host_stat.percent, 50)

        a.add_host_process(p_hostname=HOSTNAME, p_percent=100)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 1)
        self.assertIn(HOSTNAME, a.host_stats)
        host_stat = a.host_stats[HOSTNAME]
        self.assertEqual(host_stat.count, 3)
        self.assertEqual(host_stat.percent, 100)

        a.add_host_process(p_hostname=HOSTNAME, p_percent=75)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 1)
        self.assertIn(HOSTNAME, a.host_stats)
        host_stat = a.host_stats[HOSTNAME]
        self.assertEqual(host_stat.count, 4)
        self.assertEqual(host_stat.percent, 100)

        a.add_host_process(p_hostname=HOSTNAME2)

        self.assertIsNotNone(a.host_stats)
        self.assertEqual(len(a.host_stats), 2)
        self.assertIn(HOSTNAME2, a.host_stats)
        host_stat = a.host_stats[HOSTNAME2]
        self.assertEqual(host_stat.count, 1)
예제 #7
0
    def test_max_time_per_day(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)

        activity = process_statistics.Activity(p_start_time=activity_start)

        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)

        stat_info.current_activity = activity

        # Check that playing is allowed up to the maximum session time
        rule_set.max_time_per_day = 1199
        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)

        rule_set.max_time_per_day = 1201

        # 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)
    def test_day_statistics_add_activity(self):
        ds = process_statistics.DayStatistics()

        self.assertIsNotNone(ds.activities)
        self.assertEqual(len(ds.activities), 0)

        reference_time = datetime.datetime.utcnow()
        a = process_statistics.Activity(p_start_time=reference_time)
        a.add_host_process(p_hostname=HOSTNAME)

        ds.add_activity(p_activity=a)

        self.assertIsNotNone(ds.activities)
        self.assertEqual(len(ds.activities), 1)

        self.assertIsNotNone(ds.host_process_counts)
        self.assertEqual(len(ds.host_process_counts), 1)
        self.assertIn(HOSTNAME, ds.host_process_counts)
        self.assertEqual(ds.host_process_counts[HOSTNAME], 1)
    def test_min_break_time(self):

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

        previous_activity_start = reference_time + datetime.timedelta(seconds=-1200)
        previous_activity_end = previous_activity_start + datetime.timedelta(seconds=600)

        previous_activity = process_statistics.Activity(p_start_time=previous_activity_start)
        previous_activity.set_end_time(previous_activity_end)

        previous_short_activity_end = previous_activity_start + datetime.timedelta(seconds=200)
        previous_short_activity = process_statistics.Activity(p_start_time=previous_activity_start)
        previous_short_activity.set_end_time(previous_short_activity_end)

        rule_set.max_activity_duration = 600

        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)

        stat_info.previous_activity = previous_activity

        # Check that playing is not allowed after full break minus one second
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-299)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_set.min_break = 300

        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, rule_handler.RULE_MIN_BREAK)

        # Check that playing is allowed after full break minus one second but with free_play activated
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-299)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_set.min_break = 300
        rule_set.free_play = True
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, 0)

        # Check that playing is allowed after full break
        rule_set.free_play = False
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-300)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_set.min_break = 300
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, 0)

        # Check that playing is not allowed after break after short period (1/3 of max) minus one second
        stat_info.previous_activity = previous_short_activity
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-99)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, rule_handler.RULE_MIN_BREAK)
        self.assertEqual(rule_result_info.args['break_minutes_left'], 0)

        # Check that playing is not allowed after break after short period (1/3 of max) minus one second
        # but with max_activity_duration=0
        stat_info.previous_activity = previous_short_activity
        rule_result_info = rule_handler.RuleResultInfo()
        rule_set.max_activity_duration = 0
        activity_start_time = reference_time + datetime.timedelta(seconds=-299)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, rule_handler.RULE_MIN_BREAK)

        # Check that playing is allowed after break after short period (1/3 of max)
        # but with max_activity_duration=0
        stat_info.previous_activity = previous_short_activity
        rule_result_info = rule_handler.RuleResultInfo()
        rule_set.max_activity_duration = 0
        activity_start_time = reference_time + datetime.timedelta(seconds=-300)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, 0)

        # Check that playing is allowed after break after short period (1/3 of max)
        stat_info.previous_activity = previous_short_activity
        rule_set.max_activity_duration = 600
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-100)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, 0)

        # Check that playing is not allowed after break after short period (1/3 of max) minus 31 second and
        # that the number of break minutes left = 1
        stat_info.previous_activity = previous_short_activity
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-69)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, rule_handler.RULE_MIN_BREAK)
        self.assertEqual(rule_result_info.args['break_minutes_left'], 1)

        # Check that playing is not allowed after break after short period (1/3 of max) minus 91 second and
        # that the number of break minutes left = 2
        stat_info.previous_activity = previous_short_activity
        rule_result_info = rule_handler.RuleResultInfo()
        activity_start_time = reference_time + datetime.timedelta(seconds=-9)
        stat_info.last_inactivity_start_time = activity_start_time
        rule_handler.RuleHandler.check_min_break(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_MIN_BREAK, rule_handler.RULE_MIN_BREAK)
        self.assertEqual(rule_result_info.args['break_minutes_left'], 2)