pytest_mqtt5.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import logging
  6. import os
  7. import pytest
  8. from pytest_embedded import Dut
  9. @pytest.mark.esp32
  10. @pytest.mark.ethernet
  11. def test_examples_protocol_mqtt5(dut: Dut) -> None:
  12. """
  13. steps: |
  14. 1. join AP
  15. 2. connect to mqtt://mqtt.eclipseprojects.io
  16. 3. check conneciton success
  17. """
  18. # check and log bin size
  19. binary_file = os.path.join(dut.app.binary_path, 'mqtt5.bin')
  20. bin_size = os.path.getsize(binary_file)
  21. logging.info('mqtt5_bin_size : {}KB'.format(bin_size // 1024))
  22. # check if connected or not
  23. dut.expect_exact('MQTT_EVENT_CONNECTED', timeout=30)
  24. # check log
  25. res = dut.expect(r'sent publish successful, msg_id=(\d+)[^\d]')
  26. msgid_pub1 = res.group(1).decode('utf8')
  27. res = dut.expect(r'sent subscribe successful, msg_id=(\d+)[^\d]')
  28. msgid_sub1 = res.group(1).decode('utf8')
  29. res = dut.expect(r'sent subscribe successful, msg_id=(\d+)[^\d]')
  30. msgid_sub2 = res.group(1).decode('utf8')
  31. res = dut.expect(r'sent unsubscribe successful, msg_id=(\d+)[^\d]')
  32. msgid_unsub = res.group(1).decode('utf8')
  33. res = dut.expect(r'MQTT_EVENT_PUBLISHED, msg_id=(\d+)[^\d]')
  34. msgid_pubd = res.group(1).decode('utf8')
  35. assert msgid_pubd == msgid_pub1
  36. res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)[^\d]')
  37. msgid_subd = res.group(1).decode('utf8')
  38. assert msgid_subd == msgid_sub1
  39. dut.expect_exact('sent publish successful, msg_id=0')
  40. res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)[^\d]')
  41. msgid_subd = res.group(1).decode('utf8')
  42. assert msgid_subd == msgid_sub2
  43. dut.expect_exact('sent publish successful, msg_id=0')
  44. dut.expect_exact('MQTT_EVENT_DATA')
  45. dut.expect_exact('key is board, value is esp32')
  46. dut.expect_exact('key is u, value is user')
  47. dut.expect_exact('key is p, value is password')
  48. dut.expect_exact('payload_format_indicator is 1')
  49. dut.expect_exact('response_topic is /topic/test/response')
  50. dut.expect_exact('correlation_data is 123456')
  51. dut.expect_exact('TOPIC=/topic/qos1')
  52. dut.expect_exact('DATA=data_3')
  53. res = dut.expect(r'MQTT_EVENT_UNSUBSCRIBED, msg_id=(\d+)[^\d]')
  54. msgid_unsubd = res.group(1).decode('utf8')
  55. assert msgid_unsubd == msgid_unsub
  56. dut.expect_exact('MQTT_EVENT_DISCONNECTED')
  57. logging.info('MQTT5 pytest pass')