Debian Codegraph @

tests/test_plugin_rsyslog.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 import socket
31 from unittest import mock
32
33 import pytest
34
35 import apprise
36
37 logging.disable(logging.CRITICAL)
38
39 from apprise.exception import AppriseImproperlyConfigured # noqa E402
40 from apprise.plugins.rsyslog import NotifyRSyslog # noqa E402
41
42
43 @mock.patch("socket.socket")
44 @mock.patch("os.getpid")
45 def test_plugin_rsyslog_by_url(mock_getpid, mock_socket):
46 """NotifyRSyslog() Apprise URLs."""
47 payload = "test"
48 mock_connection = mock.Mock()
49
50 # Fix pid response since it can vary in length and this impacts the
51 # sendto() payload response
52 mock_getpid.return_value = 123
53
54 # our payload length
55 mock_connection.sendto.return_value = 16
56 mock_socket.return_value = mock_connection
57
58 # an invalid URL
59 assert NotifyRSyslog.parse_url(object) is None
60 assert NotifyRSyslog.parse_url(42) is None
61 assert NotifyRSyslog.parse_url(None) is None
62
63 # localhost does not lookup to any of the facility codes so this
64 # gets interpreted as a host
65 obj = apprise.Apprise.instantiate("rsyslog://localhost")
66 assert isinstance(obj, NotifyRSyslog)
67 assert obj.url().startswith("rsyslog://localhost") is True
68 assert r"logpid=yes" in obj.url()
69 assert bool(obj.notify(body=payload)) is True
70
71 mock_connection.sendto.return_value = 18
72 obj = apprise.Apprise.instantiate("rsyslog://localhost/?facility=local5")
73 assert isinstance(obj, NotifyRSyslog)
74 assert obj.url().startswith("rsyslog://localhost/local5") is True
75 assert r"logpid=yes" in obj.url()
76 assert bool(obj.notify(body=payload)) is True
77
78 # Invalid instantiation
79 assert (
80 apprise.Apprise.instantiate("rsyslog://localhost/?facility=invalid")
81 is None
82 )
83
84 mock_connection.sendto.return_value = 17
85 # j will cause a search to take place and match to daemon
86 obj = apprise.Apprise.instantiate("rsyslog://localhost/?facility=d")
87 assert isinstance(obj, NotifyRSyslog)
88 assert obj.url().startswith("rsyslog://localhost/daemon") is True
89 assert r"logpid=yes" in obj.url()
90 assert bool(obj.notify(body=payload)) is True
91
92 # Test bad return count
93 mock_connection.sendto.return_value = 0
94 assert bool(obj.notify(body=payload)) is False
95
96 # Test with port
97 mock_connection.sendto.return_value = 17
98 obj = apprise.Apprise.instantiate("rsyslog://localhost:518")
99 assert isinstance(obj, NotifyRSyslog)
100 assert obj.url().startswith("rsyslog://localhost:518") is True
101 assert r"logpid=yes" in obj.url()
102 assert bool(obj.notify(body=payload)) is True
103
104 # Set length to include title (for test)
105 mock_connection.sendto.return_value = 39
106 assert (
107 bool(obj.notify(body=payload, title="Testing a title entry")) is True
108 )
109
110 # Return length back to where it was
111 mock_connection.sendto.return_value = 16
112
113 # Test with default port
114 obj = apprise.Apprise.instantiate("rsyslog://localhost:514")
115 assert isinstance(obj, NotifyRSyslog)
116 assert obj.url().startswith("rsyslog://localhost") is True
117 assert r"logpid=yes" in obj.url()
118 assert bool(obj.notify(body=payload)) is True
119
120 # Specify a facility
121 obj = apprise.Apprise.instantiate("rsyslog://localhost/kern")
122 assert isinstance(obj, NotifyRSyslog)
123 assert obj.url().startswith("rsyslog://localhost/kern") is True
124 assert r"logpid=yes" in obj.url()
125 assert bool(obj.notify(body=payload)) is True
126
127 # Specify a facility requiring a lookup and having the port identified
128 # resolves any ambiguity
129 obj = apprise.Apprise.instantiate("rsyslog://localhost:514/d")
130 assert isinstance(obj, NotifyRSyslog)
131 assert obj.url().startswith("rsyslog://localhost/daemon") is True
132 assert r"logpid=yes" in obj.url()
133 mock_connection.sendto.return_value = 17 # daemon is one more byte in size
134 assert bool(obj.notify(body=payload)) is True
135
136 obj = apprise.Apprise.instantiate("rsyslog://localhost:9000/d?logpid=no")
137 assert isinstance(obj, NotifyRSyslog)
138 assert obj.url().startswith("rsyslog://localhost:9000/daemon") is True
139 assert r"logpid=no" in obj.url()
140
141 # Verify our URL ID is generated
142 assert isinstance(obj.url_id(), str)
143
144 # Test notifications
145 # + 1 byte in size due to user
146 # + length of pid returned
147 mock_connection.sendto.return_value = (
148 len(payload) + 5 + len(str(mock_getpid.return_value))
149 )
150 assert bool(obj.notify(body=payload)) is True
151 # This only fails because the underlining sendto() will return a
152 # length different then what was expected
153 assert bool(obj.notify(body="a different payload size")) is False
154
155 # Test timeouts and errors that can occur
156 mock_connection.sendto.return_value = None
157 mock_connection.sendto.side_effect = socket.gaierror
158 assert bool(obj.notify(body=payload)) is False
159
160 mock_connection.sendto.side_effect = socket.timeout
161 assert bool(obj.notify(body=payload)) is False
162
163
164 def test_plugin_rsyslog_edge_cases():
165 """NotifyRSyslog() Edge Cases."""
166
167 # Default
168 obj = NotifyRSyslog(host="localhost", facility=None)
169 assert isinstance(obj, NotifyRSyslog)
170 assert obj.url().startswith("rsyslog://localhost/user") is True
171 assert r"logpid=yes" in obj.url()
172
173 # Exception should be thrown about the fact no bot token was specified
174 with pytest.raises(AppriseImproperlyConfigured):
175 NotifyRSyslog(host="localhost", facility="invalid")
176
177 with pytest.raises(AppriseImproperlyConfigured):
178 NotifyRSyslog(host="localhost", facility=object)