Skip to content
Snippets Groups Projects
Commit 82e239a0 authored by MJB's avatar MJB
Browse files

updates to telegraf tests to correctly test measurement queries

parent 3fc79692
No related branches found
No related tags found
No related merge requests found
......@@ -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
#!/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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment