From 5aab3fc3b41ca06e66463f66ed9cc936a03a18fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 16 Dec 2016 21:05:38 -0500 Subject: tests: move login tests from doctest to separate file This way we can skip ENOENT (which happens in containers). While at it, let's extend the tests a bit, so that we at least call all functions and check the type of the return value. Also, drop '.nspawn' from the machine name, nspawn doesn't use that suffix any more. --- docs/login.rst | 6 +++--- setup.py | 4 +++- systemd/test/test_login.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 systemd/test/test_login.py diff --git a/docs/login.rst b/docs/login.rst index 2ee807c..23204e0 100644 --- a/docs/login.rst +++ b/docs/login.rst @@ -17,12 +17,12 @@ external event loop: >>> import select >>> from systemd import login - >>> m = login.Monitor("machine") + >>> m = login.Monitor("machine") # doctest: +SKIP >>> p = select.poll() - >>> p.register(m, m.get_events()) + >>> p.register(m, m.get_events()) # doctest: +SKIP >>> login.machine_names() # doctest: +SKIP [] >>> p.poll() # doctest: +SKIP [(3, 1)] >>> login.machine_names() # doctest: +SKIP - ['fedora-19.nspawn'] + ['fedora-25'] diff --git a/setup.py b/setup.py index 1acfbc5..70c2780 100644 --- a/setup.py +++ b/setup.py @@ -92,7 +92,9 @@ setup (name = 'systemd-python', 'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)', ], py_modules = ['systemd.journal', 'systemd.daemon', - 'systemd.test.test_daemon', 'systemd.test.test_journal'], + 'systemd.test.test_daemon', + 'systemd.test.test_journal', + 'systemd.test.test_login'], ext_modules = [_journal, _reader, _daemon, diff --git a/systemd/test/test_login.py b/systemd/test/test_login.py new file mode 100644 index 0000000..afb5f45 --- /dev/null +++ b/systemd/test/test_login.py @@ -0,0 +1,48 @@ +from __future__ import print_function +import select +import contextlib +import errno + +from systemd import login + +import pytest + +@contextlib.contextmanager +def skip_oserror(code): + try: + yield + except (OSError, IOError) as e: + if e.errno == code: + pytest.skip() + raise + +def test_seats(): + # just check that we get some sequence back + with skip_oserror(errno.ENOENT): + seats = login.seats() + assert len(seats) >= 0 + +def test_sessions(): + with skip_oserror(errno.ENOENT): + sessions = login.sessions() + assert len(sessions) >= 0 + +def test_machine_names(): + with skip_oserror(errno.ENOENT): + machine_names = login.machine_names() + assert len(machine_names) >= 0 + +def test_uids(): + with skip_oserror(errno.ENOENT): + uids = login.uids() + assert len(uids) >= 0 + +def test_monitor(): + p = select.poll() + + with skip_oserror(errno.ENOENT): + m = login.Monitor("machine") + p.register(m, m.get_events()) + login.machine_names() + p.poll(1) + login.machine_names() -- cgit v1.2.1 From 14b05bfff57feed1f20e6b562bed188bc08252dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 17 Dec 2016 07:39:05 -0500 Subject: Adjust import order, spacing, operators as recommended by pylint --- systemd/daemon.py | 3 ++- systemd/journal.py | 11 ++++++----- systemd/test/test_daemon.py | 22 +++++++++++----------- systemd/test/test_journal.py | 4 ++-- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/systemd/daemon.py b/systemd/daemon.py index 5d398f2..625219c 100644 --- a/systemd/daemon.py +++ b/systemd/daemon.py @@ -1,3 +1,5 @@ +from socket import AF_UNSPEC as _AF_UNSPEC + from ._daemon import (__version__, booted, notify, @@ -9,7 +11,6 @@ from ._daemon import (__version__, _is_socket_unix, _is_mq, LISTEN_FDS_START) -from socket import AF_UNSPEC as _AF_UNSPEC def _convert_fileobj(fileobj): try: diff --git a/systemd/journal.py b/systemd/journal.py index 524ca9f..30d1f39 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -26,10 +26,11 @@ import uuid as _uuid import traceback as _traceback import os as _os import logging as _logging -if _sys.version_info >= (3,3): - from collections import ChainMap as _ChainMap from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG) +if _sys.version_info >= (3,3): + from collections import ChainMap as _ChainMap + from ._journal import __version__, sendv, stream_fd from ._reader import (_Reader, NOP, APPEND, INVALIDATE, LOCAL_ONLY, RUNTIME_ONLY, @@ -241,7 +242,7 @@ class Reader(_Reader): Entries will be processed with converters specified during Reader creation. - Optional skip value will return the `skip`\-th log entry. + Optional skip value will return the `skip`-th log entry. Currently a standard dictionary of fields is returned, but in the future this might be changed to a different mapping type, so the @@ -261,7 +262,7 @@ class Reader(_Reader): Equivalent to get_next(-skip). - Optional skip value will return the -`skip`\-th log entry. + Optional skip value will return the -`skip`-th log entry. Entries will be processed with converters specified during Reader creation. @@ -561,7 +562,7 @@ class JournalHandler(_logging.Handler): msg = self.format(record) pri = self.mapPriority(record.levelno) mid = getattr(record, 'MESSAGE_ID', None) - extras = { k:str(v) for k,v in self._extra.items() } + extras = {k:str(v) for k,v in self._extra.items()} extras.update({ k:str(v) for k,v in record.__dict__.items() }) diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py index c1e08c7..ae76e54 100644 --- a/systemd/test/test_daemon.py +++ b/systemd/test/test_daemon.py @@ -243,12 +243,12 @@ def test_listen_fds_default_unset(): assert listen_fds() == [] def test_notify_no_socket(): - assert notify('READY=1') == False + assert notify('READY=1') is False with skip_enosys(): - assert notify('FDSTORE=1', fds=[]) == False - assert notify('FDSTORE=1', fds=[1,2]) == False - assert notify('FDSTORE=1', pid=os.getpid()) == False - assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == False + assert notify('FDSTORE=1', fds=[]) is False + assert notify('FDSTORE=1', fds=[1, 2]) is False + assert notify('FDSTORE=1', pid=os.getpid()) is False + assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) is False if sys.version_info >= (3,): connection_error = ConnectionRefusedError @@ -264,7 +264,7 @@ def test_notify_bad_socket(): with skip_enosys(): notify('FDSTORE=1', fds=[]) with pytest.raises(connection_error): - notify('FDSTORE=1', fds=[1,2]) + notify('FDSTORE=1', fds=[1, 2]) with pytest.raises(connection_error): notify('FDSTORE=1', pid=os.getpid()) with pytest.raises(connection_error): @@ -282,9 +282,9 @@ def test_notify_with_socket(tmpdir): sock.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1) os.environ['NOTIFY_SOCKET'] = path - assert notify('READY=1') == True + assert notify('READY=1') with skip_enosys(): - assert notify('FDSTORE=1', fds=[]) == True - assert notify('FDSTORE=1', fds=[1,2]) == True - assert notify('FDSTORE=1', pid=os.getpid()) == True - assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == True + assert notify('FDSTORE=1', fds=[]) + assert notify('FDSTORE=1', fds=[1, 2]) + assert notify('FDSTORE=1', pid=os.getpid()) + assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index fd38036..582b031 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -194,14 +194,14 @@ def test_reader_has_runtime_files(tmpdir): with j: with skip_oserror(errno.ENOSYS): ans = j.has_runtime_files() - assert ans == False + assert ans is False def test_reader_has_persistent_files(tmpdir): j = journal.Reader(path=tmpdir.strpath) with j: with skip_oserror(errno.ENOSYS): ans = j.has_runtime_files() - assert ans == False + assert ans is False def test_reader_converters(tmpdir): converters = {'xxx' : lambda arg: 'yyy'} -- cgit v1.2.1 From c7e393b3ca58013b2afcd02620c64c6fe66d21f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 17 Dec 2016 07:50:24 -0500 Subject: journal: improve docstring formatting a bit --- systemd/journal.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/systemd/journal.py b/systemd/journal.py index 30d1f39..93f8044 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -226,8 +226,8 @@ class Reader(_Reader): def add_match(self, *args, **kwargs): """Add one or more matches to the filter journal log entries. - All matches of different field are combined in a logical AND, and - matches of the same field are automatically combined in a logical OR. + All matches of different field are combined with logical AND, and + matches of the same field are automatically combined with logical OR. Matches can be passed as strings of form "FIELD=value", or keyword arguments FIELD="value". """ @@ -242,7 +242,7 @@ class Reader(_Reader): Entries will be processed with converters specified during Reader creation. - Optional skip value will return the `skip`-th log entry. + Optional `skip` value will return the `skip`-th log entry. Currently a standard dictionary of fields is returned, but in the future this might be changed to a different mapping type, so the @@ -262,7 +262,7 @@ class Reader(_Reader): Equivalent to get_next(-skip). - Optional skip value will return the -`skip`-th log entry. + Optional `skip` value will return the -`skip`-th log entry. Entries will be processed with converters specified during Reader creation. -- cgit v1.2.1