This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
logtracker/tests/conftest.py

49 lines
1.3 KiB
Python

import json
import os
import pytest
import psycopg2
from django.db import connection
from django.contrib.auth.models import User
from logtracker.agent import journald, exim
basepath = os.path.dirname(__file__)
def run_sql(sql):
conn = psycopg2.connect(database='postgres')
cur = conn.cursor()
cur.execute(sql)
conn.close()
@pytest.fixture
def journald_data(request, db, client):
with open('%s/testdata.journald' % basepath) as fh:
for line in fh.readlines():
parsed = journald.parse_line(line)
write(parsed)
def write(entry):
with connection.cursor() as c:
entry['data'] = json.dumps(entry['data'])
c.execute('''insert into collection_entry (host, service, priority, timestamp, data) values (%s, %s, %s, %s, %s)''',
(entry['host'], entry['service'], entry['priority'], entry['timestamp'], entry['data']))
@pytest.fixture
def exim_data(request, db, client):
with open('%s/testdata.exim' % basepath) as fh:
for line in fh.readlines():
parsed = exim.parse_line(line)
if parsed:
write(parsed)
@pytest.fixture
def auth(db, client):
User.objects.create_user(username='john', password='Doe')
client.login(username='john', password='Doe')