Skip to content
Permalink
Browse files

Test bogus arg to log.err().

Separate the log.err() sub-tests into separate tests.

git-svn-id: https://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk@11288 e27351fd-9f3e-4f54-a53b-843176b1656c
  • Loading branch information...
wsanchez committed Jun 3, 2013
1 parent 2678618 commit 73db1010d119e571ac8b6656cc5c87d2e82e6f05
@@ -339,10 +339,8 @@ def bindEmit(level):
#
# Attach methods to Logger
#
def log_emit(self, message=None, raiseException=None, **kwargs):
def log_emit(self, message=None, **kwargs):
self.emit(level, message, **kwargs)
if raiseException:
raise raiseException(message)

def will_emit(self):
return self.willLogAtLevel(level)
@@ -329,60 +329,100 @@ def test_legacy_msg(self):
self.assertIdentical(log.emitted["message"], None)


def test_legacy_err(self):
def test_legacy_err_implicit(self):
"""
Test LegacyLogger's log.err()
Test LegacyLogger's log.err() capturing the in-flight exception.
"""
log = TestLegacyLogger()

exception = RuntimeError("Oh me, oh my.")
kwargs = { "foo": "bar", "obj": object() }

try:
raise exception
except RuntimeError:
log.err(**kwargs)

self.legacy_err(log, kwargs, None, exception)


def test_legacy_err_exception(self):
"""
Test LegacyLogger's log.err() with a given exception.
"""
log = TestLegacyLogger()

exception = RuntimeError("Oh me, oh my.")
kwargs = { "foo": "bar", "obj": object() }
why = "Because I said so."

def implicit():
try:
raise exception
except RuntimeError:
log.err(**kwargs)

def withException():
try:
raise exception
except RuntimeError as e:
log.err(e, why, **kwargs)

def withFailure():
try:
raise exception
except RuntimeError:
log.err(Failure(), why, **kwargs)

def withBogus():
try:
raise exception
except RuntimeError:
log.err(object(), why, **kwargs)

for rabbleRouser in (implicit, withException, withFailure):
rabbleRouser()

#
# log.failure() will cause trial to complain, so here we check that
# trial saw the correct error and remove it from the list of things to
# complain about.
#
errors = self.flushLoggedErrors(RuntimeError)
self.assertEquals(len(errors), 1)

self.assertIdentical(log.emitted["level"], LogLevel.error)
self.assertIdentical(log.emitted["message"], None)
self.assertIdentical(log.emitted["kwargs"]["failure"].__class__, Failure)
self.assertIdentical(log.emitted["kwargs"]["failure"].value, exception)

if rabbleRouser is implicit:
self.assertIdentical(log.emitted["kwargs"]["why"], None)
else:
self.assertIdentical(log.emitted["kwargs"]["why"], why)
try:
raise exception
except RuntimeError as e:
log.err(e, why, **kwargs)

self.legacy_err(log, kwargs, why, exception)


def test_legacy_err_failure(self):
"""
Test LegacyLogger's log.err() with a given L{Failure}.
"""
log = TestLegacyLogger()

exception = RuntimeError("Oh me, oh my.")
kwargs = { "foo": "bar", "obj": object() }
why = "Because I said so."

try:
raise exception
except RuntimeError:
log.err(Failure(), why, **kwargs)

self.legacy_err(log, kwargs, why, exception)

for key, value in kwargs.items():
self.assertIdentical(log.emitted["kwargs"][key], value)

def test_legacy_err_bogus(self):
"""
Test LegacyLogger's log.err() with a bogus argument.
"""
log = TestLegacyLogger()

exception = RuntimeError("Oh me, oh my.")
kwargs = { "foo": "bar", "obj": object() }
why = "Because I said so."
bogus = object()

try:
raise exception
except RuntimeError:
log.err(bogus, why, **kwargs)

errors = self.flushLoggedErrors(exception.__class__)
self.assertEquals(len(errors), 0)

self.assertIdentical(log.emitted["level"], LogLevel.error)
self.assertEquals(log.emitted["message"], repr(bogus))
self.assertIdentical(log.emitted["kwargs"]["why"], why)

for key, value in kwargs.items():
self.assertIdentical(log.emitted["kwargs"][key], value)


def legacy_err(self, log, kwargs, why, exception):
#
# log.failure() will cause trial to complain, so here we check that
# trial saw the correct error and remove it from the list of things to
# complain about.
#
errors = self.flushLoggedErrors(exception.__class__)
self.assertEquals(len(errors), 1)

self.assertIdentical(log.emitted["level"], LogLevel.error)
self.assertEquals(log.emitted["message"], None)
self.assertIdentical(log.emitted["kwargs"]["failure"].__class__, Failure)
self.assertIdentical(log.emitted["kwargs"]["failure"].value, exception)
self.assertIdentical(log.emitted["kwargs"]["why"], why)

for key, value in kwargs.items():
self.assertIdentical(log.emitted["kwargs"][key], value)
@@ -60,8 +60,9 @@ def __init__(self, xmlFile):
try:
_ignore_tree, proxies_node = readXML(self.xmlFile, ELEMENT_PROXIES)
except ValueError, e:
log.error("XML parse error for '%s' because: %s" % (self.xmlFile, e,), raiseException=RuntimeError)
log.failure("XML parse error for '%s'" % (self.xmlFile,), e)

# FIXME: RuntimeError is dumb.
self._parseXML(proxies_node)

def _parseXML(self, rootnode):
@@ -72,7 +73,7 @@ def _parseXML(self, rootnode):
for child in rootnode.getchildren():

if child.tag != ELEMENT_RECORD:
log.error("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, self.xmlFile,))

repeat = int(child.get(ATTRIBUTE_REPEAT, "1"))

@@ -90,11 +91,11 @@ def _parseXML(self, rootnode):
):
self._parseMembers(node, write_proxies if node.tag == ELEMENT_PROXIES else read_proxies)
else:
log.error("Invalid element '%s' in proxies file: '%s'" % (node.tag, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid element '%s' in proxies file: '%s'" % (node.tag, self.xmlFile,))

# Must have at least a guid
if not guid:
log.error("Invalid record '%s' without a guid in proxies file: '%s'" % (child, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid record '%s' without a guid in proxies file: '%s'" % (child, self.xmlFile,))

if repeat > 1:
for i in xrange(1, repeat+1):
@@ -90,7 +90,7 @@ def __init__(self, xmlFile, externalUpdate=True):
try:
_ignore_tree, accounts_node = readXML(self.xmlFile.path, ELEMENT_ACCOUNTS)
except ValueError, e:
log.error("XML parse error for '%s' because: %s" % (self.xmlFile, e,), raiseException=RuntimeError)
raise RuntimeError("XML parse error for '%s' because: %s" % (self.xmlFile, e,))
self._parseXML(accounts_node)

def _parseXML(self, node):
@@ -80,7 +80,7 @@ def __init__(self, xmlFile, items):
try:
_ignore_tree, augments_node = readXML(self.xmlFile, ELEMENT_AUGMENTS)
except ValueError, e:
log.error("XML parse error for '%s' because: %s" % (self.xmlFile, e,), raiseException=RuntimeError)
raise RuntimeError("XML parse error for '%s' because: %s" % (self.xmlFile, e,))

self._parseXML(augments_node)

@@ -92,7 +92,7 @@ def _parseXML(self, rootnode):
for child in rootnode.getchildren():

if child.tag != ELEMENT_RECORD:
log.error("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, self.xmlFile,))

repeat = int(child.get(ATTRIBUTE_REPEAT, "1"))

@@ -117,11 +117,11 @@ def _parseXML(self, rootnode):
):
fields[node.tag] = node.text == VALUE_TRUE
else:
log.error("Invalid element '%s' in augment file: '%s'" % (node.tag, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid element '%s' in augment file: '%s'" % (node.tag, self.xmlFile,))

# Must have at least a uid
if ELEMENT_UID not in fields:
log.error("Invalid record '%s' without a uid in augment file: '%s'" % (child, self.xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid record '%s' without a uid in augment file: '%s'" % (child, self.xmlFile,))

if repeat > 1:
for i in xrange(1, repeat+1):
@@ -267,12 +267,12 @@ def parse(xmlFile, ignoreIPLookupFailures=False):
try:
_ignore_tree, servers_node = readXML(xmlFile, ELEMENT_SERVERS)
except ValueError, e:
log.error("XML parse error for '%s' because: %s" % (xmlFile, e,), raiseException=RuntimeError)
raise RuntimeError("XML parse error for '%s' because: %s" % (xmlFile, e,))

for child in servers_node.getchildren():

if child.tag != ELEMENT_SERVER:
log.error("Unknown server type: '%s' in servers file: '%s'" % (child.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Unknown server type: '%s' in servers file: '%s'" % (child.tag, xmlFile,))

server = Server()
server.isImplicit = child.get(ATTR_IMPLICIT, ATTR_VALUE_YES) == ATTR_VALUE_YES
@@ -289,10 +289,10 @@ def parse(xmlFile, ignoreIPLookupFailures=False):
elif node.tag == ELEMENT_PARTITIONS:
ServersParser._parsePartition(xmlFile, node, server)
else:
log.error("Invalid element '%s' in servers file: '%s'" % (node.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid element '%s' in servers file: '%s'" % (node.tag, xmlFile,))

if server.id is None or server.uri is None:
log.error("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,))

server.check(ignoreIPLookupFailures=ignoreIPLookupFailures)
results[server.id] = server
@@ -306,7 +306,7 @@ def _parsePartition(xmlFile, partitions, server):
for child in partitions.getchildren():

if child.tag != ELEMENT_PARTITION:
log.error("Unknown partition type: '%s' in servers file: '%s'" % (child.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Unknown partition type: '%s' in servers file: '%s'" % (child.tag, xmlFile,))

id = None
uri = None
@@ -316,9 +316,9 @@ def _parsePartition(xmlFile, partitions, server):
elif node.tag == ELEMENT_URI:
uri = node.text
else:
log.error("Invalid element '%s' in augment file: '%s'" % (node.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid element '%s' in augment file: '%s'" % (node.tag, xmlFile,))

if id is None or uri is None:
log.error("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,), raiseException=RuntimeError)
raise RuntimeError("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,))

server.addPartition(id, uri)

0 comments on commit 73db101

Please sign in to comment.
You can’t perform that action at this time.