ci: Migrateunit test for mqtt test to unit test app

- Split mqtt and mqtt5 tests
 - Move common test code to a common component
 - Add necessary python scripts
This commit is contained in:
Euripedes Rocha 2023-03-15 07:18:14 +01:00
parent 826c927c09
commit 1acfc9b5e0
29 changed files with 189 additions and 82 deletions

View File

@ -0,0 +1,7 @@
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
components/mqtt/test_apps:
disable_test:
- if: IDF_TARGET not in ["esp32", "esp32c2"]
temporary: false
reason: Not needed to test on all targets (chosen two, one for each architecture)

View File

@ -0,0 +1,4 @@
idf_component_register(SRCS test_mqtt_connection.c
INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include
PRIV_REQUIRES unity esp_event esp_netif esp_eth)

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -0,0 +1,9 @@
#This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
"../common")
set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_mqtt_client_test)

View File

@ -0,0 +1,2 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |

View File

@ -1,9 +1,9 @@
set(srcs test_mqtt_client_broker.c test_mqtt_connection.c test_mqtt.c)
set(srcs test_mqtt_client_broker.c test_mqtt.c)
if(CONFIG_MQTT_PROTOCOL_5)
list(APPEND srcs test_mqtt5_client_broker.c test_mqtt5.c)
endif()
idf_component_register(SRCS "${srcs}"
PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash)
PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -11,33 +11,30 @@
*/
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "unity.h"
#include "unity_fixture.h"
#include "unity_fixture_extras.h"
#include "test_utils.h"
#include "memory_checks.h"
#include "mqtt_client.h"
#include "nvs_flash.h"
#include "esp_ota_ops.h"
#include "sdkconfig.h"
#include "test_mqtt_client_broker.h"
#include "test_mqtt_connection.h"
#include "esp_mac.h"
#include "esp_partition.h"
static void test_leak_setup(const char * file, long line)
{
uint8_t mac[6];
struct timeval te;
gettimeofday(&te, NULL); // get current time
esp_read_mac(mac, ESP_MAC_WIFI_STA);
printf("%s:%ld: time=%jd.%lds, mac:" MACSTR "\n", file, line, (intmax_t)te.tv_sec, te.tv_usec, MAC2STR(mac));
TEST_GROUP(mqtt);
TEST_SETUP(mqtt){
test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
}
TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]")
TEST_TEAR_DOWN(mqtt){
test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
}
TEST(mqtt, init_with_invalid_url)
{
test_leak_setup(__FILE__, __LINE__);
const esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = "INVALID",
};
@ -45,9 +42,8 @@ TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]")
TEST_ASSERT_EQUAL(NULL, client );
}
TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]")
TEST(mqtt, init_and_deinit)
{
test_leak_setup(__FILE__, __LINE__);
const esp_mqtt_client_config_t mqtt_cfg = {
// no connection takes place, but the uri has to be valid for init() to succeed
.broker.address.uri = "mqtts://localhost:8883",
@ -66,10 +62,13 @@ static const char* this_bin_addr(void)
return binary_address;
}
TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]")
TEST(mqtt, enqueue_and_destroy_outbox)
{
const char * bin_addr = this_bin_addr();
test_leak_setup(__FILE__, __LINE__);
// Reseting leak detection since this_bin_addr adds to allocated memory.
test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
const int messages = 20;
const int size = 2000;
const esp_mqtt_client_config_t mqtt_cfg = {
@ -93,7 +92,7 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]")
/**
* This test cases uses ethernet kit, so build and use it only if EMAC supported
*/
TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]")
TEST(mqtt, broker_tests)
{
test_case_uses_tcpip();
connect_test_fixture_setup();
@ -105,5 +104,19 @@ TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]")
connect_test_fixture_teardown();
}
#endif // SOC_EMAC_SUPPORTED
TEST_GROUP_RUNNER(mqtt) {
RUN_TEST_CASE(mqtt, init_with_invalid_url);
RUN_TEST_CASE(mqtt, init_and_deinit);
RUN_TEST_CASE(mqtt, enqueue_and_destroy_outbox);
#if SOC_EMAC_SUPPORTED
RUN_TEST_CASE(mqtt, broker_tests);
#endif // SOC_EMAC_SUPPORTED
}
void app_main(void){
UNITY_MAIN(mqtt);
}

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import pytest
from pytest_embedded import Dut
@pytest.mark.esp32
@pytest.mark.esp32c2
@pytest.mark.ethernet
def test_mqtt_client(dut: Dut) -> None:
dut.expect_unity_test_output()

