pytest_esp_local_ctrl.py 4.6 KB

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