From 5d1e3fbcdcb03bf1c13cb9bd01b9d6bb62f776d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 14 Dec 2016 23:35:42 -0500 Subject: 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. --- systemd/journal.py | 8 ++++---- systemd/test/test_journal.py | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/systemd/journal.py b/systemd/journal.py index e9f714e..41ffbdc 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -456,8 +456,8 @@ def stream(identifier=None, priority=LOG_INFO, level_prefix=False): newline character is written. >>> from systemd import journal - >>> stream = journal.stream('myapp') - >>> res = stream.write('message...\n') + >>> stream = journal.stream('myapp') # doctest: +SKIP + >>> res = stream.write('message...\n') # doctest: +SKIP will produce the following message in the journal:: @@ -470,8 +470,8 @@ def stream(identifier=None, priority=LOG_INFO, level_prefix=False): This interface can be used conveniently with the print function: >>> from __future__ import print_function - >>> fileobj = journal.stream() - >>> print('message...', file=fileobj) # doctest: +SKIP + >>> stream = journal.stream() # doctest: +SKIP + >>> print('message...', file=stream) # doctest: +SKIP priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`, `LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`. diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index dceec3f..2ecfc16 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -1,3 +1,4 @@ +from __future__ import print_function import contextlib import datetime import errno @@ -13,11 +14,11 @@ import pytest TEST_MID = uuid.UUID('8441372f8dca4ca98694a6091fd8519f') @contextlib.contextmanager -def skip_enosys(): +def skip_oserror(code): try: yield - except OSError as e: - if e.errno == errno.ENOSYS: + except (OSError, IOError) as e: + if e.errno == code: pytest.skip() raise @@ -96,7 +97,7 @@ def test_reader_init_path_nondirectory_fd(): def test_reader_init_path_fd(tmpdir): fd = os.open(tmpdir.strpath, os.O_RDONLY) - with skip_enosys(): + with skip_oserror(errno.ENOSYS): j1 = journal.Reader(path=fd) assert list(j1) == [] @@ -139,7 +140,7 @@ def test_reader_this_machine(tmpdir): def test_reader_query_unique(tmpdir): j = journal.Reader(path=tmpdir.strpath) with j: - with skip_enosys(): + with skip_oserror(errno.ENOSYS): ans = j.query_unique('FOOBAR') assert isinstance(ans, set) assert ans == set() @@ -147,7 +148,7 @@ def test_reader_query_unique(tmpdir): def test_reader_enumerate_fields(tmpdir): j = journal.Reader(path=tmpdir.strpath) with j: - with skip_enosys(): + with skip_oserror(errno.ENOSYS): ans = j.enumerate_fields() assert isinstance(ans, set) assert ans == set() @@ -155,14 +156,14 @@ def test_reader_enumerate_fields(tmpdir): def test_reader_has_runtime_files(tmpdir): j = journal.Reader(path=tmpdir.strpath) with j: - with skip_enosys(): + with skip_oserror(errno.ENOSYS): ans = j.has_runtime_files() assert ans == False def test_reader_has_persistent_files(tmpdir): j = journal.Reader(path=tmpdir.strpath) with j: - with skip_enosys(): + with skip_oserror(errno.ENOSYS): ans = j.has_runtime_files() assert ans == False @@ -200,3 +201,13 @@ def test_seek_realtime(tmpdir): long_ago = datetime.datetime(1970, 5, 4) j.seek_realtime(long_ago) + +def test_journal_stream(): + # This will fail when running in a bare chroot without /run/systemd/journal/stdout + with skip_oserror(errno.ENOENT): + stream = journal.stream('test_journal.py') + + res = stream.write('message...\n') + assert res in (11, None) # Python2 returns None + + print('printed message...', file=stream) -- cgit v1.2.1