mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
made test resumable and importable
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
### low level logging and protocol handling
|
||||
|
||||
# normally you shouldnt need to look into this file too much
|
||||
|
||||
import logging
|
||||
import colorlog
|
||||
import config
|
||||
@@ -18,6 +20,9 @@ logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.E
|
||||
### mqtt stuff
|
||||
logging.getLogger("MQTT").debug("Connecting to {mqtt_broker}".format(mqtt_broker=config.mqtt_broker))
|
||||
|
||||
|
||||
log=logging.getLogger("testcore")
|
||||
|
||||
mqtt_client = mqtt.Client()
|
||||
mqtt_client.connect(config.mqtt_broker, 1883, 60)
|
||||
mqtt_client.loop_start()
|
||||
|
||||
+10
-6
@@ -86,18 +86,22 @@ class EspEasy:
|
||||
""".format(**kwargs)
|
||||
)
|
||||
|
||||
def recv_domoticz_http(self, sensor_type, timeout=60):
|
||||
def recv_domoticz_http(self, sensor_type, idx, timeout=60):
|
||||
"""recv a domoticz http request from espeasy, and convert back to espeasy values"""
|
||||
|
||||
start_time=time.time()
|
||||
logging.getLogger("domoticz http").info("Waiting for request with sensortype {sensor_type}".format(sensor_type=sensor_type))
|
||||
logging.getLogger("domoticz http").info("Waiting for request idx {idx} with sensortype {sensor_type}".format(sensor_type=sensor_type,idx=idx))
|
||||
|
||||
# read and parse http requests
|
||||
while time.time()-start_time<timeout:
|
||||
request=http_requests.get(block=True, timeout=timeout)
|
||||
if request.path == "/json.html":
|
||||
if reuqest.params.get('param')=='udevice':
|
||||
svalues=request.params.get('svalue').split(";")
|
||||
if request.path == "/json.htm" and int(request.params.get('idx'))==idx:
|
||||
if request.params.get('param')=='udevice':
|
||||
svalues_str=request.params.get('svalue').split(";")
|
||||
svalues=[]
|
||||
for svalue in svalues_str:
|
||||
svalues.append(float(svalue))
|
||||
|
||||
if sensor_type==SENSOR_TYPE_SINGLE and len(svalues)==1:
|
||||
return svalues
|
||||
elif sensor_type==SENSOR_TYPE_DUAL and len(svalues)==2:
|
||||
@@ -122,7 +126,7 @@ class EspEasy:
|
||||
elif request.params.get('switchcmd') == 'On':
|
||||
return [1]
|
||||
elif request.params.get('switchcmd') == 'Set Level':
|
||||
return [request.params.get('level')]
|
||||
return [int(request.params.get('level'))]
|
||||
|
||||
raise(Exception("Timeout"))
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#in each test just do: from esptest import *
|
||||
|
||||
#look in this file for basic functions to use in your tests
|
||||
|
||||
# from espeasy import *
|
||||
from node import *
|
||||
@@ -9,6 +10,8 @@ from espeasy import *
|
||||
|
||||
import config
|
||||
import time
|
||||
import shelve
|
||||
import os
|
||||
from espcore import *
|
||||
|
||||
|
||||
@@ -19,3 +22,59 @@ espeasy=[]
|
||||
for n in config.nodes:
|
||||
node.append(Node(n, "node"+str(len(node))))
|
||||
espeasy.append(EspEasy(node[-1]))
|
||||
|
||||
|
||||
steps=[]
|
||||
|
||||
### keep test state, so we can skip tests.
|
||||
state={
|
||||
'module': None,
|
||||
'name': None
|
||||
}
|
||||
with shelve.open("test.state") as shelve_db:
|
||||
if 'state' in shelve_db:
|
||||
global state
|
||||
state=shelve_db['state']
|
||||
|
||||
|
||||
def step(step):
|
||||
"""add test step. test can resume from every test-step"""
|
||||
if state['module'] and ( state['module'] != step.__module__ or state['name'] != step.__name__):
|
||||
log.debug("Skipping step "+step.__module__ + "." + step.__name__)
|
||||
else:
|
||||
steps.append(step)
|
||||
# add the rest of the steps as well
|
||||
state['module']=None
|
||||
|
||||
|
||||
### run all the tests
|
||||
def run():
|
||||
"""run all tests"""
|
||||
|
||||
for step in steps:
|
||||
print()
|
||||
log.info("*** Starting "+step.__module__ + "." + step.__name__ +": "+str(step.__doc__))
|
||||
|
||||
# store this step so we may resume later
|
||||
state['module']=step.__module__
|
||||
state['name']=step.__name__
|
||||
with shelve.open("test.state") as shelve_db:
|
||||
shelve_db['state']=state
|
||||
|
||||
#run the step. if there is an exception we resume this step the next time
|
||||
step()
|
||||
|
||||
|
||||
# log.info("Completed step")
|
||||
|
||||
# all Completed
|
||||
os.unlink("test.state")
|
||||
log.info("*** All tests complete ***")
|
||||
|
||||
|
||||
### auxillary test functions
|
||||
|
||||
def test_in_range(value, min, max):
|
||||
|
||||
if value < min or value > max:
|
||||
raise("Value {value} should be between {min} and {max}".format(value=value, min=min, max=max))
|
||||
|
||||
+30
-20
@@ -9,32 +9,42 @@ from esptest import *
|
||||
# tests:
|
||||
# - correct values are send to domoticz via http
|
||||
# - correct values are send to domoticz via mqtt
|
||||
# - test if powercycle value is valid (https://github.com/letscontrolit/ESPEasy/issues/719)
|
||||
|
||||
# espeasy[0].controller_thingspeak(index=1, controllerip=config.http_server, controllerport=config.http_port)
|
||||
|
||||
### config mqtt and the 2 devices
|
||||
# espeasy[0].controller_domoticz_http(index=1, controllerip=config.http_server, controllerport=config.http_port)
|
||||
# espeasy[0].device_p004(index=1, taskdevicepin1=2, plugin_004_dev=0, plugin_004_res=9, TDID1=1417)
|
||||
# espeasy[0].device_p004(index=2, taskdevicepin1=2, plugin_004_dev=1, plugin_004_res=9, TDID1=1418)
|
||||
|
||||
print(espeasy[0].recv_domoticz_http(SENSOR_TYPE_SINGLE))
|
||||
@step
|
||||
def prepare():
|
||||
"""configure devices and controller"""
|
||||
espeasy[0].controller_domoticz_http(index=1, controllerip=config.http_server, controllerport=config.http_port)
|
||||
espeasy[0].device_p004(index=1, taskdevicepin1=2, plugin_004_dev=0, plugin_004_res=9, TDID1=1417)
|
||||
espeasy[0].device_p004(index=2, taskdevicepin1=2, plugin_004_dev=1, plugin_004_res=9, TDID1=1418)
|
||||
|
||||
|
||||
exit
|
||||
@step
|
||||
def recv():
|
||||
"""wait for result"""
|
||||
results=espeasy[0].recv_domoticz_http(SENSOR_TYPE_SINGLE,1417)
|
||||
test_in_range(results[0], -5,40)
|
||||
results=espeasy[0].recv_domoticz_http(SENSOR_TYPE_SINGLE,1418)
|
||||
test_in_range(results[0], -5,40)
|
||||
|
||||
|
||||
|
||||
# @step
|
||||
# def prepare_mqtt():
|
||||
# """switch to mqtt"""
|
||||
# espeasy[0].controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
|
||||
#
|
||||
#wait for request
|
||||
temp1=http_expect_request("/update",{ 'apikey': 'thingspeakkey1234'}, timeout=10)
|
||||
temp1=http_expect_request("/json.htm", { 'idx': '1417' }, timeout=10)
|
||||
temp2=http_expect_request("/json.htm", { 'idx': '1418' }, timeout=10)
|
||||
|
||||
if not ( float(temp1.params['svalue'])>0 and float(temp1.params['svalue'])<40 and float(temp2.params['svalue'])>0 and float(temp2.params['svalue'])<40):
|
||||
raise(Exception("Wrong results"))
|
||||
#
|
||||
# @step
|
||||
# def recv():
|
||||
# """wait for mqtt result"""
|
||||
# results=espeasy[0].recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,1417)
|
||||
# test_in_range(results[0], -5,40)
|
||||
# results=espeasy[0].recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,1418)
|
||||
# test_in_range(results[0], -5,40)
|
||||
|
||||
|
||||
### reconfig for mqtt and expect results
|
||||
espeasy[0].controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
|
||||
temp1=mqtt_expect_json("domoticz/in", { 'idx': 1417 }, timeout=10)
|
||||
temp2=mqtt_expect_json("domoticz/in", { 'idx': 1418 }, timeout=10)
|
||||
|
||||
if not ( float(temp1['svalue'])>0 and float(temp1['svalue'])<40 and float(temp2['svalue'])>0 and float(temp2['svalue'])<40):
|
||||
raise(Exception("Wrong results"))
|
||||
run()
|
||||
|
||||
Reference in New Issue
Block a user