Commit 69bf449f authored by Łukasz Nowak's avatar Łukasz Nowak

support checking the http response headers

See merge request nexedi/slapos.toolbox!84
parents 41107678 b63368f4
Pipeline #11445 failed with stage
in 0 seconds
...@@ -162,6 +162,7 @@ class RunPromise(GenericPromise): ...@@ -162,6 +162,7 @@ class RunPromise(GenericPromise):
url = self.getConfig('url') url = self.getConfig('url')
status_code = self.getConfig('status-code') status_code = self.getConfig('status-code')
ip_list = self.getConfig('ip-list', '').split() ip_list = self.getConfig('ip-list', '').split()
http_header_dict = json.loads(self.getConfig('http-header-dict', '{}'))
entry_list = [q for q in self.surykatka_json[key] if q['url'] == url] entry_list = [q for q in self.surykatka_json[key] if q['url'] == url]
if len(entry_list) == 0: if len(entry_list) == 0:
...@@ -181,6 +182,13 @@ class RunPromise(GenericPromise): ...@@ -181,6 +182,13 @@ class RunPromise(GenericPromise):
'IP %s got status code %s instead of %s' % ( 'IP %s got status code %s instead of %s' % (
entry['ip'], status_code_explanation, status_code)) entry['ip'], status_code_explanation, status_code))
error = True error = True
if http_header_dict and http_header_dict != entry['http_header_dict']:
appendError(
'HTTP Header dict was %s instead of %s' % (
json.dumps(entry['http_header_dict'], sort_keys=True),
json.dumps(http_header_dict, sort_keys=True),
))
error = True
db_ip_list = [q['ip'] for q in entry_list] db_ip_list = [q['ip'] for q in entry_list]
if len(ip_list): if len(ip_list):
if set(ip_list) != set(db_ip_list): if set(ip_list) != set(db_ip_list):
...@@ -190,12 +198,14 @@ class RunPromise(GenericPromise): ...@@ -190,12 +198,14 @@ class RunPromise(GenericPromise):
error = True error = True
if error: if error:
return return
info_message = '%s: OK with status code %s' % (key, status_code)
if http_header_dict:
info_message += ' and HTTP Header dict %s' % (
json.dumps(http_header_dict, sort_keys=True),
)
if len(ip_list) > 0: if len(ip_list) > 0:
self.appendInfoMessage('%s: OK status code %s on IPs %s' % ( info_message += ' on IPs %s' % (' '.join(ip_list))
key, status_code, ' '.join(ip_list))) self.appendInfoMessage(info_message)
else:
self.appendInfoMessage('%s: OK with status code %s' % (
key, status_code))
def senseElapsedTime(self): def senseElapsedTime(self):
key = 'elapsed_time' key = 'elapsed_time'
......
...@@ -284,7 +284,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -284,7 +284,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run() self.launcher.run()
self.assertPassedMessage( self.assertPassedMessage(
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : http_query: OK status code 302 on IPs " "https://www.erp5.com/ : http_query: OK with status code 302 on IPs "
"127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will " "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will "
"expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days" "expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days"
) )
...@@ -345,7 +345,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -345,7 +345,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run() self.launcher.run()
self.assertPassedMessage( self.assertPassedMessage(
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : http_query: OK status code 302 on IPs " "https://www.erp5.com/ : http_query: OK with status code 302 on IPs "
"127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will " "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will "
"expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days " "expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days "
"elapsed_time: OK IP 127.0.0.1 replied in 4.00s which is shorter than " "elapsed_time: OK IP 127.0.0.1 replied in 4.00s which is shorter than "
...@@ -412,7 +412,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -412,7 +412,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : elapsed_time: ERROR IP 127.0.0.1 replied in " "https://www.erp5.com/ : elapsed_time: ERROR IP 127.0.0.1 replied in "
"6.00s which is longer than maximum 5.00s elapsed_time: ERROR IP " "6.00s which is longer than maximum 5.00s elapsed_time: ERROR IP "
"127.0.0.2 failed to reply http_query: OK status code 302 on IPs " "127.0.0.2 failed to reply http_query: OK with status code 302 on IPs "
"127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will " "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will "
"expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days" "expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days"
) )
...@@ -470,7 +470,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -470,7 +470,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run() self.launcher.run()
self.assertPassedMessage( self.assertPassedMessage(
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : http_query: OK status code 302 on IPs " "https://www.erp5.com/ : http_query: OK with status code 302 on IPs "
"127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will " "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate on 127.0.0.1 will "
"expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days" "expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days"
) )
...@@ -515,10 +515,78 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -515,10 +515,78 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run() self.launcher.run()
self.assertPassedMessage( self.assertPassedMessage(
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"http://www.erp5.com/ : http_query: OK status code 302 on IPs 127.0.0.1 " "http://www.erp5.com/ : http_query: OK with status code 302 on IPs 127.0.0.1 "
"127.0.0.2" "127.0.0.2"
) )
def test_http_with_header_dict(self):
self.writeSurykatkaPromise(
{
'report': 'http_query',
'json-file': self.json_file,
'url': 'http://www.erp5.com/',
'status-code': '200',
'http-header-dict': '{"Vary": "Accept-Encoding", "Cache-Control": "max-age=300, public"}',
'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
}
)
self.writeSurykatkaJson("""{
"http_query": [
{
"date": "Wed, 11 Dec 2019 09:35:28 -0000",
"ip": "176.31.129.213",
"http_header_dict": {"Vary": "Accept-Encoding", "Cache-Control": "max-age=300, public"},
"status_code": 200,
"url": "http://www.erp5.com/"
}
],
"ssl_certificate": [
]
}
""")
self.configureLauncher(enable_anomaly=True)
self.launcher.run()
self.assertPassedMessage(
self.getPromiseResult(self.promise_name),
"http://www.erp5.com/ : http_query: OK with status code 200 "
"and HTTP Header dict {\"Cache-Control\": \"max-age=300, public\", \"Vary\": \"Accept-Encoding\"}"
)
def test_http_with_bad_header_dict(self):
self.writeSurykatkaPromise(
{
'report': 'http_query',
'json-file': self.json_file,
'url': 'http://www.erp5.com/',
'status-code': '200',
'http-header-dict': '{"Vary": "Accept-Encoding", "Cache-Control": "max-age=300, public"}',
'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
}
)
self.writeSurykatkaJson("""{
"http_query": [
{
"date": "Wed, 11 Dec 2019 09:35:28 -0000",
"ip": "176.31.129.213",
"http_header_dict": {"Vary": "Accept-Encoding,Cookie", "Cache-Control": "max-age=300, public"},
"status_code": 200,
"url": "http://www.erp5.com/"
}
],
"ssl_certificate": [
]
}
""")
self.configureLauncher(enable_anomaly=True)
with self.assertRaises(PromiseError):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http://www.erp5.com/ : http_query: ERROR HTTP Header dict was "
"{\"Cache-Control\": \"max-age=300, public\", \"Vary\": \"Accept-Encoding,Cookie\"} "
"instead of {\"Cache-Control\": \"max-age=300, public\", \"Vary\": \"Accept-Encoding\"}"
)
def test_no_ip_list(self): def test_no_ip_list(self):
self.writeSurykatkaPromise( self.writeSurykatkaPromise(
{ {
...@@ -814,7 +882,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -814,7 +882,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.assertFailedMessage( self.assertFailedMessage(
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : ssl_certificate: ERROR No data http_query: " "https://www.erp5.com/ : ssl_certificate: ERROR No data http_query: "
"OK status code 302 on IPs 127.0.0.1 127.0.0.2" "OK with status code 302 on IPs 127.0.0.1 127.0.0.2"
) )
def test_no_ssl_certificate(self): def test_no_ssl_certificate(self):
...@@ -858,7 +926,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin): ...@@ -858,7 +926,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.getPromiseResult(self.promise_name), self.getPromiseResult(self.promise_name),
"https://www.erp5.com/ : ssl_certificate: ERROR No key " "https://www.erp5.com/ : ssl_certificate: ERROR No key "
"'ssl_certificate'. If the error persist, please update surykatka. " "'ssl_certificate'. If the error persist, please update surykatka. "
"http_query: OK status code 302 on IPs 127.0.0.1 127.0.0.2" "http_query: OK with status code 302 on IPs 127.0.0.1 127.0.0.2"
) )
def test_bad_code(self): def test_bad_code(self):
......
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