def test_withoutTrialResult(self): """ When not running under I{trial} L{None} is returned. """ self.assertIs( None, _RedirectLogsForTrial(FakeSys(["myprogram.py"], b""), LogPublisher())())
def test_withoutTrialNoDestination(self): """ When C{sys.argv[0]} is not C{"trial"} no destination is added by L{redirectLogsForTrial}. """ originalDestinations = Logger._destinations._destinations[:] _RedirectLogsForTrial(FakeSys(["myprogram.py"], b""), LogPublisher())() self.assertEqual(Logger._destinations._destinations, originalDestinations)
def setUp(self): import vumi.sentry self.client = DummySentryClient() self.patch(vumi.sentry, 'vumi_raven_client', lambda dsn: self.client) self.logger = LogPublisher() self.service = SentryLoggerService("http://example.com/", "test.logger", "worker-1", logger=self.logger)
def test_trialAsPathNoDestination(self): """ When C{sys.argv[0]} has C{"trial"} as directory name but not program name no destination is added by L{redirectLogsForTrial}. """ originalDestinations = Logger._destinations._destinations[:] _RedirectLogsForTrial(FakeSys(["./trial/myprogram.py"], b""), LogPublisher())() self.assertEqual(Logger._destinations._destinations, originalDestinations)
def test_noDuplicateAddsResult(self): """ If a destination has already been added, calling L{redirectLogsForTrial} a second time returns L{None}. """ redirect = _RedirectLogsForTrial(FakeSys(["trial"], b""), LogPublisher()) destination = redirect() self.addCleanup(removeDestination, destination) result = redirect() self.assertIs(result, None)
def test_message(self, logger): """ A message logged to the given ``LogPublisher`` is converted to an Eliot log message. """ publisher = LogPublisher() observer = EliotObserver(publisher) observer.logger = logger publisher.addObserver(observer) publisher.msg(b"Hello", b"world") assertHasMessage(self, logger, TWISTED_LOG_MESSAGE, dict(error=False, message=u"Hello world"))
def test_noDuplicateAdds(self): """ If a destination has already been added, calling L{redirectLogsForTrial} a second time does not add another destination. """ redirect = _RedirectLogsForTrial(FakeSys(["trial"], b""), LogPublisher()) destination = redirect() self.addCleanup(removeDestination, destination) originalDestinations = Logger._destinations._destinations[:] redirect() self.assertEqual(Logger._destinations._destinations, originalDestinations)
def redirectToLogPublisher(self): """ Redirect Eliot logs to a Twisted log publisher. @return: L{list} of L{str} - the written, formatted Twisted log messages will eventually be added to it. """ written = [] publisher = LogPublisher() publisher.addObserver(lambda m: written.append(textFromEventDict(m))) destination = _RedirectLogsForTrial(FakeSys(["trial"], b""), publisher)() self.addCleanup(removeDestination, destination) return written
def test_startLoggingOverridesWarning(self): """ startLogging() overrides global C{warnings.showwarning} such that warnings go to Twisted log observers. """ self._startLoggingCleanup() newPublisher = NewLogPublisher() class SysModule: stdout = object() stderr = object() tempLogPublisher = LogPublisher( newPublisher, newPublisher, logBeginner=LogBeginner(newPublisher, StringIO(), SysModule, warnings), ) # Trial reports warnings in two ways. First, it intercepts the global # 'showwarning' function *itself*, after starting logging (by way of # the '_collectWarnings' function which collects all warnings as a # around the test's 'run' method). Second, it has a log observer which # immediately reports warnings when they're propagated into the log # system (which, in normal operation, happens only at the end of the # test case). In order to avoid printing a spurious warning in this # test, we first replace the global log publisher's 'showwarning' in # the module with our own. self.patch(log, "theLogPublisher", tempLogPublisher) # And, one last thing, pretend we're starting from a fresh import, or # warnings.warn won't be patched at all. log._oldshowwarning = None # Global mutable state is bad, kids. Stay in school. fakeFile = StringIO() # We didn't previously save log messages, so let's make sure we don't # save them any more. evt = {"pre-start": "event"} received = [] def preStartObserver(x): if "pre-start" in x.keys(): received.append(x) newPublisher(evt) newPublisher.addObserver(preStartObserver) log.startLogging(fakeFile, setStdout=False) self.addCleanup(tempLogPublisher._stopLogging) self.assertEqual(received, []) warnings.warn("hello!") output = fakeFile.getvalue() self.assertIn("UserWarning: hello!", output)
def assertDestinationAdded(self, programPath): """ Assert that when running under the given program a new destination is added by L{redirectLogsForTrial}. @param programPath: A path to a program. @type programPath: L{str} """ destination = _RedirectLogsForTrial(FakeSys([programPath], b""), LogPublisher())() # If this was not added as destination, removing it will raise an # exception: try: removeDestination(destination) except ValueError: self.fail("Destination was not added.")
def test_error(self, logger): """ An error logged to the given ``LogPublisher`` is converted to an Eliot log message. """ publisher = LogPublisher() observer = EliotObserver(publisher) observer.logger = logger publisher.addObserver(observer) # No public API for this unfortunately, so emulate error logging: publisher.msg(failure=Failure(ZeroDivisionError("onoes")), why=b"A zero division ono", isError=True) message = (u'A zero division ono\nTraceback (most recent call ' u'last):\nFailure: exceptions.ZeroDivisionError: onoes\n') assertHasMessage(self, logger, TWISTED_LOG_MESSAGE, dict(error=True, message=message))
def setUp(self): self.patch(junebug.logging_service, 'LogFile', DummyLogFile) self.logger = LogPublisher() self.logpath = self.mktemp() self.service = JunebugLoggerService( 'worker-id', self.logpath, 1000000, 7, logger=self.logger)
def setup(module): module.log = LogPublisher() module.log.err = err