From a93828172f8f62b0bd2f1363c0932a5853ce7a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 7 Aug 2015 11:47:37 -0400 Subject: tests: add first test This is based on the code in https://github.com/systemd/python-systemd/pull/4 by Jacek Konieczny . --- systemd/test/test_daemon.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 systemd/test/test_daemon.py diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py new file mode 100644 index 0000000..230581d --- /dev/null +++ b/systemd/test/test_daemon.py @@ -0,0 +1,64 @@ +import sys +import os +import posix +from systemd.daemon import _is_fifo, is_fifo + +import pytest + +def test__is_fifo(tmpdir): + path = tmpdir.join('test.fifo').strpath + posix.mkfifo(path) + fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) + + assert _is_fifo(fd, None) + assert _is_fifo(fd, path) + +def test__is_fifo_file(tmpdir): + file = tmpdir.join('test.fifo') + file.write('boo') + path = file.strpath + fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) + + assert not _is_fifo(fd, None) + assert not _is_fifo(fd, path) + +def test__is_fifo_bad_fd(tmpdir): + path = tmpdir.join('test.fifo').strpath + + with pytest.raises(OSError): + assert not _is_fifo(-1, None) + + with pytest.raises(OSError): + assert not _is_fifo(-1, path) + +def test_is_fifo(tmpdir): + path = tmpdir.join('test.fifo').strpath + posix.mkfifo(path) + fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) + file = os.fdopen(fd, 'r') + + assert is_fifo(file, None) + assert is_fifo(file, path) + assert is_fifo(fd, None) + assert is_fifo(fd, path) + +def test_is_fifo_file(tmpdir): + file = tmpdir.join('test.fifo') + file.write('boo') + path = file.strpath + fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK) + file = os.fdopen(fd, 'r') + + assert not is_fifo(file, None) + assert not is_fifo(file, path) + assert not is_fifo(fd, None) + assert not is_fifo(fd, path) + +def test_is_fifo_bad_fd(tmpdir): + path = tmpdir.join('test.fifo').strpath + + with pytest.raises(OSError): + assert not is_fifo(-1, None) + + with pytest.raises(OSError): + assert not is_fifo(-1, path) -- cgit v1.2.1