pytest_esp_local_ctrl.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. import logging
  4. import os
  5. import re
  6. import sys
  7. import pexpect
  8. import pytest
  9. from common_test_methods import get_env_config_variable
  10. from pytest_embedded import Dut
  11. def get_sdk_path() -> str:
  12. idf_path = os.getenv('IDF_PATH')
  13. assert idf_path
  14. assert os.path.exists(idf_path)
  15. return idf_path
  16. class CustomProcess(object):
  17. def __init__(self, cmd: str, logfile: str, verbose:bool =True) -> None:
  18. self.verbose = verbose
  19. self.f = open(logfile, 'w')
  20. if self.verbose:
  21. logging.info('Starting {} > {}'.format(cmd, self.f.name))
  22. self.pexpect_proc = pexpect.spawn(cmd, timeout=60, logfile=self.f, encoding='utf-8', codec_errors='ignore')
  23. def __enter__(self): # type: ignore
  24. return self
  25. def close(self) -> None:
  26. self.pexpect_proc.terminate(force=True)
  27. def __exit__(self, type, value, traceback): # type: ignore
  28. self.close()
  29. self.f.close()
  30. @pytest.mark.esp32
  31. @pytest.mark.esp32c3
  32. @pytest.mark.esp32s3
  33. @pytest.mark.wifi_router
  34. @pytest.mark.parametrize(
  35. 'config',
  36. [
  37. 'default',
  38. 'http',
  39. ],
  40. indirect=True,
  41. )
  42. def test_examples_esp_local_ctrl(config: str, dut: Dut) -> None:
  43. rel_project_path = os.path.join('examples', 'protocols', 'esp_local_ctrl')
  44. idf_path = get_sdk_path()
  45. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  46. dut.expect('Please input ssid password:')
  47. env_name = 'wifi_router'
  48. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  49. ap_password = get_env_config_variable(env_name, 'ap_password')
  50. dut.write(f'{ap_ssid} {ap_password}')
  51. dut_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]')[1].decode()
  52. if config == 'default':
  53. dut.expect('esp_https_server: Starting server')
  54. dut.expect('esp_https_server: Server listening on port 443')
  55. dut.expect('control: esp_local_ctrl service started with name : my_esp_ctrl_device')
  56. def dut_expect_read() -> None:
  57. dut.expect_exact('control: Reading property : timestamp (us)', timeout=20)
  58. dut.expect_exact('control: Reading property : property1')
  59. dut.expect_exact('control: Reading property : property2')
  60. dut.expect_exact('control: Reading property : property3')
  61. # Running mDNS services in docker is not a trivial task. Therefore, the script won't connect to the host name but
  62. # to IP address. However, the certificates were generated for the host name and will be rejected.
  63. if config == 'default':
  64. cmd = ' '.join([sys.executable, os.path.join(idf_path, rel_project_path, 'scripts/esp_local_ctrl.py'),
  65. '--sec_ver 2',
  66. '--sec2_username wifiprov',
  67. '--sec2_pwd abcd1234',
  68. '--name', dut_ip,
  69. '--dont-check-hostname']) # don't reject the certificate because of the hostname
  70. elif config == 'http':
  71. cmd = ' '.join([sys.executable, os.path.join(idf_path, rel_project_path, 'scripts/esp_local_ctrl.py'),
  72. '--sec_ver 2',
  73. '--transport http',
  74. '--sec2_username wifiprov',
  75. '--sec2_pwd abcd1234',
  76. '--name', dut_ip,
  77. '--dont-check-hostname'])
  78. esp_local_ctrl_log = os.path.join(idf_path, rel_project_path, 'esp_local_ctrl.log')
  79. with CustomProcess(cmd, esp_local_ctrl_log) as ctrl_py:
  80. def expect_properties(prop1: int, prop3: str) -> None:
  81. dut_expect_read()
  82. ctrl_py.pexpect_proc.expect_exact('==== Available Properties ====')
  83. ctrl_py.pexpect_proc.expect(re.compile(r'S.N. Name\s+Type\s+Flags\s+Value'))
  84. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 1\] timestamp \(us\)\s+TIME\(us\)\s+Read-Only\s+\d+'))
  85. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 2\] property1\s+INT32\s+{}'.format(prop1)))
  86. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 3\] property2\s+BOOLEAN\s+Read-Only\s+(True)|(False)'))
  87. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 4\] property3\s+STRING\s+{}'.format(prop3)))
  88. ctrl_py.pexpect_proc.expect_exact('Select properties to set (0 to re-read, \'q\' to quit) :')
  89. property1 = 123456789
  90. property3 = ''
  91. ctrl_py.pexpect_proc.expect_exact('Connecting to {}'.format(dut_ip))
  92. if config == 'default':
  93. dut.expect('esp_https_server: performing session handshake', timeout=60)
  94. expect_properties(property1, property3)
  95. ctrl_py.pexpect_proc.sendline('1')
  96. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (timestamp (us)) :')
  97. ctrl_py.pexpect_proc.sendline('2')
  98. ctrl_py.pexpect_proc.expect_exact('Failed to set values!')
  99. dut.expect_exact('control: timestamp (us) is read-only')
  100. expect_properties(property1, property3)
  101. property1 = 638
  102. ctrl_py.pexpect_proc.sendline('2')
  103. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property1) :')
  104. ctrl_py.pexpect_proc.sendline(str(property1))
  105. dut.expect_exact('control: Setting property1 value to {}'.format(property1))
  106. expect_properties(property1, property3)
  107. property3 = 'test'
  108. ctrl_py.pexpect_proc.sendline('4')
  109. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property3) :')
  110. ctrl_py.pexpect_proc.sendline(property3)
  111. dut.expect_exact('control: Setting property3 value to {}'.format(property3))
  112. expect_properties(property1, property3)
  113. ctrl_py.pexpect_proc.sendline('q')
  114. ctrl_py.pexpect_proc.expect_exact('Quitting...')