add test_mqttserver.py
add test_mqttserver.py
This commit is contained in:
parent
3060e7db20
commit
0cb5c0b4f3
3 changed files with 156 additions and 4 deletions
1
Pipfile
1
Pipfile
|
|
@ -18,6 +18,7 @@ pbr = "*"
|
||||||
pytest-asyncio = "*"
|
pytest-asyncio = "*"
|
||||||
pytest-aiohttp = "*"
|
pytest-aiohttp = "*"
|
||||||
pytest-cov = "*"
|
pytest-cov = "*"
|
||||||
|
testfixtures = "*"
|
||||||
|
|
||||||
[pipenv]
|
[pipenv]
|
||||||
allow_prereleases = true
|
allow_prereleases = true
|
||||||
|
|
|
||||||
30
Pipfile.lock
generated
30
Pipfile.lock
generated
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "89b1dc5fde60f217d24d1b66c94fa5640ca23925420992a2c14718f61866dc61"
|
"sha256": "ce534b7ec3bc05783e99636abeb05165298c96698d1c6e1288595a1d3174168b"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {},
|
"requires": {},
|
||||||
|
|
@ -427,6 +427,13 @@
|
||||||
],
|
],
|
||||||
"version": "==4.5.2"
|
"version": "==4.5.2"
|
||||||
},
|
},
|
||||||
|
"packaging": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:0c98a5d0be38ed775798ece1b9727178c4469d9c3b4ada66e8e6b7849f8732af",
|
||||||
|
"sha256:9e1cbf8c12b1f1ce0bb5344b8d7ecf66a6f8a6e91bcb0c84593ed6d3ab5c4ab3"
|
||||||
|
],
|
||||||
|
"version": "==19.0"
|
||||||
|
},
|
||||||
"pbr": {
|
"pbr": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:0ce920b865091450bbcd452b35cf6d6eb8a6d9ce13ad2210d6e77557f85cf32b",
|
"sha256:0ce920b865091450bbcd452b35cf6d6eb8a6d9ce13ad2210d6e77557f85cf32b",
|
||||||
|
|
@ -457,12 +464,19 @@
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"version": "==2.3.1"
|
"version": "==2.3.1"
|
||||||
},
|
},
|
||||||
|
"pyparsing": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:1873c03321fc118f4e9746baf201ff990ceb915f433f23b395f5580d1840cb2a",
|
||||||
|
"sha256:9b6323ef4ab914af344ba97510e966d64ba91055d6b9afa6b30799340e89cc03"
|
||||||
|
],
|
||||||
|
"version": "==2.4.0"
|
||||||
|
},
|
||||||
"pytest": {
|
"pytest": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:1a8aa4fa958f8f451ac5441f3ac130d9fc86ea38780dd2715e6d5c5882700b24",
|
"sha256:6032845e68a17a96e8da3088037f899b56357769a724122056265ca2ea1890ee",
|
||||||
"sha256:b8bf138592384bd4e87338cb0f256bf5f615398a649d4bd83915f0e4047a5ca6"
|
"sha256:bea27a646a3d74cbbcf8d3d4a06b2dfc336baf3dc2cc85cf70ad0157e73e8322"
|
||||||
],
|
],
|
||||||
"version": "==4.5.0"
|
"version": "==4.6.2"
|
||||||
},
|
},
|
||||||
"pytest-aiohttp": {
|
"pytest-aiohttp": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
|
|
@ -495,6 +509,14 @@
|
||||||
],
|
],
|
||||||
"version": "==1.12.0"
|
"version": "==1.12.0"
|
||||||
},
|
},
|
||||||
|
"testfixtures": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:819e9090bf3cce5dfaf56817f55ede76b3ae490ad0e9ffe3060a6618d11894c3",
|
||||||
|
"sha256:a6e95634a80bd6e5adc7e861729f4abd84433e91b4215d48a2098a096c0261b7"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==6.8.2"
|
||||||
|
},
|
||||||
"toml": {
|
"toml": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c",
|
"sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c",
|
||||||
|
|
|
||||||
129
tests/test_mqttserver.py
Normal file
129
tests/test_mqttserver.py
Normal file
|
|
@ -0,0 +1,129 @@
|
||||||
|
import mock
|
||||||
|
import bumper
|
||||||
|
import asyncio
|
||||||
|
import pytest
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import tinydb
|
||||||
|
import pytest_asyncio
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
import hbmqtt
|
||||||
|
import logging
|
||||||
|
from testfixtures import LogCapture
|
||||||
|
|
||||||
|
|
||||||
|
async def test_helperbot_message():
|
||||||
|
with LogCapture("helperbot") as l:
|
||||||
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
|
mqtt_server = bumper.MQTTServer(mqtt_address)
|
||||||
|
broker = hbmqtt.broker.Broker(
|
||||||
|
mqtt_server.default_config, plugin_namespace="hbmqtt.test.plugins"
|
||||||
|
)
|
||||||
|
await broker.start()
|
||||||
|
|
||||||
|
# Test broadcast message
|
||||||
|
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address)
|
||||||
|
await mqtt_helperbot.start_helper_bot()
|
||||||
|
assert (
|
||||||
|
mqtt_helperbot.Client._connected_state._value == True
|
||||||
|
) # Check helperbot is connected
|
||||||
|
msg_payload = "<ctl ts='1547822804960' td='DustCaseST' st='0'/>"
|
||||||
|
msg_topic_name = "iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x"
|
||||||
|
await mqtt_helperbot.Client.publish(
|
||||||
|
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pass
|
||||||
|
l.check_present(
|
||||||
|
(
|
||||||
|
"helperbot",
|
||||||
|
"DEBUG",
|
||||||
|
"Received Broadcast - Topic: iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x - Message: <ctl ts='1547822804960' td='DustCaseST' st='0'/>",
|
||||||
|
)
|
||||||
|
) # Check broadcast message was logged
|
||||||
|
l.clear()
|
||||||
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
|
# Send command to bot
|
||||||
|
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address)
|
||||||
|
await mqtt_helperbot.start_helper_bot()
|
||||||
|
assert (
|
||||||
|
mqtt_helperbot.Client._connected_state._value == True
|
||||||
|
) # Check helperbot is connected
|
||||||
|
msg_payload = "{}"
|
||||||
|
msg_topic_name = (
|
||||||
|
"iot/p2p/GetWKVer/helper1/bumper/helper1/bot_serial/ls1ok3/wC3g/q/iCmuqp/j"
|
||||||
|
)
|
||||||
|
await mqtt_helperbot.Client.publish(
|
||||||
|
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pass
|
||||||
|
l.check_present(
|
||||||
|
(
|
||||||
|
"helperbot",
|
||||||
|
"DEBUG",
|
||||||
|
"Send Command - Topic: iot/p2p/GetWKVer/helper1/bumper/helper1/bot_serial/ls1ok3/wC3g/q/iCmuqp/j - Message: {}",
|
||||||
|
)
|
||||||
|
) # Check send command message was logged
|
||||||
|
l.clear()
|
||||||
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
|
# Received response to command
|
||||||
|
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address)
|
||||||
|
await mqtt_helperbot.start_helper_bot()
|
||||||
|
assert (
|
||||||
|
mqtt_helperbot.Client._connected_state._value == True
|
||||||
|
) # Check helperbot is connected
|
||||||
|
msg_payload = '{"ret":"ok","ver":"0.13.5"}'
|
||||||
|
msg_topic_name = (
|
||||||
|
"iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helper1/bumper/helper1/p/iCmuqp/j"
|
||||||
|
)
|
||||||
|
await mqtt_helperbot.Client.publish(
|
||||||
|
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pass
|
||||||
|
l.check_present(
|
||||||
|
(
|
||||||
|
"helperbot",
|
||||||
|
"DEBUG",
|
||||||
|
'Received Response - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helper1/bumper/helper1/p/iCmuqp/j - Message: {"ret":"ok","ver":"0.13.5"}',
|
||||||
|
)
|
||||||
|
) # Check received response message was logged
|
||||||
|
l.clear()
|
||||||
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
|
# Received unknown message
|
||||||
|
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address)
|
||||||
|
await mqtt_helperbot.start_helper_bot()
|
||||||
|
assert (
|
||||||
|
mqtt_helperbot.Client._connected_state._value == True
|
||||||
|
) # Check helperbot is connected
|
||||||
|
msg_payload = 'test'
|
||||||
|
msg_topic_name = (
|
||||||
|
"iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helper1/p/iCmuqp/j"
|
||||||
|
)
|
||||||
|
await mqtt_helperbot.Client.publish(
|
||||||
|
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pass
|
||||||
|
l.check_present(
|
||||||
|
(
|
||||||
|
"helperbot",
|
||||||
|
"DEBUG",
|
||||||
|
'Received Message - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helper1/p/iCmuqp/j - Message: test',
|
||||||
|
)
|
||||||
|
) # Check received message was logged
|
||||||
|
l.clear()
|
||||||
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
await broker.shutdown()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue