def test_get_exclusions(self): reader = [{'MESSAGE': 'message 1', 'MESSAGE1': 'x', 'KEY': 'multiple'}, {'MESSAGE': 'message 1', 'MESSAGE1': 'x', 'KEY': 'multiple'}, {'MESSAGE': 'message 1', 'MESSAGE1': 'x'}, {'MESSAGE': 'message 1', 'MESSAGE1': 'x'}, {'MESSAGE': 'message 2', 'KEY': 'multiple'}, {'MESSAGE': 'message 2', 'KEY': 'single'}] dbr = get_formatter('config') formatted = '' for entry in reader: formatted += dbr.format(entry) formatted += dbr.flush() assert formatted == '\n'.join([ "exclusions:", " # 4 occurrences (out of 6)", " - MESSAGE:", " - message 1", " MESSAGE1:", " - x", " # 2 occurrences (out of 2)", " - MESSAGE:", " - message 2", '' ])
def test_format(self, entry, expected): entry['__REALTIME_TIMESTAMP'] = datetime.fromtimestamp(0, tz=timezone.utc) formatter = get_formatter('short') formatted = formatter.format(entry) date = 'Jan 01 00:00:00 ' assert formatted.startswith(date) assert formatted[len(date):] == expected
def test_bytes(self, bdata, brepr): """ Should decode to unicode or a number array """ entry = {'BDATA': bdata} formatter = get_formatter('json') out = json.loads(formatter.format(entry)) assert out['BDATA'] == brepr
def test_uuid(self): """ Should be string representation of UUID """ entry = {'_BOOT_ID': uuid.uuid1()} formatter = get_formatter('json') out = json.loads(formatter.format(entry)) assert out['_BOOT_ID'] == str(entry['_BOOT_ID'])
def test_timestamp(self): """ Should output microseconds since the epoch """ dt = datetime.fromtimestamp(5, tz=timezone(timedelta(hours=1))) entry = {'__REALTIME_TIMESTAMP': dt} formatter = get_formatter('json') out = json.loads(formatter.format(entry)) assert out['__REALTIME_TIMESTAMP'] == 5000000
def test_timestamp(self): dt = datetime.fromtimestamp(0, tz=timezone(timedelta(hours=1))) entry = {'__REALTIME_TIMESTAMP': dt, 'MESSAGE': 'epoch'} formatter = get_formatter('short') # Should output in local time expected = 'Jan 01 01:00:00' assert expected in formatter.format(entry)
def test_uuid(self): dbr = get_formatter('config') message_id = uuid1() formatted = dbr.format({'MESSAGE_ID': message_id}) formatted += dbr.flush() assert formatted == '\n'.join([ "exclusions:", " # 1 occurrences (out of 1)", " - MESSAGE_ID:", " - {0}".format(str(message_id)), '' ])
def test_timedelta(self): """ Should be in microseconds """ sec = 1 us = 700 mono_ts = timedelta(0, sec, us) entry = {'__MONOTONIC_TIMESTAMP': mono_ts} formatter = get_formatter('json') out = json.loads(formatter.format(entry)) diff = out['__MONOTONIC_TIMESTAMP'] - (sec * 1000000 + us) assert diff < 0.001
def test_monotonic(self): """ Should be in microseconds """ us = 700 elapsed = timedelta(microseconds=us) boot_id = uuid.uuid1() timestamp = journal.Monotonic((elapsed, boot_id)) entry = {'__MONOTONIC_TIMESTAMP': timestamp} formatter = get_formatter('json') out = json.loads(formatter.format(entry)) assert out['__MONOTONIC_TIMESTAMP'] == us
def test_multiline(self): """ Check each entry is formatted as a single output line """ count = 5 output = StringIO() formatter = get_formatter('json') for n in range(count): output.write(formatter.format({'MESSAGE': 'entry'})) output.write(formatter.flush()) output.seek(0) assert len(output.read().splitlines()) == count
def test_login(self): setlocale(LC_ALL, 'en_US.UTF-8') # check locale-aware sorting formatter = get_formatter('login') base = formatter.FILTER_INCLUSIONS[0].copy() base['MESSAGE_ID'] = [UUID(uuid) for uuid in base['MESSAGE_ID']] for user in ['user1', 'user2', 'user1', 'User3']: entry = base.copy() entry['USER_ID'] = user assert formatter.format(entry) == '' assert formatter.flush().splitlines() == [ '', 'User logins:', '', ' 2 x user1', ' 1 x user2', ' 1 x User3', ]
def test_systemd(self): setlocale(LC_ALL, 'en_US.UTF-8') # check locale-aware sorting formatter = get_formatter('systemd') base = formatter.FILTER_INCLUSIONS[0].copy() for unit in ['unit1', 'unit2', 'unit1', 'Unit3']: entry = base.copy() entry.update({ 'MESSAGE': 'Unit %s.service entered failed state.' % unit, 'UNIT': '%s.service' % unit, }) assert formatter.format(entry) == '' assert formatter.flush().splitlines() == [ '', 'Failed systemd units:', '', ' 2 x unit1.service', ' 1 x unit2.service', ' 1 x Unit3.service', ]
def test_no_failed_units(self): formatter = get_formatter('systemd') assert formatter.flush() == ''
def test_no_logins(self): formatter = get_formatter('login') assert formatter.flush() == ''
def test_reboot(self): formatter = get_formatter('reboot') assert formatter.format({'_BOOT_ID': '1'}) == '' assert formatter.format({'_BOOT_ID': '2'}) == '-- Reboot --\n' assert formatter.format({'_BOOT_ID': '2'}) == '' assert formatter.flush() == ''