Ver Fonte

esp_local_ctrl example pytest migration

Harshit Malpani há 3 anos atrás
pai
commit
63b1b1bd44

+ 2 - 0
examples/protocols/esp_local_ctrl/README.md

@@ -1,3 +1,5 @@
+| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 |
+| ----------------- | ----- | -------- | -------- | -------- |
 # ESP Local Control using HTTPS server
 
 This example creates a `esp_local_ctrl` service over HTTPS transport, for securely controlling the device over local network. In this case the device name is resolved through `mDNS`, which in this example is `my_esp_ctrl_device.local`.

+ 47 - 21
examples/protocols/esp_local_ctrl/example_test.py → examples/protocols/esp_local_ctrl/pytest_esp_local_ctrl.py

@@ -1,30 +1,60 @@
+# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+# SPDX-License-Identifier: Unlicense OR CC0-1.0
 from __future__ import unicode_literals
 
+import logging
 import os
 import re
 import sys
 
-import ttfw_idf
+import pexpect
+import pytest
+from pytest_embedded import Dut
 
 
-@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
-def test_examples_esp_local_ctrl(env, extra_data):
+def get_sdk_path() -> str:
+    idf_path = os.getenv('IDF_PATH')
+    assert idf_path
+    assert os.path.exists(idf_path)
+    return idf_path
+
+
+class CustomProcess(object):
+    def __init__(self, cmd: str, logfile: str, verbose:bool =True) -> None:
+        self.verbose = verbose
+        self.f = open(logfile, 'w')
+        if self.verbose:
+            logging.info('Starting {} > {}'.format(cmd, self.f.name))
+        self.pexpect_proc = pexpect.spawn(cmd, timeout=60, logfile=self.f, encoding='utf-8', codec_errors='ignore')
+
+    def __enter__(self):    # type: ignore
+        return self
+
+    def close(self) -> None:
+        self.pexpect_proc.terminate(force=True)
+
+    def __exit__(self, type, value, traceback):     # type: ignore
+        self.close()
+        self.f.close()
+
+
+@pytest.mark.supported_targets
+@pytest.mark.wifi
+def test_examples_esp_local_ctrl(dut: Dut) -> None:
 
     rel_project_path = os.path.join('examples', 'protocols', 'esp_local_ctrl')
-    dut = env.get_dut('esp_local_ctrl', rel_project_path)
-    idf_path = dut.app.get_sdk_path()
-    dut.start_app()
+    idf_path = get_sdk_path()
 
-    dut_ip = dut.expect(re.compile(r'esp_netif_handlers: sta ip: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'))[0]
+    dut_ip = dut.expect(r'esp_netif_handlers: sta ip: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')[1].decode()
     dut.expect('esp_https_server: Starting server')
     dut.expect('esp_https_server: Server listening on port 443')
     dut.expect('control: esp_local_ctrl service started with name : my_esp_ctrl_device')
 
-    def dut_expect_read():
-        dut.expect('control: Reading property : timestamp (us)')
-        dut.expect('control: Reading property : property1')
-        dut.expect('control: Reading property : property2')
-        dut.expect('control: Reading property : property3')
+    def dut_expect_read() -> None:
+        dut.expect_exact('control: Reading property : timestamp (us)')
+        dut.expect_exact('control: Reading property : property1')
+        dut.expect_exact('control: Reading property : property2')
+        dut.expect_exact('control: Reading property : property3')
 
     # Running mDNS services in docker is not a trivial task. Therefore, the script won't connect to the host name but
     # to IP address. However, the certificates were generated for the host name and will be rejected.
@@ -33,9 +63,9 @@ def test_examples_esp_local_ctrl(env, extra_data):
                     '--name', dut_ip,
                     '--dont-check-hostname'])  # don't reject the certificate because of the hostname
     esp_local_ctrl_log = os.path.join(idf_path, rel_project_path, 'esp_local_ctrl.log')
-    with ttfw_idf.CustomProcess(cmd, esp_local_ctrl_log) as ctrl_py:
+    with CustomProcess(cmd, esp_local_ctrl_log) as ctrl_py:
 
-        def expect_properties(prop1, prop3):
+        def expect_properties(prop1: int, prop3: str) -> None:
             dut_expect_read()
             ctrl_py.pexpect_proc.expect_exact('==== Available Properties ====')
             ctrl_py.pexpect_proc.expect(re.compile(r'S.N. Name\s+Type\s+Flags\s+Value'))
@@ -56,26 +86,22 @@ def test_examples_esp_local_ctrl(env, extra_data):
         ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (timestamp (us)) :')
         ctrl_py.pexpect_proc.sendline('2')
         ctrl_py.pexpect_proc.expect_exact('Failed to set values!')
-        dut.expect('control: timestamp (us) is read-only')
+        dut.expect_exact('control: timestamp (us) is read-only')
         expect_properties(property1, property3)
 
         property1 = 638
         ctrl_py.pexpect_proc.sendline('2')
         ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property1) :')
         ctrl_py.pexpect_proc.sendline(str(property1))
-        dut.expect('control: Setting property1 value to {}'.format(property1))
+        dut.expect_exact('control: Setting property1 value to {}'.format(property1))
         expect_properties(property1, property3)
 
         property3 = 'test'
         ctrl_py.pexpect_proc.sendline('4')
         ctrl_py.pexpect_proc.expect_exact('Enter value to set for property (property3) :')
         ctrl_py.pexpect_proc.sendline(property3)
-        dut.expect('control: Setting property3 value to {}'.format(property3))
+        dut.expect_exact('control: Setting property3 value to {}'.format(property3))
         expect_properties(property1, property3)
 
         ctrl_py.pexpect_proc.sendline('q')
         ctrl_py.pexpect_proc.expect_exact('Quitting...')
-
-
-if __name__ == '__main__':
-    test_examples_esp_local_ctrl()