mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
Examples: migration mqtt examples to pytest
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import ssl
|
||||
@@ -5,8 +8,9 @@ import sys
|
||||
from threading import Event, Thread
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import ttfw_idf
|
||||
from tiny_test_fw import DUT
|
||||
import pexpect
|
||||
import pytest
|
||||
from pytest_embedded import Dut
|
||||
|
||||
event_client_connected = Event()
|
||||
event_stop_client = Event()
|
||||
@@ -16,19 +20,20 @@ message_log = ''
|
||||
|
||||
|
||||
# The callback for when the client receives a CONNACK response from the server.
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
def on_connect(client, userdata, flags, rc): # type: (mqtt.Client, str, bool, str) -> None
|
||||
_ = (userdata, flags)
|
||||
print('Connected with result code ' + str(rc))
|
||||
event_client_connected.set()
|
||||
client.subscribe('/topic/qos0')
|
||||
|
||||
|
||||
def mqtt_client_task(client):
|
||||
def mqtt_client_task(client): # type: (mqtt.Client) -> None
|
||||
while not event_stop_client.is_set():
|
||||
client.loop()
|
||||
|
||||
|
||||
# The callback for when a PUBLISH message is received from the server.
|
||||
def on_message(client, userdata, msg):
|
||||
def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client.MQTTMessage) -> None
|
||||
global message_log
|
||||
global event_client_received_correct
|
||||
global event_client_received_binary
|
||||
@@ -55,8 +60,9 @@ def on_message(client, userdata, msg):
|
||||
message_log += 'Received data:' + msg.topic + ' ' + payload + '\n'
|
||||
|
||||
|
||||
@ttfw_idf.idf_example_test(env_tag='ethernet_router')
|
||||
def test_examples_protocol_mqtt_ssl(env, extra_data):
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.ethernet
|
||||
def test_examples_protocol_mqtt_ssl(dut): # type: (Dut) -> None
|
||||
broker_url = ''
|
||||
broker_port = 0
|
||||
"""
|
||||
@@ -67,18 +73,17 @@ def test_examples_protocol_mqtt_ssl(env, extra_data):
|
||||
4. Test ESP32 client received correct qos0 message
|
||||
5. Test python client receives binary data from running partition and compares it with the binary
|
||||
"""
|
||||
dut1 = env.get_dut('mqtt_ssl', 'examples/protocols/mqtt/ssl', dut_class=ttfw_idf.ESP32DUT)
|
||||
# check and log bin size
|
||||
binary_file = os.path.join(dut1.app.binary_path, 'mqtt_ssl.bin')
|
||||
binary_file = os.path.join(dut.app.binary_path, 'mqtt_ssl.bin')
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance('mqtt_ssl_bin_size', '{}KB'
|
||||
.format(bin_size // 1024))
|
||||
logging.info('[Performance][mqtt_ssl_bin_size]: %s KB', bin_size // 1024)
|
||||
|
||||
# Look for host:port in sdkconfig
|
||||
try:
|
||||
value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI'])
|
||||
value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get('BROKER_URI'))
|
||||
assert value is not None
|
||||
broker_url = value.group(1)
|
||||
broker_port = int(value.group(2))
|
||||
bin_size = min(int(dut1.app.get_sdkconfig()['CONFIG_BROKER_BIN_SIZE_TO_SEND']), bin_size)
|
||||
bin_size = min(int(dut.app.sdkconfig.get('BROKER_BIN_SIZE_TO_SEND')), bin_size)
|
||||
except Exception:
|
||||
print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig')
|
||||
raise
|
||||
@@ -105,25 +110,20 @@ def test_examples_protocol_mqtt_ssl(env, extra_data):
|
||||
print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port))
|
||||
if not event_client_connected.wait(timeout=30):
|
||||
raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
|
||||
dut1.start_app()
|
||||
try:
|
||||
ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0]
|
||||
ip_address = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[0]
|
||||
print('Connected to AP with IP: {}'.format(ip_address))
|
||||
except DUT.ExpectTimeout:
|
||||
except pexpect.TIMEOUT:
|
||||
print('ENV_TEST_FAILURE: Cannot connect to AP')
|
||||
raise
|
||||
print('Checking py-client received msg published from esp...')
|
||||
if not event_client_received_correct.wait(timeout=30):
|
||||
raise ValueError('Wrong data received, msg log: {}'.format(message_log))
|
||||
print('Checking esp-client received msg published from py-client...')
|
||||
dut1.expect(re.compile(r'DATA=send binary please'), timeout=30)
|
||||
dut.expect(r'DATA=send binary please', timeout=30)
|
||||
print('Receiving binary data from running partition...')
|
||||
if not event_client_received_binary.wait(timeout=30):
|
||||
raise ValueError('Binary not received within timeout')
|
||||
finally:
|
||||
event_stop_client.set()
|
||||
thread1.join()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_examples_protocol_mqtt_ssl()
|
Reference in New Issue
Block a user