Skip to content

Commit cae7d1d

Browse files
Add more syslog tests (GH-97953)
1 parent 80b3e32 commit cae7d1d

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

Lib/test/audit-tests.py

+20
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,26 @@ def hook(event, args):
429429
sys.addaudithook(hook)
430430
_wmi.exec_query("SELECT * FROM Win32_OperatingSystem")
431431

432+
def test_syslog():
433+
import syslog
434+
435+
def hook(event, args):
436+
if event.startswith("syslog."):
437+
print(event, *args)
438+
439+
sys.addaudithook(hook)
440+
syslog.openlog('python')
441+
syslog.syslog('test')
442+
syslog.setlogmask(syslog.LOG_DEBUG)
443+
syslog.closelog()
444+
# implicit open
445+
syslog.syslog('test2')
446+
# open with default ident
447+
syslog.openlog(logoption=syslog.LOG_NDELAY, facility=syslog.LOG_LOCAL0)
448+
sys.argv = None
449+
syslog.openlog()
450+
syslog.closelog()
451+
432452

433453
if __name__ == "__main__":
434454
from test.support import suppress_msvcrt_asserts

Lib/test/test_audit.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
class AuditTest(unittest.TestCase):
19+
maxDiff = None
1920

2021
@support.requires_subprocess()
2122
def do_test(self, *args):
@@ -185,7 +186,6 @@ def test_sys_getframe(self):
185186

186187
self.assertEqual(actual, expected)
187188

188-
189189
def test_wmi_exec_query(self):
190190
import_helper.import_module("_wmi")
191191
returncode, events, stderr = self.run_python("test_wmi_exec_query")
@@ -199,6 +199,29 @@ def test_wmi_exec_query(self):
199199

200200
self.assertEqual(actual, expected)
201201

202+
def test_syslog(self):
203+
syslog = import_helper.import_module("syslog")
204+
205+
returncode, events, stderr = self.run_python("test_syslog")
206+
if returncode:
207+
self.fail(stderr)
208+
209+
if support.verbose:
210+
print('Events:', *events, sep='\n ')
211+
212+
self.assertSequenceEqual(
213+
events,
214+
[('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'),
215+
('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'),
216+
('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'),
217+
('syslog.closelog', '', ''),
218+
('syslog.syslog', ' ', f'{syslog.LOG_INFO} test2'),
219+
('syslog.openlog', ' ', f'audit-tests.py 0 {syslog.LOG_USER}'),
220+
('syslog.openlog', ' ', f'audit-tests.py {syslog.LOG_NDELAY} {syslog.LOG_LOCAL0}'),
221+
('syslog.openlog', ' ', f'None 0 {syslog.LOG_USER}'),
222+
('syslog.closelog', '', '')]
223+
)
224+
202225

203226
if __name__ == "__main__":
204227
unittest.main()

Lib/test/test_syslog.py

+50-6
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

+1-1
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)