Skip to content

Commit 12cfcb5

Browse files
committed
fix(mdns): Make unit test executable with pytest
1 parent 2cfffb0 commit 12cfcb5

File tree

8 files changed

+88
-59
lines changed

8 files changed

+88
-59
lines changed

.github/workflows/target-test.yml

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,30 @@ jobs:
6262
matrix:
6363
idf_ver: ["latest"]
6464
idf_target: ["esp32", "esp32s2", "esp32c3"]
65-
config: ["eth_custom_netif", "eth_def", "eth_no_ipv6", "eth_socket"]
65+
test: [ { app: example, path: "components/mdns/examples" }, { app: unit_test, path: "components/mdns/tests/unit_test" } ]
6666
runs-on: ubuntu-20.04
6767
container: espressif/idf:${{ matrix.idf_ver }}
68-
env:
69-
TEST_DIR: components/mdns/examples
7068
steps:
7169
- name: Checkout esp-protocols
7270
uses: actions/checkout@v3
7371
with:
7472
submodules: recursive
75-
- name: Build ${{ matrix.example }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }} for ${{ matrix.config }}
76-
env:
77-
IDF_TARGET: ${{ matrix.idf_target }}
73+
- name: Build ${{ matrix.test.app }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }}
7874
shell: bash
79-
working-directory: ${{ env.TEST_DIR }}
75+
working-directory: ${{ matrix.test.path }}
8076
run: |
77+
${IDF_PATH}/install.sh --enable-pytest
8178
. ${IDF_PATH}/export.sh
82-
rm -rf sdkconfig sdkconfig.defaults build build_${{ matrix.config }}
83-
cat sdkconfig.ci.${{ matrix.config }} >> sdkconfig.defaults
84-
idf.py set-target ${{ matrix.idf_target }}
85-
idf.py build
86-
mv build build_${{ matrix.config }}
87-
- name: Merge binaries with IDF-${{ matrix.idf_ver }} for ${{ matrix.config }}
88-
working-directory: ${{ env.TEST_DIR }}
89-
env:
90-
IDF_TARGET: ${{ matrix.idf_target }}
91-
shell: bash
92-
run: |
93-
. ${IDF_PATH}/export.sh
94-
cd build_${{ matrix.config }}
95-
esptool.py --chip ${{ matrix.idf_target }} merge_bin --fill-flash-size 4MB -o flash_image.bin @flash_args
79+
python $IDF_PATH/tools/ci/ci_build_apps.py . --target ${{ matrix.idf_target }} -vv --preserve-all --pytest-app
80+
for dir in `ls -d build_*`; do
81+
$GITHUB_WORKSPACE/ci/clean_build_artifacts.sh `pwd`/$dir
82+
zip -qur artifacts.zip $dir
83+
done
9684
- uses: actions/upload-artifact@v3
85+
if: ${{ matrix.idf_target }} == "esp32"
9786
with:
98-
name: examples_app_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.config }}
99-
path: |
100-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/bootloader/bootloader.bin
101-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/partition_table/partition-table.bin
102-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/*.bin
103-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/*.elf
104-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/flasher_args.json
105-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/config/sdkconfig.h
106-
${{ env.TEST_DIR }}/build_${{ matrix.config }}/config/sdkconfig.json
87+
name: mdns_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.test.app }}
88+
path: ${{ matrix.test.path }}/artifacts.zip
10789
if-no-files-found: error
10890

10991
build_asio:
@@ -240,14 +222,12 @@ jobs:
240222
matrix:
241223
idf_ver: ["latest"]
242224
idf_target: ["esp32"]
243-
config: ["eth_custom_netif", "eth_def", "eth_no_ipv6", "eth_socket"]
244-
name: Run mDNS Example Test on target
225+
test: [ { app: example, path: "components/mdns/examples" }, { app: unit_test, path: "components/mdns/tests/unit_test" } ]
226+
name: Run mDNS target tests
245227
needs: build_mdns
246228
runs-on:
247229
- self-hosted
248230
- ESP32-ETHERNET-KIT
249-
env:
250-
TEST_DIR: components/mdns/examples
251231
# Skip running on forks since it won't have access to secrets
252232
if: github.repository == 'espressif/esp-protocols'
253233
steps:
@@ -256,28 +236,27 @@ jobs:
256236
- uses: actions/checkout@v3
257237
- uses: actions/download-artifact@v3
258238
with:
259-
name: examples_app_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.config }}
260-
path: ${{ env.TEST_DIR }}/build_${{ matrix.config }}
239+
name: mdns_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.test.app }}
240+
path: ${{ matrix.test.path }}/ci/
261241
- name: Install Python packages
262242
env:
263243
PIP_EXTRA_INDEX_URL: "https://www.piwheels.org/simple"
264244
run: |
265245
sudo apt-get install -y dnsutils
266-
- name: Download Example Test to target ${{ matrix.config }}
267-
run: |
268-
python -m esptool --chip ${{ matrix.idf_target }} write_flash 0x0 components/mdns/examples/build_${{ matrix.config }}/flash_image.bin
269-
- name: Run Example Test on target ${{ matrix.config }}
270-
working-directory: components/mdns/examples
246+
- name: Run ${{ matrix.test.app }} application on ${{ matrix.idf_target }}
247+
working-directory: ${{ matrix.test.path }}
271248
run: |
249+
unzip ci/artifacts.zip -d ci
250+
for dir in `ls -d ci/build_*`; do
272251
rm -rf build sdkconfig.defaults
273-
mv build_${{ matrix.config }} build
274-
cat sdkconfig.ci.${{ matrix.config }} >> sdkconfig.defaults
275-
python -m pytest --log-cli-level DEBUG --junit-xml=./examples_results_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.config }}.xml --target=${{ matrix.idf_target }}
252+
mv $dir build
253+
python -m pytest --log-cli-level DEBUG --junit-xml=./results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${dir#"ci/build_"}.xml --target=${{ matrix.idf_target }}
254+
done
276255
- uses: actions/upload-artifact@v3
277256
if: always()
278257
with:
279-
name: examples_results_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.config }}
280-
path: ${{ env.TEST_DIR }}/*.xml
258+
name: results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}.xml
259+
path: ${{ matrix.test.path }}/*.xml
281260

282261
run-target-asio:
283262
strategy:
@@ -348,7 +327,6 @@ jobs:
348327
idf.py set-target ${{ matrix.idf_target }}
349328
idf.py build
350329
$GITHUB_WORKSPACE/ci/clean_build_artifacts.sh ${GITHUB_WORKSPACE}/${TEST_DIR}/build
351-
ls build
352330
- uses: actions/upload-artifact@v3
353331
with:
354332
name: modem_target_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.test.app }}

ci/clean_build_artifacts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# - flasher args
55
# - sdkconfigs (header and json)
66
# (Ignoring the command failure as it refuses to delete nonempty dirs)
7-
find $1 ! -regex ".*/build/[^/]+.\(bin\|elf\)" -a ! -regex ".*\(bootloader\|partition-table\).bin" -a ! -name "flasher_args.json" -a ! -regex ".*/build/config/sdkconfig.\(h\|json\)" -delete || true
7+
find $1 ! -regex ".*/build[^/]*/[^/]+.\(bin\|elf\)" -a ! -regex ".*\(bootloader\|partition-table\).bin" -a ! -name "flasher_args.json" -a ! -regex ".*/build[^/]*/config/sdkconfig.\(h\|json\)" -delete || true

components/mdns/examples/pytest_mdns.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Unlicense OR CC0-1.0
33
import re
44
import select
@@ -8,8 +8,11 @@
88
import time
99
from threading import Event, Thread
1010

11-
import dpkt
12-
import dpkt.dns
11+
try:
12+
import dpkt
13+
import dpkt.dns
14+
except ImportError:
15+
pass
1316

1417

1518
def get_dns_query_for_esp(esp_host):
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
idf_component_register(SRC_DIRS "."
2-
PRIV_REQUIRES cmock test_utils mdns)
1+
# This is the project CMakeLists.txt file for the test subproject
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
set(EXTRA_COMPONENT_DIRS ../.. "$ENV{IDF_PATH}/tools/unit-test-app/components")
5+
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
project(mdns_test)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
idf_component_register(SRCS "test_mdns.c"
3+
REQUIRES test_utils
4+
INCLUDE_DIRS "."
5+
PRIV_REQUIRES unity mdns)

components/mdns/tests/unit_test/test_mdns.c renamed to components/mdns/tests/unit_test/main/test_mdns.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
6-
#include "test_utils.h"
6+
77
#include "mdns.h"
88
#include "esp_event.h"
99
#include "unity.h"
10+
#include "test_utils.h"
1011

12+
#include "unity_fixture.h"
13+
#include "memory_checks.h"
1114

