Beispiel #1
0
    def test_stats(self, capsys):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'__CURSOR': '1',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'exclude'})
            .and_return({'__CURSOR': '2',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'include'})
            .and_return({}))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("""
cursor-file: {cursor}
exclusions:
- MESSAGE: [exclude]
""".format(cursor=cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, 'stats'])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert out == "\n".join([" FREQUENCY  EXCLUSION",
                                 "         1  {'MESSAGE': ['exclude']}",
                                 ""])
Beispiel #2
0
    def test_help_output(self, capsys):
        cli = CLI(args=['--help-output'])
        cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert out
Beispiel #3
0
    def test_formatter_filter(self, capsys):
        """
        Just a coverage test
        """
        entry = {
            '__CURSOR': '1',
            '__REALTIME_TIMESTAMP': datetime.now(),
            'PRIORITY': 6,
            'MESSAGE': 'login session started',
            'MESSAGE_ID': uuid.UUID('8d45620c1a4348dbb17410da57c60c66'),
            '_COMM': 'systemd-logind',
            'USER_ID': 'abc',
        }

        (flexmock(journal.Reader, add_match=None, add_disjunction=None)
            .should_receive('get_next')
            .and_return(entry)
            .and_return({}))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("""
cursor-file: {cursor}
""".format(cursor=cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name,
                                '-p', 'err', '-o', 'login'])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert out
Beispiel #4
0
    def test_multiple_output_formats(self, capsys):
        entry = {
            '__CURSOR': '1',
            '__REALTIME_TIMESTAMP': datetime.now(),
            'MESSAGE': 'message',
        }

        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return(entry)
            .and_return({}))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("""
cursor-file: {cursor}
""".format(cursor=cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name,
                                '-o', 'cat,cat,json'])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        lines = out.splitlines()
        assert len(lines) == 3
        assert lines[0] == lines[1] == 'message'
        output = json.loads(lines[2])
        del entry['__REALTIME_TIMESTAMP']
        del output['__REALTIME_TIMESTAMP']
        assert output == entry
Beispiel #5
0
    def test_inclusions_yaml(self):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({}))
        watcher = Watcher()
        (flexmock(journal.Reader)
            .should_receive('add_match')
            .replace_with(watcher.watch_call('add_match')))
        (flexmock(journal.Reader)
            .should_receive('add_conjunction')
            .replace_with(watcher.watch_call('add_conjunction')))
        (flexmock(journal.Reader)
            .should_receive('add_disjunction')
            .replace_with(watcher.watch_call('add_disjunction')))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("""
cursor-file: {cursor}
inclusions:
- PRIORITY: [0, 1, 2, 3]
- PRIORITY: [4, 5, 6]
  _SYSTEMD_UNIT: [myservice.service]
""".format(cursor=cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name])
                cli.run()

        # Should add matches for all of the first group
        assert set(watcher.calls[:4]) == set([
            ('add_match', (), "{'PRIORITY': '0'}"),
            ('add_match', (), "{'PRIORITY': '1'}"),
            ('add_match', (), "{'PRIORITY': '2'}"),
            ('add_match', (), "{'PRIORITY': '3'}"),
        ])

        # Then a disjunction
        assert watcher.calls[4] == ('add_disjunction', (), '{}')

        # Then matches for all of the second group
        assert set(watcher.calls[5:9]) == set([
            ('add_match', (), "{'PRIORITY': '4'}"),
            ('add_match', (), "{'PRIORITY': '5'}"),
            ('add_match', (), "{'PRIORITY': '6'}"),
            ('add_match', (), "{'_SYSTEMD_UNIT': 'myservice.service'}"),
        ])

        # And a final disjuction
        assert watcher.calls[9] == ('add_disjunction', (), '{}')
