pytest_esp_local_ctrl.py 5.8 KB

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