From 2c98e840e82c398ba28485cd5feda5c9594fd689 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Sun, 15 Mar 2020 10:26:46 +0100 Subject: [PATCH] Extend unittest coverage --- check_http_json.py | 2 +- test/test_check_http_json.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/check_http_json.py b/check_http_json.py index bf1becd..80a4b7b 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -169,7 +169,7 @@ class JsonHelper: if key.find(self.arrayOpener) != -1: return self.getSubArrayElement(key, data) else: - if key in data: + if type(data) == dict and key in data: return data[key] else: return (None, 'not_found') diff --git a/test/test_check_http_json.py b/test/test_check_http_json.py index 710cb46..6320c60 100644 --- a/test/test_check_http_json.py +++ b/test/test_check_http_json.py @@ -255,3 +255,20 @@ class UtilTest(unittest.TestCase): # This should not throw a KeyError data = '{}' self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing']), data, WARNING_CODE) + + def test_subelem(self): + + rules = RulesHelper() + data = '{"foo": {"foo": {"foo": "bar"}}}' + + self.check_data(rules.dash_E(['foo.foo.foo.foo.foo']), data, CRITICAL_CODE) + + def test_subarrayelem_missing_elem(self): + + rules = RulesHelper() + data = '[{"capacity": {"value": 1000}},{"capacity": {"value": 2200}}]' + + self.check_data(rules.dash_E(['(*).capacity.value']), data, OK_CODE) + self.check_data(rules.dash_E(['(*).capacity.value.too_deep']), data, CRITICAL_CODE) + # Should not throw keyerror + self.check_data(rules.dash_E(['foo']), data, CRITICAL_CODE)