From 82e239a0bea2edb09fcfc1a599c0c6d18e2acbe6 Mon Sep 17 00:00:00 2001
From: MJB <mjb@it-innovation.soton.ac.uk>
Date: Tue, 6 Mar 2018 13:09:12 +0000
Subject: [PATCH] updates to telegraf tests to correctly test measurement
 queries

---
 test/services/pytest/install.sh              |  4 +-
 test/telegraf-agents/test_telegraf_agents.py | 68 ++++++++++++++++----
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/test/services/pytest/install.sh b/test/services/pytest/install.sh
index 82fb18f..36a51d0 100644
--- a/test/services/pytest/install.sh
+++ b/test/services/pytest/install.sh
@@ -24,6 +24,6 @@
 #//
 #/////////////////////////////////////////////////////////////////////////
 apt-get update
-apt-get install python3 python3-pip
+apt-get install python3 python3-pip python-influxdb
 update-alternatives --install /usr/bin/python python /usr/bin/python3 10
-pip install pytest pyyaml
\ No newline at end of file
+pip install pytest pyyaml
diff --git a/test/telegraf-agents/test_telegraf_agents.py b/test/telegraf-agents/test_telegraf_agents.py
index 0321e61..555171b 100644
--- a/test/telegraf-agents/test_telegraf_agents.py
+++ b/test/telegraf-agents/test_telegraf_agents.py
@@ -1,5 +1,4 @@
 #!/usr/bin/python3
-
 import sys
 
 if sys.version_info[0] < 3:
@@ -7,6 +6,7 @@ if sys.version_info[0] < 3:
 
 import pytest
 import os
+import json
 from urllib.parse import urlencode
 from urllib.request import Request, urlopen
 from os.path import join, dirname
@@ -27,27 +27,73 @@ def test_ping(telegraf_agent_config):
         response = os.system("ping -c 1 " + x['ip_address'])
         assert response == 0, "Could not ping " + x['name'] + " on ip address " + x['ip_address']
 
-@pytest.mark.parametrize("queries", 
-    [{'nginx': {'measurement': 'nginx', 'query': 'SELECT mean("requests") AS "mean_active" FROM "CLMCMetrics"."autogen"."nginx"', 'result': '1'}}])
-def test_nginx(telegraf_agent_config, queries):
+@pytest.mark.parametrize("query", 
+    [{'measurement': 'nginx', 'query': 'SELECT mean("requests") AS "mean_active" FROM "CLMCMetrics"."autogen"."nginx"', 'expected_result': '0'},
+     {'measurement': 'cpu', 'query': 'SELECT mean("usage_idle") AS "mean_usage_idle" FROM "CLMCMetrics"."autogen"."cpu"', 'expected_result': '0'},
+     {'measurement': 'mongodb', 'query': 'SELECT mean("net_in_bytes") AS "mean_net_in_bytes" FROM "CLMCMetrics"."autogen"."mongodb"', 'expected_result': '0'},
+    ])
+def test_all_inputs(telegraf_agent_config, query):
+    """Tests measurements are received from an input plugin aggregated across all services
+    
+    Arguments:
+        telegraf_agent_config {Structure according to rspec.yml format} -- The resource specification for the services under test
+        query {test to run} -- a mean query run against a specific measurement value with an expected minimum mean result value
+    """
+
+    influxdb_url = "http://" + telegraf_agent_config['hosts'][0]['ip_address'] + ":8086"
+
+    measurements = send_query(influxdb_url, 'SHOW measurements ON "CLMCMetrics"') 
+    assert measurements is not None, "Show measurements returned no results "
+    l_value = [query['measurement']]
+    assert l_value in measurements['results'][0]['series'][0]['values'], "{0} not in measurement list".format(query['measurement'])
+
+    measurement_result = send_query(influxdb_url, query['query'])
+    actual_result = int(measurement_result['results'][0]['series'][0]['values'][0][1])
+    assert  actual_result > int(query['expected_result']), "actual result {0} is not > expected result {1} for query {2}".format(actual_result, query['expected_result'], query['query'])
 
-    measurements = send_query("http://localhost:8086", 'SHOW measurements ON "CLMCMetrics"') 
-    assert queries['nginx']['measurement'] in measurements, "Measurement " + measurement + " not in the CLMCMetrics database"
+@pytest.mark.parametrize("service", 
+    [{'ipendpoint': 'id', 'measurements': [{'measurement': 'cpu', 'query': 'query', 'result': 'result'} , {'measurement': 'nginx', 'query': 'query', 'result': 'result'}, {'measurement': 'mongo', 'query': 'query', 'result': 'result'}]},
+     {'ipendpoint': 'id', 'measurements': [{'measurement': 'cpu', 'query': 'query', 'result': 'result'} , {'nmeasurementme': 'nginx', 'query': 'query', 'result': 'result'}]}
+    ])
+def test_multiple_inputs_on_a_service(telegraf_agent_config, service):
+    """This test checks that a service configured with multiple input plugins as separate telegraf config files generates measurements in the database
+    
+    Arguments:
+        telegraf_agent_config {[type]} -- The resource specification for the services under test
+        service {[type]} -- Includes the IP endpoint configured with multiple inputs, and a list of queries for each measurement generated by the inputs
+    """
+
+    # for each item in the measurement list run the query and test the result 
+    assert 1
 
-    result = send_query("http://localhost:8086", queries['nginx']['query'])
-    print("result" + result)
-    # assert results is correct
+@pytest.mark.parametrize("query", 
+    [{'query': 'filter query', 'expected_result': '0'},
+     {'query': 'filter query', 'expected_result': '0'},
+     {'query': 'filter query', 'expected_result': '0'},
+    ])
+def test_global_tag_filtering(telegraf_agent_config, query):
+    """Tests that the global tags are inserted correctly into the global configuration using the install CLMC script
     
+    Arguments:
+        telegraf_agent_config {[type]} -- [description]
+    """
+
+    # run query
+    # check result
+
+    assert 1    
+   
 def send_query(url, query):
     """
     An auxiliary static method to send a query to a url and retrieve the result
 
     :param url: the target url to which the query is sent to - a string containing a valid URL address
     :param query: the query to be executed on the given URL
-    :return: the result of the executed query
+    :return: the result of the executed query as a python data structure
     """
     query = urlencode({"q": query}).encode("ascii")
     request = Request("{0}/query".format(url), query)
     result = urlopen(request)
 
-    return result.read().decode("utf-8").strip()
+    json_string = result.read().decode("utf-8").strip()
+    return json.loads(json_string)
-- 
GitLab