Exemplo n.º 1
0
    def test2do_timer(self):
        """In this test case the functionality of the timer is tested. The eventCollectTime must not be 0."""
        description = "Test2DefaultMailNotificationEventHandler"
        default_mail_notification_event_handler = DefaultMailNotificationEventHandler(self.analysis_context)
        self.analysis_context.register_component(self, description)

        t = time()
        match_context = MatchContext(self.pid)
        fixed_dme = FixedDataModelElement('s3', self.pid)
        match_element = fixed_dme.get_match_element("match", match_context)

        log_atom = LogAtom(fixed_dme.fixed_data, ParserMatch(match_element), t, self)
        default_mail_notification_event_handler.receive_event(
            self.test % self.__class__.__name__, 'New value for pathes %s: %s' % (
                'match/s3', repr(match_element.match_object)), [log_atom.raw_data], None, log_atom, self)

        t = 0
        default_mail_notification_event_handler.do_timer(t)
        # skipcq: PYL-W1510, BAN-B602
        result = subprocess.run(self.mail_call, shell=True, stdout=subprocess.PIPE)

        self.assertFalse(self.__expected_string % (
            match_element.get_path(), repr(match_element.get_match_object()), self.__class__.__name__, description, 1,
            match_element.get_match_string().decode() + "\n\n") in str(result.stdout, 'utf-8'))

        t = time()
        default_mail_notification_event_handler.next_alert_time = t + 500
        default_mail_notification_event_handler.do_timer(t)

        # skipcq: PYL-W1510, BAN-B602
        result = subprocess.run(self.mail_call, shell=True, stdout=subprocess.PIPE)
        self.assertFalse(self.__expected_string % (
            match_element.get_path(), repr(match_element.get_match_object()), self.__class__.__name__, description, 1,
            match_element.get_match_string().decode() + "\n\n") in str(result.stdout, 'utf-8'))

        default_mail_notification_event_handler.next_alert_time = t
        default_mail_notification_event_handler.do_timer(t)

        sleep(2)
        # skipcq: PYL-W1510, BAN-B602
        result = subprocess.run(self.mail_call, shell=True, stdout=subprocess.PIPE)
        # skipcq: PYL-W1510, BAN-B602
        subprocess.run(self.mail_delete_call, shell=True, stdout=subprocess.PIPE)

        if datetime.fromtimestamp(t).strftime(self.datetime_format_string) not in str(result.stdout, 'utf-8'):
            print("ERROR: %s t not found in mail!" % description, file=sys.stderr)

        self.assertTrue(self.__expected_string % (
            match_element.get_path(), repr(match_element.get_match_object()), self.__class__.__name__, description, 1,
            match_element.get_match_string().decode() + "\n\n") in str(result.stdout, 'utf-8'), msg="%s vs \n %s" % (
            self.__expected_string % (
                match_element.get_path(), repr(match_element.get_match_object()), self.__class__.__name__, description, 1,
                match_element.get_match_string().decode() + "\n\n"), str(result.stdout, 'utf-8')))
Exemplo n.º 2
0
    def test1log_multiple_lines_event(self):
        """
        In this test case multiple lines should be received, before sending an email to root@localhost.
        Make sure no mail notifications are in /var/spool/mail/root, before running this test. This test case must wait some time to
        ensure, that the mail can be read.
        """
        description = "Test1DefaultMailNotificationEventHandler"
        match_context = MatchContext(self.pid)
        fixed_dme = FixedDataModelElement('s1', self.pid)
        match_element = fixed_dme.get_match_element("match", match_context)

        match_context = MatchContext(self.pid)
        fixed_dme2 = FixedDataModelElement('s2', self.pid)
        match_element2 = fixed_dme2.get_match_element("match", match_context)

        default_mail_notification_event_handler = DefaultMailNotificationEventHandler(self.analysis_context)
        self.analysis_context.register_component(self, description)

        t = time()
        log_atom = LogAtom(fixed_dme.fixed_data, ParserMatch(match_element), t, self)
        default_mail_notification_event_handler.receive_event(
            self.test % self.__class__.__name__, 'New value for pathes %s, %s: %s' % (
                'match/s1', 'match/s2', repr(match_element.match_object)), [log_atom.raw_data, log_atom.raw_data], None, log_atom, self)

        t += 600
        log_atom = LogAtom(fixed_dme.fixed_data, ParserMatch(match_element), t, self)
        # set the next_alert_time instead of sleeping 10 seconds
        default_mail_notification_event_handler.next_alert_time = time()
        default_mail_notification_event_handler.receive_event(
            self.test % self.__class__.__name__, 'New value for pathes %s, %s: %s' % (
                'match/s1', 'match/s2', repr(match_element.match_object)), [log_atom.raw_data, log_atom.raw_data], None, log_atom, self)
        sleep(2)
        # skipcq: PYL-W1510, BAN-B602
        result = subprocess.run(self.mail_call, shell=True, stdout=subprocess.PIPE)
        # skipcq: PYL-W1510, BAN-B602
        subprocess.run(self.mail_delete_call, shell=True, stdout=subprocess.PIPE)

        if datetime.fromtimestamp(t - 600).strftime(self.datetime_format_string) not in str(result.stdout, 'utf-8'):
            print("ERROR: %s t-600 not found in mail!" % description, file=sys.stderr)
        if datetime.fromtimestamp(t).strftime(self.datetime_format_string) not in str(result.stdout, 'utf-8'):
            print("ERROR: %s t not found in mail!" % description, file=sys.stderr)

        self.assertTrue(self.__expected_string % (
            "" + match_element.get_path() + ", " + match_element2.get_path(), repr(match_element.get_match_object()),
            self.__class__.__name__, description, 2, match_element.get_match_string().decode() + "\n  " +
            match_element2.get_match_string().decode()) in
            str(result.stdout, 'utf-8'), msg="%s vs \n %s" % (self.__expected_string % (
                match_element.get_path(), repr(match_element.get_match_object()), self.__class__.__name__, description, 1,
                match_element.get_match_string().decode() + "\n\n"), str(result.stdout, 'utf-8')))

        self.assertTrue(self.__expected_string % (
            "" + match_element.get_path() + ", " + match_element2.get_path(), repr(match_element.get_match_object()),
            self.__class__.__name__, description, 2, match_element.get_match_string().decode() + "\n  " +
            match_element2.get_match_string().decode() + "\n\n") in str(result.stdout, 'utf-8'))