示例#1
0
    def test_last_info_message_will_be_in_status(self):
        filename = '/tmp/jabber_test/basic.log'

        log = Log(filename, error_timeout=120)

        self.assertTrue(not log.status)

        self.set_date('2011-09-21 01:05:10')
        open(filename, 'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                                  '2011-09-21 01:00:02,854 - basic - ERROR - Line 02\n' +
                                  '2011-09-21 01:00:03,854 - basic - INFO - Line 03\n' +
                                  '2011-09-21 01:00:04,854 - basic - ERROR - Line 04\n' +
                                  '2011-09-21 01:00:05,854 - basic - INFO - Line 05\n'
                                  )

        log.flush()
        self.assertTrue(log.status)
        self.assertEquals(log.status.time, '2011-09-21 01:00:05')
        self.assertEquals(log.status.message, 'Line 05')

        self.set_date('2011-09-21 01:05:10')
        open(filename, 'w').write('2011-09-21 01:05:16,854 - basic - INFO - Line 06\n' +
                                  '2011-09-21 01:05:17,854 - basic - INFO - Line 07\n'
                                  )

        log.flush()
        self.assertTrue(log.status)
        self.assertEquals(log.status.time, '2011-09-21 01:05:17')
        self.assertEquals(log.status.message, 'Line 07')
示例#2
0
    def test_error_messages_will_set_error_status(self):
        filename = '/tmp/jabber_test/basic.log'

        log = Log(filename)

        open(filename, 'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n')
        self.set_date('2011-09-21 01:00:02')

        log.flush()

        self.assertTrue(not log.error)
        
        open(filename, 'a').write('2011-09-21 01:00:02,854 - basic - ERROR - Some error message\n')
        self.set_date('2011-09-21 01:00:03')

        log.flush()
        self.assertTrue(log.error)
        self.assertEquals(log.error.time, '2011-09-21 01:00:02')
        self.assertEquals(log.error.message, 'Some error message')
        
        open(filename, 'a').write('2011-09-21 01:00:03,854 - basic - ERROR - Error message #2\n' +
                                  '2011-09-21 01:00:04,854 - basic - ERROR - Error message #3\n'
                                  )
        self.set_date('2011-09-21 01:00:04')

        log.flush()
        self.assertTrue(log.error)
        self.assertEquals(log.error.time, '2011-09-21 01:00:04')
        self.assertEquals(log.error.message, 'Error message #3')
示例#3
0
    def test_log_will_be_reopened_if_rotated(self):
        filename = '/tmp/jabber_test/basic.log'

        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n')
        
        open(filename, 'w').write(content)
        
        self.assertEquals(log.flush(), content.strip())

        content = ('2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n')

        # File is rewritten, not appended
        open(filename, 'w').write(content)
        
        self.assertEquals(log.flush(), content.strip())
