Skip to content

Commit 59da288

Browse files
committed
Add tests for services
While adding some tests, minor issues with individual service plugins have been fixed. At the same time, this drops official support for Python 2.
1 parent 786dafb commit 59da288

File tree

17 files changed

+1249
-40
lines changed

17 files changed

+1249
-40
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macos-latest, windows-latest]
13-
python-version: ["2.7", "3.6", "3.7", "3.8", "3.9"]
13+
python-version: ["3.6", "3.7", "3.8", "3.9"]
1414
include:
1515
- os: ubuntu-latest
1616
path: ~/.cache/pip

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ in progress
1010
- Fix "http" service plugin
1111
- Improve machinery to launch a notification service plugin standalone.
1212
Now, it works without any ``mqttwarn.ini`` configuration file at all.
13+
- Begin adding tests for services
14+
- Drop official support for Python 2
15+
1316

1417
2021-06-12 0.24.0
1518
=================
@@ -18,6 +21,7 @@ in progress
1821
- [core] Make "functions" setting in configuration file optional
1922
- [ci] Build and publish Docker multi-platform images
2023

24+
2125
2021-06-08 0.23.1
2226
=================
2327

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ Requirements
215215
============
216216
You'll need at least the following components:
217217

218-
* Python. The program should work on Python 2, Python 3 and PyPy.
218+
* Python. The program should work on Python 3 and PyPy3.
219219
* An MQTT broker. We recommend Mosquitto_.
220220
* Some more Python modules to satisfy service dependencies defined in the ``setup.py`` file.
221221

mqttwarn/services/alexa-notify-me.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ def plugin(srv, item):
2929
"accessCode": item.addrs[0]
3030
})
3131

32-
requests.post(url="https://api.notifymyecho.com/v1/NotifyMe", data=body)
32+
response = requests.post(url="https://api.notifymyecho.com/v1/NotifyMe", data=body)
33+
response.raise_for_status()
3334

3435
srv.logging.debug("Successfully sent to NotifyMe service.")
3536

3637
except Exception as e:
37-
srv.logging.warning("Failed to send message to NotifyMe service." % e)
38+
srv.logging.warning("Failed to send message to NotifyMe service: %s" % e)
3839
return False
3940

4041
return True

mqttwarn/services/amqp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def plugin(srv, item):
3636

3737
srv.logging.debug("Successfully published AMQP notification")
3838
except Exception as e:
39-
srv.logging.warn("Error on AMQP publish to %s [%s/%s]: %s" % (item.target, exchange, routing_key, e))
39+
srv.logging.warning("Error on AMQP publish to %s [%s/%s]: %s" % (item.target, exchange, routing_key, e))
4040
return False
4141

4242
return True

mqttwarn/services/apns.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ def plugin(srv, item):
1919
try:
2020
cert_file, key_file = addrs
2121
except:
22-
srv.logging.warn("Incorrect service configuration")
22+
srv.logging.warning("Incorrect service configuration")
2323
return False
2424

25-
if 'payload' not in data or 'apns_token' not in data:
26-
srv.logging.warn("Cannot notify via APNS: payload or apns_token are missing")
25+
if 'apns_token' not in data:
26+
srv.logging.warning("Cannot notify via APNS: apns_token is missing")
2727
return False
2828

2929
apns_token = data['apns_token']
30-
payload = data['payload']
3130

3231
custom = {}
3332
try:
33+
payload = data['payload']
3434
mdata = json.loads(payload)
3535
if 'custom' in mdata:
3636
custom = mdata['custom']
@@ -42,4 +42,6 @@ def plugin(srv, item):
4242
pload = Payload(alert=text, custom=custom, sound="default", badge=1)
4343
apns.gateway_server.send_notification(apns_token, pload)
4444

45+
srv.logging.debug("Successfully published APNS notification to %s" % apns_token)
46+
4547
return True

mqttwarn/services/apprise.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def plugin(srv, item):
1616
addresses = item.addrs
1717

1818
if not addresses:
19-
srv.logging.warn("Skipped sending notification to Apprise %s, "
20-
"no addresses configured" % (item.target))
19+
srv.logging.warning("Skipped sending notification to Apprise %s, "
20+
"no addresses configured" % (item.target))
2121
return False
2222

2323
sender = item.config.get('sender')

mqttwarn/services/asterisk.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ def plugin(srv, item):
3636
srv.logging.info("Call {}".format(response))
3737
manager.logoff()
3838
except asterisk.manager.ManagerSocketException as e:
39-
srv.logging.error("Error connecting to the manage: {}".format(e))
39+
srv.logging.error("Error connecting to the manager: {}".format(e))
40+
return False
4041
except asterisk.manager.ManagerAuthException as e:
4142
srv.logging.error("Error logging in to the manager: {}".format(e))
43+
return False
4244
except asterisk.manager.ManagerException as e:
4345
srv.logging.error("Error: {}".format(e))
46+
return False
4447

48+
# Remember to clean up
4549
finally:
46-
# remember to clean up
4750
try:
4851
manager.close()
49-
except asterisk.manager.ManagerSocketException:
52+
except asterisk.manager.ManagerSocketException: # pragma: no cover
5053
pass
5154

5255
return True

mqttwarn/services/autoremote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def plugin(srv, item):
3636
srv.logging.debug("Successfully sent to autoremote service")
3737

3838
except Exception as e:
39-
srv.logging.warning("Failed to send message to autoremote service" % e)
39+
srv.logging.warning("Failed to send message to autoremote service: %s" % e)
4040
return False
4141

4242
return True

mqttwarn/services/carbon.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ def plugin(srv, item):
3535
srv.logging.error("target `carbon': cannot split string")
3636
return False
3737

38-
if len(parts) == 1:
38+
parts_count = len(parts)
39+
if parts_count == 1:
3940
metric_name = item.data.get('topic', 'ohno').replace('/', '.')
4041
value = parts[0]
4142
tics = int(time.time())
43+
elif parts_count == 2:
44+
metric_name = parts[0]
45+
value = parts[1]
46+
tics = int(time.time())
47+
elif parts_count == 3:
48+
metric_name = parts[0]
49+
value = parts[1]
50+
tics = int(parts[2])
4251
else:
43-
if len(parts) == 2:
44-
metric_name = parts[0]
45-
value = parts[1]
46-
tics = int(time.time())
47-
else:
48-
if len(parts) == 3:
49-
metric_name = parts[0]
50-
value = parts[1]
51-
tics = int(parts[2])
52+
srv.logging.error("target `carbon': error decoding message")
53+
return False
5254

5355
if metric_name.startswith('.'): # omit dot there caused by useless leading slash in topic
5456
metric_name = metric_name[1:]

0 commit comments

Comments
 (0)