From 4c62544a7563ca192ff56cf1110c8edbc28b1cd5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 12 Feb 2025 13:02:40 +0100 Subject: [PATCH] fix(examples): Test the SLIP netif example on target as well --- .../workflows/examples_build-host-test.yml | 46 ++++++++ .../slip_custom_netif/pytest_slip.py | 100 ++++++++++++++++++ .../esp_netif/slip_custom_netif/sdkconfig.ci | 6 ++ 3 files changed, 152 insertions(+) create mode 100644 examples/esp_netif/slip_custom_netif/pytest_slip.py create mode 100644 examples/esp_netif/slip_custom_netif/sdkconfig.ci diff --git a/.github/workflows/examples_build-host-test.yml b/.github/workflows/examples_build-host-test.yml index 8fea131b84..8aac77de5a 100644 --- a/.github/workflows/examples_build-host-test.yml +++ b/.github/workflows/examples_build-host-test.yml @@ -19,6 +19,9 @@ jobs: warning: "Warning: The smallest app partition is nearly full" runs-on: ubuntu-22.04 container: espressif/idf:${{ matrix.idf_ver }} + env: + TARGET_TEST: examples/esp_netif/slip_custom_netif/ + TARGET_TEST_DIR: build_esp32c3_target steps: - name: Checkout esp-protocols uses: actions/checkout@v4 @@ -31,6 +34,16 @@ jobs: python -m pip install idf-build-apps # Build default configs for all targets python ./ci/build_apps.py examples -m examples/.build-test-rules.yml -d -c + # Build target tests + python ./ci/build_apps.py ${TARGET_TEST} -r sdkconfig.ci=target + cd ${TARGET_TEST} + ${GITHUB_WORKSPACE}/ci/clean_build_artifacts.sh `pwd`/${TARGET_TEST_DIR} + zip -qur artifacts.zip ${TARGET_TEST_DIR} + - uses: actions/upload-artifact@v4 + with: + name: slip_target_${{ matrix.idf_ver }} + path: ${{ env.TARGET_TEST }}/artifacts.zip + if-no-files-found: error build_and_run_on_host: if: contains(github.event.pull_request.labels.*.name, 'examples') || github.event_name == 'push' @@ -51,3 +64,36 @@ jobs: python ./ci/build_apps.py examples/mqtt -l -t linux timeout 5 ./examples/mqtt/build_linux_default/esp_mqtt_demo.elf | tee test.log || true grep 'MQTT_EVENT_DATA' test.log + + run_on_target: + # Skip running on forks since it won't have access to secrets + if: | + github.repository == 'espressif/esp-protocols' && + ( contains(github.event.pull_request.labels.*.name, 'examples') || github.event_name == 'push' ) + name: Slip example target test + needs: build_all_examples + strategy: + matrix: + idf_ver: ["release-v5.3", "latest"] + runs-on: + - self-hosted + - modem + env: + TARGET_TEST: examples/esp_netif/slip_custom_netif/ + TARGET_TEST_DIR: build_esp32c3_target + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: slip_target_${{ matrix.idf_ver }} + path: ${{ env.TARGET_TEST }}/ci/ + - name: Run Test + working-directory: ${{ env.TARGET_TEST }} + run: | + python -m pip install pytest-embedded-serial-esp pytest-embedded-idf pytest-rerunfailures pytest-timeout pytest-ignore-test-results + unzip ci/artifacts.zip -d ci + for dir in `ls -d ci/build_*`; do + rm -rf build sdkconfig.defaults + mv $dir build + python -m pytest --log-cli-level DEBUG --target=esp32c3 + done diff --git a/examples/esp_netif/slip_custom_netif/pytest_slip.py b/examples/esp_netif/slip_custom_netif/pytest_slip.py new file mode 100644 index 0000000000..f8fd4b5133 --- /dev/null +++ b/examples/esp_netif/slip_custom_netif/pytest_slip.py @@ -0,0 +1,100 @@ +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +from __future__ import print_function, unicode_literals + +import subprocess +import time +from threading import Event, Thread + +import netifaces + + +def is_esp32(port): + """ + Check if the given port is connected to an ESP32 using esptool. + """ + try: + result = subprocess.run( + ['esptool.py', '--port', port, 'chip_id'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True + ) + return 'ESP32' in result.stdout + except subprocess.CalledProcessError: + return False + + +def run_server(server_stop, port): + print('Launching SLIP netif on port: {}'.format(port)) + try: + arg_list = ['sudo', 'slattach', '-v', '-L', '-s', '115200','-p', 'slip', port] + p = subprocess.Popen(arg_list, stdout=subprocess.PIPE, bufsize=1) + while not server_stop.is_set(): + if p.poll() is not None: + if p.poll() == 16: + print('[SLIP:] Terminated: hang-up received') + break + else: + raise ValueError( + 'ENV_TEST_FAILURE: SLIP terminated unexpectedly with {}' + .format(p.poll())) + line = p.stdout.readline() + if line: + print('[SLIP:]{}'.format(line.rstrip())) + time.sleep(0.1) + except Exception as e: + print(e) + raise ValueError('ENV_TEST_FAILURE: Error running SLIP netif') + finally: + p.terminate() + print('SLIP netif stopped') + + +def test_examples_protocol_slip(dut): + + # the PPP test env is reused for SLIP test and it uses three ttyUSB's: two for ESP32 board and another one for the ppp server + server_port = None + for i in ['/dev/ttyUSB0', '/dev/ttyUSB1', '/dev/ttyUSB2']: + if i == dut.serial.port: + print(f'DUT port: {i}') + elif is_esp32(i): + print(f'Some other ESP32: {i}') + else: + print(f'Port for PPPD: {i}') + server_port = i + if server_port is None: + print( + 'ENV_TEST_FAILURE: Cannot locate PPPD port' + ) + raise + # Attach to the SLI netif + server_stop = Event() + t = Thread(target=run_server, args=(server_stop, server_port)) + t.start() + try: + ppp_server_timeout = time.time() + 30 + while 'sl0' not in netifaces.interfaces(): + print( + "SLIP netif haven't appear yet, list of active netifs:{}" + .format(netifaces.interfaces())) + time.sleep(0.5) + if time.time() > ppp_server_timeout: + raise ValueError( + 'ENV_TEST_FAILURE: SLIP netif failed to setup sl0 interface within timeout' + ) + + cmd = ['sudo', 'ifconfig', 'sl0', '10.0.0.1', 'dstaddr', '10.0.0.2'] + try: + subprocess.run(cmd, check=True) + print('SLIP interface configured successfully.') + except subprocess.CalledProcessError as e: + print(f'Failed to configure SLIP interface: {e}') + cmd = ['ping', '10.0.0.2'] + try: + subprocess.run(cmd, check=True) + print('Pinging SLIP interface successfully.') + except subprocess.CalledProcessError as e: + print(f'Failed to ping SLIP interface: {e}') + + finally: + server_stop.set() + t.join() diff --git a/examples/esp_netif/slip_custom_netif/sdkconfig.ci b/examples/esp_netif/slip_custom_netif/sdkconfig.ci new file mode 100644 index 0000000000..0d5023936f --- /dev/null +++ b/examples/esp_netif/slip_custom_netif/sdkconfig.ci @@ -0,0 +1,6 @@ +CONFIG_IDF_TARGET="esp32c3" +CONFIG_LWIP_PPP_SUPPORT=y +CONFIG_LWIP_SLIP_SUPPORT=y +CONFIG_EXAMPLE_IPV4=y +CONFIG_EXAMPLE_UART_RX_PIN=6 +CONFIG_EXAMPLE_UART_RX_PIN=7