Commit bf69ab0e authored by godog's avatar godog

fix tests

parent 7fb2037b
......@@ -5,9 +5,9 @@ from lens2 import utils
LOGS = [
"2010-03-13T07:28:45 sleppa daemon.info ntpd[5867]: ntpd 4.2.4p4@1.1520-o Fri Dec 4 18:18:33 UTC 2009 (1)",
"2010-03-13T07:28:45 sleppa daemon.info ntpd[5867]: kernel time sync status 0040",
"2010-03-13T07:28:50 sleppa kernel.info kernel: [ 17.939952] wlan0: no IPv6 routers present",
"Mar 13 07:28:45 sleppa daemon.info ntpd[5867]: ntpd 4.2.4p4@1.1520-o Fri Dec 4 18:18:33 UTC 2009 (1)",
"Mar 13 07:28:45 sleppa daemon.info ntpd[5867]: kernel time sync status 0040",
"Mar 13 07:28:50 sleppa kernel.info kernel: [ 17.939952] wlan0: no IPv6 routers present",
]
......@@ -16,14 +16,14 @@ class SyslogParserTest(unittest.TestCase):
def test_syslog_parser(self):
parser = syslog_parser.SyslogParser()
# parse the logs, discard key information
out = [parser.parse(x) for x in LOGS]
out = [parser(x) for x in LOGS]
# check host
for entry in out:
self.assertEquals("sleppa", entry["host"])
# check timestamps
self.assertEquals(datetime(2010, 3, 13, 7, 28, 45, 0, utils.UTC()),
self.assertEquals(utils._to_timestamp(datetime(2010, 3, 13, 7, 28, 45, 0, utils.UTC())),
out[0]["timestamp"])
self.assertEquals(datetime(2010, 3, 13, 7, 28, 50, 0, utils.UTC()),
self.assertEquals(utils._to_timestamp(datetime(2010, 3, 13, 7, 28, 50, 0, utils.UTC())),
out[2]["timestamp"])
# check facility
self.assertEquals("daemon", out[0]["facility"])
......
......@@ -33,14 +33,14 @@ class UtilsTest(unittest.TestCase):
utils._now = old_now
def test_format_log(self):
log = {'timestamp': datetime(2010, 12, 13, 1, 2, 3, 0, utils.UTC()),
log = {'timestamp': utils._to_timestamp(datetime(2010, 12, 13, 1, 2, 3, 0, utils.UTC())),
'host': 'server', 'program': 'sshd', 'pid': 1234,
'facility': 'auth', 'severity': 'info',
'msg': 'Something'}
expected = '2010-12-13T01:02:03+00:00 server auth.info sshd[1234]: Something\n'
expected = '2010-12-13T01:02:03 server auth.info sshd[1234]: Something\n'
self.assertEquals(expected,
utils.format_log(log))
del log['pid']
expected = '2010-12-13T01:02:03+00:00 server auth.info sshd: Something\n'
expected = '2010-12-13T01:02:03 server auth.info sshd: Something\n'
self.assertEquals(expected,
utils.format_log(log))
......@@ -15,7 +15,12 @@ _utc = UTC()
# Make this a local function to simplify testing.
def _now():
return datetime.now()
def _to_timestamp(d):
from_epoch = (d - datetime(1970, 1,1, tzinfo=UTC())).total_seconds()
return from_epoch
def parse_iso8601_date(s):
return datetime.strptime(s[:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=_utc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment