Skip to content

Commit e121ae4

Browse files
authored
Merge pull request #34 from keszybz/test-skipping
Improve test skipping on old systems.
2 parents 8921c79 + c7e393b commit e121ae4

File tree

7 files changed

+77
-25
lines changed

7 files changed

+77
-25
lines changed

docs/login.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ external event loop:
1717

1818
>>> import select
1919
>>> from systemd import login
20-
>>> m = login.Monitor("machine")
20+
>>> m = login.Monitor("machine") # doctest: +SKIP
2121
>>> p = select.poll()
22-
>>> p.register(m, m.get_events())
22+
>>> p.register(m, m.get_events()) # doctest: +SKIP
2323
>>> login.machine_names() # doctest: +SKIP
2424
[]
2525
>>> p.poll() # doctest: +SKIP
2626
[(3, 1)]
2727
>>> login.machine_names() # doctest: +SKIP
28-
['fedora-19.nspawn']
28+
['fedora-25']

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def lib(*names, **kw):
9292
'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
9393
],
9494
py_modules = ['systemd.journal', 'systemd.daemon',
95-
'systemd.test.test_daemon', 'systemd.test.test_journal'],
95+
'systemd.test.test_daemon',
96+
'systemd.test.test_journal',
97+
'systemd.test.test_login'],
9698
ext_modules = [_journal,
9799
_reader,
98100
_daemon,

systemd/daemon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from socket import AF_UNSPEC as _AF_UNSPEC
2+
13
from ._daemon import (__version__,
24
booted,
35
notify,
@@ -9,7 +11,6 @@
911
_is_socket_unix,
1012
_is_mq,
1113
LISTEN_FDS_START)
12-
from socket import AF_UNSPEC as _AF_UNSPEC
1314

1415
def _convert_fileobj(fileobj):
1516
try:

systemd/journal.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
import traceback as _traceback
2727
import os as _os
2828
import logging as _logging
29-
if _sys.version_info >= (3,3):
30-
from collections import ChainMap as _ChainMap
3129
from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
3230
LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
31+
if _sys.version_info >= (3,3):
32+
from collections import ChainMap as _ChainMap
33+
3334
from ._journal import __version__, sendv, stream_fd
3435
from ._reader import (_Reader, NOP, APPEND, INVALIDATE,
3536
LOCAL_ONLY, RUNTIME_ONLY,
@@ -225,8 +226,8 @@ def __next__(self):
225226
def add_match(self, *args, **kwargs):
226227
"""Add one or more matches to the filter journal log entries.
227228
228-
All matches of different field are combined in a logical AND, and
229-
matches of the same field are automatically combined in a logical OR.
229+
All matches of different field are combined with logical AND, and
230+
matches of the same field are automatically combined with logical OR.
230231
Matches can be passed as strings of form "FIELD=value", or keyword
231232
arguments FIELD="value".
232233
"""
@@ -241,7 +242,7 @@ def get_next(self, skip=1):
241242
Entries will be processed with converters specified during Reader
242243
creation.
243244
244-
Optional skip value will return the `skip`\-th log entry.
245+
Optional `skip` value will return the `skip`-th log entry.
245246
246247
Currently a standard dictionary of fields is returned, but in the
247248
future this might be changed to a different mapping type, so the
@@ -261,7 +262,7 @@ def get_previous(self, skip=1):
261262
262263
Equivalent to get_next(-skip).
263264
264-
Optional skip value will return the -`skip`\-th log entry.
265+
Optional `skip` value will return the -`skip`-th log entry.
265266
266267
Entries will be processed with converters specified during Reader
267268
creation.
@@ -561,7 +562,7 @@ def emit(self, record):
561562
msg = self.format(record)
562563
pri = self.mapPriority(record.levelno)
563564
mid = getattr(record, 'MESSAGE_ID', None)
564-
extras = { k:str(v) for k,v in self._extra.items() }
565+
extras = {k:str(v) for k,v in self._extra.items()}
565566
extras.update({
566567
k:str(v) for k,v in record.__dict__.items()
567568
})

systemd/test/test_daemon.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ def test_listen_fds_default_unset():
243243
assert listen_fds() == []
244244

245245
def test_notify_no_socket():
246-
assert notify('READY=1') == False
246+
assert notify('READY=1') is False
247247
with skip_enosys():
248-
assert notify('FDSTORE=1', fds=[]) == False
249-
assert notify('FDSTORE=1', fds=[1,2]) == False
250-
assert notify('FDSTORE=1', pid=os.getpid()) == False
251-
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == False
248+
assert notify('FDSTORE=1', fds=[]) is False
249+
assert notify('FDSTORE=1', fds=[1, 2]) is False
250+
assert notify('FDSTORE=1', pid=os.getpid()) is False
251+
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) is False
252252

253253
if sys.version_info >= (3,):
254254
connection_error = ConnectionRefusedError
@@ -264,7 +264,7 @@ def test_notify_bad_socket():
264264
with skip_enosys():
265265
notify('FDSTORE=1', fds=[])
266266
with pytest.raises(connection_error):
267-
notify('FDSTORE=1', fds=[1,2])
267+
notify('FDSTORE=1', fds=[1, 2])
268268
with pytest.raises(connection_error):
269269
notify('FDSTORE=1', pid=os.getpid())
270270
with pytest.raises(connection_error):
@@ -282,9 +282,9 @@ def test_notify_with_socket(tmpdir):
282282
sock.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1)
283283
os.environ['NOTIFY_SOCKET'] = path
284284

