Skip to content

Commit a947f32

Browse files
committed
tests: add tests for notify()
1 parent 6baa4b4 commit a947f32

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

systemd/test/test_daemon.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
is_socket_inet, _is_socket_inet,
1111
is_socket_unix, _is_socket_unix,
1212
is_mq, _is_mq,
13-
listen_fds)
13+
listen_fds,
14+
notify)
1415

1516
import pytest
1617

@@ -196,3 +197,37 @@ def test_listen_fds_default_unset():
196197
assert listen_fds(False) == [3]
197198
assert listen_fds() == [3]
198199
assert listen_fds() == []
200+
201+
def test_notify_no_socket():
202+
assert notify('READY=1') == False
203+
assert notify('FDSTORE=1', fds=[]) == False
204+
assert notify('FDSTORE=1', fds=[1,2]) == False
205+
assert notify('FDSTORE=1', pid=os.getpid()) == False
206+
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == False
207+
208+
def test_notify_bad_socket():
209+
os.environ['NOTIFY_SOCKET'] = '/dev/null'
210+
211+
with pytest.raises(ConnectionRefusedError):
212+
notify('READY=1')
213+
with pytest.raises(ConnectionRefusedError):
214+
notify('FDSTORE=1', fds=[])
215+
with pytest.raises(ConnectionRefusedError):
216+
notify('FDSTORE=1', fds=[1,2])
217+
with pytest.raises(ConnectionRefusedError):
218+
notify('FDSTORE=1', pid=os.getpid())
219+
with pytest.raises(ConnectionRefusedError):
220+
notify('FDSTORE=1', pid=os.getpid(), fds=(1,))
221+
222+
def test_notify_with_socket(tmpdir):
223+
path = tmpdir.join('socket').strpath
224+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
225+
sock.bind(path)
226+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_PASSCRED, 1)
227+
os.environ['NOTIFY_SOCKET'] = path
228+
229+
assert notify('READY=1') == True
230+
assert notify('FDSTORE=1', fds=[]) == True
231+
assert notify('FDSTORE=1', fds=[1,2]) == True
232+
assert notify('FDSTORE=1', pid=os.getpid()) == True
233+
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) == True

0 commit comments

Comments
 (0)