Commit c81840c9 authored by Lorenz Steinert's avatar Lorenz Steinert

added log print evaluation

parent c4e44fcb
......@@ -3,6 +3,7 @@
"""test the esme module"""
import os
import time
import logging
import unittest
import configparser
import ldap3 as ldap
......@@ -18,7 +19,6 @@ class FsrLdapTest(unittest.TestCase):
def logging_info(self, arg):
self.info += [arg]
def setUp(self):
"""setup for the tests"""
share_dir = "/tmp/python3-esme-testing"
......@@ -42,6 +42,11 @@ class FsrLdapTest(unittest.TestCase):
self.sharedir = "/tmp/python3-esme-testing"
self.info = []
self.warning = []
self.tmp_info = logging.info
self.tmp_warning = logging.warning
logging.info = self.logging_info
logging.warning = self.logging_warning
@staticmethod
def ldap_search_fail(*arg, attributes=None):
......@@ -55,16 +60,22 @@ class FsrLdapTest(unittest.TestCase):
"""test the get_fsr_ldap function"""
self.assertEqual(self.test_ldap_server._get_fsr_ldap(),
['testing test1'])
self.assertEqual(self.info, ['Fetching FSR from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
def test_get_fsr_extern_ldap(self):
"""test the get_fsr_extern_ldap function"""
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap(),
['testing test2'])
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
def test_get_protokollant_ldap(self):
ldap.Connection = self.con
esme.current_user = lambda: 'testing test1'
esme.current_user = lambda: 'test1'
esme.input = lambda x: ""
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}), 'testing test1')
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
def test_get_fallback_fsr(self):
"""test the get_fallback_fsr function"""
......@@ -72,6 +83,8 @@ class FsrLdapTest(unittest.TestCase):
self.assertEqual(self.test_ldap_server._get_fallback_fsr(sharedir), ['testing test1'])
os.remove("/tmp/python3-esme-testing/fsr")
self.assertEqual(self.test_ldap_server._get_fallback_fsr(sharedir), Error.NO_FILE)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_fallback_fsr_extern(self):
"""test the get_fallback_fsr_extern function"""
......@@ -79,38 +92,63 @@ class FsrLdapTest(unittest.TestCase):
self.assertEqual(self.test_ldap_server._get_fallback_fsr_extern(sharedir), ['testing test2'])
os.remove("/tmp/python3-esme-testing/fsr_extern")
self.assertEqual(self.test_ldap_server._get_fallback_fsr_extern(sharedir), Error.NO_FILE)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_fsr_ldap_no_server(self):
"""test the get_fsr_ldap function without ldap connection"""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_ldap(),
Error.UNSEC_SEARCH)
self.assertEqual(self.info, ['Fetching FSR from LDAP ...'])
self.assertEqual(self.warning, [])
def test_get_fsr_extern_ldap_no_server(self):
"""test the get_fst_extern_ldap function without ldap connection"""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap(),
Error.UNSEC_SEARCH)
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...'])
self.assertEqual(self.warning, [])
def test_get_protokollant_ldap_no_server(self):
esme.current_user = lambda: 'testing test1'
esme.input = lambda x: ""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}),
'testing test3')
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...'])
self.assertEqual(self.warning, [])
def test_get_fsr_ldap_ldap_exception(self):
"""test the get_fsr_ldap function with a LDAPExcepton"""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server._get_fsr_ldap()[0],
Error.LDAP_FAIL)
self.assertEqual(self.info, ['Fetching FSR from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback File."])
def test_get_fsr_extern_ldap_ldap_exception(self):
"""test the get_fsr_extern_ldap function with a LDAPExcepton"""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap()[0],
Error.LDAP_FAIL)
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback File."])
def test_get_protokollant_ldap_no_server(self):
ldap.Connection = self.con_non
def test_get_protokollant_ldap_ldap_exception(self):
esme.current_user = lambda: 'testing test1'
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}), 'testing test3')
esme.input = lambda x: ""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}),
'testing test3')
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback default."])
def tearDown(self):
"""cleanup after the tests"""
logging.info = self.tmp_info
logging.warning = self.tmp_warning
if os.path.isfile("/tmp/python3-esme-testing/fsr"):
os.remove("/tmp/python3-esme-testing/fsr")
if os.path.isfile("/tmp/python3-esme-testing/fsr_extern"):
......@@ -121,6 +159,12 @@ class FsrLdapTest(unittest.TestCase):
class GetFuncTest(unittest.TestCase):
"""test case for the get methods without ldap"""
def logging_warning(self, arg):
self.warning += [arg]
def logging_info(self, arg):
self.info += [arg]
def setUp(self):
"""setup for the tests"""
self.time_save = time.localtime
......@@ -153,6 +197,12 @@ class GetFuncTest(unittest.TestCase):
self.config1['DEFAULT'] = {'tmp': 'tmp'}
with open("/tmp/python3-esme-testing/etc/proto.ini.default", 'w') as fobj:
self.config1.write(fobj)
self.info = []
self.warning = []
self.tmp_info = logging.info
self.tmp_warning = logging.warning
logging.info = self.logging_info
logging.warning = self.logging_warning
def test_get_sprcher(self):
"""test the get_sprecher function"""
......@@ -162,6 +212,8 @@ class GetFuncTest(unittest.TestCase):
esme.input = lambda x: 'testinput'
self.assertEqual(esme.get_sprecher(), "testinput")
self.assertEqual(esme.get_sprecher(self.config), "testinput")
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_protokollant(self):
"""test the get_protokollant function"""
......@@ -171,6 +223,8 @@ class GetFuncTest(unittest.TestCase):
esme.input= lambda x: ''
self.assertEqual(esme.get_protokollant(), "")
self.assertEqual(esme.get_protokollant(self.config), "testprotokollant")
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_config(self):
"""test the get_config function"""
......@@ -179,18 +233,28 @@ class GetFuncTest(unittest.TestCase):
os.remove("/tmp/python3-esme-testing/etc/proto.ini")
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc"), self.config1)
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc", default=True), self.config1)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No non default ini file found using default file.'])
def test_get_share(self):
"""test the get_share function"""
self.assertEqual(esme.get_share("", self.config), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("/tmp/python3-esme-testing/share")), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("", self.config, '/tmp/python3-esme-testing/share')), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("/tmp/python3-esme-testing/share")),
'/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("", self.config, '/tmp/python3-esme-testing/share')),
'/tmp/python3-esme-testing/share')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_path(self):
"""test the get_path function"""
self.assertEqual(esme.get_path("", self.config), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("/tmp/python3-esme-testing/etc/test")), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("", self.config, '/tmp/python3-esme-testing')), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("/tmp/python3-esme-testing/etc/test")),
'/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("", self.config, '/tmp/python3-esme-testing')),
'/tmp/python3-esme-testing')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_ldap_server(self):
"""test the get_ldap_server function"""
......@@ -198,6 +262,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_ldap_server(server='testing'), 'testing')
self.assertEqual(esme.get_ldap_server(self.config, 'testing'), 'testing')
self.assertEqual(esme.get_ldap_server(), 'rincewind.fs.physik.uni-kl.de')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No LDAP Server supplied. Triing to use "rincwind.fs.physik.uni-kl.de".'])
def test_get_ldap_base(self):
"""test the get_ldap_base function"""
......@@ -205,6 +271,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_ldap_base(base='dc=testing,dc=de'), 'dc=testing,dc=de')
self.assertEqual(esme.get_ldap_base(self.config, 'dc=testing,dc=de'), 'dc=testing,dc=de')
self.assertEqual(esme.get_ldap_base(), 'dc=fs,dc=physik,dc=uni-kl,dc=de')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No LDAP search base supplied. Triing to use "dc=fs,dc=physik,dc=uni-kl,dc=de".'])
def test_get_server_timeout(self):
"""test the get_server_timeout function"""
......@@ -212,6 +280,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_server_timeout(test=True), 1)
self.assertEqual(esme.get_server_timeout(self.config, True), 1)
self.assertEqual(esme.get_server_timeout(), 10)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_editor(self):
"""test the get_editor function"""
......@@ -219,7 +289,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_editor('vim'), 'testinput')
esme.input = lambda x: ''
self.assertEqual(esme.get_editor('vim'), 'vim')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_date(self):
"""test the get_date function"""
......@@ -230,11 +301,15 @@ class GetFuncTest(unittest.TestCase):
esme.input = lambda x: '01.01.2002'
self.assertEqual(esme.get_date(), ('01.01.2002', '2002', '01', '01'))
self.assertEqual(esme.get_date(True), ('01.01.2002', '2002', '01', '01'))
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def tearDown(self):
"""cleanup after tests"""
esme.input = input
time.localtime = self.time_save
logging.info = self.tmp_info
logging.warning = self.tmp_warning
if os.path.isfile('/tmp/python3-esme-testing/fsr'):
os.remove("/tmp/python3-esme-testing/fsr")
if os.path.isfile('/tmp/python3-esme-testing/fsr_extern'):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment