Example #1
0
def test_record_stage1_nok():
    from pgtoolkit.log import Record, UnknownData

    lines = ["pouet\n", "toto\n"]
    with pytest.raises(UnknownData) as ei:
        Record.parse_stage1(lines)
    assert 'pouet\ntoto\n' in str(ei.value)
Example #2
0
def test_record_stage2_ok(mocker):
    from pgtoolkit.log import Record
    record = Record(
        prefix="2018-06-15 10:49:26.088 UTC [8420]: [3-1] app=psql,db=postgres,client=[local],user=postgres ",  # noqa
        severity='LOG', message_lines=['message'], raw_lines=[],
    )

    prefix_parser = mocker.Mock(name='prefix_parser')
    prefix_parser.return_value = dict(remote_host='[local]')
    record.parse_stage2(prefix_parser)
    assert '[local]' == record.remote_host
Example #3
0
def test_main(mocker, caplog, capsys):
    pkg = 'pgtoolkit.log.__main__'
    mocker.patch(pkg + '.logging.basicConfig', autospec=True)
    open_ = mocker.patch(pkg + '.open_or_stdin', autospec=True)
    parse = mocker.patch(pkg + '.parse', autospec=True)

    from datetime import datetime
    from pgtoolkit.log import Record, UnknownData
    from pgtoolkit.log.__main__ import main

    open_.return_value = mocker.MagicMock()
    parse.return_value = [
        Record('prefix', 'LOG', timestamp=datetime.utcnow()),
        UnknownData(['unknown line\n']),
    ]
    log_line_prefix = '%m [%p]: '
    main(argv=[log_line_prefix], environ=dict())
    out, err = capsys.readouterr()
    assert 'LOG' in out

    if isinstance(caplog.records, list):
        records = caplog.records
    else:  # Compat python pytest-capturelog for py26
        records = caplog.records()

    for record in records:
        if 'unknown line' in record.message:
            break
    else:
        assert False, "Bad line not logged"
Example #4
0
def test_main_ko(mocker):
    mocker.patch('pgtoolkit.log.logging.basicConfig', autospec=True)
    open_ = mocker.patch('pgtoolkit.log.open_or_stdin', autospec=True)
    parse = mocker.patch('pgtoolkit.log.parse', autospec=True)

    from pgtoolkit.log import main, Record

    open_.return_value = mocker.MagicMock()
    parse.return_value = [Record('prefix', 'LOG', badentry=object())]
    assert 1 == main(argv=['%m'], environ=dict())
Example #5
0
def test_main_ko(mocker):
    pkg = "pgtoolkit.log.__main__"
    mocker.patch(pkg + ".logging.basicConfig", autospec=True)
    open_ = mocker.patch(pkg + ".open_or_stdin", autospec=True)
    parse = mocker.patch(pkg + ".parse", autospec=True)

    from pgtoolkit.log import Record
    from pgtoolkit.log.__main__ import main

    open_.return_value = mocker.MagicMock()
    parse.return_value = [Record("prefix", "LOG", badentry=object())]
    assert 1 == main(argv=["%m"], environ=dict())
Example #6
0
def test_record_stage1_ok():
    from pgtoolkit.log import Record

    lines = """\
2018-06-15 10:49:26.088 UTC [8420]: [3-1] app=psql,db=postgres,client=[local],user=postgres LOG:  duration: 1.449 ms  statement: SELECT d.datname as "Name",
\t       pg_catalog.array_to_string(d.datacl, E'\\n') AS "Access privileges"
\tFROM pg_catalog.pg_database d
\tORDER BY 1;
""".splitlines(True)  # noqa

    record = Record.parse_stage1(lines)
    assert 'LOG' in repr(record)
    assert 4 == len(record.raw_lines)
    assert 'LOG' == record.severity
    assert 4 == len(record.message_lines)
    assert record.message_lines[0].startswith('duration: ')