From e63f419803b371c0aef89277f46a062b32d3402a Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Thu, 22 Mar 2018 11:35:01 +0000
Subject: [PATCH] [ Issue #57 and #47 ] - moved the kapacitor conf setup POST
 in a pytest fixture

---
 test/streaming/conftest.py       | 13 +++++++++++++
 test/streaming/setupCLMC.sh      | 10 ----------
 test/streaming/test_streaming.py |  2 ++
 3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/test/streaming/conftest.py b/test/streaming/conftest.py
index 77e0f1d..16a6a05 100644
--- a/test/streaming/conftest.py
+++ b/test/streaming/conftest.py
@@ -2,6 +2,7 @@
 
 import pytest
 import yaml
+import requests
 
 
 @pytest.fixture(scope="module", params=[{'config': {'rspec': '/vagrant/test/streaming/rspec.yml'}}])
@@ -16,3 +17,15 @@ def streaming_config(request):
     with open(request.param['config']['rspec'], 'r') as stream:
         data_loaded = yaml.load(stream)
     return data_loaded
+
+
+@pytest.fixture(scope="module", autouse=True,
+                params=[{'config': {'kapacitor_url': 'http://localhost:8888/chronograf/v1/sources/1/kapacitors', 'kapacitor_file': '/vagrant/test/streaming/kapacitor.json'}}])
+def kapacitor_config(request):
+
+    kapacitor_configuration = request.param['config']['kapacitor_file']
+    with open(kapacitor_configuration, "r") as rule_file:
+        data = "".join(line.strip() for line in rule_file.readlines())
+
+    kapacitor_url = request.param['config']['kapacitor_url']
+    requests.post(url=kapacitor_url, data=data, headers={"Content-Type": "application/json"})
diff --git a/test/streaming/setupCLMC.sh b/test/streaming/setupCLMC.sh
index 6d2bd38..e7e2fc9 100644
--- a/test/streaming/setupCLMC.sh
+++ b/test/streaming/setupCLMC.sh
@@ -33,18 +33,8 @@ echo $TEST_DIR"/kapacitor.conf"
 cp $TEST_DIR/kapacitor.conf /etc/kapacitor/kapacitor.conf
 systemctl start kapacitor
 
-# wait for kapacitor to restart
-# TODO: do this better
-sleep 5
-
 # Set up Influx data source
 curl -i -X POST -H "Content-Type: application/json" http://localhost:8888/chronograf/v1/sources -d @$TEST_DIR/influx.json
 
-# Set up Kapacitor
-curl -i -X POST -H "Content-Type: application/json" http://localhost:8888/chronograf/v1/sources/1/kapacitors -d @$TEST_DIR/kapacitor.json
-
-# Set up rules
-curl -i -X POST -H "Content-Type: application/json" http://localhost:9092/kapacitor/v1/tasks -d @$TEST_DIR/rules.json
-
 # Set up dashboard
 curl -i -X POST -H "Content-Type: application/json" http://localhost:8888/chronograf/v1/dashboards -d @$TEST_DIR/dashboard.json
diff --git a/test/streaming/test_streaming.py b/test/streaming/test_streaming.py
index aa8a969..0b0b695 100644
--- a/test/streaming/test_streaming.py
+++ b/test/streaming/test_streaming.py
@@ -69,6 +69,8 @@ class TestStreamingAlerts(object):
 
             counter += time_delay  # the counter tracks the time taken, for the rules under test usually a 20 seconds time frame is enough
             if counter >= 8*time_delay:
+                for t in threads:  # kill all running threads in case of test failure
+                    t.stop()
                 break
 
         assert alert_created, "Alerts test failed: no log file is created indicating a triggered alert for rule {0}.".format(rule)
-- 
GitLab