示例#4
0
    def test_rewind_will_work_with_non_existing_log(self):
        filename = '/tmp/jabber_test/empty.log'
        assert not os.path.exists(filename)

        log = Log(filename)

        self.assertEquals(log.flush(), '')
        self.assertEquals(log.flush(), '')

        open(filename, 'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n')

        self.assertEquals(log.flush(),
                          '2011-09-21 01:00:01,854 - basic - INFO - Line 01')
        
        open(filename, 'a').write('2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                                  '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n')

        self.assertEquals(log.flush(),
                          '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                          '2011-09-21 03:00:01,854 - basic - INFO - Line 03')
示例#5
0
    def test_rewind_will_work_with_non_existing_log(self):
        filename = '/tmp/jabber_test/empty.log'
        assert not os.path.exists(filename)

        log = Log(filename)

        self.assertEquals(log.flush(), '')
        self.assertEquals(log.flush(), '')

        open(filename,
             'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n')

        self.assertEquals(log.flush(),
                          '2011-09-21 01:00:01,854 - basic - INFO - Line 01')

        open(filename,
             'a').write('2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                        '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n')

        self.assertEquals(
            log.flush(), '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
            '2011-09-21 03:00:01,854 - basic - INFO - Line 03')
示例#6
0
    def test_old_error_messages_will_be_ignored_on_startup(self):
        filename = '/tmp/jabber_test/basic.log'

        self.set_date('2011-09-21 01:05:10')
        open(filename,
             'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                        '2011-09-21 01:00:02,854 - basic - ERROR - Line 02\n' +
                        '2011-09-21 01:00:03,854 - basic - INFO - Line 03\n' +
                        '2011-09-21 01:00:04,854 - basic - ERROR - Line 04\n' +
                        '2011-09-21 01:00:05,854 - basic - INFO - Line 05\n')

        log = Log(filename, error_timeout=120)

        self.assertTrue(not log.error)
示例#7
0
    def test_old_errors_will_not_be_shown_on_initialization(self):
        self.set_date('2011-09-21 01:00:03')

        filename1 = '/tmp/jabber_test/first.log'
        log1 = Log('first')
        filename2 = '/tmp/jabber_test/secnd.log'
        log2 = Log('secnd')

        open(filename1,
             'w').write('2011-09-21 00:30:01,854 - first - ERROR - Error 01\n')
        open(filename2,
             'w').write('2011-09-21 00:30:02,854 - secnd - INFO - Line 02\n')

        bot = Bot('user@server', 'pass')
        bot.logs['first'] = Log('/tmp/jabber_test/first.log', error_timeout=60)
        bot.logs['secnd'] = Log('/tmp/jabber_test/secnd.log', error_timeout=60)
        bot.status = lambda: ('', 'Ok')
        bot.client = fudge.Fake('client').is_a_stub()

        # Status should be 'Ok'
        bot.cycle()
        self.assertEquals(bot.status_msg, '2011-09-21 01:00:03 Ok')
        self.assertEquals(bot.status_show, '')
示例#8
0
    def test_recent_error_messages_will_set_error_status_on_startup(self):
        filename = '/tmp/jabber_test/basic.log'

        self.set_date('2011-09-21 01:00:10')
        open(filename,
             'w').write('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                        '2011-09-21 01:00:02,854 - basic - ERROR - Line 02\n' +
                        '2011-09-21 01:00:03,854 - basic - INFO - Line 03\n' +
                        '2011-09-21 01:00:04,854 - basic - ERROR - Line 04\n' +
                        '2011-09-21 01:00:05,854 - basic - INFO - Line 05\n')

        log = Log(filename, error_timeout=120)

        self.assertTrue(log.error)
        self.assertEquals(log.error.time, '2011-09-21 01:00:04')
        self.assertEquals(log.error.message, 'Line 04')
示例#9
0
    def test_error_status_will_expire(self):
        filename = '/tmp/jabber_test/basic.log'

        self.set_date('2011-09-21 01:00:03')
        open(filename, 'w').write(
            '2011-09-21 01:00:02,854 - basic - ERROR - Some error message\n')

        log = Log(filename, error_timeout=120)

        self.assertTrue(log.error)
        self.assertEquals(log.error.time, '2011-09-21 01:00:02')
        self.assertEquals(log.error.message, 'Some error message')

        self.set_date('2011-09-21 01:04:03')

        self.assertTrue(not log.error)
示例#10
0
    def test_error_will_override_status(self):
        bot = Bot('user@server', 'pass')
        bot.logs['first'] = Log('/tmp/jabber_test/first.log')
        bot.logs['secnd'] = Log('/tmp/jabber_test/secnd.log')
        bot.status = lambda: ('', 'Ok')
        bot.client = fudge.Fake('client').is_a_stub()

        filename1 = '/tmp/jabber_test/first.log'
        log1 = Log('first')
        filename2 = '/tmp/jabber_test/secnd.log'
        log2 = Log('secnd')

        self.set_date('2011-09-21 01:00:03')

        open(filename1,
             'w').write('2011-09-21 01:00:01,854 - first - INFO - Line 01\n')
        open(filename2,
             'w').write('2011-09-21 01:00:02,854 - secnd - INFO - Line 02\n')

        # Status should be 'Ok'
        bot.cycle()
        self.assertEquals(bot.status_msg, '2011-09-21 01:00:03 Ok')
        self.assertEquals(bot.status_show, '')

        # One info should not interfere with status
        self.set_date('2011-09-21 01:00:04')
        open(filename1,
             'a').write('2011-09-21 01:00:03,854 - first - INFO - Line 03\n')
        bot.cycle()
        self.assertEquals(bot.status_msg, '2011-09-21 01:00:03 Ok')
        self.assertEquals(bot.status_show, '')

        # Error should change status
        self.set_date('2011-09-21 01:00:05')
        open(filename1,
             'a').write('2011-09-21 01:00:04,854 - first - ERROR - Error 01\n')
        bot.cycle()
        self.assertEquals(bot.status_msg,
                          '2011-09-21 01:00:04 first: Error 01')
        self.assertEquals(bot.status_show, 'dnd')

        # Status update won't change bot status, because error is more important
        self.set_date('2011-09-21 01:00:06')
        bot.status = lambda: ('', 'New status')
        bot.cycle()
        self.assertEquals(bot.status_msg,
                          '2011-09-21 01:00:04 first: Error 01')
        self.assertEquals(bot.status_show, 'dnd')

        # New error will override previous one
        self.set_date('2011-09-21 01:00:07')
        open(filename2,
             'a').write('2011-09-21 01:00:06,854 - secnd - ERROR - Error 02\n')
        bot.cycle()
        self.assertEquals(bot.status_msg,
                          '2011-09-21 01:00:06 secnd: Error 02')
        self.assertEquals(bot.status_show, 'dnd')

        # New error of same log will override previous one
        self.set_date('2011-09-21 01:00:08')
        open(filename2,
             'a').write('2011-09-21 01:00:07,854 - secnd - ERROR - Error 03\n')
        bot.cycle()
        self.assertEquals(bot.status_msg,
                          '2011-09-21 01:00:07 secnd: Error 03')
        self.assertEquals(bot.status_show, 'dnd')
示例#11
0
    def test_rewind_by_lines_tolerates_multi_line_logs(self):
        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   'this is a second line of same log line\n' +
                   'there is even a third line\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n')
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(1)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(2)
        self.assertEquals(
            log.flush(), '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(3)
        self.assertEquals(
            log.flush(), '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
            'this is a second line of same log line\n' +
            'there is even a third line\n' +
            '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        open(filename,
             'a').write('2011-09-21 11:00:01,854 - basic - INFO - Line 11\n')

        log.rewind(4)
        self.assertEquals(
            log.flush(), '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
            'this is a second line of same log line\n' +
            'there is even a third line\n' +
            '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n' +
            '2011-09-21 11:00:01,854 - basic - INFO - Line 11')
示例#12
0
    def test_rewind_by_lines_tolerates_multi_line_logs(self):
        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   'this is a second line of same log line\n' +
                   'there is even a third line\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n'
                   )
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(1)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')
        
        log.rewind(2)
        self.assertEquals(log.flush(),
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')
        
        log.rewind(3)
        self.assertEquals(log.flush(),
                          '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                          'this is a second line of same log line\n' +
                          'there is even a third line\n' +
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        open(filename, 'a').write('2011-09-21 11:00:01,854 - basic - INFO - Line 11\n')

        log.rewind(4)
        self.assertEquals(log.flush(),
                          '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                          'this is a second line of same log line\n' +
                          'there is even a third line\n' +
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n' +
                          '2011-09-21 11:00:01,854 - basic - INFO - Line 11')
示例#13
0
    def test_rewind_by_time_tolerates_multi_line_logs(self):
        self.set_date('2011-09-21 10:00:05')

        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   'this is a second line of same log line\n' +
                   'there is even a third line\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n')
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=5)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3605)
        self.assertEquals(
            log.flush(), '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=7205)
        self.assertEquals(
            log.flush(), '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
            'this is a second line of same log line\n' +
            'there is even a third line\n' +
            '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        log.rewind(dtime=1)  #this will override previous rewind
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=3600 * 24)
        self.assertEquals(log.flush(), content.strip())
示例#14
0
    def test_log_can_be_rewinded_by_time_shift(self):
        self.set_date('2011-09-21 10:00:05')

        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n')
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=5)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3605)
        self.assertEquals(
            log.flush(), '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        log.rewind(dtime=1)  #this will override previous rewind
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=3600 * 24)
        self.assertEquals(log.flush(), content.strip())
