Merge branch 'bugfix/mdns_test_cleanup' into 'master'

mdns tests: execute test services only when running example in ci

See merge request idf/esp-idf!4174
This commit is contained in:
Anton Maklakov 2019-01-31 14:46:10 +08:00
commit 2790d4a049
5 changed files with 47 additions and 26 deletions

View File

@ -29,9 +29,10 @@ Build the project and flash it to the board, then run monitor tool to view seria
make -j4 flash monitor
```
- Wait for WiFi to connect to your access point
- You can now ping the device at `[board-hostname].local`, where `[board-hostname]` is a string created from preconfigured hostname (`esp32-mdns` by default) and last 3 bytes from device MAC address. Please check the serial output log for the specific board-hostname (`esp32-mdns_80FFFF` in the log below)
- You can now ping the device at `[board-hostname].local`, where `[board-hostname]` is preconfigured hostname, `esp32-mdns` by default.
- You can also browse for `_http._tcp` on the same network to find the advertised service
- Pressing the BOOT button will start querying the local network for the predefined in `check_button` hosts and services
- Note that for purpose of CI tests, configuration options of `RESOLVE_TEST_SERVICES` and `MDNS_ADD_MAC_TO_HOSTNAME` are available, but disabled by default. If enabled, then the hostname suffix of last 3 bytes from device MAC address is added, e.g. `esp32-mdns-80FFFF`, and a query for test service is issued.
(To exit the serial monitor, type ``Ctrl-]``.)
@ -42,7 +43,7 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui
```
I (0) cpu_start: Starting scheduler on APP CPU.
I (276) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (276) mdns-test: mdns hostname set to: [esp32-mdns_80FFFF]
I (276) mdns-test: mdns hostname set to: [esp32-mdns]
I (286) wifi: wifi driver task: 3ffc2fa4, prio:23, stack:3584, core=0
I (286) wifi: wifi firmware version: a3be639
I (286) wifi: config NVS flash: enabled
@ -67,10 +68,6 @@ I (2786) wifi: connected with myssid, channel 11
I (2786) wifi: pm start, type: 1
I (4786) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2
I (4786) mdns-test: Query A: tinytester.local
W (6876) mdns-test: ESP_ERR_NOT_FOUND: Host was not found!
I (6876) mdns-test: Query PTR: _tiny._tcp.local
W (9976) mdns-test: No results found!
I (21126) mdns-test: Query A: esp32.local
W (23176) mdns-test: ESP_ERR_NOT_FOUND: Host was not found!
I (23176) mdns-test: Query PTR: _arduino._tcp.local

View File

@ -28,11 +28,18 @@ menu "Example Configuration"
config RESOLVE_TEST_SERVICES
bool "Resolve test services"
default y
default n
help
Enable resolving test services on startup.
These services are advertized and evaluated in automated tests.
When executed locally, these will not be resolved and warnings appear in the log.
Please set to false to disable initial querying to avoid warnings.
config MDNS_ADD_MAC_TO_HOSTNAME
bool "Add mac suffix to hostname"
default n
help
If enabled, a portion of MAC address is added to the hostname, this is used
for evaluation of tests in CI
endmenu

View File

@ -34,7 +34,6 @@
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
static const char c_config_hostname[] = CONFIG_MDNS_HOSTNAME;
/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
@ -44,6 +43,7 @@ const int IP6_CONNECTED_BIT = BIT1;
static const char *TAG = "mdns-test";
static bool auto_reconnect = true;
static char* generate_hostname();
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
@ -98,15 +98,7 @@ static void initialise_wifi(void)
static void initialise_mdns(void)
{
_Static_assert(sizeof(c_config_hostname) < CONFIG_MAIN_TASK_STACK_SIZE/2, "Configured mDNS name consumes more than half of the stack. Please select a shorter host name or extend the main stack size please.");
const size_t config_hostname_len = sizeof(c_config_hostname) - 1; // without term char
char hostname[config_hostname_len + 1 + 3*2 + 1]; // adding underscore + 3 digits + term char
uint8_t mac[6];
// adding 3 LSBs from mac addr to setup a board specific name
esp_read_mac(mac, ESP_MAC_WIFI_STA);
snprintf(hostname, sizeof(hostname), "%s_%02x%02X%02X", c_config_hostname, mac[3], mac[4], mac[5]);
char* hostname = generate_hostname();
//initialize mDNS
ESP_ERROR_CHECK( mdns_init() );
//set mDNS hostname (required if you want to advertise services)
@ -128,6 +120,7 @@ static void initialise_mdns(void)
ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") );
//change TXT item value
ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "u", "admin") );
free(hostname);
}
static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
@ -259,3 +252,21 @@ void app_main()
initialise_button();
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
}
/** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
* @return host name string allocated from the heap
*/
static char* generate_hostname()
{
#ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
return strdup(CONFIG_MDNS_HOSTNAME);
#else
uint8_t mac[6];
char *hostname;
esp_read_mac(mac, ESP_MAC_WIFI_STA);
if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) {
abort();
}
return hostname;
#endif
}

View File

@ -29,7 +29,6 @@ g_done = False
def mdns_server(esp_host):
global g_run_server
global g_done
UDP_IP = "0.0.0.0"
UDP_PORT = 5353
@ -41,7 +40,6 @@ def mdns_server(esp_host):
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
dns = dpkt.dns.DNS(b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01')
# sock.sendto(dns.pack(),(MCAST_GRP,UDP_PORT))
sock.settimeout(30)
resp_dns = dpkt.dns.DNS(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
resp_dns.op = dpkt.dns.DNS_QR | dpkt.dns.DNS_AA
@ -71,14 +69,16 @@ def mdns_server(esp_host):
sock.sendto(dns.pack(),(MCAST_GRP,UDP_PORT))
print("Sending esp32-mdns query")
time.sleep(0.5)
sock.sendto(resp_dns.pack(),(MCAST_GRP,UDP_PORT))
except socket.timeout:
break
except dpkt.UnpackError:
continue
@IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_mdns(env, extra_data):
global g_run_server
global g_done
"""
steps: |
1. join AP + init mdns example
@ -97,24 +97,28 @@ def test_examples_protocol_mdns(env, extra_data):
# 2. get the dut host name (and IP address)
specific_host = dut1.expect(re.compile(r"mdns hostname set to: \[([^\]]+)\]"), timeout=30)
specific_host = str(specific_host[0])
thread1 = Thread(target=mdns_server, args=(specific_host,))
thread1.start()
try:
dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
except DUT.ExpectTimeout:
g_run_server = False
thread1.join()
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
# 3. check the mdns name is accessible
thread1 = Thread(target=mdns_server, args=(specific_host,))
thread1.start()
start = time.time()
while (time.time() - start) <= 60:
if g_done:
print("Test passed")
break
g_run_server = False
thread1.join()
time.sleep(0.5)
if g_done is False:
raise ValueError('Test has failed: did not receive mdns answer within timeout')
# 4. check DUT output if mdns advertized host is resolved
try:
dut1.expect(re.compile(r"mdns-test: Query A: tinytester.local resolved to: 127.0.0.1"), timeout=30)
finally:
g_run_server = False
thread1.join()
if __name__ == '__main__':

View File

@ -0,0 +1,2 @@
CONFIG_RESOLVE_TEST_SERVICES=y
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y