Пример #1
0
    def test_formatter_filters(self):
        incl_entries = [
            {
                'TEST': 'yes',
                'MESSAGE': 'message',
            },
            {
                'TEST': 'yes',
                'MESSAGE': 'message',
                'OUTPUT': None,  # Test returning None from format()
            },
        ]
        excl_entries = [
            {
                'TEST': 'no',
                'MESSAGE': 'message',
            },
            {
                'TEST': 'yes',
                'MESSAGE': 'ignore',
            },
        ]
        (flexmock(journal.Reader, add_match=None, add_disjunction=None)
            .should_receive('get_next')
            .and_return(incl_entries[0])
            .and_return(incl_entries[1])
            .and_return(excl_entries[0])
            .and_return(excl_entries[1])
            .and_return({}))

        formatter = MySpecialFormatter()
        jfilter = JournalFilter(journal.Reader(), [formatter])
        jfilter.format(StringIO())
        assert formatter.entries_received == incl_entries
Пример #2
0
    def test_exclusion_regexp(self):
        entries = [{'MESSAGE': 'exclude this'},
                   {'MESSAGE': 'message 1'},
                   {'MESSAGE': 'exclude that'},
                   {'MESSAGE': 'message 2'}]

        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return(entries[0])
            .and_return(entries[1])
            .and_return(entries[2])
            .and_return(entries[3])
            .and_return({}))
        exclusions = [{'MESSAGE': ['/1/']},  # shouldn't exclude anything
                      {'MESSAGE': ['/exclude th/']},
                      {'MESSAGE': ['/exclude/']}]
        formatter = EntryFormatter()
        jfilter = JournalFilter(journal.Reader(), [formatter],
                                default_exclusions=exclusions)
        output = StringIO()
        jfilter.format(output)
        output.seek(0)
        lines = output.read().splitlines()
        assert lines == [entry['MESSAGE']
                         for entry in [entries[1]] + [entries[3]]]
        stats = jfilter.get_statistics()
        for stat in stats:
            if stat.exclusion['MESSAGE'] == ['/1/']:
                assert stat.hits == 0
                break
Пример #3
0
    def test_statistics(self):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'MESSAGE': 'exclude'})
            .and_return({'MESSAGE': 'include'})
            .and_return({}))

        exclusions = [{'MESSAGE': ['exclude']}]
        jfilter = JournalFilter(journal.Reader(), exclusions=exclusions)
        list(jfilter)
        statistics = jfilter.get_statistics()
        assert len(statistics) == 1
        assert statistics[0].hits == 1
Пример #4
0
    def test_statistics(self):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'MESSAGE': 'exclude'})
            .and_return({'MESSAGE': 'include'})
            .and_return({}))

        exclusions = [{'MESSAGE': ['exclude']}]
        formatter = EntryFormatter()
        jfilter = JournalFilter(journal.Reader(), [formatter],
                                default_exclusions=exclusions)
        output = StringIO()
        jfilter.format(output)
        statistics = jfilter.get_statistics()
        assert len(statistics) == 1
        assert statistics[0].hits == 1
Пример #5
0
    def test_no_exclusions(self):
        entries = [{'MESSAGE': 'message 1'},
                   {'MESSAGE': 'message 2'}]
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return(entries[0])
            .and_return(entries[1])
            .and_return({}))

        formatter = EntryFormatter()
        jfilter = JournalFilter(journal.Reader(), [formatter])
        output = StringIO()
        jfilter.format(output)
        output.seek(0)
        lines = output.read().splitlines()
        assert lines == [entry['MESSAGE'] for entry in entries]
Пример #6
0
    def run(self):
        if self.handle_options():
            return

        setlocale(LC_ALL, '')
        formatters = self.get_formatters()

        if any(formatter.FILTER_INCLUSIONS is None
               for formatter in formatters):
            default_inclusions = self.config.get('inclusions')
        else:
            # None of our formatters need the inclusions from config
            default_inclusions = None

        if default_inclusions:
            inclusions = default_inclusions[:]
        else:
            inclusions = []

        explicit_inclusions = []
        for formatter in formatters:
            if formatter.FILTER_INCLUSIONS is not None:
                explicit_inclusions.extend(formatter.FILTER_INCLUSIONS)

        reader = SelectiveReader(this_boot=self.args.b,
                                 log_level=self.log_level,
                                 inclusions=inclusions,
                                 explicit_inclusions=explicit_inclusions)
        with LatestJournalEntries(cursor_file=self.cursor_file,
                                  reader=reader,
                                  dry_run=self.args.dry_run,
                                  seek_cursor=not self.args.b) as entries:
            exclusions = self.config.get('exclusions', [])
            jfilter = JournalFilter(entries, formatters,
                                    default_inclusions=default_inclusions,
                                    default_exclusions=exclusions)
            if self.args.cmd == 'stats':
                self.show_stats(jfilter)
            else:
                jfilter.format(sys.stdout)
Пример #7
0
    def test_exclusion_regexp(self):
        entries = [{'MESSAGE': 'exclude this'},
                   {'MESSAGE': 'message 1'},
                   {'MESSAGE': 'exclude that'},
                   {'MESSAGE': 'message 2'}]

        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return(entries[0])
            .and_return(entries[1])
            .and_return(entries[2])
            .and_return(entries[3])
            .and_return({}))
        exclusions = [{'MESSAGE': ['/1/']},  # shouldn't exclude anything
                      {'MESSAGE': ['/exclude th/']},
                      {'MESSAGE': ['/exclude/']}]
        jfilter = JournalFilter(journal.Reader(), exclusions=exclusions)
        assert list(jfilter) == [entries[1]] + [entries[3]]
        stats = jfilter.get_statistics()
        for stat in stats:
            if stat.exclusion['MESSAGE'] == ['/1/']:
                assert stat.hits == 0
                break
Пример #8
0
    def test_exclusion(self):
        priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str)
        entries = [{'MESSAGE': 'exclude this',
                    'SYSLOG_IDENTIFIER': 'from here'},

                   {'PRIORITY': priority_type(6),
                    'MESSAGE': 'exclude this too'},

                   {'MESSAGE': 'message 1',
                    'SYSLOG_IDENTIFIER': 'foo'},

                   {'MESSAGE': 'exclude this',
                    'SYSLOG_IDENTIFIER': 'at your peril'},

                   {'MESSAGE': 'message 2'}]

        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return(entries[0])
            .and_return(entries[1])
            .and_return(entries[2])
            .and_return(entries[3])
            .and_return(entries[4])
            .and_return({}))
        exclusions = [{'MESSAGE': ['exclude this',
                                   'and this'],
                       'SYSLOG_IDENTIFIER': ['from here']},
                      {'PRIORITY': ['info']}]
        formatter = EntryFormatter()
        jfilter = JournalFilter(journal.Reader(), [formatter],
                                default_exclusions=exclusions)
        output = StringIO()
        jfilter.format(output)
        output.seek(0)
        lines = output.read().splitlines()
        assert lines == [entry['MESSAGE'] for entry in entries[2:]]