Standalone logging tests, most of them new, and an attempt to fix the parent

msgid logic so it's a little less broken.
This commit is contained in:
Itamar Turner-Trauring 2020-08-03 14:09:28 -04:00
parent 02daa12031
commit 622ed2f971
2 changed files with 115 additions and 2 deletions

View File

@ -0,0 +1,113 @@
"""
Tests for allmydata.util.log.
Ported to Python 3.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from future.utils import PY2
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401
from twisted.trial import unittest
from twisted.python.failure import Failure
from foolscap.logging import log
from allmydata.util import log as tahoe_log
class SampleError(Exception):
pass
class LoggingObject0(tahoe_log.PrefixingLogMixin):
pass
class LoggingObject1(tahoe_log.PrefixingLogMixin):
pass
class LoggingObject2(tahoe_log.PrefixingLogMixin):
pass
class LoggingObject3(tahoe_log.PrefixingLogMixin):
pass
class Log(unittest.TestCase):
def setUp(self):
self.messages = []
def msg(msg, facility, parent, *args, **kwargs):
self.messages.append((msg, facility, parent, args, kwargs))
return "msg{}".format(len(self.messages))
self.patch(log, "msg", msg)
def test_err(self):
"""Logging with log.err() causes tests to fail."""
try:
raise SampleError("simple sample")
except:
f = Failure()
tahoe_log.err(format="intentional sample error",
failure=f, level=tahoe_log.OPERATIONAL, umid="wO9UoQ")
self.flushLoggedErrors(SampleError)
def test_default_facility(self):
obj = LoggingObject1(facility="defaultfac")
obj.log("hello")
obj.log("world", facility="override")
self.assertEqual(self.messages[-2][1], "defaultfac")
self.assertEqual(self.messages[-1][1], "override")
def test_with_prefix(self):
obj = LoggingObject0("fac", prefix="pre1")
obj.log("hello")
obj.log("world")
self.assertEqual(self.messages[-2][0], '<LoggingObject0 #1>(pre1): hello')
self.assertEqual(self.messages[-1][0], '<LoggingObject0 #1>(pre1): world')
def test_no_prefix(self):
obj = LoggingObject2()
obj.log("hello")
obj.log("world")
self.assertEqual(self.messages[-2][0], '<LoggingObject2 #1>: hello')
self.assertEqual(self.messages[-1][0], '<LoggingObject2 #1>: world')
def test_numming(self):
obj = LoggingObject3()
obj2 = LoggingObject3()
obj.log("hello")
obj2.log("world")
self.assertEqual(self.messages[-2][0], '<LoggingObject3 #1>: hello')
self.assertEqual(self.messages[-1][0], '<LoggingObject3 #2>: world')
def test_parent_id(self):
obj = LoggingObject1()
result = obj.log("zero")
self.assertEqual(result, "msg1")
obj.log("one", parent="par1")
obj.log("two", parent="par2")
obj.log("three")
obj.log("four")
self.assertEqual([m[2] for m in self.messages],
[None, "par1", "par2", "msg1", "msg1"])
def test_grandparent_id(self):
obj = LoggingObject1(grandparentmsgid="grand")
result = obj.log("zero")
self.assertEqual(result, "msg1")
obj.log("one", parent="par1")
obj.log("two", parent="par2")
obj.log("three")
obj.log("four")
self.assertEqual([m[2] for m in self.messages],
["grand", "par1", "par2", "msg1", "msg1"])

View File

@ -36,8 +36,8 @@ class LogMixin(object):
def log(self, msg, facility=None, parent=None, *args, **kwargs):
if facility is None:
facility = self._facility
pmsgid = None
if parent is None:
pmsgid = parent
if pmsgid is None:
pmsgid = self._parentmsgid
if pmsgid is None:
pmsgid = self._grandparentmsgid