Beispiel #6
0
    def test_debrief(self, capsys):
        entries = [
            {'__CURSOR': '1',
             'MESSAGE': 'message 1',
             'KEY': 'multiple',
             '__REALTIME_TIMESTAMP': datetime.now()},
            {'__CURSOR': '2',
             'MESSAGE': 'message 1',
             'KEY': 'multiple',
             '__REALTIME_TIMESTAMP': datetime.now()},
            {'__CURSOR': '3',
             'MESSAGE': 'message 1',
             '__REALTIME_TIMESTAMP': datetime.now()},
            {'__CURSOR': '4',
             'MESSAGE': 'message 1',
             '__REALTIME_TIMESTAMP': datetime.now()},
            {'__CURSOR': '5',
             'MESSAGE': 'message 2',
             'KEY': 'multiple',
             '__REALTIME_TIMESTAMP': datetime.now()},
            {'__CURSOR': '6',
             'MESSAGE': 'message 2',
             'KEY': 'single',
             '__REALTIME_TIMESTAMP': datetime.now()},
        ]
        expectation = (flexmock(journal.Reader).should_receive('get_next'))
        for entry in entries:
            expectation = expectation.and_return(entry)

        expectation.and_return({})

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("cursor-file: {0}".format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, 'debrief'])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert out == "\n".join([
            "exclusions:",
            "  # 4 occurrences (out of 6)",
            "  - MESSAGE:",
            "    - message 1",
            "  # 2 occurrences (out of 2)",
            "  - MESSAGE:",
            "    - message 2",
            ''])
Beispiel #7
0
    def test_log_level(self):
        (flexmock(journal.Reader)
            .should_receive('log_level')
            .with_args(journal.LOG_ERR)
            .once())
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({}))

        with NamedTemporaryFile(mode='wt') as configfile:
            with NamedTemporaryFile(mode='rt') as cursorfile:
                configfile.write('cursor-file: {0}\n'.format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, '-p', 'err'])
                cli.run()
Beispiel #8
0
    def test_dry_run(self):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'__CURSOR': '1',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'message'})
            .and_return({}))

        with NamedTemporaryFile(mode='wt') as configfile:
            with NamedTemporaryFile(mode='rt') as cursorfile:
                configfile.write('cursor-file: {0}\n'.format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, '--dry-run'])
                cli.run()
                assert not cursorfile.read()
Beispiel #9
0
    def test_reset(self):
        with NamedTemporaryFile(mode='wt') as configfile:
            with NamedTemporaryFile(mode='rt') as cursorfile:
                configfile.write('cursor-file: {0}\n'.format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, 'reset'])
                cli.run()
                # Cursor file is deleted
                assert not os.access(cursorfile.name, os.F_OK)
                open(cursorfile.name, mode='w').close()

            # No errors when the cursor file doesn't exist
            cli = CLI(args=['--conf', configfile.name, 'reset'])
            cli.run()
            assert not os.access(cursorfile.name, os.F_OK)
Beispiel #10
0
    def test_debrief_no_input(self, capsys):
        """
        Check it handles there being no input
        """
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({}))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("cursor-file: {0}".format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name, 'debrief'])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert not out
Beispiel #11
0
    def test_normal_run(self, capsys):
        (flexmock(journal.Reader, add_match=None, add_disjunction=None)
            .should_receive('get_next')
            .and_return({'__CURSOR': '1',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'message1'})
            .and_return({'__CURSOR': '2',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'message2'})
            .and_return({}))

        with NamedTemporaryFile(mode='wt') as configfile:
            with NamedTemporaryFile(mode='rt') as cursorfile:
                configfile.write('cursor-file: {0}\n'.format(cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert len(out.splitlines()) == 2
Beispiel #12
0
    def test_this_boot(self):
        final_cursor = '1'
        (flexmock(journal.Reader)
            .should_receive('this_boot')
            .once())
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'__CURSOR': final_cursor,
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'message'})
            .and_return({}))

        with NamedTemporaryFile(mode='wt') as configfile:
            with NamedTemporaryFile(mode='w+t') as cursorfile:
                configfile.write('cursor-file: {0}\n'.format(cursorfile.name))
                configfile.flush()
                cursorfile.write(final_cursor)
                cursorfile.flush()
                cli = CLI(args=['--conf', configfile.name, '-b'])
                cli.run()
                cursorfile.seek(0)
                assert cursorfile.read() == final_cursor
Beispiel #13
0
    def test_exclusions_yaml(self, capsys):
        (flexmock(journal.Reader)
            .should_receive('get_next')
            .and_return({'__CURSOR': '1',
                         '__REALTIME_TIMESTAMP': datetime.now(),
                         'MESSAGE': 'message'})
            .and_return({}))

        with NamedTemporaryFile(mode='rt') as cursorfile:
            with NamedTemporaryFile(mode='wt') as configfile:
                configfile.write("""
cursor-file: {cursor}
exclusions:
- MESSAGE: [1]
""".format(cursor=cursorfile.name))
                configfile.flush()
                cli = CLI(args=['--conf', configfile.name])
                cli.run()

        (out, err) = capsys.readouterr()
        assert not err
        assert 'message' in out