diff --git a/src/service/clmcservice/alertsapi/tests.py b/src/service/clmcservice/alertsapi/tests.py index 2c2066f153ab0e36d6c93bb2f8f4734dc77de6a3..11984dcd9727ea95406cb9b260eb85e0873881e2 100644 --- a/src/service/clmcservice/alertsapi/tests.py +++ b/src/service/clmcservice/alertsapi/tests.py @@ -26,10 +26,11 @@ # Python standard libs from os import listdir from os.path import isfile, join -from yaml import load + # PIP installed libs import pytest +from yaml import load from pyramid import testing from toscaparser.tosca_template import ToscaTemplate @@ -136,4 +137,30 @@ class TestAlertsConfigurationAPI(object): :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 diff --git a/src/service/clmcservice/alertsapi/utilities.py b/src/service/clmcservice/alertsapi/utilities.py index 7cec89edaa928bf678c69a3cb96f63624403028d..4bb0d2fd428c2ae69e69211efac8aa863efb4ea6 100644 --- a/src/service/clmcservice/alertsapi/utilities.py +++ b/src/service/clmcservice/alertsapi/utilities.py @@ -98,7 +98,7 @@ class TICKScriptTemplateFiller: @staticmethod 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. @@ -158,7 +158,7 @@ class TICKScriptTemplateFiller: @staticmethod 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. @@ -214,7 +214,7 @@ class TICKScriptTemplateFiller: return template_vars @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. @@ -242,7 +242,7 @@ class TICKScriptTemplateFiller: "value": alert_period }, "throughputThreshold": { - "type": "int", + "type": "float", "value": critical_value }, "topicID": { diff --git a/src/service/clmcservice/alertsapi/views.py b/src/service/clmcservice/alertsapi/views.py index 0053d7fa1866cdca04a9a400d0d437a42f530b1e..70cbf7faa9f7fc2bf8b15d82b6fcf3bd3949122b 100644 --- a/src/service/clmcservice/alertsapi/views.py +++ b/src/service/clmcservice/alertsapi/views.py @@ -24,6 +24,8 @@ # Python standard libs import logging +from json import dumps +from urllib.parse import urlparse # PIP installed libs from pyramid.httpexceptions import HTTPBadRequest @@ -97,7 +99,7 @@ class AlertsConfigurationAPI(object): for trigger in policy.triggers: event_id = trigger.name 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(".") condition = trigger.trigger_tpl["condition"] @@ -109,7 +111,7 @@ class AlertsConfigurationAPI(object): where_clause = None if "resource_type" in trigger.trigger_tpl["condition"]: 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" (>=) @@ -134,10 +136,10 @@ class AlertsConfigurationAPI(object): } # 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() # log the response - log.info(response_content) + log.info(response_content, response.status_code) # exttranc http handlers http_handlers = trigger.trigger_tpl["action"]["implementation"] @@ -145,11 +147,12 @@ class AlertsConfigurationAPI(object): # subscribe all http handlers to the created topic kapacitor_api_handlers_url = "http://localhost:9092/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id) 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) - 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() - 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, "service_function_chain_instance_id": sfc_instance} diff --git a/src/service/clmcservice/resources/TICKscript/deadman_template.tick b/src/service/clmcservice/resources/TICKscript/deadman-template.tick similarity index 92% rename from src/service/clmcservice/resources/TICKscript/deadman_template.tick rename to src/service/clmcservice/resources/TICKscript/deadman-template.tick index a772b8199dc6737f2e2c4e4dcb87237380305c52..e28851c7c00d7cede3332c1acebdadcff5d50660 100644 --- a/src/service/clmcservice/resources/TICKscript/deadman_template.tick +++ b/src/service/clmcservice/resources/TICKscript/deadman-template.tick @@ -4,7 +4,7 @@ var rp = 'autogen' // default value for the retention policy 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 diff --git a/src/service/clmcservice/resources/TICKscript/relative_template.tick b/src/service/clmcservice/resources/TICKscript/relative-template.tick similarity index 94% rename from src/service/clmcservice/resources/TICKscript/relative_template.tick rename to src/service/clmcservice/resources/TICKscript/relative-template.tick index e158ab9d2cd6c6a15dec29f2e30c1d895c7dfef0..d892a9677d1520decba2cfd5504a06761e99c15d 100644 --- a/src/service/clmcservice/resources/TICKscript/relative_template.tick +++ b/src/service/clmcservice/resources/TICKscript/relative-template.tick @@ -6,7 +6,7 @@ var measurement string 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 diff --git a/src/service/clmcservice/resources/TICKscript/threshold_template.tick b/src/service/clmcservice/resources/TICKscript/threshold-template.tick similarity index 100% rename from src/service/clmcservice/resources/TICKscript/threshold_template.tick rename to src/service/clmcservice/resources/TICKscript/threshold-template.tick