http examples pytest migration

This commit is contained in:
Harshit Malpani
2022-05-10 11:47:12 +05:30
parent aa3ddbc3c6
commit 83ace52a36
14 changed files with 683 additions and 726 deletions

View File

@@ -1,30 +1,19 @@
#!/usr/bin/env python
#
# Copyright 2021 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
from __future__ import print_function
import http.client
import logging
import os
import re
import socket
import sys
import tiny_test_fw
import ttfw_idf
from tiny_test_fw import Utility
import pexpect
import pytest
from pytest_embedded import Dut
try:
import wifi_tools
@@ -35,7 +24,7 @@ except ImportError:
import wifi_tools
def test_redirect(ip, port): # type: (str, str) -> str # pylint: disable=unused-argument
def test_redirect(ip: str, port: str) -> str:
# Establish HTTP connection
sess = http.client.HTTPConnection(ip + ':' + port, timeout=15)
@@ -60,7 +49,7 @@ def test_redirect(ip, port): # type: (str, str) -> str # pylint: disable=unused
return uri
def test_captive_page(ip, port, uri): # type: (str, str, str) -> bool # pylint: disable=unused-argument
def test_captive_page(ip: str, port: str, uri: str) -> bool:
# Establish HTTP connection
sess = http.client.HTTPConnection(ip + ':' + port, timeout=15)
@@ -79,25 +68,27 @@ def test_captive_page(ip, port, uri): # type: (str, str, str) -> bool # pylint:
return True
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT', ignore=True)
def test_example_captive_portal(env, extra_data): # type: (tiny_test_fw.Env.Env, None) -> None # pylint: disable=unused-argument
# Acquire DUT
dut1 = env.get_dut('captive_portal', 'examples/protocols/http_server/captive_portal', dut_class=ttfw_idf.ESP32DUT)
@pytest.mark.esp32
@pytest.mark.esp32c3
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.wifi_bt
@pytest.mark.xfail(reason='Runner unable to connect to target over WiFi', run=False)
def test_example_captive_portal(dut: Dut) -> None:
# Get binary file
binary_file = os.path.join(dut1.app.binary_path, 'captive_portal.bin')
binary_file = os.path.join(dut.app.binary_path, 'captive_portal.bin')
bin_size = os.path.getsize(binary_file)
ttfw_idf.log_performance('captive_portal_bin_size', '{}KB'.format(bin_size // 1024))
# Upload binary and start testing
dut1.start_app()
logging.info('captive_portal_bin_size : {}KB'.format(bin_size // 1024))
# Parse IP address of STA
Utility.console_log('Waiting to connect with softAP')
ap_ip = dut1.expect(re.compile(r'Set up softAP with IP: (\d+.\d+.\d+.\d+)'), timeout=60)[0]
logging.info('Waiting to connect with softAP')
ap_ip = dut.expect(r'Set up softAP with IP: (\d+.\d+.\d+.\d+)', timeout=60)[1]
[ssid, password] = dut1.expect(re.compile(r"wifi_init_softap finished. SSID:'(\S+)' password:'(\S+)'"), timeout=30)
port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
wifi_info = dut.expect(r"wifi_init_softap finished. SSID:'(\S+)' password:'(\S+)'", timeout=30)
ssid = wifi_info[1].decode()
password = wifi_info[2].decode()
port = dut.expect(r"(?:[\s\S]*)Starting server on port: '(\d+)'", timeout=30)[1]
iface = wifi_tools.get_wiface_name()
if iface is None:
@@ -112,17 +103,16 @@ def test_example_captive_portal(env, extra_data): # type: (tiny_test_fw.Env.Env
try:
ip = ctrl.connect(ssid, password)
except RuntimeError as err:
Utility.console_log('error: {}'.format(err))
logging.info('error: {}'.format(err))
try:
got_ip = dut1.expect(re.compile(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)'), timeout=60)
Utility.console_log('got_ip: {}'.format(got_ip))
got_ip = got_ip[0]
got_ip = dut.expect(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)', timeout=60)[1].decode()
logging.info('got_ip: {}'.format(got_ip))
if ip != got_ip:
raise RuntimeError('SoftAP connected to another host! {} != {}'.format(ip, got_ip))
except tiny_test_fw.DUT.ExpectTimeout:
except pexpect.exceptions.TIMEOUT:
# print what is happening on DUT side
Utility.console_log('in exception tiny_test_fw.DUT.ExpectTimeout')
Utility.console_log(dut1.read())
logging.info('in exception tiny_test_fw.DUT.ExpectTimeout')
logging.info(dut.read())
raise
print('Connected to DUT SoftAP')
@@ -136,7 +126,3 @@ def test_example_captive_portal(env, extra_data): # type: (tiny_test_fw.Env.Env
test_captive_page(ap_ip, port, uri)
finally:
ctrl.reset()
if __name__ == '__main__':
test_example_captive_portal() # pylint: disable=no-value-for-parameter