285-
assert notify('READY=1') == True
285+
assert notify('READY=1')
286286
with skip_enosys():
287-
assert notify('FDSTORE=1', fds=[]) == True
288-
assert notify('FDSTORE=1', fds=[1,2]) == True
289-
assert notify('FDSTORE=1', pid=os.getpid()) == True
290-
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == True
287+
assert notify('FDSTORE=1', fds=[])
288+
assert notify('FDSTORE=1', fds=[1, 2])
289+
assert notify('FDSTORE=1', pid=os.getpid())
290+
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,))

systemd/test/test_journal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ def test_reader_has_runtime_files(tmpdir):
194194
with j:
195195
with skip_oserror(errno.ENOSYS):
196196
ans = j.has_runtime_files()
197-
assert ans == False
197+
assert ans is False
198198

199199
def test_reader_has_persistent_files(tmpdir):
200200
j = journal.Reader(path=tmpdir.strpath)
201201
with j:
202202
with skip_oserror(errno.ENOSYS):
203203
ans = j.has_runtime_files()
204-
assert ans == False
204+
assert ans is False
205205

206206
def test_reader_converters(tmpdir):
207207
converters = {'xxx' : lambda arg: 'yyy'}

systemd/test/test_login.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from __future__ import print_function
2+
import select
3+
import contextlib
4+
import errno
5+
6+
from systemd import login
7+
8+
import pytest
9+
10+
@contextlib.contextmanager
11+
def skip_oserror(code):
12+
try:
13+
yield
14+
except (OSError, IOError) as e:
15+
if e.errno == code:
16+
pytest.skip()
17+
raise
18+
19+
def test_seats():
20+
# just check that we get some sequence back
21+
with skip_oserror(errno.ENOENT):
22+
seats = login.seats()
23+
assert len(seats) >= 0
24+
25+
def test_sessions():
26+
with skip_oserror(errno.ENOENT):
27+
sessions = login.sessions()
28+
assert len(sessions) >= 0
29+
30+
def test_machine_names():
31+
with skip_oserror(errno.ENOENT):
32+
machine_names = login.machine_names()
33+
assert len(machine_names) >= 0
34+
35+
def test_uids():
36+
with skip_oserror(errno.ENOENT):
37+
uids = login.uids()
38+
assert len(uids) >= 0
39+
40+
def test_monitor():
41+
p = select.poll()
42+
43+
with skip_oserror(errno.ENOENT):
44+
m = login.Monitor("machine")
45+
p.register(m, m.get_events())
46+
login.machine_names()
47+
p.poll(1)
48+
login.machine_names()

0 commit comments

Comments
 (0)