diff --git a/src/test/clmctest/alerts/test_alerts.py b/src/test/clmctest/alerts/test_alerts.py index 1adaad618b61341eca90f6d5206521ec2b0aa0a2..db664a6d6dfd9156015cc25a1194df65d2f0f145 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()