ENH: consistent operation of formatExceptionInfo + unittest for it

pull/225/head
Yaroslav Halchenko 2013-05-09 22:46:59 -04:00
parent 728aaec6b8
commit 90d6a4a6cd
4 changed files with 56 additions and 7 deletions

View File

@ -17,11 +17,7 @@
# along with Fail2Ban; if not, write to the Free Software # along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Author: Cyril Jaquier __author__ = "Cyril Jaquier, Arturo 'Buanzo' Busleiman"
# Author: Arturo 'Buanzo' Busleiman
#
__author__ = "Cyril Jaquier"
__copyright__ = "Copyright (c) 2009 Cyril Jaquier" __copyright__ = "Copyright (c) 2009 Cyril Jaquier"
__license__ = "GPL" __license__ = "GPL"
@ -33,6 +29,12 @@ def formatExceptionInfo():
excName = cla.__name__ excName = cla.__name__
try: try:
excArgs = exc.__dict__["args"] excArgs = exc.__dict__["args"]
# Assure that we always return a string, without unneeded
# 'decorations' with python <= 2.5 where args would be a tuple
if isinstance(excArgs, tuple) and len(excArgs) == 1:
excArgs = excArgs[0]
excArgs = str(excArgs)
except KeyError: except KeyError:
# And always provide a string output
excArgs = str(exc) excArgs = str(exc)
return (excName, excArgs) return (excName, excArgs)

View File

@ -36,6 +36,7 @@ from testcases import servertestcase
from testcases import datedetectortestcase from testcases import datedetectortestcase
from testcases import actiontestcase from testcases import actiontestcase
from testcases import sockettestcase from testcases import sockettestcase
from testcases import misctestcase
from testcases.utils import FormatterWithTraceBack from testcases.utils import FormatterWithTraceBack
from server.mytime import MyTime from server.mytime import MyTime
@ -150,6 +151,8 @@ tests.addTest(unittest.makeSuite(clientreadertestcase.JailReaderTest))
tests.addTest(unittest.makeSuite(clientreadertestcase.JailsReaderTest)) tests.addTest(unittest.makeSuite(clientreadertestcase.JailsReaderTest))
# CSocket and AsyncServer # CSocket and AsyncServer
tests.addTest(unittest.makeSuite(sockettestcase.Socket)) tests.addTest(unittest.makeSuite(sockettestcase.Socket))
# Misc helpers
tests.addTest(unittest.makeSuite(misctestcase.HelpersTest))
# Filter # Filter
if not opts.no_network: if not opts.no_network:

View File

@ -70,8 +70,8 @@ class RequestHandler(asynchat.async_chat):
self.close_when_done() self.close_when_done()
def handle_error(self): def handle_error(self):
e1,e2 = helpers.formatExceptionInfo() e1, e2 = helpers.formatExceptionInfo()
logSys.error("Unexpected communication error: "+e2) logSys.error("Unexpected communication error: %s" % str(e2))
logSys.error(traceback.format_exc().splitlines()) logSys.error(traceback.format_exc().splitlines())
self.close() self.close()

44
testcases/misctestcase.py Normal file
View File

@ -0,0 +1,44 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
# vi: set ft=python sts=4 ts=4 sw=4 noet :
# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
__author__ = "Yaroslav Halchenko"
__copyright__ = "Copyright (c) 2013 Yaroslav Halchenko"
__license__ = "GPL"
import unittest
from common.helpers import formatExceptionInfo
class HelpersTest(unittest.TestCase):
def testFormatExceptionInfoBasic(self):
try:
raise ValueError("Very bad exception")
except:
name, args = formatExceptionInfo()
self.assertEqual(name, "ValueError")
self.assertEqual(args, "Very bad exception")
def testFormatExceptionConvertArgs(self):
try:
raise ValueError("Very bad", None)
except:
name, args = formatExceptionInfo()
self.assertEqual(name, "ValueError")
# might be fragile due to ' vs "
self.assertEqual(args, "('Very bad', None)")