2022-06-15 10:46:55 -04:00
|
|
|
# SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
""" Test Env, manages DUT, App and EnvConfig, interface for test cases to access these components """
|
2021-01-25 21:49:01 -05:00
|
|
|
import functools
|
2017-10-09 22:44:55 -04:00
|
|
|
import os
|
|
|
|
import threading
|
2021-01-25 21:49:01 -05:00
|
|
|
import traceback
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2018-01-07 07:24:38 -05:00
|
|
|
import netifaces
|
|
|
|
|
2019-12-27 04:58:36 -05:00
|
|
|
from . import EnvConfig
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _synced(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def decorator(self, *args, **kwargs):
|
|
|
|
with self.lock:
|
|
|
|
ret = func(self, *args, **kwargs)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
decorator.__doc__ = func.__doc__
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
class Env(object):
|
|
|
|
"""
|
|
|
|
test env, manages DUTs and env configs.
|
|
|
|
|
|
|
|
:keyword app: class for default application
|
|
|
|
:keyword dut: class for default DUT
|
|
|
|
:keyword env_tag: test env tag, used to select configs from env config file
|
|
|
|
:keyword env_config_file: test env config file path
|
|
|
|
:keyword test_name: test suite name, used when generate log folder name
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
CURRENT_LOG_FOLDER = ''
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
app=None,
|
|
|
|
dut=None,
|
|
|
|
env_tag=None,
|
|
|
|
env_config_file=None,
|
2018-01-16 09:16:03 -05:00
|
|
|
test_suite_name=None,
|
2017-10-09 22:44:55 -04:00
|
|
|
**kwargs):
|
|
|
|
self.app_cls = app
|
|
|
|
self.default_dut_cls = dut
|
|
|
|
self.config = EnvConfig.Config(env_config_file, env_tag)
|
2018-01-16 09:16:03 -05:00
|
|
|
self.log_path = self.app_cls.get_log_folder(test_suite_name)
|
2017-10-09 22:44:55 -04:00
|
|
|
if not os.path.exists(self.log_path):
|
|
|
|
os.makedirs(self.log_path)
|
|
|
|
|
2020-11-12 07:44:44 -05:00
|
|
|
Env.CURRENT_LOG_FOLDER = self.log_path
|
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
self.allocated_duts = dict()
|
|
|
|
self.lock = threading.RLock()
|
|
|
|
|
|
|
|
@_synced
|
2019-10-15 08:15:06 -04:00
|
|
|
def get_dut(self, dut_name, app_path, dut_class=None, app_class=None, app_config_name=None, **dut_init_args):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
|
|
|
get_dut(dut_name, app_path, dut_class=None, app_class=None)
|
|
|
|
|
|
|
|
:param dut_name: user defined name for DUT
|
|
|
|
:param app_path: application path, app instance will use this path to process application info
|
|
|
|
:param dut_class: dut class, if not specified will use default dut class of env
|
|
|
|
:param app_class: app class, if not specified will use default app of env
|
2019-10-15 08:15:06 -04:00
|
|
|
:param app_config_name: app build config
|
2019-03-16 08:07:52 -04:00
|
|
|
:keyword dut_init_args: extra kwargs used when creating DUT instance
|
2017-10-09 22:44:55 -04:00
|
|
|
:return: dut instance
|
|
|
|
"""
|
|
|
|
if dut_name in self.allocated_duts:
|
2021-01-25 21:49:01 -05:00
|
|
|
dut = self.allocated_duts[dut_name]['dut']
|
2017-10-09 22:44:55 -04:00
|
|
|
else:
|
|
|
|
if dut_class is None:
|
|
|
|
dut_class = self.default_dut_cls
|
|
|
|
if app_class is None:
|
|
|
|
app_class = self.app_cls
|
2021-01-14 02:31:01 -05:00
|
|
|
|
|
|
|
app_target = dut_class.TARGET
|
2019-10-15 08:15:06 -04:00
|
|
|
detected_target = None
|
2021-01-14 02:31:01 -05:00
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
try:
|
|
|
|
port = self.config.get_variable(dut_name)
|
2021-01-14 02:31:01 -05:00
|
|
|
if not app_target:
|
|
|
|
result, detected_target = dut_class.confirm_dut(port)
|
2017-10-09 22:44:55 -04:00
|
|
|
except ValueError:
|
|
|
|
# try to auto detect ports
|
2021-01-25 21:49:01 -05:00
|
|
|
allocated_ports = [self.allocated_duts[x]['port'] for x in self.allocated_duts]
|
2017-10-09 22:44:55 -04:00
|
|
|
available_ports = dut_class.list_available_ports()
|
|
|
|
for port in available_ports:
|
|
|
|
if port not in allocated_ports:
|
2019-10-15 08:15:06 -04:00
|
|
|
result, detected_target = dut_class.confirm_dut(port)
|
|
|
|
if result:
|
2017-10-09 22:44:55 -04:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
port = None
|
2019-10-15 08:15:06 -04:00
|
|
|
|
|
|
|
if not app_target:
|
|
|
|
app_target = detected_target
|
|
|
|
if not app_target:
|
|
|
|
raise ValueError("DUT class doesn't specify the target, and autodetection failed")
|
|
|
|
app_inst = app_class(app_path, app_config_name, app_target)
|
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
if port:
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
dut_config = self.get_variable(dut_name + '_port_config')
|
2017-10-09 22:44:55 -04:00
|
|
|
except ValueError:
|
|
|
|
dut_config = dict()
|
2019-03-16 08:07:52 -04:00
|
|
|
dut_config.update(dut_init_args)
|
2019-08-22 04:45:25 -04:00
|
|
|
dut = dut_class(dut_name, port,
|
2022-10-28 01:21:27 -04:00
|
|
|
os.path.join(self.log_path, dut_name + '.txt'),
|
2019-08-22 04:45:25 -04:00
|
|
|
app_inst,
|
|
|
|
**dut_config)
|
2021-01-25 21:49:01 -05:00
|
|
|
self.allocated_duts[dut_name] = {'port': port, 'dut': dut}
|
2017-10-09 22:44:55 -04:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise ValueError('Failed to get DUT')
|
2017-10-09 22:44:55 -04:00
|
|
|
return dut
|
|
|
|
|
|
|
|
@_synced
|
|
|
|
def close_dut(self, dut_name):
|
|
|
|
"""
|
|
|
|
close_dut(dut_name)
|
|
|
|
close one DUT by name if DUT name is valid (the name used by ``get_dut``). otherwise will do nothing.
|
|
|
|
|
|
|
|
:param dut_name: user defined name for DUT
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
dut = self.allocated_duts.pop(dut_name)['dut']
|
2017-10-09 22:44:55 -04:00
|
|
|
dut.close()
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@_synced
|
|
|
|
def get_variable(self, variable_name):
|
|
|
|
"""
|
|
|
|
get_variable(variable_name)
|
|
|
|
get variable from config file. If failed then try to auto-detected it.
|
|
|
|
|
|
|
|
:param variable_name: name of the variable
|
|
|
|
:return: value of variable if successfully found. otherwise None.
|
|
|
|
"""
|
|
|
|
return self.config.get_variable(variable_name)
|
|
|
|
|
2018-01-07 07:24:38 -05:00
|
|
|
PROTO_MAP = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'ipv4': netifaces.AF_INET,
|
|
|
|
'ipv6': netifaces.AF_INET6,
|
|
|
|
'mac': netifaces.AF_LINK,
|
2018-01-07 07:24:38 -05:00
|
|
|
}
|
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
@_synced
|
2021-01-25 21:49:01 -05:00
|
|
|
def get_pc_nic_info(self, nic_name='pc_nic', proto='ipv4'):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
|
|
|
get_pc_nic_info(nic_name="pc_nic")
|
2018-01-07 07:24:38 -05:00
|
|
|
try to get info of a specified NIC and protocol.
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2018-01-07 07:24:38 -05:00
|
|
|
:param nic_name: pc nic name. allows passing variable name, nic name value.
|
|
|
|
:param proto: "ipv4", "ipv6" or "mac"
|
|
|
|
:return: a dict of nic info if successfully found. otherwise None.
|
|
|
|
nic info keys could be different for different protocols.
|
|
|
|
key "addr" is available for both mac, ipv4 and ipv6 pic info.
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2018-01-07 07:24:38 -05:00
|
|
|
interfaces = netifaces.interfaces()
|
|
|
|
if nic_name in interfaces:
|
|
|
|
# the name is in the interface list, we regard it as NIC name
|
|
|
|
if_addr = netifaces.ifaddresses(nic_name)
|
|
|
|
else:
|
|
|
|
# it's not in interface name list, we assume it's variable name
|
|
|
|
_nic_name = self.get_variable(nic_name)
|
|
|
|
if_addr = netifaces.ifaddresses(_nic_name)
|
|
|
|
|
|
|
|
return if_addr[self.PROTO_MAP[proto]][0]
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
@_synced
|
2018-06-15 05:33:39 -04:00
|
|
|
def close(self, dut_debug=False):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
|
|
|
close()
|
|
|
|
close all DUTs of the Env.
|
|
|
|
|
2018-06-15 05:33:39 -04:00
|
|
|
:param dut_debug: if dut_debug is True, then print all dut expect failures before close it
|
2019-03-16 08:07:52 -04:00
|
|
|
:return: exceptions during close DUT
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2019-03-16 08:07:52 -04:00
|
|
|
dut_close_errors = []
|
2017-10-09 22:44:55 -04:00
|
|
|
for dut_name in self.allocated_duts:
|
2021-01-25 21:49:01 -05:00
|
|
|
dut = self.allocated_duts[dut_name]['dut']
|
2019-03-16 08:07:52 -04:00
|
|
|
try:
|
2020-04-02 11:50:35 -04:00
|
|
|
if dut_debug:
|
|
|
|
dut.print_debug_info()
|
2019-03-16 08:07:52 -04:00
|
|
|
dut.close()
|
|
|
|
except Exception as e:
|
2020-11-17 23:48:27 -05:00
|
|
|
traceback.print_exc()
|
2019-03-16 08:07:52 -04:00
|
|
|
dut_close_errors.append(e)
|
2017-10-09 22:44:55 -04:00
|
|
|
self.allocated_duts = dict()
|
2019-03-16 08:07:52 -04:00
|
|
|
return dut_close_errors
|