Skip to content
Snippets Groups Projects
Commit fafde653 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

[ Issue #56 ] - Refactored StreamingSim into fixture

parent 579bd854
Branches
No related tags found
No related merge requests found
...@@ -2,8 +2,9 @@ import LineProtocolGenerator as lp ...@@ -2,8 +2,9 @@ import LineProtocolGenerator as lp
import time import time
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import sys import pytest
import random import random
import sys
# Simulation parameters # Simulation parameters
TICK_TIME = 1 TICK_TIME = 1
...@@ -17,7 +18,7 @@ AGENT_URL1 = 'http://192.168.50.11:8186' ...@@ -17,7 +18,7 @@ AGENT_URL1 = 'http://192.168.50.11:8186'
AGENT_URL2 = 'http://192.168.50.12:8186' AGENT_URL2 = 'http://192.168.50.12:8186'
# Simulator for services # Simulator for services
class sim: class Sim:
def __init__(self, influx_url): def __init__(self, influx_url):
# We don't need this as the db is CLMC metrics # We don't need this as the db is CLMC metrics
self.influx_db = 'CLMCMetrics' self.influx_db = 'CLMCMetrics'
...@@ -185,12 +186,12 @@ class sim: ...@@ -185,12 +186,12 @@ class sim:
def _deleteDB(self): def _deleteDB(self):
self._sendInfluxQuery(self.influx_url, 'DROP DATABASE ' + self.influx_db) self._sendInfluxQuery(self.influx_url, 'DROP DATABASE ' + self.influx_db)
@staticmethod
def _sendInfluxQuery(self, url, query): def _sendInfluxQuery(url, query):
query = urllib.parse.urlencode({'q': query}) query = urllib.parse.urlencode({'q': query})
query = query.encode('ascii') query = query.encode('ascii')
req = urllib.request.Request(url + '/query ', query) 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): def _sendInfluxData(self, url, data):
data = data.encode() data = data.encode()
...@@ -198,6 +199,35 @@ class sim: ...@@ -198,6 +199,35 @@ class sim:
req = urllib.request.Request(url + '/write?db=' + self.influx_db, data, header) req = urllib.request.Request(url + '/write?db=' + self.influx_db, data, header)
urllib.request.urlopen(req) urllib.request.urlopen(req)
simulator = sim(INFLUX_DB_URL)
@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) 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)
...@@ -2,7 +2,9 @@ from urllib.parse import urlencode ...@@ -2,7 +2,9 @@ from urllib.parse import urlencode
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from json import loads from json import loads
from os.path import join, dirname from os.path import join, dirname
import pytest from pytest import fixture
from StreamingSim import run_simulation_fixture
class TestSimulation(object): class TestSimulation(object):
...@@ -10,10 +12,11 @@ class TestSimulation(object): ...@@ -10,10 +12,11 @@ class TestSimulation(object):
A 'testing' class used to group all the tests related to the simulation data 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 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 :param queries_to_test: the fixture to use - returns a JSON object represented as a python dictionary
""" """
...@@ -63,7 +66,7 @@ class TestSimulation(object): ...@@ -63,7 +66,7 @@ class TestSimulation(object):
return result.read().decode("utf-8").strip() return result.read().decode("utf-8").strip()
@staticmethod @staticmethod
@pytest.fixture @fixture
def queries_to_test(): def queries_to_test():
""" """
A pytest fixture used to read the queries, which would be tested, from a JSON file A pytest fixture used to read the queries, which would be tested, from a JSON file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment