Debian Codegraph @

tests/test_plugin_rsyslog.py

raw symbols SCIP
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.plugins.rsyslog import NotifyRSyslog # noqa E402
40
41
42 @mock.patch("socket.socket")
43 @mock.patch("os.getpid")
44 def test_plugin_rsyslog_by_url(mock_getpid, mock_socket):
45 """NotifyRSyslog() Apprise URLs."""
46 payload = "test"
47 mock_connection = mock.Mock()
48
49 # Fix pid response since it can vary in length and this impacts the
50 # sendto() payload response
51 mock_getpid.return_value = 123
52
53 # our payload length
54 mock_connection.sendto.return_value = 16
55 mock_socket.return_value = mock_connection
56
57 # an invalid URL
58 assert NotifyRSyslog.parse_url(object) is None
59 assert NotifyRSyslog.parse_url(42) is None
60 assert NotifyRSyslog.parse_url(None) is None
61
62 # localhost does not lookup to any of the facility codes so this
63 # gets interpreted as a host
64 obj = apprise.Apprise.instantiate("rsyslog://localhost")
65 assert isinstance(obj, NotifyRSyslog)
66 assert obj.url().startswith("rsyslog://localhost") is True
67 assert r"logpid=yes" in obj.url()
68 assert obj.notify(body=payload) is True
69
70 mock_connection.sendto.return_value = 18
71 obj = apprise.Apprise.instantiate("rsyslog://localhost/?facility=local5")
72 assert isinstance(obj, NotifyRSyslog)
73 assert obj.url().startswith("rsyslog://localhost/local5") is True
74 assert r"logpid=yes" in obj.url()
75 assert obj.notify(body=payload) is True
76
77 # Invalid instantiation
78 assert (
79 apprise.Apprise.instantiate("rsyslog://localhost/?facility=invalid")
80 is None
81 )
82
83 mock_connection.sendto.return_value = 17
84 # j will cause a search to take place and match to daemon
85 obj = apprise.Apprise.instantiate("rsyslog://localhost/?facility=d")
86 assert isinstance(obj, NotifyRSyslog)
87 assert obj.url().startswith("rsyslog://localhost/daemon") is True
88 assert r"logpid=yes" in obj.url()
89 assert obj.notify(body=payload) is True
90
91 # Test bad return count
92 mock_connection.sendto.return_value = 0
93 assert obj.notify(body=payload) is False
94
95 # Test with port
96 mock_connection.sendto.return_value = 17
97 obj = apprise.Apprise.instantiate("rsyslog://localhost:518")
98 assert isinstance(obj, NotifyRSyslog)
99 assert obj.url().startswith("rsyslog://localhost:518") is True
100 assert r"logpid=yes" in obj.url()
101 assert obj.notify(body=payload) is True
102
103 # Set length to include title (for test)
104 mock_connection.sendto.return_value = 39
105 assert obj.notify(body=payload, title="Testing a title entry") is True
106
107 # Return length back to where it was
108 mock_connection.sendto.return_value = 16
109
110 # Test with default port
111 obj = apprise.Apprise.instantiate("rsyslog://localhost:514")
112 assert isinstance(obj, NotifyRSyslog)
113 assert obj.url().startswith("rsyslog://localhost") is True
114 assert r"logpid=yes" in obj.url()
115 assert obj.notify(body=payload) is True
116
117 # Specify a facility
118 obj = apprise.Apprise.instantiate("rsyslog://localhost/kern")
119 assert isinstance(obj, NotifyRSyslog)
120 assert obj.url().startswith("rsyslog://localhost/kern") is True
121 assert r"logpid=yes" in obj.url()
122 assert obj.notify(body=payload) is True
123
124 # Specify a facility requiring a lookup and having the port identified
125 # resolves any ambiguity
126 obj = apprise.Apprise.instantiate("rsyslog://localhost:514/d")
127 assert isinstance(obj, NotifyRSyslog)
128 assert obj.url().startswith("rsyslog://localhost/daemon") is True
129 assert r"logpid=yes" in obj.url()
130 mock_connection.sendto.return_value = 17 # daemon is one more byte in size
131 assert obj.notify(body=payload) is True
132
133 obj = apprise.Apprise.instantiate("rsyslog://localhost:9000/d?logpid=no")
134 assert isinstance(obj, NotifyRSyslog)
135 assert obj.url().startswith("rsyslog://localhost:9000/daemon") is True
136 assert r"logpid=no" in obj.url()
137
138 # Verify our URL ID is generated
139 assert isinstance(obj.url_id(), str)
140
141 # Test notifications
142 # + 1 byte in size due to user
143 # + length of pid returned
144 mock_connection.sendto.return_value = (
145 len(payload) + 5 + len(str(mock_getpid.return_value))
146 )
147 assert obj.notify(body=payload) is True
148 # This only fails because the underlining sendto() will return a
149 # length different then what was expected
150 assert obj.notify(body="a different payload size") is False
151
152 # Test timeouts and errors that can occur
153 mock_connection.sendto.return_value = None
154 mock_connection.sendto.side_effect = socket.gaierror
155 assert obj.notify(body=payload) is False
156
157 mock_connection.sendto.side_effect = socket.timeout
158 assert obj.notify(body=payload) is False
159
160
161 def test_plugin_rsyslog_edge_cases():
162 """NotifyRSyslog() Edge Cases."""
163
164 # Default
165 obj = NotifyRSyslog(host="localhost", facility=None)
166 assert isinstance(obj, NotifyRSyslog)
167 assert obj.url().startswith("rsyslog://localhost/user") is True
168 assert r"logpid=yes" in obj.url()
169
170 # Exception should be thrown about the fact no bot token was specified
171 with pytest.raises(TypeError):
172 NotifyRSyslog(host="localhost", facility="invalid")
173
174 with pytest.raises(TypeError):
175 NotifyRSyslog(host="localhost", facility=object)