diff --git a/src/test/clmctest/alerts/conftest.py b/src/test/clmctest/alerts/conftest.py index 2e4575d0a16a2b27cfde35ffad0ebbc0059c0268..3d40a90fcc14f9f21edd9dcd2f4408f24f5ea1c7 100644 --- a/src/test/clmctest/alerts/conftest.py +++ b/src/test/clmctest/alerts/conftest.py @@ -31,9 +31,13 @@ from shutil import rmtree from signal import SIGKILL from json import load from pkg_resources import resource_filename +from requests import delete, get from clmctest.alerts.alert_handler_server import LOG_TEST_FOLDER_PATH +KAPACITOR_PORT = 9092 + + @fixture(scope="module") def rspec_config(): """ @@ -51,11 +55,23 @@ def rspec_config(): @fixture(autouse=True, scope="module") -def set_up_tear_down_fixture(): +def set_up_tear_down_fixture(rspec_config): """ Set up/tear down fixture for the alerts integration test. """ + global KAPACITOR_PORT + + kapacitor_host = None + for host in rspec_config: + if host["name"] == "clmc-service": + kapacitor_host = host["ip_address"] + break + + assert kapacitor_host is not None + + kapacitor_url = "http://{0}:{1}".format(kapacitor_host, KAPACITOR_PORT) + if exists(LOG_TEST_FOLDER_PATH): rmtree(LOG_TEST_FOLDER_PATH) # clean out the log directory makedirs(LOG_TEST_FOLDER_PATH) # create the log directory @@ -73,3 +89,21 @@ def set_up_tear_down_fixture(): kill(process_id, SIGKILL) if exists(LOG_TEST_FOLDER_PATH): rmtree(LOG_TEST_FOLDER_PATH) + + print("Deleting Kapacitor tasks, topics and handlers that were created for this test...") + test_sfc_instance_scope = "CLMCMetrics.MS_I1" + # get all tasks from kapacitor (that were created in this test) and delete them + kapacitor_tasks = get("{0}/kapacitor/v1/tasks".format(kapacitor_url)).json()["tasks"] + kapacitor_task_links = [task["link"]["href"] for task in kapacitor_tasks if task["id"].startswith(test_sfc_instance_scope)] + for task_link in kapacitor_task_links: + delete("{0}{1}".format(kapacitor_url, task_link)) + + # get all topics and handlers from kapacitor (that were created in this test) and delete them + kapacitor_topics = get("{0}/kapacitor/v1/alerts/topics".format(kapacitor_url)).json()["topics"] + for topic in kapacitor_topics: + if topic["id"].startswith(test_sfc_instance_scope): + topic_handlers = get("{0}{1}".format(kapacitor_url, topic["handlers-link"]["href"])).json()["handlers"] + for handler in topic_handlers: + delete("{0}{1}".format(kapacitor_url, handler["link"]["href"])) + + delete("{0}{1}".format(kapacitor_url, topic["link"]["href"]))