CI: Improve common test methods

also fix ota test cases
This commit is contained in:
Chen Yudong
2022-07-07 00:34:06 +08:00
parent 692fbc169c
commit 2f75733ad7
46 changed files with 700 additions and 684 deletions

View File

@@ -4,6 +4,7 @@
import logging
import os
import socket
from typing import Any
import netifaces
import yaml
@@ -12,43 +13,40 @@ ENV_CONFIG_FILE_SEARCH = [
os.path.join(os.environ['IDF_PATH'], 'EnvConfig.yml'),
os.path.join(os.environ['IDF_PATH'], 'ci-test-runner-configs', os.environ.get('CI_RUNNER_DESCRIPTION', ''), 'EnvConfig.yml'),
]
ENV_CONFIG_TEMPLATE = '''
$IDF_PATH/EnvConfig.yml:
```yaml
<env_name>:
key: var
key2: var2
<another_env>:
key: var
```
'''
def get_my_ip_by_interface(interface_name: str, ip_type: int = netifaces.AF_INET) -> str:
for i in netifaces.ifaddresses(interface_name)[ip_type]:
interface_name = i['addr'].replace('%{}'.format(interface_name), '')
assert isinstance(interface_name, str)
return interface_name
def get_host_ip_by_interface(interface_name: str, ip_type: int = netifaces.AF_INET) -> str:
for _addr in netifaces.ifaddresses(interface_name)[ip_type]:
host_ip = _addr['addr'].replace('%{}'.format(interface_name), '')
assert isinstance(host_ip, str)
return host_ip
return ''
def get_my_ip4_by_getway(getway: str = '') -> str:
getways = netifaces.gateways()
for gw, iface_name, _ in getways[netifaces.AF_INET]:
if gw and gw == getway:
interface = iface_name
break
else:
interface = getways['default'][netifaces.AF_INET][1]
logging.debug('Using interface: {}.'.format(interface))
address = netifaces.ifaddresses(interface)[netifaces.AF_INET]
assert isinstance(address[0]['addr'], str)
return address[0]['addr']
def get_my_ip4_by_dest_ip(dest_ip: str = '') -> str:
def get_host_ip4_by_dest_ip(dest_ip: str = '') -> str:
if not dest_ip:
dest_ip = '8.8.8.8'
s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s1.connect((dest_ip, 80))
my_ip = s1.getsockname()[0]
host_ip = s1.getsockname()[0]
s1.close()
assert isinstance(my_ip, str)
return my_ip
assert isinstance(host_ip, str)
print(f'Using host ip: {host_ip}')
return host_ip
def get_my_interface_by_dest_ip(dest_ip: str = '') -> str:
my_ip = get_my_ip4_by_dest_ip(dest_ip)
my_ip = get_host_ip4_by_dest_ip(dest_ip)
interfaces = netifaces.interfaces()
for interface in interfaces:
try:
@@ -62,22 +60,39 @@ def get_my_interface_by_dest_ip(dest_ip: str = '') -> str:
return ''
def get_env_config(env_key: str = '', config_file: str = '') -> dict:
if not config_file:
for _file in ENV_CONFIG_FILE_SEARCH:
if os.path.exists(_file):
config_file = _file
if not config_file:
return dict()
def get_env_config_variable(env_name: str, key: str, default: Any = None) -> Any:
"""
Get test environment related variable
with open(config_file, 'r') as f:
config = yaml.load(f.read(), Loader=yaml.SafeLoader)
assert isinstance(config, dict)
if not env_key:
return config
if env_key in config:
_config = config[env_key]
assert isinstance(_config, dict)
return _config
logging.warning('Can not get env config, key: {}'.format(env_key))
return dict()
config file format: $IDF_PATH/EnvConfig.yml
```
<env_name>:
key: var
key2: var2
<env_name2>:
key: var
```
"""
config = dict()
for _file in ENV_CONFIG_FILE_SEARCH:
try:
with open(_file, 'r') as f:
data = yaml.load(f.read(), Loader=yaml.SafeLoader)
config = data[env_name]
break
except (FileNotFoundError, KeyError):
pass
else:
pass
var = config.get(key, default)
if var is None:
logging.warning(f'Failed to get env variable {env_name}/{key}.')
logging.info(f'Env config file template: {ENV_CONFIG_TEMPLATE}')
if not os.environ.get('CI_JOB_ID'):
# Try to get variable from stdin
var = input(f'You can input the variable now:')
else:
raise ValueError(f'Env variable not found: {env_name}/{key}')
logging.debug(f'Got env variable {env_name}/{key}: {var}')
return var