pytest_esp_local_ctrl.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # SPDX-FileCopyrightText: 2022 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.supported_targets
  32. @pytest.mark.temp_skip_ci(targets=['esp32c6'], reason='c6 support TBD')
  33. @pytest.mark.wifi_router
  34. def test_examples_esp_local_ctrl(dut: Dut) -> None:
  35. rel_project_path = os.path.join('examples', 'protocols', 'esp_local_ctrl')
  36. idf_path = get_sdk_path()
  37. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  38. dut.expect('Please input ssid password:')
  39. env_name = 'wifi_router'
  40. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  41. ap_password = get_env_config_variable(env_name, 'ap_password')
  42. dut.write(f'{ap_ssid} {ap_password}')
  43. dut_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]')[1].decode()
  44. dut.expect('esp_https_server: Starting server')
  45. dut.expect('esp_https_server: Server listening on port 443')
  46. dut.expect('control: esp_local_ctrl service started with name : my_esp_ctrl_device')
  47. def dut_expect_read() -> None:
  48. dut.expect_exact('control: Reading property : timestamp (us)', timeout=20)
  49. dut.expect_exact('control: Reading property : property1')
  50. dut.expect_exact('control: Reading property : property2')
  51. dut.expect_exact('control: Reading property : property3')
  52. # Running mDNS services in docker is not a trivial task. Therefore, the script won't connect to the host name but
  53. # to IP address. However, the certificates were generated for the host name and will be rejected.
  54. cmd = ' '.join([sys.executable, os.path.join(idf_path, rel_project_path, 'scripts/esp_local_ctrl.py'),
  55. '--sec_ver 2',
  56. '--sec2_username wifiprov',
  57. '--sec2_pwd abcd1234',
  58. '--name', dut_ip,
  59. '--dont-check-hostname']) # don't reject the certificate because of the hostname
  60. esp_local_ctrl_log = os.path.join(idf_path, rel_project_path, 'esp_local_ctrl.log')
  61. with CustomProcess(cmd, esp_local_ctrl_log) as ctrl_py:
  62. def expect_properties(prop1: int, prop3: str) -> None:
  63. dut_expect_read()
  64. ctrl_py.pexpect_proc.expect_exact('==== Available Properties ====')
  65. ctrl_py.pexpect_proc.expect(re.compile(r'S.N. Name\s+Type\s+Flags\s+Value'))
  66. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 1\] timestamp \(us\)\s+TIME\(us\)\s+Read-Only\s+\d+'))
  67. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 2\] property1\s+INT32\s+{}'.format(prop1)))
  68. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 3\] property2\s+BOOLEAN\s+Read-Only\s+(True)|(False)'))
  69. ctrl_py.pexpect_proc.expect(re.compile(r'\[ 4\] property3\s+STRING\s+{}'.format(prop3)))
  70. ctrl_py.pexpect_proc.expect_exact('Select properties to set (0 to re-read, \'q\' to quit) :')
  71. property1 = 123456789
  72. property3 = ''
  73. ctrl_py.pexpect_proc.expect_exact('Connecting to {}'.format(dut_ip))
  74. dut.expect('esp_https_server: performing session handshake', timeout=60)
  75. expect_properties(property1, property3)
  76. ctrl_py.pexpect_proc.sendline('1')
  77. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (timestamp (us)) :')
  78. ctrl_py.pexpect_proc.sendline('2')
  79. ctrl_py.pexpect_proc.expect_exact('Failed to set values!')
  80. dut.expect_exact('control: timestamp (us) is read-only')
  81. expect_properties(property1, property3)
  82. property1 = 638
  83. ctrl_py.pexpect_proc.sendline('2')
  84. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property1) :')
  85. ctrl_py.pexpect_proc.sendline(str(property1))
  86. dut.expect_exact('control: Setting property1 value to {}'.format(property1))
  87. expect_properties(property1, property3)
  88. property3 = 'test'
  89. ctrl_py.pexpect_proc.sendline('4')
  90. ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property3) :')
  91. ctrl_py.pexpect_proc.sendline(property3)
  92. dut.expect_exact('control: Setting property3 value to {}'.format(property3))
  93. expect_properties(property1, property3)
  94. ctrl_py.pexpect_proc.sendline('q')
  95. ctrl_py.pexpect_proc.expect_exact('Quitting...')