Skip to content

Commit f382c43

Browse files
[3.10] Add more syslog tests (GH-97953).
(cherry picked from commit cae7d1d) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 15a11a6 commit f382c43

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

Lib/test/audit-tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,27 @@ def hook(event, *args):
408408
raise RuntimeError("Expected sqlite3.load_extension to fail")
409409

410410

411+
def test_syslog():
412+
import syslog
413+
414+
def hook(event, args):
415+
if event.startswith("syslog."):
416+
print(event, *args)
417+
418+
sys.addaudithook(hook)
419+
syslog.openlog('python')
420+
syslog.syslog('test')
421+
syslog.setlogmask(syslog.LOG_DEBUG)
422+
syslog.closelog()
423+
# implicit open
424+
syslog.syslog('test2')
425+
# open with default ident
426+
syslog.openlog(logoption=syslog.LOG_NDELAY, facility=syslog.LOG_LOCAL0)
427+
sys.argv = None
428+
syslog.openlog()
429+
syslog.closelog()
430+
431+
411432
if __name__ == "__main__":
412433
from test.support import suppress_msvcrt_asserts
413434

Lib/test/test_audit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717

1818
class AuditTest(unittest.TestCase):
19+
<<<<<<< HEAD
20+
=======
21+
maxDiff = None
22+
23+
@support.requires_subprocess()
24+
>>>>>>> cae7d1d7a7 (Add more syslog tests (GH-97953))
1925
def do_test(self, *args):
2026
with subprocess.Popen(
2127
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
@@ -170,5 +176,29 @@ def test_sqlite3(self):
170176
self.assertEqual(actual, expected)
171177

172178

179+
def test_syslog(self):
180+
syslog = import_helper.import_module("syslog")
181+
182+
returncode, events, stderr = self.run_python("test_syslog")
183+
if returncode:
184+
self.fail(stderr)
185+
186+
if support.verbose:
187+
print('Events:', *events, sep='\n ')
188+
189+
self.assertSequenceEqual(
190+
events,
191+
[('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'),
192+
('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'),
193+
('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'),
194+
('syslog.closelog', '', ''),
195+
('syslog.syslog', ' ', f'{syslog.LOG_INFO} test2'),
196+
('syslog.openlog', ' ', f'audit-tests.py 0 {syslog.LOG_USER}'),
197+
('syslog.openlog', ' ', f'audit-tests.py {syslog.LOG_NDELAY} {syslog.LOG_LOCAL0}'),
198+
('syslog.openlog', ' ', f'None 0 {syslog.LOG_USER}'),
199+
('syslog.closelog', '', '')]
200+
)
201+
202+
173203
if __name__ == "__main__":
174204
unittest.main()

Lib/test/test_syslog.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from test.support import import_helper
1+
from test.support import import_helper, threading_helper
22
syslog = import_helper.import_module("syslog") #skip if not supported
3+
from test import support
4+
import sys
5+
import threading
6+
import time
37
import unittest
48

59
# XXX(nnorwitz): This test sucks. I don't know of a platform independent way
@@ -8,6 +12,9 @@
812

913
class Test(unittest.TestCase):
1014

15+
def tearDown(self):
16+
syslog.closelog()
17+
1118
def test_openlog(self):
1219
syslog.openlog('python')
1320
# Issue #6697.
@@ -18,22 +25,59 @@ def test_syslog(self):
1825
syslog.syslog('test message from python test_syslog')
1926
syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog')
2027

28+
def test_syslog_implicit_open(self):
29+
syslog.closelog() # Make sure log is closed
30+
syslog.syslog('test message from python test_syslog')
31+
syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog')
32+
2133
def test_closelog(self):
2234
syslog.openlog('python')
2335
syslog.closelog()
36+
syslog.closelog() # idempotent operation
2437

2538
def test_setlogmask(self):
26-
syslog.setlogmask(syslog.LOG_DEBUG)
39+
mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
40+
oldmask = syslog.setlogmask(mask)
41+
self.assertEqual(syslog.setlogmask(0), mask)
42+
self.assertEqual(syslog.setlogmask(oldmask), mask)
2743

2844
def test_log_mask(self):
29-
syslog.LOG_MASK(syslog.LOG_INFO)
30-
31-
def test_log_upto(self):
32-
syslog.LOG_UPTO(syslog.LOG_INFO)
45+
mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
46+
self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_WARNING))
47+
self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_ERR))
48+
self.assertFalse(mask & syslog.LOG_MASK(syslog.LOG_INFO))
3349

3450
def test_openlog_noargs(self):
3551
syslog.openlog()
3652
syslog.syslog('test message from python test_syslog')
3753

54+
@threading_helper.requires_working_threading()
55+
def test_syslog_threaded(self):
56+
start = threading.Event()
57+
stop = False
58+
def opener():
59+
start.wait(10)
60+
i = 1
61+
while not stop:
62+
syslog.openlog(f'python-test-{i}') # new string object
63+
i += 1
64+
def logger():
65+
start.wait(10)
66+
while not stop:
67+
syslog.syslog('test message from python test_syslog')
68+
69+
orig_si = sys.getswitchinterval()
70+
support.setswitchinterval(1e-9)
71+
try:
72+
threads = [threading.Thread(target=opener)]
73+
threads += [threading.Thread(target=logger) for k in range(10)]
74+
with threading_helper.start_threads(threads):
75+
start.set()
76+
time.sleep(0.1)
77+
stop = True
78+
finally:
79+
sys.setswitchinterval(orig_si)
80+
81+
3882
if __name__ == "__main__":
3983
unittest.main()

Modules/syslogmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ syslog_setlogmask(PyObject *self, PyObject *args)
235235

236236
if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
237237
return NULL;
238-
if (PySys_Audit("syslog.setlogmask", "(O)", args ? args : Py_None) < 0) {
238+
if (PySys_Audit("syslog.setlogmask", "l", maskpri) < 0) {
239239
return NULL;
240240
}
241241
omaskpri = setlogmask(maskpri);

0 commit comments

Comments
 (0)