Skip to content

Commit

Permalink
splitted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Sep 30, 2017
1 parent a71340c commit d9b5ed1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Test mqtt messages exchanges between a client and the cc3200 board
"""
import pytest
import os
import asyncio
import pynais as ns

# the Leds message
import commands as cmd

import pytest
import pynais as ns


# the firmware test machinery
import pynais.iottest as iot

Expand Down Expand Up @@ -73,38 +75,6 @@ def test_init_board():
iot.run('nais', tc.fw_deploy_ctx, events=events, handler=handle_event)


@pytest.mark.asyncio
async def test_add_profile():
broker = await start_broker()

profile = ns.msg.Profile(uid='baba', pwd='there_is_no_secret_for_me')
msg = ns.marshall(profile)

cli = mqtt.MQTTClient()
await cli.connect(tc.broker)

await cli.publish('{}/{}'.format(tc.network, tc.board), msg, qos=QOS_0)

await cli.subscribe([('hb/{}/{}'.format(tc.network, tc.board), QOS_0)])

try:
# wait for the ack
message = await cli.deliver_message(timeout=3)
packet = message.publish_packet
payload = packet.payload.data
print("%s => %s" %
(packet.variable_header.topic_name, str(payload)))

ack = ns.unmarshall(payload)
print(ack)
except asyncio.TimeoutError:
assert 0
finally:
await cli.disconnect()
await broker.shutdown()
await asyncio.sleep(15)


@pytest.mark.asyncio
async def test_switch_led():
broker = await start_broker()
Expand Down
106 changes: 106 additions & 0 deletions burba/tests/test_cc3200_mqtt_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Test mqtt messages exchanges between a client and the cc3200 board
"""
import pytest
import os
import asyncio
import pynais as ns

# the Leds message
import commands as cmd

# the firmware test machinery
import pynais.iottest as iot

import hbmqtt.client as mqtt
from hbmqtt.mqtt.constants import QOS_0, QOS_1, QOS_2
import hbmqtt.broker

# import firmware configuration
import tc

import time

events = (
('board init', 'BOARD_INIT'),
('board ready', 'BOARD_READY')
)

def handle_event(event, uart):
if (event.name == 'BOARD_INIT'):
profile = ns.msg.Profile(
os.environ['TEST_UID'], pwd=os.environ['TEST_PWD'])
uart.write(ns.marshall(profile))

board_cfg = ns.msg.Config(host=ns.my_ip_address(),
network=tc.network, board=tc.board)
uart.write(ns.marshall(board_cfg))

return 1

if (event.name == 'BOARD_READY'):
return 1

if (event.name == 'SERIAL_TIMEOUT'):
assert 0


async def start_broker():
config = {
'listeners': {
'default': {
'type': 'tcp',
'bind': '0.0.0.0:1883',
}
},
'sys_interval': 10
}

# start broker
broker = hbmqtt.broker.Broker(config)
await broker.start()

#await a little bit for board connection ...
await asyncio.sleep(4)

return broker


def test_init_board():

# check that wlan env variables username and password are set
tc.check_test_requirements()

iot.run('nais', tc.fw_deploy_ctx, events=events, handler=handle_event)


@pytest.mark.asyncio
async def test_add_profile():
broker = await start_broker()

profile = ns.msg.Profile(uid='baba', pwd='there_is_no_secret_for_me')
msg = ns.marshall(profile)

cli = mqtt.MQTTClient()
await cli.connect(tc.broker)

await cli.publish('{}/{}'.format(tc.network, tc.board), msg, qos=QOS_0)

await cli.subscribe([('hb/{}/{}'.format(tc.network, tc.board), QOS_0)])

try:
# wait for the ack
message = await cli.deliver_message(timeout=3)
packet = message.publish_packet
payload = packet.payload.data
print("%s => %s" %
(packet.variable_header.topic_name, str(payload)))

ack = ns.unmarshall(payload)
print(ack)
except asyncio.TimeoutError:
assert 0
finally:
await cli.disconnect()
await broker.shutdown()


0 comments on commit d9b5ed1

Please sign in to comment.