1215
#define MDNS_HOSTNAME "test-hostname"
1316
#define MDNS_DELEGATE_HOSTNAME "delegate-hostname"
@@ -16,6 +19,18 @@
1619
#define MDNS_SERVICE_PROTO "_tcp"
1720
#define MDNS_SERVICE_PORT 80
1821

22+
TEST_GROUP(mdns);
23+
24+
TEST_SETUP(mdns)
25+
{
26+
test_utils_record_free_mem();
27+
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
28+
}
29+
30+
TEST_TEAR_DOWN(mdns)
31+
{
32+
test_utils_finish_and_evaluate_leaks(32, 64);
33+
}
1934

2035
static void yield_to_all_priorities(void)
2136
{
@@ -27,15 +42,15 @@ static void yield_to_all_priorities(void)
2742
}
2843

2944

30-
TEST_CASE("mdns api to fail in invalid state", "[mdns][leaks=64]")
45+
TEST(mdns, api_fails_with_invalid_state)
3146
{
3247
TEST_ASSERT_NOT_EQUAL(ESP_OK, mdns_init() );
3348
TEST_ASSERT_NOT_EQUAL(ESP_OK, mdns_hostname_set(MDNS_HOSTNAME) );
3449
TEST_ASSERT_NOT_EQUAL(ESP_OK, mdns_instance_name_set(MDNS_INSTANCE) );
3550
TEST_ASSERT_NOT_EQUAL(ESP_OK, mdns_service_add(MDNS_INSTANCE, MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_SERVICE_PORT, NULL, 0) );
3651
}
3752

38-
TEST_CASE("mdns init and deinit", "[mdns][leaks=64]")
53+
TEST(mdns, init_deinit)
3954
{
4055
test_case_uses_tcpip();
4156
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
@@ -45,7 +60,7 @@ TEST_CASE("mdns init and deinit", "[mdns][leaks=64]")
4560
esp_event_loop_delete_default();
4661
}
4762

48-
TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][leaks=64]")
63+
TEST(mdns, api_fails_with_expected_err)
4964
{
5065
mdns_txt_item_t serviceTxtData[CONFIG_MDNS_MAX_SERVICES] = { {NULL, NULL},
5166
};
@@ -91,7 +106,7 @@ TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][le
91106
esp_event_loop_delete_default();
92107
}
93108

94-
TEST_CASE("mdns query api return expected err-code and do not leak memory", "[leaks=64]")
109+
TEST(mdns, query_api_fails_with_expected_err)
95110
{
96111
mdns_result_t *results = NULL;
97112
esp_ip6_addr_t addr6;
@@ -118,3 +133,16 @@ TEST_CASE("mdns query api return expected err-code and do not leak memory", "[le
118133
mdns_free();
119134
esp_event_loop_delete_default();
120135
}
136+
137+
TEST_GROUP_RUNNER(mdns)
138+
{
139+
RUN_TEST_CASE(mdns, api_fails_with_invalid_state)
140+
RUN_TEST_CASE(mdns, api_fails_with_expected_err)
141+
RUN_TEST_CASE(mdns, query_api_fails_with_expected_err)
142+
RUN_TEST_CASE(mdns, init_deinit)
143+
}
144+
145+
void app_main(void)
146+
{
147+
UNITY_MAIN(mdns);
148+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from pytest_embedded import Dut
5+
6+
7+
def test_lwip(dut: Dut) -> None:
8+
dut.expect_unity_test_output()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_UNITY_ENABLE_FIXTURE=y
2+
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n

0 commit comments

Comments
 (0)