summaryrefslogtreecommitdiff
path: root/systemd/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-03-11 14:26:27 -0500
committerGitHub <noreply@github.com>2017-03-11 14:26:27 -0500
commit753a4ca5ffbea9e7b937dbe5c567353f9ca21f85 (patch)
tree7881ff7c011727acc5c9da7078c70b0aaa440455 /systemd/test
parentdce0a855c3281e7051b1cbe0f73386d1c90ef320 (diff)
parentbc691d8e293a593fbd14ad1d592d06f4f490ed29 (diff)
downloadpython-systemd-753a4ca5ffbea9e7b937dbe5c567353f9ca21f85.tar.gz
Merge pull request #31 from keszybz/is_socket_sockaddr
daemon: add basic support for sd_is_socket_sockaddr
Diffstat (limited to 'systemd/test')
-rw-r--r--systemd/test/test_daemon.py45
-rw-r--r--systemd/test/test_journal.py27
2 files changed, 59 insertions, 13 deletions
diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py
index 7733552..c1e08c7 100644
--- a/systemd/test/test_daemon.py
+++ b/systemd/test/test_daemon.py
@@ -9,6 +9,7 @@ from systemd.daemon import (booted,
is_socket, _is_socket,
is_socket_inet, _is_socket_inet,
is_socket_unix, _is_socket_unix,
+ is_socket_sockaddr, _is_socket_sockaddr,
is_mq, _is_mq,
listen_fds,
notify)
@@ -122,15 +123,18 @@ def test_no_mismatch():
assert not is_fifo(sock)
assert not is_mq_wrapper(sock)
assert not is_socket_inet(sock)
+ assert not is_socket_sockaddr(sock, '127.0.0.1:2000')
fd = sock.fileno()
assert not is_fifo(fd)
assert not is_mq_wrapper(fd)
assert not is_socket_inet(fd)
+ assert not is_socket_sockaddr(fd, '127.0.0.1:2000')
assert not _is_fifo(fd)
assert not _is_mq_wrapper(fd)
assert not _is_socket_inet(fd)
+ assert not _is_socket_sockaddr(fd, '127.0.0.1:2000')
def test_is_socket():
with closing_socketpair(socket.AF_UNIX) as pair:
@@ -141,12 +145,43 @@ def test_is_socket():
assert not is_socket(arg, socket.AF_INET)
assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
+ assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
+
+ assert _is_socket(arg)
+ assert _is_socket(arg, socket.AF_UNIX)
+ assert not _is_socket(arg, socket.AF_INET)
+ assert _is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
+ assert not _is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
+ assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
+
+def test_is_socket_sockaddr():
+ with contextlib.closing(socket.socket(socket.AF_INET)) as sock:
+ sock.bind(('127.0.0.1', 0))
+ addr, port = sock.getsockname()
+ port = ':{}'.format(port)
+
+ for listening in (0, 1):
+ for arg in (sock, sock.fileno()):
+ assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM)
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM)
- assert is_socket(sock)
- assert is_socket(arg, socket.AF_UNIX)
- assert not is_socket(arg, socket.AF_INET)
- assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
- assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening)
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1)
+ assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening)
+
+ with pytest.raises(ValueError):
+ is_socket_sockaddr(arg, '127.0.0.1', flowinfo=123456)
+
+ assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
+ assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
+
+ with pytest.raises(ValueError):
+ _is_socket_sockaddr(arg, '127.0.0.1', 0, 123456)
+
+ assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
+ assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
+
+ sock.listen(11)
def test__is_socket():
with closing_socketpair(socket.AF_UNIX) as pair:
diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py
index fd13cd0..fd38036 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
@@ -40,11 +41,11 @@ class MockSender:
self.buf.append(args)
@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
@@ -132,7 +133,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) == []
@@ -175,7 +176,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()
@@ -183,7 +184,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()
@@ -191,14 +192,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
@@ -236,3 +237,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)