View File

@ -0,0 +1,3 @@
# General options for additional checks
CONFIG_ESP_TASK_WDT_EN=n
CONFIG_UNITY_ENABLE_FIXTURE=y

View File

@ -0,0 +1,9 @@
#This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
"../common")
set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_mqtt5_client_test)

View File

@ -0,0 +1,2 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |

View File

@ -0,0 +1,5 @@
set(srcs test_mqtt5_client_broker.c test_mqtt5.c)
idf_component_register(SRCS "${srcs}"
PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -0,0 +1,14 @@
menu "ESP-MQTT Unit Test Config"
config MQTT_TEST_BROKER_URI
string "URI of the test broker"
default "mqtt://mqtt.eclipseprojects.io"
help
URL of an mqtt broker which this test connects to.
config MQTT5_TEST_BROKER_URI
string "URI of the test broker"
default "mqtt://mqtt.eclipseprojects.io"
help
URL of an mqtt broker which this test connects to.
endmenu

View File

@ -5,62 +5,60 @@
*/
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "unity.h"
#include "unity_fixture.h"
#include "unity_fixture_extras.h"
#include "test_utils.h"
#include "memory_checks.h"
#include "mqtt_client.h"
#include "nvs_flash.h"
#include "esp_ota_ops.h"
#include "sdkconfig.h"
#include "test_mqtt5_client_broker.h"
#include "test_mqtt_connection.h"
#include "esp_mac.h"
#include "esp_partition.h"
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2)
TEST_GROUP(mqtt5);
TEST_SETUP(mqtt5)
{
test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
}
TEST_TEAR_DOWN(mqtt5)
{
test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
}
static esp_mqtt5_user_property_item_t user_property_arr[3] = {
{"board", "esp32"},
{"u", "user"},
{"p", "password"}
};
static void test_leak_setup(const char * file, long line)
TEST(mqtt5, init_with_invalid_url)
{
uint8_t mac[6];
struct timeval te;
gettimeofday(&te, NULL); // get current time
esp_read_mac(mac, ESP_MAC_WIFI_STA);
printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac));
test_utils_record_free_mem();
}
TEST_CASE("mqtt5 init with invalid url", "[mqtt5][leaks=0]")
{
test_leak_setup(__FILE__, __LINE__);
const esp_mqtt_client_config_t mqtt5_cfg = {
.broker.address.uri = "INVALID",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
.broker.address.uri = "INVALID",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg);
TEST_ASSERT_EQUAL(NULL, client );
}
TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]")
TEST(mqtt5, init_and_deinit)
{
test_leak_setup(__FILE__, __LINE__);
const esp_mqtt_client_config_t mqtt5_cfg = {
// no connection takes place, but the uri has to be valid for init() to succeed
.broker.address.uri = "mqtts://localhost:8883",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
.credentials.username = "123",
.credentials.authentication.password = "456",
.session.last_will.topic = "/topic/will",
.session.last_will.msg = "i will leave",
.session.last_will.msg_len = 12,
.session.last_will.qos = 1,
.session.last_will.retain = true,
// no connection takes place, but the uri has to be valid for init() to succeed
.broker.address.uri = "mqtts://localhost:8883",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
.credentials.username = "123",
.credentials.authentication.password = "456",
.session.last_will.topic = "/topic/will",
.session.last_will.msg = "i will leave",
.session.last_will.msg_len = 12,
.session.last_will.qos = 1,
.session.last_will.retain = true,
};
esp_mqtt5_connection_property_config_t connect_property = {
.session_expiry_interval = 10,
@ -88,25 +86,27 @@ TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]")
esp_mqtt_client_destroy(client);
}
static const char* this_bin_addr(void)
static const char *this_bin_addr(void)
{
esp_partition_mmap_handle_t out_handle;
const void *binary_address;
const esp_partition_t* partition = esp_ota_get_running_partition();
const esp_partition_t *partition = esp_ota_get_running_partition();
esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle);
return binary_address;
}
TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]")
TEST(mqtt5, enqueue_and_destroy_outbox)
{
const char * bin_addr = this_bin_addr();
test_leak_setup(__FILE__, __LINE__);
const char *bin_addr = this_bin_addr();
// Reseting leak detection since this_bin_addr adds to allocated memory.
test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
const int messages = 20;
const int size = 2000;
const esp_mqtt_client_config_t mqtt5_cfg = {
// no connection takes place, but the uri has to be valid for init() to succeed
.broker.address.uri = "mqtts://localhost:8883",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
// no connection takes place, but the uri has to be valid for init() to succeed
.broker.address.uri = "mqtts://localhost:8883",
.session.protocol_ver = MQTT_PROTOCOL_V_5,
};
esp_mqtt5_publish_property_config_t publish_property = {
.payload_format_indicator = 1,
@ -129,7 +129,7 @@ TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]")
}
int bytes_after = esp_get_free_heap_size();
// check that outbox allocated all messages on heap
TEST_ASSERT_GREATER_OR_EQUAL(messages*size, bytes_before - bytes_after);
TEST_ASSERT_GREATER_OR_EQUAL(messages * size, bytes_before - bytes_after);
esp_mqtt_client_destroy(client);
}
@ -138,7 +138,7 @@ TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]")
/**
* This test cases uses ethernet kit, so build and use it only if EMAC supported
*/
TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]")
TEST(mqtt5, broker_tests)
{
test_case_uses_tcpip();
connect_test_fixture_setup();
@ -151,4 +151,22 @@ TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]")
connect_test_fixture_teardown();
}
#endif // SOC_EMAC_SUPPORTED
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2)
TEST_GROUP_RUNNER(mqtt5)
{
#if !DISABLED_FOR_TARGETS(ESP32H2)
RUN_TEST_CASE(mqtt5, init_with_invalid_url);
RUN_TEST_CASE(mqtt5, init_and_deinit);
RUN_TEST_CASE(mqtt5, enqueue_and_destroy_outbox);
#if SOC_EMAC_SUPPORTED
RUN_TEST_CASE(mqtt5, broker_tests);
#endif // SOC_EMAC_SUPPORTED
#endif //!DISABLED_FOR_TARGETS(ESP32H2)
}
void app_main(void)
{
UNITY_MAIN(mqtt5);
}

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import pytest
from pytest_embedded import Dut
@pytest.mark.esp32
@pytest.mark.esp32c2
@pytest.mark.ethernet
def test_mqtt5_client(dut: Dut) -> None:
dut.expect_unity_test_output()

View File

@ -0,0 +1,4 @@
# General options for additional checks
CONFIG_ESP_TASK_WDT_EN=n
CONFIG_MQTT_PROTOCOL_5=y
CONFIG_UNITY_ENABLE_FIXTURE=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c2"
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc
CONFIG_NEWLIB_TIME_SYSCALL_USE_NONE=n
CONFIG_NEWLIB_TIME_SYSCALL_USE_HRT=y
CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC=n

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c6"
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32h2"
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc
TEST_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c2"
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c6"
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32h2"
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash vfs

View File

@ -1,5 +0,0 @@
TEST_COMPONENTS=mqtt
CONFIG_MQTT_PROTOCOL_5=y
CONFIG_MQTT5_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTTV5_BROKER_TCP}"
CONFIG_LWIP_TCPIP_CORE_LOCKING=y
CONFIG_LWIP_CHECK_THREAD_SAFETY=y