From 174686a98052890c845d42916470726fa1264717 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Sun, 16 Feb 2020 10:04:47 +0100 Subject: [PATCH] Move test to separat file --- .travis.yml | 11 ++ check_http_json.py | 198 +------------------------------- test/__init__.py | 0 test/test_check_http_json.py | 213 +++++++++++++++++++++++++++++++++++ 4 files changed, 226 insertions(+), 196 deletions(-) create mode 100644 .travis.yml create mode 100644 test/__init__.py create mode 100644 test/test_check_http_json.py diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..32c17af --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: python +python: + - "3.5" + - "3.6" + - "3.7" +install: + - pip install coverage +script: + - python -m unittest discover + - coverage run -m unittest discover + - coverage report -m --include check_http_json.py diff --git a/check_http_json.py b/check_http_json.py index b89ed65..750c65c 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -24,8 +24,8 @@ WARNING_CODE = 1 CRITICAL_CODE = 2 UNKNOWN_CODE = 3 -__version__ = '1.4.0' -__version_date__ = '2019-05-09' +__version__ = '2.0.0' +__version_date__ = '2022-02-16' class NagiosHelper: """Help with Nagios specific status string formatting.""" @@ -484,200 +484,6 @@ def debugPrint(debug_flag, message, pretty_flag=False): else: print(message) -if __name__ == "__main__" and len(sys.argv) >= 2 and sys.argv[1] == 'UnitTest': - import unittest - - class RulesHelper: - separator = '.' - debug = False - key_threshold_warning = None - key_value_list = None - key_value_list_not = None - key_list = None - key_threshold_critical = None - key_value_list_critical = None - key_value_list_not_critical = None - key_value_list_unknown = None - key_list_critical = None - metric_list = None - - def dash_m(self, data): - self.metric_list = data - return self - - def dash_e(self, data): - self.key_list = data - return self - - def dash_E(self, data): - self.key_list_critical = data - return self - - def dash_q(self, data): - self.key_value_list = data - return self - - def dash_Q(self, data): - self.key_value_list_critical = data - return self - - def dash_y(self, data): - self.key_value_list_not = data - return self - - def dash_Y(self, data): - self.key_value_list_not_critical = data - return self - - def dash_w(self, data): - self.key_threshold_warning = data - return self - - def dash_c(self, data): - self.key_threshold_critical = data - return self - - class UnitTest(unittest.TestCase): - rules = RulesHelper() - - def check_data(self, args, jsondata, code): - data = json.loads(jsondata) - nagios = NagiosHelper() - processor = JsonRuleProcessor(data, args) - nagios.append_warning(processor.checkWarning()) - nagios.append_critical(processor.checkCritical()) - nagios.append_metrics(processor.checkMetrics()) - self.assertEqual(code, nagios.getCode()) - - def test_metrics(self): - self.check_data(RulesHelper().dash_m(['metric,,1:4,1:5']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_m(['metric,,1:5,1:4']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_m(['metric,,1:5,1:5,6,10']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_m(['metric,,1:5,1:5,1,4']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_m(['metric,s,@1:4,@6:10,1,10']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_m(['(*).value,s,1:5,1:5']), - '[{"value": 5},{"value": 100}]', CRITICAL_CODE) - - def test_exists(self): - self.check_data(RulesHelper().dash_e(['nothere']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_E(['nothere']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_e(['metric']), - '{"metric": 5}', OK_CODE) - - def test_equality(self): - self.check_data(RulesHelper().dash_q(['metric,6']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_Q(['metric,6']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_q(['metric,5']), - '{"metric": 5}', OK_CODE) - - def test_non_equality(self): - self.check_data(RulesHelper().dash_y(['metric,6']), - '{"metric": 6}', WARNING_CODE) - self.check_data(RulesHelper().dash_Y(['metric,6']), - '{"metric": 6}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_y(['metric,5']), - '{"metric": 6}', OK_CODE) - - def test_warning_thresholds(self): - self.check_data(RulesHelper().dash_w(['metric,5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,5:']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,~:5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,1:5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,@5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,@5:']), - '{"metric": 4}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,@~:5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,@1:5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_w(['metric,5']), - '{"metric": 6}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,5:']), - '{"metric": 4}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,~:5']), - '{"metric": 6}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,1:5']), - '{"metric": 6}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,@5']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,@5:']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,@~:5']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['metric,@1:5']), - '{"metric": 5}', WARNING_CODE) - self.check_data(RulesHelper().dash_w(['(*).value,@1:5']), - '[{"value": 5},{"value": 1000}]', WARNING_CODE) - - def test_critical_thresholds(self): - self.check_data(RulesHelper().dash_c(['metric,5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,5:']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,~:5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,1:5']), - '{"metric": 5}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,@5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,@5:']), - '{"metric": 4}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,@~:5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,@1:5']), - '{"metric": 6}', OK_CODE) - self.check_data(RulesHelper().dash_c(['metric,5']), - '{"metric": 6}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,5:']), - '{"metric": 4}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,~:5']), - '{"metric": 6}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,1:5']), - '{"metric": 6}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,@5']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,@5:']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,@~:5']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['metric,@1:5']), - '{"metric": 5}', CRITICAL_CODE) - self.check_data(RulesHelper().dash_c(['(*).value,@1:5']), - '[{"value": 5},{"value": 1000}]', CRITICAL_CODE) - - def test_separator(self): - rules = RulesHelper() - rules.separator = '_' - self.check_data( - rules.dash_q( - ['(0)_gauges_jvm.buffers.direct.capacity(1)_value,1234']), - '''[{ "gauges": { "jvm.buffers.direct.capacity": [ - {"value": 215415},{"value": 1234}]}}]''', - OK_CODE) - self.check_data( - rules.dash_q( - ['(*)_gauges_jvm.buffers.direct.capacity(1)_value,1234']), - '''[{ "gauges": { "jvm.buffers.direct.capacity": [ - {"value": 215415},{"value": 1234}]}}, - { "gauges": { "jvm.buffers.direct.capacity": [ - {"value": 215415},{"value": 1235}]}}]''', - WARNING_CODE) - unittest.main() - exit(0) """Program entry point""" if __name__ == "__main__": diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_check_http_json.py b/test/test_check_http_json.py new file mode 100644 index 0000000..2485a91 --- /dev/null +++ b/test/test_check_http_json.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python3 + + +import json +import unittest +from unittest.mock import patch +import sys + +sys.path.append('..') + +from check_http_json import * + + +OK_CODE = 0 +WARNING_CODE = 1 +CRITICAL_CODE = 2 +UNKNOWN_CODE = 3 + + +class RulesHelper: + separator = '.' + debug = False + key_threshold_warning = None + key_value_list = None + key_value_list_not = None + key_list = None + key_threshold_critical = None + key_value_list_critical = None + key_value_list_not_critical = None + key_value_list_unknown = None + key_list_critical = None + metric_list = None + + def dash_m(self, data): + self.metric_list = data + return self + + def dash_e(self, data): + self.key_list = data + return self + + def dash_E(self, data): + self.key_list_critical = data + return self + + def dash_q(self, data): + self.key_value_list = data + return self + + def dash_Q(self, data): + self.key_value_list_critical = data + return self + + def dash_y(self, data): + self.key_value_list_not = data + return self + + def dash_Y(self, data): + self.key_value_list_not_critical = data + return self + + def dash_w(self, data): + self.key_threshold_warning = data + return self + + def dash_c(self, data): + self.key_threshold_critical = data + return self + + +class UtilTest(unittest.TestCase): + """ + Tests for the util fucntions + """ + + rules = RulesHelper() + + def check_data(self, args, jsondata, code): + data = json.loads(jsondata) + nagios = NagiosHelper() + processor = JsonRuleProcessor(data, args) + nagios.append_warning(processor.checkWarning()) + nagios.append_critical(processor.checkCritical()) + nagios.append_metrics(processor.checkMetrics()) + self.assertEqual(code, nagios.getCode()) + + def test_metrics(self): + self.check_data(RulesHelper().dash_m(['metric,,1:4,1:5']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_m(['metric,,1:5,1:4']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_m(['metric,,1:5,1:5,6,10']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_m(['metric,,1:5,1:5,1,4']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_m(['metric,s,@1:4,@6:10,1,10']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_m(['(*).value,s,1:5,1:5']), + '[{"value": 5},{"value": 100}]', CRITICAL_CODE) + + def test_exists(self): + self.check_data(RulesHelper().dash_e(['nothere']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_E(['nothere']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_e(['metric']), + '{"metric": 5}', OK_CODE) + + def test_equality(self): + self.check_data(RulesHelper().dash_q(['metric,6']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_Q(['metric,6']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_q(['metric,5']), + '{"metric": 5}', OK_CODE) + + def test_non_equality(self): + self.check_data(RulesHelper().dash_y(['metric,6']), + '{"metric": 6}', WARNING_CODE) + self.check_data(RulesHelper().dash_Y(['metric,6']), + '{"metric": 6}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_y(['metric,5']), + '{"metric": 6}', OK_CODE) + + def test_warning_thresholds(self): + self.check_data(RulesHelper().dash_w(['metric,5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,5:']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,~:5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,1:5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,@5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,@5:']), + '{"metric": 4}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,@~:5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,@1:5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_w(['metric,5']), + '{"metric": 6}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,5:']), + '{"metric": 4}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,~:5']), + '{"metric": 6}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,1:5']), + '{"metric": 6}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,@5']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,@5:']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,@~:5']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['metric,@1:5']), + '{"metric": 5}', WARNING_CODE) + self.check_data(RulesHelper().dash_w(['(*).value,@1:5']), + '[{"value": 5},{"value": 1000}]', WARNING_CODE) + + def test_critical_thresholds(self): + self.check_data(RulesHelper().dash_c(['metric,5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,5:']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,~:5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,1:5']), + '{"metric": 5}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,@5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,@5:']), + '{"metric": 4}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,@~:5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,@1:5']), + '{"metric": 6}', OK_CODE) + self.check_data(RulesHelper().dash_c(['metric,5']), + '{"metric": 6}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,5:']), + '{"metric": 4}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,~:5']), + '{"metric": 6}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,1:5']), + '{"metric": 6}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,@5']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,@5:']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,@~:5']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['metric,@1:5']), + '{"metric": 5}', CRITICAL_CODE) + self.check_data(RulesHelper().dash_c(['(*).value,@1:5']), + '[{"value": 5},{"value": 1000}]', CRITICAL_CODE) + + def test_separator(self): + rules = RulesHelper() + rules.separator = '_' + self.check_data( + rules.dash_q( + ['(0)_gauges_jvm.buffers.direct.capacity(1)_value,1234']), + '''[{ "gauges": { "jvm.buffers.direct.capacity": [ + {"value": 215415},{"value": 1234}]}}]''', + OK_CODE) + self.check_data( + rules.dash_q( + ['(*)_gauges_jvm.buffers.direct.capacity(1)_value,1234']), + '''[{ "gauges": { "jvm.buffers.direct.capacity": [ + {"value": 215415},{"value": 1234}]}}, + { "gauges": { "jvm.buffers.direct.capacity": [ + {"value": 215415},{"value": 1235}]}}]''', + WARNING_CODE)