-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_sd_notify.py
67 lines (56 loc) · 2.72 KB
/
test_sd_notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from unittest import TestCase
from unittest.mock import Mock, patch, sentinel
from sd_notify import Notifier
class NotifierTestCase(TestCase):
def test_send(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
cut._send("Hello, world!")
cut.socket.sendto.assert_called_once_with(b"Hello, world!", sentinel.address)
def test_enabled(self):
# With no environment set
with patch("os.getenv", return_value=None) as getenv_mock:
cut = Notifier(sock=Mock(spec=["sendto"]))
res = cut.enabled()
getenv_mock.assert_called_once_with("NOTIFY_SOCKET")
self.assertIs(res, False)
# With empty string set
with patch("os.getenv", return_value="") as getenv_mock:
cut = Notifier(sock=Mock(spec=["sendto"]))
res = cut.enabled()
getenv_mock.assert_called_once_with("NOTIFY_SOCKET")
self.assertIs(res, False)
# With environment set
with patch("os.getenv", return_value="a string") as getenv_mock:
cut = Notifier(sock=Mock(spec=["sendto"]))
res = cut.enabled()
getenv_mock.assert_called_once_with("NOTIFY_SOCKET")
self.assertIs(res, True)
def test_ready(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
with patch('sd_notify.Notifier._send') as patched_send:
cut.ready()
patched_send.assert_called_once_with("READY=1\n")
def test_status(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
with patch('sd_notify.Notifier._send') as patched_send:
cut.status("Hello, world!")
patched_send.assert_called_once_with("STATUS=Hello, world!\n")
def test_notify(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
with patch('sd_notify.Notifier._send') as patched_send:
cut.notify()
patched_send.assert_called_once_with("WATCHDOG=1\n")
def test_notify_error(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
with patch('sd_notify.Notifier._send') as patched_send:
# Without msg arg
cut.notify_error()
patched_send.assert_called_once_with("WATCHDOG=trigger\n")
def test_notify_error_with_message(self):
cut = Notifier(sock=Mock(spec=["sendto"]), addr=sentinel.address)
with patch('sd_notify.Notifier._send') as patched_send:
cut.notify_error(msg="Hello world!")
self.assertEqual(patched_send.call_count, 2)
patched_send.assert_any_call("WATCHDOG=trigger\n")
patched_send.assert_any_call("STATUS=Hello world!\n")