From 5b0c239ecad378515fc00c6dd1fce8e82292cc4b Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Mon, 8 Apr 2019 13:37:04 +0100
Subject: [PATCH] Refactors alerts integration test

---
 src/test/clmctest/alerts/test_alerts.py | 48 ++++++++++++++-----------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/src/test/clmctest/alerts/test_alerts.py b/src/test/clmctest/alerts/test_alerts.py
index 1adaad6..db664a6 100644
--- a/src/test/clmctest/alerts/test_alerts.py
+++ b/src/test/clmctest/alerts/test_alerts.py
@@ -246,17 +246,12 @@ class TestAlerts(object):
         # find the latest timestamp of the registered alerts
         max_post_timestamp = 0
         tasks = get("http://{0}/kapacitor/v1/tasks".format(clmc_service_host)).json()["tasks"]
-        for task in tasks:
-            # get the configured variables of this alert
-            task_config = task["vars"]
-            # if configured for this SFC instance
-            if task_config["sfc"]["value"] == sfc and task_config["sfci"]["value"] == sfc_instance:
-                created_datestr = task["created"][:26]  # ignore the timezone and only take the first 6 digits of the microseconds
-                task_created_timestamp = datetime.datetime.strptime(created_datestr, "%Y-%m-%dT%H:%M:%S.%f")
-                max_post_timestamp = max(max_post_timestamp, task_created_timestamp.timestamp())
-
-        print("Sleeping 2 seconds to ensure a difference between the timestamps when creating the alerts and when updating them...")
-        sleep(2)
+        for timestamp in tasks_timestamps(tasks, sfc, sfc_instance):
+            max_post_timestamp = max(max_post_timestamp, timestamp)
+
+        delay = 2  # seconds
+        print("Sleeping {0} seconds to ensure a difference between the timestamps when creating the alerts and when updating them...".format(delay))
+        sleep(delay)
 
         # update the alerts with a PUT request and check that the "created" metadata is updated implying that the alerts were recreated
         print("Sending alerts specification to clmc service for updating...")
@@ -275,19 +270,32 @@ class TestAlerts(object):
         # find the earliest timestamp of the updated alerts
         min_put_timestamp = float("inf")
         tasks = get("http://{0}/kapacitor/v1/tasks".format(clmc_service_host)).json()["tasks"]
-        for task in tasks:
-            # get the configured variables of this alert
-            task_config = task["vars"]
-            # if configured for this SFC instance
-            if task_config["sfc"]["value"] == sfc and task_config["sfci"]["value"] == sfc_instance:
-                created_datestr = task["created"][:26]  # ignore the timezone and only take the first 6 digits of the microseconds
-                task_created_timestamp = datetime.datetime.strptime(created_datestr, "%Y-%m-%dT%H:%M:%S.%f")
-                min_put_timestamp = min(min_put_timestamp, task_created_timestamp.timestamp())
+        for timestamp in tasks_timestamps(tasks, sfc, sfc_instance):
+            min_put_timestamp = min(min_put_timestamp, timestamp)
 
         print("Latest timestamp during the POST request", max_post_timestamp, "Earliest timestamp during the PUT request", min_put_timestamp)
-        assert max_post_timestamp < min_put_timestamp, "There is an alert that wasn't updated properly with a PUT request"
+        assert min_put_timestamp - max_post_timestamp >= delay, "There is an alert that wasn't updated properly with a PUT request"
 
         # delete the alerts with a DELETE request
         with open(alerts_spec, 'rb') as alerts:
             files = {'alert-spec': alerts}
             delete("http://{0}/clmc-service/alerts".format(clmc_service_host), files=files)
+
+
+def tasks_timestamps(all_tasks, sfc_id, sfc_instance_id):
+    """
+    Generates the timestamps for the tasks related to the given SFC and SFC instance.
+
+    :param all_tasks: the full list of tasks from kapacitor
+    :param sfc_id: SFC identifier
+    :param sfc_instance_id: SFC instance identifier
+    """
+
+    for task in all_tasks:
+        # get the configured variables of this alert
+        task_config = task["vars"]
+        # if configured for this SFC instance
+        if task_config["sfc"]["value"] == sfc_id and task_config["sfci"]["value"] == sfc_instance_id:
+            created_datestr = task["created"][:26]  # ignore the timezone and only take the first 6 digits of the microseconds
+            task_created_timestamp = datetime.datetime.strptime(created_datestr, "%Y-%m-%dT%H:%M:%S.%f")
+            yield task_created_timestamp.timestamp()
-- 
GitLab