pytest_esp_local_ctrl.py 5.0 KB

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