Skip to content

Commit 4d7d91f

Browse files
[3.10] Add more syslog tests (GH-97953). (GH-98101)
(cherry picked from commit cae7d1d)
1 parent 820ef62 commit 4d7d91f

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717

1818
class AuditTest(unittest.TestCase):
19+
maxDiff = None
20+
1921
def do_test(self, *args):
2022
with subprocess.Popen(
2123
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
@@ -170,5 +172,29 @@ def test_sqlite3(self):
170172
self.assertEqual(actual, expected)
171173

172174

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

Lib/test/test_syslog.py

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