Commit c58eff92 authored by Łukasz Nowak's avatar Łukasz Nowak

check_surykatka_json: Rewrite tests and improve code

Tests are rewritten for easier coverage of all cases, including all-ok case.
In the same time code has been improved while detecting inconsistencies.
parent b904068c
...@@ -107,9 +107,9 @@ class RunPromise(GenericPromise): ...@@ -107,9 +107,9 @@ class RunPromise(GenericPromise):
self.appendError('url is incorrect') self.appendError('url is incorrect')
return return
if key not in self.surykatka_json: if key not in self.surykatka_json:
self.appendError( self.appendError("%r not in %r" % (key, self.json_file))
'No key %r. If the error persist, please update surykatka.' % (key,))
return return
entry_list = [ entry_list = [
q for q in self.surykatka_json[key] if q['hostname'] == hostname] q for q in self.surykatka_json[key] if q['hostname'] == hostname]
if len(entry_list) == 0: if len(entry_list) == 0:
...@@ -169,7 +169,7 @@ class RunPromise(GenericPromise): ...@@ -169,7 +169,7 @@ class RunPromise(GenericPromise):
if http_header_dict: if http_header_dict:
if http_header_dict != entry['http_header_dict']: if http_header_dict != entry['http_header_dict']:
self.appendError( self.appendError(
'IP %s expected HTTP Header %s != of %s' % ( 'IP %s expected HTTP Header %s != %s' % (
entry['ip'], entry['ip'],
json.dumps(http_header_dict, sort_keys=True), json.dumps(http_header_dict, sort_keys=True),
json.dumps(entry['http_header_dict'], sort_keys=True))) json.dumps(entry['http_header_dict'], sort_keys=True)))
...@@ -193,11 +193,11 @@ class RunPromise(GenericPromise): ...@@ -193,11 +193,11 @@ class RunPromise(GenericPromise):
entry_list = [ entry_list = [
q for q in self.surykatka_json[key] q for q in self.surykatka_json[key]
if q['domain'] == hostname and q['rdtype'] == 'A'] if q['domain'] == hostname and q['rdtype'] == 'A']
if len(entry_list) == 0:
self.appendError('No data')
return
if len(ip_set): if len(ip_set):
if len(entry_list) == 0:
self.appendError('No data')
return
for entry in sorted(entry_list, key=operator.itemgetter('resolver_ip')): for entry in sorted(entry_list, key=operator.itemgetter('resolver_ip')):
response_ip_set = set([ response_ip_set = set([
q.strip() for q in entry['response'].split(",") if q.strip()]) q.strip() for q in entry['response'].split(",") if q.strip()])
...@@ -232,6 +232,9 @@ class RunPromise(GenericPromise): ...@@ -232,6 +232,9 @@ class RunPromise(GenericPromise):
else: else:
port = 80 port = 80
ip_set = set(self.getConfig('ip-list', '').split()) ip_set = set(self.getConfig('ip-list', '').split())
if len(ip_set) == 0:
self.appendOk('No check configured')
return
entry_list = [ entry_list = [
q for q in self.surykatka_json[key] q for q in self.surykatka_json[key]
...@@ -240,22 +243,19 @@ class RunPromise(GenericPromise): ...@@ -240,22 +243,19 @@ class RunPromise(GenericPromise):
if len(entry_list) == 0: if len(entry_list) == 0:
self.appendError('No data') self.appendError('No data')
return return
if len(ip_set) > 0: for ip in sorted(ip_set):
for ip in sorted(ip_set): ok = False
ok = False for entry in sorted(entry_list, key=operator.itemgetter('ip')):
for entry in sorted(entry_list, key=operator.itemgetter('ip')): if entry['ip'] == ip:
if entry['ip'] == ip: if entry['state'] == 'closed':
if entry['state'] == 'closed': ok = False
ok = False break
break if entry['state'] == 'open':
if entry['state'] == 'open': ok = True
ok = True if ok:
if ok: self.appendOk('IP %s:%s' % (ip, port))
self.appendOk('IP %s:%s' % (ip, port)) else:
else: self.appendError('IP %s:%s' % (ip, port))
self.appendError('IP %s:%s' % (ip, port))
else:
self.appendOk('No check configured')
def senseElapsedTime(self): def senseElapsedTime(self):
key = 'elapsed_time' key = 'elapsed_time'
...@@ -263,9 +263,7 @@ class RunPromise(GenericPromise): ...@@ -263,9 +263,7 @@ class RunPromise(GenericPromise):
surykatka_key = 'http_query' surykatka_key = 'http_query'
if surykatka_key not in self.surykatka_json: if surykatka_key not in self.surykatka_json:
self.appendError( self.appendError("%r not in %r" % (surykatka_key, self.json_file))
'No key %r. If the error persist, please update surykatka.' % (
surykatka_key,))
return return
url = self.getConfig('url') url = self.getConfig('url')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment