Skip to content
Snippets Groups Projects
Commit 93f64487 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Fixes errors found during testing

parent 75e35f14
No related branches found
No related tags found
No related merge requests found
...@@ -26,10 +26,11 @@ ...@@ -26,10 +26,11 @@
# Python standard libs # Python standard libs
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
from yaml import load
# PIP installed libs # PIP installed libs
import pytest import pytest
from yaml import load
from pyramid import testing from pyramid import testing
from toscaparser.tosca_template import ToscaTemplate from toscaparser.tosca_template import ToscaTemplate
...@@ -136,4 +137,30 @@ class TestAlertsConfigurationAPI(object): ...@@ -136,4 +137,30 @@ class TestAlertsConfigurationAPI(object):
:param app_config: fixture for setUp/tearDown of the web service registry :param app_config: fixture for setUp/tearDown of the web service registry
""" """
pass test_data_path = join(ROOT_DIR, *["resources", "tosca", "test-data", "clmc-validator", "valid"])
for test_file_path in listdir(test_data_path):
request = testing.DummyRequest()
alert_spec_abs_path = join(test_data_path, test_file_path)
print(alert_spec_abs_path)
with open(alert_spec_abs_path) as alert_spec:
request.POST['alert-spec'] = FieldStorageMock(test_file_path, alert_spec) # a simple mock class is used to mimic the FieldStorage class
print(AlertsConfigurationAPI(request).post_alerts_specification())
break
class FieldStorageMock(object):
def __init__(self, filename, file):
"""
Used to mock the behaviour of the cgi.FieldStorage class - two attributes needed only to forward a file to the view.
:param filename: file name
:param file: file object
"""
self.filename = filename
self.file = file
...@@ -98,7 +98,7 @@ class TICKScriptTemplateFiller: ...@@ -98,7 +98,7 @@ class TICKScriptTemplateFiller:
@staticmethod @staticmethod
def _fill_threshold_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None, def _fill_threshold_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None,
comparison_operator=None, alert_period=None, topic_id=None, where_clause=None): comparison_operator=None, alert_period=None, topic_id=None, where_clause=None, **kwargs):
""" """
Creates a dictionary object ready to be posted to kapacitor to create a "threshold" task from template. Creates a dictionary object ready to be posted to kapacitor to create a "threshold" task from template.
...@@ -158,7 +158,7 @@ class TICKScriptTemplateFiller: ...@@ -158,7 +158,7 @@ class TICKScriptTemplateFiller:
@staticmethod @staticmethod
def _fill_relative_template_vars(db=None, measurement=None, field=None, critical_value=None, comparison_operator=None, def _fill_relative_template_vars(db=None, measurement=None, field=None, critical_value=None, comparison_operator=None,
alert_period=None, topic_id=None, where_clause=None): alert_period=None, topic_id=None, where_clause=None, **kwargs):
""" """
Creates a dictionary object ready to be posted to kapacitor to create a "relative" task from template. Creates a dictionary object ready to be posted to kapacitor to create a "relative" task from template.
...@@ -214,7 +214,7 @@ class TICKScriptTemplateFiller: ...@@ -214,7 +214,7 @@ class TICKScriptTemplateFiller:
return template_vars return template_vars
@staticmethod @staticmethod
def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, where_clause=None): def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, where_clause=None, **kwargs):
""" """
Creates a dictionary object ready to be posted to kapacitor to create a "deadman" task from template. Creates a dictionary object ready to be posted to kapacitor to create a "deadman" task from template.
...@@ -242,7 +242,7 @@ class TICKScriptTemplateFiller: ...@@ -242,7 +242,7 @@ class TICKScriptTemplateFiller:
"value": alert_period "value": alert_period
}, },
"throughputThreshold": { "throughputThreshold": {
"type": "int", "type": "float",
"value": critical_value "value": critical_value
}, },
"topicID": { "topicID": {
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
# Python standard libs # Python standard libs
import logging import logging
from json import dumps
from urllib.parse import urlparse
# PIP installed libs # PIP installed libs
from pyramid.httpexceptions import HTTPBadRequest from pyramid.httpexceptions import HTTPBadRequest
...@@ -97,7 +99,7 @@ class AlertsConfigurationAPI(object): ...@@ -97,7 +99,7 @@ class AlertsConfigurationAPI(object):
for trigger in policy.triggers: for trigger in policy.triggers:
event_id = trigger.name event_id = trigger.name
event_type = trigger.trigger_tpl["event_type"] event_type = trigger.trigger_tpl["event_type"]
template_id = "{0}_template".format(event_type) template_id = "{0}-template".format(event_type)
measurement, field = trigger.trigger_tpl["metric"].split(".") measurement, field = trigger.trigger_tpl["metric"].split(".")
condition = trigger.trigger_tpl["condition"] condition = trigger.trigger_tpl["condition"]
...@@ -109,7 +111,7 @@ class AlertsConfigurationAPI(object): ...@@ -109,7 +111,7 @@ class AlertsConfigurationAPI(object):
where_clause = None where_clause = None
if "resource_type" in trigger.trigger_tpl["condition"]: if "resource_type" in trigger.trigger_tpl["condition"]:
tags = condition["resource_type"] tags = condition["resource_type"]
where_clause = " AND ".join(map(lambda tag_name: '"{0}"=\'{1}\''.format(tag_name, tags[tag_name]), tags)) where_clause = " AND ".join(map(lambda tag_name: '"{0}"==\'{1}\''.format(tag_name, tags[tag_name]), tags))
comparison_operator = COMPARISON_OPERATORS[condition.get("comparison_operator", "gte")] # if not specified, use "gte" (>=) comparison_operator = COMPARISON_OPERATORS[condition.get("comparison_operator", "gte")] # if not specified, use "gte" (>=)
...@@ -134,10 +136,10 @@ class AlertsConfigurationAPI(object): ...@@ -134,10 +136,10 @@ class AlertsConfigurationAPI(object):
} }
# send the request and receive a response # send the request and receive a response
response = post(kapacitor_api_tasks_url, data=kapacitor_http_request_body) response = post(kapacitor_api_tasks_url, data=dumps(kapacitor_http_request_body))
response_content = response.json() response_content = response.json()
# log the response # log the response
log.info(response_content) log.info(response_content, response.status_code)
# exttranc http handlers # exttranc http handlers
http_handlers = trigger.trigger_tpl["action"]["implementation"] http_handlers = trigger.trigger_tpl["action"]["implementation"]
...@@ -145,11 +147,12 @@ class AlertsConfigurationAPI(object): ...@@ -145,11 +147,12 @@ class AlertsConfigurationAPI(object):
# subscribe all http handlers to the created topic # subscribe all http handlers to the created topic
kapacitor_api_handlers_url = "http://localhost:9092/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id) kapacitor_api_handlers_url = "http://localhost:9092/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id)
for http_handler_url in http_handlers: for http_handler_url in http_handlers:
handler_id = "{0}.{1}.{2}".format(policy.name, event_id, http_handler_url) http_handler_host = urlparse(http_handler_url).netloc
handler_id = "{0}.{1}.{2}".format(policy.name, event_id, http_handler_host)
kapacitor_http_request_body = fill_http_post_handler_vars(handler_id, http_handler_url) kapacitor_http_request_body = fill_http_post_handler_vars(handler_id, http_handler_url)
response = post(kapacitor_api_handlers_url, data=kapacitor_http_request_body) response = post(kapacitor_api_handlers_url, data=dumps(kapacitor_http_request_body))
response_content = response.json() response_content = response.json()
log.info(response_content) log.info(response_content, response.status_code)
return {"msg": "Alerts specification has been successfully validated and configured", "service_function_chain_id": sfc, return {"msg": "Alerts specification has been successfully validated and configured", "service_function_chain_id": sfc,
"service_function_chain_instance_id": sfc_instance} "service_function_chain_instance_id": sfc_instance}
...@@ -4,7 +4,7 @@ var rp = 'autogen' // default value for the retention policy ...@@ -4,7 +4,7 @@ var rp = 'autogen' // default value for the retention policy
var measurement string var measurement string
var whereClause = lambda: True // default value is a function which returns TRUE, hence no filtering of the query result var whereClause = lambda: TRUE // default value is a function which returns TRUE, hence no filtering of the query result
var messageValue = 'TRUE' // default value is TRUE, as this is what SFEMC expects as a notification for an event rule var messageValue = 'TRUE' // default value is TRUE, as this is what SFEMC expects as a notification for an event rule
......
...@@ -6,7 +6,7 @@ var measurement string ...@@ -6,7 +6,7 @@ var measurement string
var selectLambda lambda // must be a lambda specifying the field to select e.g. "requests" var selectLambda lambda // must be a lambda specifying the field to select e.g. "requests"
var whereClause = lambda: True // default value is a function which returns TRUE, hence no filtering of the query result var whereClause = lambda: TRUE // default value is a function which returns TRUE, hence no filtering of the query result
var messageValue = 'TRUE' // default value is TRUE, as this is what SFEMC expects as a notification for an event rule var messageValue = 'TRUE' // default value is TRUE, as this is what SFEMC expects as a notification for an event rule
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment