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

updated tests to parameterise tests and fixtures correctly, added influx db client

parent 82e239a0
No related branches found
No related tags found
No related merge requests found
......@@ -26,4 +26,5 @@
apt-get update
apt-get install python3 python3-pip python-influxdb
update-alternatives --install /usr/bin/python python /usr/bin/python3 10
pip install pytest pyyaml
pip3 install pytest pyyaml
pip3 install --upgrade influxdb
......@@ -2,10 +2,15 @@
import pytest
import yaml
from influxdb import InfluxDBClient
@pytest.fixture(scope="module")
def telegraf_agent_config():
"""Returns the service configuration deployed for the telegraf conf tests."""
with open("test/telegraf-agents/rspec.yml", 'r') as stream:
data_loaded = yaml.load(stream)
return data_loaded
\ No newline at end of file
return data_loaded
@pytest.fixture(params=[{'database': 'CLMCMetrics'}], scope='module')
def influxdb(telegraf_agent_config, request):
return InfluxDBClient(telegraf_agent_config['hosts'][0]['ip_address'], 8086, request.param['database'])
\ No newline at end of file
#!/usr/bin/python3
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
import pytest
import os
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from os.path import join, dirname
def test_service_names(telegraf_agent_config):
print(telegraf_agent_config['hosts'][0]['name'])
assert telegraf_agent_config['hosts'][0]['name'] == 'clmc-service'
assert telegraf_agent_config['hosts'][1]['name'] == 'apache'
assert telegraf_agent_config['hosts'][2]['name'] == 'nginx'
assert telegraf_agent_config['hosts'][3]['name'] == 'mongo'
assert telegraf_agent_config['hosts'][4]['name'] == 'ffmpeg'
assert telegraf_agent_config['hosts'][5]['name'] == 'ipendpoint'
import pytest
from influxdb import InfluxDBClient
@pytest.mark.parametrize("service_name", [
('clmc-service'),
('apache'),
('nginx'),
('mongo'),
('ffmpeg'),
('ipendpoint'),
])
def test_service_name(telegraf_agent_config, service_name):
assert any(s['name'] == service_name for s in telegraf_agent_config['hosts']), "{0} not in list of hosts".format(service_name)
def test_ping(telegraf_agent_config):
"""This test will only run on linux"""
for x in telegraf_agent_config['hosts']:
print("Testing service" + x['name'] + " " + x['ip_address'])
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("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'},
"""This test will only run on linux as the process call is not portable, there's a better way"""
for host in telegraf_agent_config['hosts']:
response = os.system("ping -c 1 " + host['ip_address'])
assert response == 0, "Could not ping {0} on ip address {1}".format(host['name'], host['ip_address'])
@pytest.mark.parametrize("measurement, query, expected_result", [
('nginx', 'SELECT mean("requests") AS "mean" FROM "CLMCMetrics"."autogen"."nginx"', 0),
('cpu', 'SELECT mean("usage_idle") AS "mean" FROM "CLMCMetrics"."autogen"."cpu"', 0),
('mongodb', 'SELECT mean("net_in_bytes") AS "mean" FROM "CLMCMetrics"."autogen"."mongodb"', 0)
])
def test_all_inputs(telegraf_agent_config, query):
def test_all_inputs(influxdb, measurement, query, expected_result):
"""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'])
@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'}]}
query_result = influxdb.query('SHOW measurements ON "CLMCMetrics"')
points = list(query_result.get_points())
assert any(p['name'] == measurement for p in points), "{0} not in measurement list".format(measurement)
query_result = influxdb.query(query)
points = list(query_result.get_points())
actual_result = points[0]['mean']
assert actual_result > expected_result, "actual result {0} is not > expected result {1} for query {2}".format(actual_result, str(expected_result), query)
@pytest.mark.parametrize("ipendpoint, measurements", [
('id1', [{'measurement': 'cpu', 'query': 'query', 'result': 'result'} , {'measurement': 'nginx', 'query': 'query', 'result': 'result'}, {'measurement': 'mongo', 'query': 'query', 'result': 'result'}]),
('id2', [{'measurement': 'cpu', 'query': 'query', 'result': 'result'} , {'measurement': 'nginx', 'query': 'query', 'result': 'result'}])
])
def test_multiple_inputs_on_a_service(telegraf_agent_config, service):
def test_multiple_inputs_on_a_service(influxdb, ipendpoint, measurements):
"""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
@pytest.mark.parametrize("query",
[{'query': 'filter query', 'expected_result': '0'},
{'query': 'filter query', 'expected_result': '0'},
{'query': 'filter query', 'expected_result': '0'},
@pytest.mark.parametrize("query, expected_result",
[('filter query', 0),
('filter query', 0),
('filter query', 0)
])
def test_global_tag_filtering(telegraf_agent_config, query):
def test_global_tag_filtering(influxdb, query, expected_result):
"""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 as a python data structure
"""
query = urlencode({"q": query}).encode("ascii")
request = Request("{0}/query".format(url), query)
result = urlopen(request)
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