示例#15
0
    def test_presence_is_only_sent_when_necessary(self):
        self.count = 0

        def message(*args):
            self.count += 1

        bot = Bot('user@server', 'pass', presence_heartbeat=50)
        bot.logs['first'] = Log('/tmp/jabber_test/first.log', error_timeout=30)
        bot.status = lambda: ('', 'Ok')
        bot.client = fudge.Fake('client').is_a_stub()
        bot.client.provides('send').calls(message)

        filename1 = '/tmp/jabber_test/first.log'

        self.set_date('2011-09-21 01:00:03')

        open(filename1,
             'w').write('2011-09-21 01:00:01,854 - first - INFO - Line 01\n')

        # Status is 'Ok', only first presence will be sent
        bot.cycle()
        self.assertEquals(self.count, 1)
        bot.cycle()
        self.assertEquals(self.count, 1)

        # Status changes, one presence will be sent
        self.set_date('2011-09-21 01:00:03')
        bot.status = lambda: ('', 'New status')
        bot.cycle()
        self.assertEquals(self.count, 2)
        bot.cycle()
        self.assertEquals(self.count, 2)

        # Error will cause new presence broadcast
        self.set_date('2011-09-21 01:00:05')
        open(filename1,
             'a').write('2011-09-21 01:00:04,854 - first - ERROR - Error 01\n')
        bot.cycle()
        self.assertEquals(self.count, 3)

        # Several seconds passes, no presence broadcast
        self.set_date('2011-09-21 01:00:06')
        bot.cycle()
        self.assertEquals(self.count, 3)
        self.set_date('2011-09-21 01:00:07')
        bot.cycle()
        self.assertEquals(self.count, 3)
        self.set_date('2011-09-21 01:00:08')
        bot.cycle()
        self.assertEquals(self.count, 3)
        self.set_date('2011-09-21 01:00:09')
        bot.cycle()
        self.assertEquals(self.count, 3)
        self.set_date('2011-09-21 01:00:10')
        bot.cycle()
        self.assertEquals(self.count, 3)

        # Error expires, one broadcast
        self.set_date('2011-09-21 01:00:35')
        bot.cycle()
        self.assertEquals(self.count, 4)

        # 40 seconds passes, no broadcast
        self.set_date('2011-09-21 01:01:15')
        bot.cycle()
        self.assertEquals(self.count, 4)

        # 11 more seconds and presence heartbeat forces presence broadcast
        self.set_date('2011-09-21 01:01:26')
        bot.cycle()
        self.assertEquals(self.count, 5)
