Skip to content

Commit 5d1e3fb

Browse files
committed
tests: skip journal.stream tests on ENOENT error
When running in a chroot, doctests that called journal.stream would fail with ENOENT. Move the tests to test_journal, where we can skip tests properly (without uglyfying the documentation). Fixes #32.
1 parent 930bf89 commit 5d1e3fb

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

systemd/journal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ def stream(identifier=None, priority=LOG_INFO, level_prefix=False):
456456
newline character is written.
457457
458458
>>> from systemd import journal
459-
>>> stream = journal.stream('myapp')
460-
>>> res = stream.write('message...\n')
459+
>>> stream = journal.stream('myapp') # doctest: +SKIP
460+
>>> res = stream.write('message...\n') # doctest: +SKIP
461461
462462
will produce the following message in the journal::
463463
@@ -470,8 +470,8 @@ def stream(identifier=None, priority=LOG_INFO, level_prefix=False):
470470
This interface can be used conveniently with the print function:
471471
472472
>>> from __future__ import print_function
473-
>>> fileobj = journal.stream()
474-
>>> print('message...', file=fileobj) # doctest: +SKIP
473+
>>> stream = journal.stream() # doctest: +SKIP
474+
>>> print('message...', file=stream) # doctest: +SKIP
475475
476476
priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`,
477477
`LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`.

systemd/test/test_journal.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import contextlib
23
import datetime
34
import errno
@@ -13,11 +14,11 @@
1314
TEST_MID = uuid.UUID('8441372f8dca4ca98694a6091fd8519f')
1415

1516
@contextlib.contextmanager
16-
def skip_enosys():
17+
def skip_oserror(code):
1718
try:
1819
yield
19-
except OSError as e:
20-
if e.errno == errno.ENOSYS:
20+
except (OSError, IOError) as e:
21+
if e.errno == code:
2122
pytest.skip()
2223
raise
2324

@@ -96,7 +97,7 @@ def test_reader_init_path_nondirectory_fd():
9697
def test_reader_init_path_fd(tmpdir):
9798
fd = os.open(tmpdir.strpath, os.O_RDONLY)
9899

99-
with skip_enosys():
100+
with skip_oserror(errno.ENOSYS):
100101
j1 = journal.Reader(path=fd)
101102
assert list(j1) == []
102103

@@ -139,30 +140,30 @@ def test_reader_this_machine(tmpdir):
139140
def test_reader_query_unique(tmpdir):
140141
j = journal.Reader(path=tmpdir.strpath)
141142
with j:
142-
with skip_enosys():
143+
with skip_oserror(errno.ENOSYS):
143144
ans = j.query_unique('FOOBAR')
144145
assert isinstance(ans, set)
145146
assert ans == set()
146147

147148
def test_reader_enumerate_fields(tmpdir):
148149
j = journal.Reader(path=tmpdir.strpath)
149150
with j:
150-
with skip_enosys():
151+
with skip_oserror(errno.ENOSYS):
151152
ans = j.enumerate_fields()
152153
assert isinstance(ans, set)
153154
assert ans == set()
154155

155156
def test_reader_has_runtime_files(tmpdir):
156157
j = journal.Reader(path=tmpdir.strpath)
157158
with j:
158-
with skip_enosys():
159+
with skip_oserror(errno.ENOSYS):
159160
ans = j.has_runtime_files()
160161
assert ans == False
161162

162163
def test_reader_has_persistent_files(tmpdir):
163164
j = journal.Reader(path=tmpdir.strpath)
164165
with j:
165-
with skip_enosys():
166+
with skip_oserror(errno.ENOSYS):
166167
ans = j.has_runtime_files()
167168
assert ans == False
168169

@@ -200,3 +201,13 @@ def test_seek_realtime(tmpdir):
200201

201202
long_ago = datetime.datetime(1970, 5, 4)
202203
j.seek_realtime(long_ago)
204+
205+
def test_journal_stream():
206+
# This will fail when running in a bare chroot without /run/systemd/journal/stdout
207+
with skip_oserror(errno.ENOENT):
208+
stream = journal.stream('test_journal.py')
209+
210+
res = stream.write('message...\n')
211+
assert res in (11, None) # Python2 returns None
212+
213+
print('printed message...', file=stream)

0 commit comments

Comments
 (0)