1
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
2
# vi: set ft=python sts=4 ts=4 sw=4 noet :
4
# This file is part of Fail2Ban.
6
# Fail2Ban is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# Fail2Ban is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Fail2Ban; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
__author__ = "Yaroslav Halchenko"
21
__copyright__ = "Copyright (c) 2013 Yaroslav Halchenko"
25
import os, sys, unittest
31
from utils import mbasename, TraceBack, FormatterWithTraceBack
32
from common.helpers import formatExceptionInfo
34
class HelpersTest(unittest.TestCase):
36
def testFormatExceptionInfoBasic(self):
38
raise ValueError("Very bad exception")
40
name, args = formatExceptionInfo()
41
self.assertEqual(name, "ValueError")
42
self.assertEqual(args, "Very bad exception")
44
def testFormatExceptionConvertArgs(self):
46
raise ValueError("Very bad", None)
48
name, args = formatExceptionInfo()
49
self.assertEqual(name, "ValueError")
50
# might be fragile due to ' vs "
51
self.assertEqual(args, "('Very bad', None)")
54
# http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
55
def recursive_glob(treeroot, pattern):
58
for base, dirs, files in os.walk(treeroot):
59
goodfiles = fnmatch.filter(dirs + files, pattern)
60
results.extend(os.path.join(base, f) for f in goodfiles)
63
class SetupTest(unittest.TestCase):
66
setup = os.path.join(os.path.dirname(__file__), '..', 'setup.py')
67
self.setup = os.path.exists(setup) and setup or None
68
if not self.setup and sys.version_info >= (2,7): # running not out of the source
69
raise unittest.SkipTest(
70
"Seems to be running not out of source distribution"
71
" -- cannot locate setup.py")
73
def testSetupInstallRoot(self):
74
if not self.setup: return # if verbose skip didn't work out
75
tmp = tempfile.mkdtemp()
76
os.system("%s %s install --root=%s >/dev/null"
77
% (sys.executable, self.setup, tmp))
80
return [os.path.join(tmp, x) for x in l]
83
return [x[len(tmp)+1:] for x in l]
85
got = strippath(sorted(glob('%s/*' % tmp)))
86
need = ['etc', 'usr', 'var']
88
# if anything is missing
89
if set(need).difference(got):
90
# below code was actually to print out not missing but
91
# rather files in 'excess'. Left in place in case we
92
# decide to revert to such more strict test
94
for missing in set(got).difference(need):
95
missing_full = os.path.join(tmp, missing)
96
files[missing] = os.path.exists(missing_full) \
97
and strippath(recursive_glob(missing_full, '*')) or None
101
msg="Got: %s Needed: %s under %s. Files under new paths: %s"
102
% (got, need, tmp, files))
104
# Assure presence of some files we expect to see in the installation
105
for f in ('etc/fail2ban/fail2ban.conf',
106
'etc/fail2ban/jail.conf'):
107
self.assertTrue(os.path.exists(os.path.join(tmp, f)),
108
msg="Can't find %s" % f)
113
class TestsUtilsTest(unittest.TestCase):
115
def testmbasename(self):
116
self.assertEqual(mbasename("sample.py"), 'sample')
117
self.assertEqual(mbasename("/long/path/sample.py"), 'sample')
118
# this one would include only the directory for the __init__ and base files
119
self.assertEqual(mbasename("/long/path/__init__.py"), 'path.__init__')
120
self.assertEqual(mbasename("/long/path/base.py"), 'path.base')
121
self.assertEqual(mbasename("/long/path/base"), 'path.base')
123
def testTraceBack(self):
124
# pretty much just a smoke test since tests runners swallow all the detail
126
for compress in True, False:
127
tb = TraceBack(compress=compress)
132
def deep_function(i):
133
if i: deep_function(i-1)
137
print deep_function(3)
141
# if we run it through 'coverage' (e.g. on travis) then we
142
# would get a traceback
144
self.assertTrue('>' in s, msg="no '>' in %r" % s)
146
self.assertFalse('>' in s, msg="'>' present in %r" % s) # There is only "fail2ban-testcases" in this case, no true traceback
147
self.assertTrue(':' in s, msg="no ':' in %r" % s)
150
def testFormatterWithTraceBack(self):
151
from StringIO import StringIO
153
Formatter = FormatterWithTraceBack
155
# and both types of traceback at once
156
fmt = ' %(tb)s | %(tbc)s : %(message)s'
157
logSys = logging.getLogger("fail2ban_tests")
158
out = logging.StreamHandler(strout)
159
out.setFormatter(Formatter(fmt))
160
logSys.addHandler(out)
163
s = strout.getvalue()
164
self.assertTrue(s.rstrip().endswith(': XXX'))
165
pindex = s.index('|')
167
# in this case compressed and not should be the same (?)
168
self.assertTrue(pindex > 10) # we should have some traceback
169
self.assertEqual(s[:pindex], s[pindex+1:pindex*2 + 1])