示例#16
0
    def test_rewind_by_time_tolerates_multi_line_logs(self):
        self.set_date('2011-09-21 10:00:05')

        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   'this is a second line of same log line\n' +
                   'there is even a third line\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n'
                   )
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=5)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3605)
        self.assertEquals(log.flush(),
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=7205)
        self.assertEquals(log.flush(),
                          '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                          'this is a second line of same log line\n' +
                          'there is even a third line\n' +
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        log.rewind(dtime=1) #this will override previous rewind
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=3600 * 24)
        self.assertEquals(log.flush(), content.strip())
示例#17
0
    def test_log_can_be_rewinded_by_line_number(self):
        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n'
                   )
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(1)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')
        
        log.rewind(2)
        self.assertEquals(log.flush(),
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')
        
        log.rewind(3)
        self.assertEquals(log.flush(),
                          '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        open(filename, 'a').write('2011-09-21 11:00:01,854 - basic - INFO - Line 11\n')

        log.rewind(4)
        self.assertEquals(log.flush(),
                          '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n' +
                          '2011-09-21 11:00:01,854 - basic - INFO - Line 11')
示例#18
0
    def test_log_can_be_rewinded_by_line_number(self):
        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n')
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(1)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(2)
        self.assertEquals(
            log.flush(), '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(3)
        self.assertEquals(
            log.flush(), '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
            '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        open(filename,
             'a').write('2011-09-21 11:00:01,854 - basic - INFO - Line 11\n')

        log.rewind(4)
        self.assertEquals(
            log.flush(), '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
            '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
            '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n' +
            '2011-09-21 11:00:01,854 - basic - INFO - Line 11')
示例#19
0
    def test_log_can_be_rewinded_by_time_shift(self):
        self.set_date('2011-09-21 10:00:05')

        filename = '/tmp/jabber_test/basic.log'
        log = Log(filename)

        content = ('2011-09-21 01:00:01,854 - basic - INFO - Line 01\n' +
                   '2011-09-21 02:00:01,854 - basic - INFO - Line 02\n' +
                   '2011-09-21 03:00:01,854 - basic - INFO - Line 03\n' +
                   '2011-09-21 04:00:01,854 - basic - INFO - Line 04\n' +
                   '2011-09-21 05:00:01,854 - basic - INFO - Line 05\n' +
                   '2011-09-21 06:00:01,854 - basic - INFO - Line 06\n' +
                   '2011-09-21 07:00:01,854 - basic - INFO - Line 07\n' +
                   '2011-09-21 08:00:01,854 - basic - INFO - Line 08\n' +
                   '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                   '2011-09-21 10:00:01,854 - basic - INFO - Line 10\n'
                   )
        open(filename, 'w').write(content)

        log.flush()
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=5)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        self.assertEquals(log.flush(),
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3605)
        self.assertEquals(log.flush(),
                          '2011-09-21 09:00:01,854 - basic - INFO - Line 09\n' +
                          '2011-09-21 10:00:01,854 - basic - INFO - Line 10')

        log.rewind(dtime=3600)
        log.rewind(dtime=1) #this will override previous rewind
        self.assertEquals(log.flush(), '')

        log.rewind(dtime=3600 * 24)
        self.assertEquals(log.flush(), content.strip())