示例#1
0
  def test_store(self):
    """Verify we store objects as JSON snapshots."""
    data = TestData('NAME', 1234, TestDetails())
    decoder = json.JSONDecoder()
    snapshot = JsonSnapshot()
    snapshot.add_object(data)

    time_function = lambda: 1.23
    journal = Journal(time_function)
    output = BytesIO()
    journal.open_with_file(output)
    offset = len(output.getvalue())

    journal.store(data)
    contents = output.getvalue()
    got_stream = RecordInputStream(BytesIO(contents[offset:]))
    got_json_str = next(got_stream)
    got = decoder.decode(got_json_str)
    json_object = snapshot.to_json_object()
    json_object['_timestamp'] = time_function()
    json_object['_thread'] = threading.current_thread().ident
    self.assertItemsEqual(json_object, got)
示例#2
0
    def __call__(self, options):
        """Process command."""
        args = dict(self.get_metadata(options))
        if options.message:
            args['_message'] = options.message

        journal = Journal()
        journal.open_with_path(options.path, **args)
        journal.terminate(_message=None)
示例#3
0
    def test_store(self):
        """Verify we store objects as JSON snapshots."""
        data = TestData('NAME', 1234, TestDetails())
        decoder = json.JSONDecoder(encoding='ASCII')
        snapshot = JsonSnapshot()
        snapshot.add_object(data)

        time_function = lambda: 1.23
        journal = Journal(time_function)
        output = StringIO()
        journal.open_with_file(output)
        offset = len(output.getvalue())

        journal.store(data)
        contents = output.getvalue()
        got_stream = RecordInputStream(StringIO(contents[offset:]))
        got_json_str = got_stream.next()
        got = decoder.decode(got_json_str)
        json_object = snapshot.to_json_object()
        json_object['_timestamp'] = time_function()
        json_object['_thread'] = threading.current_thread().ident
        self.assertItemsEqual(json_object, got)
示例#4
0
import unittest
from threading import current_thread
from io import BytesIO

from citest.base import (
    JournalLogger,
    JournalLogHandler,
    Journal)
from citest.base import RecordInputStream
from citest.base import set_global_journal

from tests.base.test_clock import TestClock

_journal_clock = TestClock()
_journal_file = BytesIO()
_journal = Journal(now_function=_journal_clock)
_journal.open_with_file(_journal_file)
set_global_journal(_journal)


class JournalLoggerTest(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    logging.getLogger().handlers = []

  def test_record(self):
      logger = logging.getLogger('testrecord')
      record = logger.makeRecord('NAME', 1, 'PATH', 2, 'MSG',
                                 'ARGS', 'EXC_INFO', 'FUNC')

  def test_journal_logger(self):
示例#5
0
  def test_iterator(self):
    journal = Journal()
    path = os.path.join(self.temp_dir, 'test_iterator.journal')
    expect = []

    journal.open_with_path(path, TestString='TestValue', TestNum=123)
    expect.append({'_type': 'JournalMessage',
                   '_value': 'Starting journal.',
                   'TestString': 'TestValue',
                   'TestNum': 123})

    journal.write_message('Initial Message')
    expect.append({'_type': 'JournalMessage', '_value': 'Initial Message'})
                   
    journal.begin_context('OUTER', TestProperty='BeginOuter')
    expect.append({'_type': 'JournalContextControl',
                   'control': 'BEGIN',
                   '_title': 'OUTER',
                   'TestProperty': 'BeginOuter'})

    journal.write_message('Context Message', format='pre')
    expect.append({'_type': 'JournalMessage', '_value': 'Context Message',
                   'format': 'pre'})
    
    journal.end_context(TestProperty='END OUTER')
    expect.append({'_type': 'JournalContextControl',
                   'control': 'END',
                   'TestProperty': 'END OUTER'})

    journal.terminate(EndProperty='xyz')
    expect.append({'_type': 'JournalMessage',
                   '_value': 'Finished journal.',
                   'EndProperty': 'xyz'})

    # We're going to pop off expect, so reverse it
    # so that we can check in order.
    expect.reverse()
    navigator = StreamJournalNavigator.new_from_path(path)
    for record in navigator:
        del(record['_thread'])
        del(record['_timestamp'])
        self.assertEquals(record, expect.pop())
    self.assertEquals([], expect)
示例#6
0
 def open_journal(self, options):
     """Opens an existing journal for appending."""
     journal = Journal()
     journal.open_with_path(options.path, _append=True, _message=None)
     return journal
示例#7
0
 def __call__(self, options):
     journal = Journal()
     journal.open_with_path(options.path)
     journal.begin_context(options.title or 'Error')
     try:
         journal.write_message(options.message,
                               **self.get_metadata(options))
     finally:
         journal.end_context(relation='ERROR')
         journal.terminate()