-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testsuite: working on generic http/mqtt controller emulatior receiving
- Loading branch information
Showing
4 changed files
with
195 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,103 @@ | ||
### low level logging and protocol handling | ||
|
||
import logging | ||
import colorlog | ||
import config | ||
import paho.mqtt.client as mqtt | ||
import json | ||
import bottle | ||
import threading | ||
from queue import Queue, Empty | ||
|
||
colorlog.basicConfig(level=logging.DEBUG) | ||
log=logging.getLogger("TEST") | ||
|
||
|
||
logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.ERROR) | ||
|
||
|
||
### mqtt stuff | ||
logging.getLogger("MQTT").debug("Connecting to {mqtt_broker}".format(mqtt_broker=config.mqtt_broker)) | ||
|
||
mqtt_client = mqtt.Client() | ||
mqtt_client.connect(config.mqtt_broker, 1883, 60) | ||
mqtt_client.loop_start() | ||
mqtt_client.subscribe('#') | ||
|
||
mqtt_messages=[] | ||
def mqtt_on_message(client, userdata, message): | ||
logging.getLogger("MQTT").debug("Received message '" + str(message.payload) + "' on topic '" | ||
+ message.topic + "' with QoS " + str(message.qos)) | ||
mqtt_messages.append(message) | ||
|
||
mqtt_client.on_message=mqtt_on_message | ||
|
||
|
||
def mqtt_expect_json(topic, matches, timeout=60): | ||
"""wait until a specific json message is received, and return it decoded. ignores all other messages""" | ||
|
||
start_time=time.time() | ||
|
||
logging.getLogger("MQTT").info("Waiting for json message on topic {topic}, with values {matches}".format(topic=topic, matches=matches)) | ||
|
||
# check mqtt results | ||
while time.time()-start_time<timeout: | ||
while mqtt_messages: | ||
message=mqtt_messages.pop() | ||
try: | ||
#ignore decoding exceptions | ||
payload=json.loads(message.payload.decode()) | ||
except: | ||
continue | ||
|
||
if message.topic == topic: | ||
ok=True | ||
for match in matches.items(): | ||
if not match[0] in payload or payload[match[0]]!=match[1]: | ||
ok=False | ||
if ok: | ||
return(payload) | ||
time.sleep(1) | ||
|
||
raise(Exception("Timeout while expecting mqtt json message")) | ||
|
||
|
||
|
||
### http server stuff. | ||
|
||
# the http server just accepts everything and stores it in the http_requests queue | ||
import bottle | ||
|
||
http_requests = Queue() | ||
@bottle.post('<filename:path>') | ||
@bottle.get('<filename:path>') | ||
def urlhandler(filename): | ||
logging.getLogger("HTTP").debug(bottle.request.method+" "+str(dict(bottle.request.params))) | ||
http_requests.put(bottle.request.copy()) | ||
|
||
http_thread=threading.Thread(target=bottle.run, kwargs=dict(host='0.0.0.0', port=config.http_port, reloader=False)) | ||
http_thread.daemon=True | ||
http_thread.start() | ||
|
||
def http_expect_request(path, matches, timeout=60): | ||
"""wait until a specific path and paraters are request on the http server. ignores all other requests""" | ||
|
||
start_time=time.time() | ||
|
||
logging.getLogger("HTTP").info("Waiting for http request on path {path}, with values {matches}".format(path=path, matches=matches)) | ||
|
||
# check http results | ||
while time.time()-start_time<timeout: | ||
while True: | ||
request=http_requests.get(block=True, timeout=timeout) | ||
# logging.getLogger("HTTP").debug("Body: "+str(request.body.str)) | ||
if request.path == path: | ||
|
||
ok=True | ||
for match in matches.items(): | ||
if not match[0] in request.params or request.params[match[0]]!=match[1]: | ||
ok=False | ||
if ok: | ||
return(request) | ||
time.sleep(1) | ||
|
||
raise(Exception("Timeout while expecting http message")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,21 @@ | ||
#basic stuff needed in each test | ||
|
||
from espeasy import * | ||
#in each test just do: from esptest import * | ||
|
||
|
||
# from espeasy import * | ||
from node import * | ||
from espeasy import * | ||
|
||
import config | ||
import time | ||
import paho.mqtt.client as mqtt | ||
import json | ||
import bottle | ||
import threading | ||
from queue import Queue, Empty | ||
from espcore import * | ||
|
||
|
||
### mqtt stuff | ||
logging.getLogger("MQTT").debug("Connecting to {mqtt_broker}".format(mqtt_broker=config.mqtt_broker)) | ||
|
||
mqtt_client = mqtt.Client() | ||
mqtt_client.connect(config.mqtt_broker, 1883, 60) | ||
mqtt_client.loop_start() | ||
mqtt_client.subscribe('#') | ||
|
||
mqtt_messages=[] | ||
def mqtt_on_message(client, userdata, message): | ||
logging.getLogger("MQTT").debug("Received message '" + str(message.payload) + "' on topic '" | ||
+ message.topic + "' with QoS " + str(message.qos)) | ||
mqtt_messages.append(message) | ||
|
||
mqtt_client.on_message=mqtt_on_message | ||
|
||
|
||
def mqtt_expect_json(topic, matches, timeout=60): | ||
"""wait until a specific json message is received, and return it decoded. ignores all other messages""" | ||
|
||
start_time=time.time() | ||
|
||
logging.getLogger("MQTT").info("Waiting for json message on topic {topic}, with values {matches}".format(topic=topic, matches=matches)) | ||
|
||
# check mqtt results | ||
while time.time()-start_time<timeout: | ||
while mqtt_messages: | ||
message=mqtt_messages.pop() | ||
try: | ||
#ignore decoding exceptions | ||
payload=json.loads(message.payload.decode()) | ||
except: | ||
continue | ||
|
||
if message.topic == topic: | ||
ok=True | ||
for match in matches.items(): | ||
if not match[0] in payload or payload[match[0]]!=match[1]: | ||
ok=False | ||
if ok: | ||
return(payload) | ||
time.sleep(1) | ||
|
||
raise(Exception("Timeout while expecting mqtt json message")) | ||
|
||
|
||
### create node objects and espeasy objects | ||
node=[] | ||
espeasy=[] | ||
|
||
for n in config.nodes: | ||
node.append(Node(n, "node"+str(len(node)))) | ||
espeasy.append(EspEasy(node[-1])) | ||
|
||
|
||
### http server stuff. | ||
|
||
# the http server just accepts everything and stores it in the http_requests queue | ||
import bottle | ||
|
||
http_requests = Queue() | ||
@bottle.route('/<filename:path>') | ||
def urlhandler(filename): | ||
http_requests.put(bottle.request.copy()) | ||
|
||
http_thread=threading.Thread(target=bottle.run, kwargs=dict(host='0.0.0.0', port=config.http_port, reloader=False)) | ||
http_thread.daemon=True | ||
http_thread.start() | ||
|
||
def http_expect_request(path, matches, timeout=60): | ||
"""wait until a specific path and paraters are request on the http server. ignores all other requests""" | ||
|
||
start_time=time.time() | ||
|
||
logging.getLogger("HTTP").info("Waiting for http request on path {path}, with values {matches}".format(path=path, matches=matches)) | ||
|
||
# check http results | ||
while time.time()-start_time<timeout: | ||
while not http_requests.empty(): | ||
request=http_requests.get() | ||
if request.path == path: | ||
ok=True | ||
for match in matches.items(): | ||
if not match[0] in request.params or request.params[match[0]]!=match[1]: | ||
ok=False | ||
if ok: | ||
return(request) | ||
time.sleep(1) | ||
|
||
raise(Exception("Timeout while expecting http message")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters