From f7b3f0c967413b2cd2b0c1473014d2e7bccd5299 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Tue, 21 Aug 2018 09:33:40 +0100
Subject: [PATCH] Updates tosca-parser test data

---
 src/service/clmcservice/alertsapi/tests.py    |  8 ++-
 .../valid/alerts_test_config-1.yaml           | 57 +++++++++++++++++++
 .../invalid/alerts_test_config-2.yaml         | 24 ++++----
 .../valid/alerts_test_config-1.yaml           | 54 +++++++++++++++++-
 .../valid/alerts_test_config-2.yaml           | 16 +++++-
 .../valid/alerts_test_config-4.yaml           | 19 ++++++-
 6 files changed, 161 insertions(+), 17 deletions(-)

diff --git a/src/service/clmcservice/alertsapi/tests.py b/src/service/clmcservice/alertsapi/tests.py
index 5b294ac..2c2066f 100644
--- a/src/service/clmcservice/alertsapi/tests.py
+++ b/src/service/clmcservice/alertsapi/tests.py
@@ -34,7 +34,8 @@ from pyramid import testing
 from toscaparser.tosca_template import ToscaTemplate
 
 # CLMC-service imports
-from clmcservice.alertsapi.utilities import adjust_tosca_definitions_import, validate_clmc_alerts_specification
+from clmcservice.alertsapi.utilities import adjust_tosca_definitions_import
+from clmcservice.alertsapi.alerts_specification_schema import validate_clmc_alerts_specification
 from clmcservice.alertsapi.views import AlertsConfigurationAPI
 from clmcservice import ROOT_DIR
 
@@ -127,6 +128,11 @@ class TestAlertsConfigurationAPI(object):
         """
         Tests the POST API endpoint of the alerts configuration API responsible for receiving alerts specifications.
 
+        Unit test consists of:
+            * Traverse all valid TOSCA Alerts Specifications in the src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid
+            * Sending a valid TOSCA Alert Specification to the view responsible for configuring Kapacitor
+            * Check that Kapacitor alerts, topics and handlers are created with the correct identifier and arguments
+
         :param app_config: fixture for setUp/tearDown of the web service registry
         """
 
diff --git a/src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid/alerts_test_config-1.yaml b/src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid/alerts_test_config-1.yaml
index 46c11df..74391ed 100644
--- a/src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid/alerts_test_config-1.yaml
+++ b/src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid/alerts_test_config-1.yaml
@@ -30,6 +30,7 @@ topology_template:
               implementation:
                 - http://sfemc.flame.eu/notify
                 - http://companyA.alert-handler.flame.eu/high-latency
+
     - low_requests_policy:
         type: eu.ict-flame.policies.StateChange
         triggers:
@@ -52,3 +53,59 @@ topology_template:
               implementation:
                 - http://sfemc.flame.eu/notify
                 - http://companyA.alert-handler.flame.eu/low-requests
+
+    - requests_diff_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          increase_in_requests:
+            description: |
+              This event triggers when the number of requests has increased relative to the number of requests received
+              120 seconds ago.
+            event_type: relative
+            metric: storage.requests
+            condition:
+              threshold: 100  # requests have increased by at least 100
+              granularity: 120
+              resource_type:
+                sf_package: storage
+                sf: storage-users
+                location: watershed
+              comparison_operator: gte
+            action:
+              implementation:
+                - http://sfemc.flame.eu/notify
+          decrease_in_requests:
+            description: |
+              This event triggers when the number of requests has decreased relative to the number of requests received
+              120 seconds ago.
+            event_type: relative
+            metric: storage.requests
+            condition:
+              threshold: -100  # requests have decreased by at least 100
+              granularity: 120
+              resource_type:
+                sf_package: storage
+                sf: storage-users
+                location: watershed
+              comparison_operator: lte
+            action:
+              implementation:
+                - http://sfemc.flame.eu/notify
+
+    - missing_measurement_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          missing_storage_measurements:
+            description: This event triggers when the number of storage measurements reported falls below the threshold value.
+            event_type: deadman
+            # deadman trigger instances monitor the whole measurement (storage in this case), so simply put a star for field value
+            # to be compliant with the <measurement>.<field> format
+            metric: storage.*
+            condition:
+              threshold: 0  # if requests are less than or equal to 0 (in other words, no measurements are reported)
+              granularity: 60  # check for for missing data for the last 60 seconds
+              resource_type:
+                sf_package: storage
+            action:
+              implementation:
+                - http://sfemc.flame.eu/notify
\ No newline at end of file
diff --git a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/invalid/alerts_test_config-2.yaml b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/invalid/alerts_test_config-2.yaml
index b7e5b8b..4eb4652 100644
--- a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/invalid/alerts_test_config-2.yaml
+++ b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/invalid/alerts_test_config-2.yaml
@@ -1,4 +1,4 @@
-# Fails because it's missing tosca version.
+tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0
 description: TOSCA Alerts Configuration document
 
 imports:
@@ -22,32 +22,30 @@ topology_template:
               threshold: 45
               granularity: 120
               aggregation_method: mean
-              resource_type:
+              global_tags:  # correct field is called resource_type, not global_tags
                 location: watershed
               comparison_operator: gt
             action:
               implementation:
                 - http://sfemc.flame.eu/notify
                 - http://companyA.alert-handler.flame.eu/high-latency
-    - low_requests_policy:
+    - requests_diff_policy:
         type: eu.ict-flame.policies.StateChange
         triggers:
-          low_requests:
+          increase_in_requests:
             description: |
-              This event triggers when the last reported number of requests for a given service function
-              falls behind a given threshold.
-            event_type: threshold
+              This event triggers when the number of requests has increased relative to the number of requests received
+              120 seconds ago.
+            event_type: relative
             metric: storage.requests
             condition:
-              threshold: 5
-              granularity: 60
-              aggregation_method: last
+              threshold: 100
+              granularity: 120
               resource_type:
                 sf_package: storage
                 sf: storage-users
                 location: watershed
-              comparison_operator: lt
+              comparison_operator: gte
             action:
               implementation:
-                - http://sfemc.flame.eu/notify
-                - http://companyA.alert-handler.flame.eu/low-requests
\ No newline at end of file
+              - http://sfemc.flame.eu/notify
\ No newline at end of file
diff --git a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-1.yaml b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-1.yaml
index dba2e22..3c7019a 100644
--- a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-1.yaml
+++ b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-1.yaml
@@ -51,4 +51,56 @@ topology_template:
             action:
               implementation:
                 - http://sfemc.flame.eu/notify
-                - http://companyA.alert-handler.flame.eu/low-requests
\ No newline at end of file
+                - http://companyA.alert-handler.flame.eu/low-requests
+    - requests_diff_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          increase_in_requests:
+            description: |
+              This event triggers when the number of requests has increased relative to the number of requests received
+              120 seconds ago.
+            event_type: relative
+            metric: storage.requests
+            condition:
+              threshold: 100
+              granularity: 120
+              resource_type:
+                sf_package: storage
+                sf: storage-users
+                location: watershed
+              comparison_operator: gte
+            action:
+              implementation:
+              - http://sfemc.flame.eu/notify
+          decrease_in_requests:
+            description: |
+              This event triggers when the number of requests has decreased relative to the number of requests received
+              120 seconds ago.
+            event_type: relative
+            metric: storage.requests
+            condition:
+              threshold: -100
+              granularity: 120
+              resource_type:
+                sf_package: storage
+                sf: storage-users
+                location: watershed
+              comparison_operator: lte
+            action:
+              implementation:
+              - http://sfemc.flame.eu/notify
+    - missing_measurement_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          missing_storage_measurements:
+            description: This event triggers when the number of storage measurements reported falls below the threshold value.
+            event_type: deadman
+            metric: storage.*
+            condition:
+              threshold: 0
+              granularity: 60
+              resource_type:
+                sf_package: storage
+            action:
+              implementation:
+              - http://sfemc.flame.eu/notify
diff --git a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-2.yaml b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-2.yaml
index 4196cf6..4df849d 100644
--- a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-2.yaml
+++ b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-2.yaml
@@ -45,4 +45,18 @@ topology_template:
             action:
               implementation:
                 - http://sfemc.flame.eu/notify
-                - http://companyA.alert-handler.flame.eu/low-requests
\ No newline at end of file
+                - http://companyA.alert-handler.flame.eu/low-requests
+    - missing_measurement_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          missing_storage_measurements:
+            event_type: deadman
+            metric: storage.field
+            condition:
+              threshold: 0
+              granularity: 60
+              resource_type:
+                sf_package: storage
+            action:
+              implementation:
+              - http://sfemc.flame.eu/notify
\ No newline at end of file
diff --git a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-4.yaml b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-4.yaml
index 8e7f188..7ad8e0e 100644
--- a/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-4.yaml
+++ b/src/service/clmcservice/resources/tosca/test-data/tosca-parser/valid/alerts_test_config-4.yaml
@@ -32,4 +32,21 @@ topology_template:
               comparison_operator: lt
             action:
               implementation:
-                - http://companyA.alert-handler.flame.eu/low-requests
\ No newline at end of file
+                - http://companyA.alert-handler.flame.eu/low-requests
+    - requests_diff_policy:
+        type: eu.ict-flame.policies.StateChange
+        triggers:
+          increase_in_requests:
+            event_type: relative
+            metric: storage.requests
+            condition:
+              threshold: 100
+              granularity: 120
+              resource_type:
+                sf_package: storage
+                sf: storage-users
+                location: watershed
+              comparison_operator: gte
+            action:
+              implementation:
+              - http://sfemc.flame.eu/notify
\ No newline at end of file
-- 
GitLab