diff --git a/check_smseagle b/check_smseagle index f4e7166..8eaae26 100755 --- a/check_smseagle +++ b/check_smseagle @@ -19,7 +19,6 @@ from argparse import ArgumentParser from urllib.parse import urljoin -import json import os import sys import urllib3 @@ -150,7 +149,7 @@ def get_strength(response): """ Parse response and returns signal_strength of given modem """ - data = json.loads(response.json()) + data = response.json() return data["signal_strength"] @@ -166,7 +165,7 @@ def main(args): try: strength = get_strength(response) - except ValueError as parse_exc: + except Exception as parse_exc: # pylint: disable=broad-except print("[UNKNOWN] - Couldn't evaluate the GSM signal strength", parse_exc) return UNKNOWN diff --git a/test_check_smseagle.py b/test_check_smseagle.py index 40c2a1f..7ce12a4 100644 --- a/test_check_smseagle.py +++ b/test_check_smseagle.py @@ -4,6 +4,7 @@ import unittest.mock as mock import sys import os +import json import check_smseagle @@ -120,9 +121,9 @@ def test_main_ok(self, mock_req): # Mock object for HTTP Response r = mock.MagicMock() # Mocking the JSON method on the response - r.json.return_value = """ + r.json.return_value = json.loads(""" {"modem_no":1,"signal_strength":66} - """ + """) # Set mock HTTP Reposonse as mock_request return value mock_req.return_value = r @@ -133,9 +134,9 @@ def test_main_ok(self, mock_req): @mock.patch('check_smseagle.make_request') def test_main_warn(self, mock_req): r = mock.MagicMock() - r.json.return_value = """ + r.json.return_value = json.loads(""" {"modem_no":1,"signal_strength":9} - """ + """) mock_req.return_value = r args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1']) @@ -145,9 +146,9 @@ def test_main_warn(self, mock_req): @mock.patch('check_smseagle.make_request') def test_main_critical(self, mock_req): r = mock.MagicMock() - r.json.return_value = """ + r.json.return_value = json.loads(""" {"modem_no":1,"signal_strength":1} - """ + """) mock_req.return_value = r args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1']) @@ -158,9 +159,7 @@ def test_main_critical(self, mock_req): @mock.patch('check_smseagle.make_request') def test_main_unknown(self, mock_req): r = mock.MagicMock() - r.json.return_value = """ - ¯\_ (ツ)_/¯ - """ + r.json.side_effect=KeyError('OHNO') mock_req.return_value = r args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1'])