def test_draw_entry_with_duplicates(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') entry.duplicates = [1, 2] # only care about the count, not the content rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content) rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [1 duplicate\n hidden]', rendered.content) entry.duplicates = [1, 2, 3, 4, 5, 6] rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [5 duplicates\n hidden]', rendered.content)
def test_draw_entry(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content)
def test_draw_entry_with_duplicates(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') entry.duplicates = [1, 2] # only care about the count, not the content rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content) rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [1 duplicate\n hidden]', rendered.content) entry.duplicates = [1, 2, 3, 4, 5, 6] rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [5 duplicates\n hidden]', rendered.content)
def test_deduplication_matches_identical_messages(self): # Simple case is that we match the same message but different timestamp. entry = LogEntry(1333738434, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"') self.assertTrue(entry.is_duplicate_of(LogEntry(1333738457, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"'))) # ... but we shouldn't match if the runlevel differs. self.assertFalse(entry.is_duplicate_of(LogEntry(1333738457, 'DEBUG', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')))
def test_deduplication(self): group = LogGroup(5) group.add(LogEntry(1333738410, 'NOTICE', 'Bootstrapped 72%: Loading relay descriptors.')) group.add(LogEntry(1333738420, 'NOTICE', 'Bootstrapped 75%: Loading relay descriptors.')) group.add(LogEntry(1333738430, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0007)')) group.add(LogEntry(1333738440, 'NOTICE', 'Bootstrapped 78%: Loading relay descriptors.')) group.add(LogEntry(1333738450, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) self.assertEqual([1333738450, 1333738440, 1333738430, 1333738420, 1333738410], [e.timestamp for e in group]) bootstrap_messages = [ 'Bootstrapped 80%: Loading relay descriptors.', 'Bootstrapped 78%: Loading relay descriptors.', 'Bootstrapped 75%: Loading relay descriptors.', 'Bootstrapped 72%: Loading relay descriptors.', ] group_items = list(group) self.assertEqual(bootstrap_messages, [e.message for e in group_items[0].duplicates]) self.assertEqual([False, True, False, True, True], [e.is_duplicate for e in group_items]) # add another duplicate message that pops the last group.add(LogEntry(1333738460, 'NOTICE', 'Bootstrapped 90%: Loading relay descriptors.')) bootstrap_messages = [ 'Bootstrapped 90%: Loading relay descriptors.', 'Bootstrapped 80%: Loading relay descriptors.', 'Bootstrapped 78%: Loading relay descriptors.', 'Bootstrapped 75%: Loading relay descriptors.', ] group_items = list(group) self.assertEqual(bootstrap_messages, [e.message for e in group_items[0].duplicates]) self.assertEqual([False, True, True, False, True], [e.is_duplicate for e in group_items]) # add another non-duplicate message that pops the last group.add(LogEntry(1333738470, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')) bootstrap_messages = [ 'Bootstrapped 90%: Loading relay descriptors.', 'Bootstrapped 80%: Loading relay descriptors.', 'Bootstrapped 78%: Loading relay descriptors.', ] group_items = list(group) self.assertEqual(None, group_items[0].duplicates) self.assertEqual(bootstrap_messages, [e.message for e in group_items[1].duplicates]) self.assertEqual([False, False, True, True, False], [e.is_duplicate for e in group_items])
def entries(): return [ LogEntry(NOW, 'NYX_WARNING', "Tor's geoip database is unavailable."), LogEntry( NOW, 'NYX_NOTICE', 'No nyx configuration loaded, using defaults. You can customize nyx by placing a configuration file at /home/atagar/.nyx/config (see https://nyx.torproject.org/nyxrc.sample for its options).' ), LogEntry(NOW, 'NOTICE', 'New control connection opened from 127.0.0.1.'), LogEntry(NOW, 'NOTICE', 'Opening OR listener on 0.0.0.0:7000'), LogEntry(NOW, 'NOTICE', 'Opening Control listener on 127.0.0.1:9051'), LogEntry(NOW, 'NOTICE', 'Opening Socks listener on 127.0.0.1:9050'), LogEntry( NOW, 'NOTICE', 'Tor v0.2.9.0-alpha-dev (git-44ea3dc3311564a9) running on Linux with Libevent 2.0.16-stable, OpenSSL 1.0.1 and Zlib 1.2.3.4.' ), LogEntry( NOW, 'NOTICE', 'Tor 0.2.9.0-alpha-dev (git-44ea3dc3311564a9) opening log file.'), ]
def test_deduplication_matches_with_wildcard(self): # matches using a wildcard specified in dedup.cfg entry = LogEntry(1333738434, 'NOTICE', 'Bootstrapped 72%: Loading relay descriptors.') self.assertTrue(entry.is_duplicate_of(LogEntry(1333738457, 'NOTICE', 'Bootstrapped 55%: Loading relay descriptors.')))
def test_deduplication_matches_based_on_prefix(self): # matches using a prefix specified in dedup.cfg entry = LogEntry(1333738434, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0007)') self.assertTrue(entry.is_duplicate_of(LogEntry(1333738457, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0015)')))
def test_maintains_certain_size(self): group = LogGroup(5) self.assertEqual(0, len(group)) group.add(LogEntry(1333738410, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')) self.assertEqual([LogEntry(1333738410, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')], list(group)) self.assertEqual(1, len(group)) group.add(LogEntry(1333738420, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0007)')) group.add(LogEntry(1333738430, 'NOTICE', 'Bootstrapped 72%: Loading relay descriptors.')) group.add(LogEntry(1333738440, 'NOTICE', 'Bootstrapped 75%: Loading relay descriptors.')) group.add(LogEntry(1333738450, 'NOTICE', 'Bootstrapped 78%: Loading relay descriptors.')) self.assertEqual(5, len(group)) # group should now be full, adding more entries pops others off group.add(LogEntry(1333738460, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) self.assertFalse(LogEntry(1333738410, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"') in list(group)) self.assertEqual(5, len(group)) # try adding a bunch that will be deduplicated, and make sure we still maintain the size group.add(LogEntry(1333738510, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738520, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738530, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738540, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738550, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738560, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) group.add(LogEntry(1333738570, 'NOTICE', 'Bootstrapped 80%: Loading relay descriptors.')) self.assertEqual([1333738570, 1333738560, 1333738550, 1333738540, 1333738530], [e.timestamp for e in group]) self.assertEqual(5, len(group))
def test_dedup_key_with_wildcard(self): # matches using a wildcard specified in dedup.cfg entry = LogEntry(1333738434, 'NOTICE', 'Bootstrapped 72%: Loading relay descriptors.') self.assertEqual('NOTICE:*Loading relay descriptors.', entry.dedup_key)
def test_dedup_key_by_prefix(self): # matches using a prefix specified in dedup.cfg entry = LogEntry(1333738434, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0007)') self.assertEqual('NYX_DEBUG:GETCONF MyFamily (', entry.dedup_key)
def test_dedup_key_by_messages(self): entry = LogEntry(1333738434, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"') self.assertEqual('INFO:tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"', entry.dedup_key)
def test_draw_entry_that_wraps(self): entry = LogEntry(NOW, 'NOTICE', 'ho hum%s...' % (', ho hum' * 20)) rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual(EXPECTED_WRAPPED_MSG, rendered.content)