diff --git a/test/streaming-sim/StreamingSim.py b/test/streaming-sim/StreamingSim.py index 0182e75dc99b9e9f28ffad87a0d4d40e5929d67b..9c9b35e586010f8bc2249f0e1db7fdce4dfab0a1 100644 --- a/test/streaming-sim/StreamingSim.py +++ b/test/streaming-sim/StreamingSim.py @@ -2,8 +2,9 @@ import LineProtocolGenerator as lp import time import urllib.parse import urllib.request -import sys +import pytest import random +import sys # Simulation parameters TICK_TIME = 1 @@ -17,7 +18,7 @@ AGENT_URL1 = 'http://192.168.50.11:8186' AGENT_URL2 = 'http://192.168.50.12:8186' # Simulator for services -class sim: +class Sim: def __init__(self, influx_url): # We don't need this as the db is CLMC metrics self.influx_db = 'CLMCMetrics' @@ -185,12 +186,12 @@ class sim: def _deleteDB(self): self._sendInfluxQuery(self.influx_url, 'DROP DATABASE ' + self.influx_db) - - def _sendInfluxQuery(self, url, query): + @staticmethod + def _sendInfluxQuery(url, query): query = urllib.parse.urlencode({'q': query}) query = query.encode('ascii') req = urllib.request.Request(url + '/query ', query) - urllib.request.urlopen(req) + return urllib.request.urlopen(req).read().decode("utf-8").strip() def _sendInfluxData(self, url, data): data = data.encode() @@ -198,6 +199,35 @@ class sim: req = urllib.request.Request(url + '/write?db=' + self.influx_db, data, header) urllib.request.urlopen(req) -simulator = sim(INFLUX_DB_URL) -simulator.run(SIMULATION_TIME_SEC) +@pytest.fixture +def run_simulation_fixture(): + global INFLUX_DB_URL + global SIMULATION_TIME_SEC + + dbs = Sim._sendInfluxQuery(INFLUX_DB_URL, "SHOW DATABASES") + + if "CLMCMetrics" not in dbs: + simulator = Sim(INFLUX_DB_URL) + simulator.run(SIMULATION_TIME_SEC) + import time + time.sleep(10) + +def run_simulation(generate=True): + global INFLUX_DB_URL + global SIMULATION_TIME_SEC + + simulator = Sim(INFLUX_DB_URL) + + if generate: + simulator.run(SIMULATION_TIME_SEC) + else: + simulator._deleteDB() + + +if __name__ == "__main__": + option = True + if len(sys.argv) > 1: + option = str(sys.argv[1]) != "-c" + + run_simulation(generate=option) diff --git a/test/streaming-sim/VerifySimResults.py b/test/streaming-sim/VerifySimResults.py index dd71e5e375a10b6f552d65c4dcf4e26edcff23aa..b2eca729fb4b2d4689c48f6f1c339cd26dea2081 100644 --- a/test/streaming-sim/VerifySimResults.py +++ b/test/streaming-sim/VerifySimResults.py @@ -2,7 +2,9 @@ from urllib.parse import urlencode from urllib.request import Request, urlopen from json import loads from os.path import join, dirname -import pytest +from pytest import fixture + +from StreamingSim import run_simulation_fixture class TestSimulation(object): @@ -10,10 +12,11 @@ class TestSimulation(object): A 'testing' class used to group all the tests related to the simulation data """ - def test_simulation(self, queries_to_test): + def test_simulation(self, run_simulation_fixture, queries_to_test): """ This is the entry point of the test. This method will be found and executed when the module is ran using pytest + :param run_simulation: the imported fixture to use to generate the testing data - the return value of the fixture is not needed in this case :param queries_to_test: the fixture to use - returns a JSON object represented as a python dictionary """ @@ -63,7 +66,7 @@ class TestSimulation(object): return result.read().decode("utf-8").strip() @staticmethod - @pytest.fixture + @fixture def queries_to_test(): """ A pytest fixture used to read the queries, which would be tested, from a JSON file