-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
128 lines (101 loc) · 4.33 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
# test.py
# created 26.11.2021
# Thomas Kaulke, [email protected], [email protected]
# https://github.com/kaulketh
# -----------------------------------------------------------
import logging
import os
import sys
import unittest
from logger import PreconfiguredLogger
TEST_LOG = PreconfiguredLogger.instance
TEST_LOG_FOLDER = PreconfiguredLogger.LOG_FOLDER
TEST_INFO_LOG = PreconfiguredLogger.INF_LOG
TEST_ERROR_LOG = PreconfiguredLogger.ERR_LOG
class LoggingTestCase(unittest.TestCase):
def test1_logging(self):
self.result = True
self.debug = "DEBUG" == logging.getLevelName(logging.DEBUG)
self.info = "INFO" == logging.getLevelName(logging.INFO)
self.warning = "WARNING" == logging.getLevelName(logging.WARNING)
self.warn = "WARN" == logging.getLevelName(logging.WARN)
self.error = "ERROR" == logging.getLevelName(logging.ERROR)
# FATAL = CRITICAL, refer documentation
self.fatal = "CRITICAL" == logging.getLevelName(logging.FATAL)
self.critical = "CRITICAL" == logging.getLevelName(logging.CRITICAL)
TEST_LOG.debug(self.debug)
TEST_LOG.info(self.info)
TEST_LOG.warning(self.warning)
# deprecated, refer documentation
# TEST_LOG.warn(self.warn)
TEST_LOG.error(self.error)
TEST_LOG.fatal(self.fatal)
TEST_LOG.critical(self.critical)
self.result &= \
self.debug \
and self.info \
and self.warning \
and self.error \
and self.fatal \
and self.critical
if self.result:
sys.stdout.write("Result: Logging OK\n")
TEST_LOG.info("Success: " + str(self.result))
self.assertEqual(True, self.result, "Logging NOK!")
def test2_file_creation(self):
self.result = True
self.folder = TEST_LOG_FOLDER
self.info_file = TEST_INFO_LOG
self.error_file = TEST_ERROR_LOG
self.result &= os.path.isdir(self.folder) \
and os.path.isfile(self.info_file) \
and os.path.isfile(self.error_file)
if self.result:
sys.stdout.write("Result: Folder and file creation OK\n")
TEST_LOG.info("Success: " + str(self.result))
self.assertEqual(True, self.result, "Folder and file creation NOK!")
def test3_file_content(self):
self.result = True
self.folder = TEST_LOG_FOLDER
self.info = TEST_INFO_LOG
self.error = TEST_ERROR_LOG
self.search_info = "INFO"
self.search_error = "ERROR"
self.search_warn = "WARNING"
self.search_critical = "CRITICAL"
with open(self.info, 'r') as read_obj:
for line in read_obj:
if self.search_info in line:
self.result &= self.search_info in line
elif self.search_error in line:
self.result &= self.result and self.search_error in line
elif self.search_warn in line:
self.result &= self.result and self.search_warn in line
elif self.search_critical in line:
self.result &= self.result and self.search_critical in line
else:
self.result &= False
with open(self.error, 'r') as read_obj:
for line in read_obj:
if self.search_error in line:
self.result &= self.result and self.search_error in line
elif self.search_warn in line:
self.result &= self.result and self.search_warn in line
elif self.search_critical in line:
self.result &= self.result and self.search_critical in line
else:
self.result &= False
if self.result:
sys.stdout.write("File content check OK\n")
TEST_LOG.info("Success: " + str(self.result))
self.assertEqual(True, self.result, "File content check NOK!")
def run(out=sys.stderr, verbosity=2):
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(sys.modules[__name__])
unittest.TextTestRunner(out, verbosity=verbosity).run(suite)
if __name__ == '__main__':
with open('test.log', 'w') as f:
run(out=f)