From d9aa96690bfe9437e99b4a087114d53dcc748780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=B1=82?= Date: Tue, 15 Feb 2022 23:20:58 +0800 Subject: [PATCH] feature: support single mode for system test add --single_mode for system test --- ancert | 35 ++++++++---- lib/component.py | 29 ++++++++-- lib/device.py | 1 + lib/utils.py | 17 +++++- tests/network/network.yaml | 2 + tests/network/set_net_status.sh | 99 +++++++++++++++++++++++---------- 6 files changed, 136 insertions(+), 47 deletions(-) diff --git a/ancert b/ancert index 8eff2c1..a4177f4 100755 --- a/ancert +++ b/ancert @@ -4,7 +4,6 @@ import os import sys import logging -import ipaddress import argparse import atexit ANCERT_HOME_DIR = os.path.dirname(os.path.realpath(__file__)) @@ -192,6 +191,7 @@ if '__main__' == __name__: args_parser.add_argument('-c', '--case', type=str, help='Case') args_parser.add_argument('-i', '--index', type=int, default=1, help='Want to test device index') args_parser.add_argument('--lts_ip', type=str, help='LTS ip address') + args_parser.add_argument('--single_mode', action='store_true', help='Run System test without LTS') args_parser.add_argument('--list_hardware', type=str, default='', choices=['All', *SUPPORT_COMPONENT], help='List hardware on the system for target category') args, unknown = args_parser.parse_known_args() @@ -200,25 +200,30 @@ if '__main__' == __name__: sys.exit(1) if args.list_hardware: if args.category: - print('--category and --list_hardware cannot use together!') + print('--category and --list_hardware can\'t use together!') sys.exit(1) else: if not args.category: print('Please select a category!') sys.exit(1) + if args.single_mode and args.lts_ip: + print('--lts_ip and --single_mode can\'t use together!') + sys.exit(1) + + if args.category in ['System']: + if not args.single_mode and not args.lts_ip: + print('Please input LTS ip address or run single mode for System test') + sys.exit(1) + + if args.category in ['Network'] and not args.lts_ip: + print('Please input LTS ip address for Network test') + sys.exit(1) + if args.category in ['System', 'Network']: - if not args.lts_ip: - print('Please input LTS ip address for System or Network test') - sys.exit(1) - else: - try: - ipaddress.ip_address(args.lts_ip) - except ValueError as e: - print('%s ip address format is not correct!' % args.lts_ip) + if args.lts_ip: + if not check_ipaddr_connection(args.lts_ip): sys.exit(1) - run_local_cmd('ping -c 1 %s' % args.lts_ip, - exit_msg='Failed to ping LTS ip address %s' % args.lts_ip, shell=True, loginit=False) logdir = logging_config(args) @@ -227,6 +232,12 @@ if '__main__' == __name__: print('-' * 66) pack_log_dir(logdir, args, RESULT) + if args.single_mode: + if args.category in ['System'] or args.list_hardware in ['System']: + print('\n*NOTE*: You are using ancert single testing mode! Single testing mode can\'t fully verify your\n' + ' System compatibility with Anolis OS. If you want to run all tests, please configure LTS\n' + ' system, then pass option --lts_ip [lts ip address] to ancert.') + ret = main(args) if ret != 0: RESULT = False diff --git a/lib/component.py b/lib/component.py index cb6c1da..324a7ab 100644 --- a/lib/component.py +++ b/lib/component.py @@ -23,9 +23,18 @@ class Component(): self.unavailable = list() self.name = self.__class__.__name__.lower() self.tested_controller = [] - self.is_system_test = False self.is_virtual_platform = is_virtual_platform() self.env = {} + self.is_single_mode = args.single_mode + if self.is_single_mode: + self.env.update({'ANCERT_SYSTEM_TEST_MODE':'single'}) + else: + self.env.update({'ANCERT_SYSTEM_TEST_MODE':'lts'}) + if args.category == 'System' or args.list_hardware == 'System': + self.is_system_test = True + else: + self.is_system_test = False + @abstractmethod def build(self, dstree): @@ -237,7 +246,8 @@ class Network(Component): def __init__(self, logdir, cases, category, args): super(Network, self).__init__(logdir, cases, category, args) self.build_cases(cases) - self.env.update({'LTS_IPADDRESS': args.lts_ip}) + if args.lts_ip: + self.env.update({'LTS_IPADDRESS': args.lts_ip}) def build(self, dstree): cur_pci_bus_device = [] @@ -266,8 +276,13 @@ class Network(Component): if dev.syspath.startswith(ctr.bus_device): if dev is ctr.device_inst: continue - if 'net' in dev.get_property('SUBSYSTEM') \ - and is_network_interface_linkup(dev.get_property('INTERFACE')): + if 'net' in dev.get_property('SUBSYSTEM'): + if self.is_single_mode: + if not dev.get_property('INTERFACE'): + continue + else: + if not is_network_interface_linkup(dev.get_property('INTERFACE')): + continue ctr.devices.append(dev) if ctr.get_property('DRIVER'): ctr.valid = True @@ -285,13 +300,17 @@ class Network(Component): for gname, cases_inst in self.groups: w_inst = [] for case in cases_inst: + if case.required: + if tested.test_mode not in case.required: + continue tested.device_inst.properties.update(self.env) interfaces = [device.get_property('INTERFACE') for device in tested.devices] tested.device_inst.properties.update( {'INTERFACES': ' '.join(interfaces)}) test = Test(case, tested.device_inst, tested, self.logdir) w_inst.append(select_worker(test)) - self.workers.append([gname, w_inst]) + if w_inst: + self.workers.append([gname, w_inst]) class Storage(Component): diff --git a/lib/device.py b/lib/device.py index 6761077..46eed3e 100644 --- a/lib/device.py +++ b/lib/device.py @@ -153,6 +153,7 @@ class Controller(): self.bus_device = self.syspath.rsplit('.', 1)[0] self.bdf = self.syspath.rsplit('/')[-1] self.device_inst.properties.update({'PCI_BDF': self.bdf}) + self.test_mode = self.component.env.get('ANCERT_SYSTEM_TEST_MODE', '') self.is_raid = is_sub_class_code(['000104'], self.classcode) self.is_boot_ctr = False if not self.device_inst.name: diff --git a/lib/utils.py b/lib/utils.py index 7b9a144..c72a4cc 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -8,6 +8,7 @@ import json import time import logging import tarfile +import ipaddress from threading import Thread from .expection import * from .config import * @@ -54,7 +55,7 @@ def run_task(gname, tasks): for future in to_run_futures: if future.exception() is not None: logging.error('Future hit exception, result: %s, exception: %s' - % (future.result(), future.exception())) + % (str(future.result()), str(future.exception()))) alarm.stop = True th.join() @@ -681,3 +682,17 @@ def rpm_verify(path, name): except Exception as e: print(e) return False + +def check_ipaddr_connection(ipaddr): + try: + ipaddress.ip_address(ipaddr) + except ValueError as e: + print('%s ip address format is not correct!' % ipaddr) + return False + try: + run_local_cmd('ping -c 1 %s' % ipaddr, + exit_msg='Failed to ping ip address %s' % ipaddr, shell=True, loginit=False) + except RunCommandError as e: + print(e) + return False + return True diff --git a/tests/network/network.yaml b/tests/network/network.yaml index ecb13b6..4d51e91 100644 --- a/tests/network/network.yaml +++ b/tests/network/network.yaml @@ -15,9 +15,11 @@ cases: description: test network connect function filepath: net_connect.sh filetype: shell + required: [lts] runtime: 300 - name: file_transport description: test network scp function filepath: file_transport.sh filetype: shell + required: [lts] runtime: 300 diff --git a/tests/network/set_net_status.sh b/tests/network/set_net_status.sh index 74618d1..681642c 100644 --- a/tests/network/set_net_status.sh +++ b/tests/network/set_net_status.sh @@ -6,50 +6,92 @@ function setup() { ip link help vlan&> /dev/null || { echo "iproute is not installed";exit 1; } } +function check_link_state() { + local link_state=$1 + local interface=$2 + + for ((i=0; i<5; i++)); do + sleep 5 + if [ "$link_state" == "up" ]; then + if ip link show "$interface" | grep -q "UP"; then + echo "$interface ip link state is $link_state" + break + fi + else + if ! ip link show "$interface" | grep -q "UP"; then + echo "$interface ip link state is $link_state" + break + fi + fi + done + + if [ $i -eq 5 ]; then + echo "test $interface $link_state failed" + test_fail + fi +} + +function check_phy_link_state() { + local link_state=$1 + local interface=$2 + + if [ "$link_state" == "up" ]; then + link_state_str="Link detected: yes" + else + link_state_str="Link detected: no" + fi + for ((i=0; i<5; i++)); do + sleep 5 + if ethtool "$interface" | grep -q "$link_state_str"; then + echo "$interface ethtool link state is $link_state" + break + fi + done + + if [ $i -eq 5 ]; then + echo "test $interface $link_state failed" + test_fail + fi +} + function setstatus() { echo "run network status set test!" ifconfig ip link show interfaces=$INTERFACES - while read line - do - echo "exclude interface:$line" + + while read line; do + echo "exclude interface: $line" interfaces=( ${interfaces[*]/$line} ) done <../../etc/exclude_interface - for interface in ${interfaces[*]} - do - echo "test interface info:$interface" + + for interface in ${interfaces[*]}; do + local check_interface_phy_link=false + echo "set $interface link to up" + ip link set "$interface" up + if ethtool "$interface" | grep -q "Link detected: yes"; then + echo "$interface physical link state: yes" + local check_interface_phy_link=true + fi + + echo "test interface: $interface" ip link set "$interface" down - for ((i=0;i<5;i++)) - do - sleep 5 - ethtool "$interface" | grep "Link detected: no" && { echo "test $interface down success";break; } - done - if [ $i -eq 5 ]; then - echo "test $interface down failed" - return 1 - fi + check_link_state "down" "$interface" + if $check_interface_phy_link; then + check_phy_link_state "down" "$interface" + fi ip link set "$interface" up - for ((i=0;i<5;i++)) - do - sleep 5 - ethtool "$interface" | grep "Link detected: yes" && { echo "test $interface up success";break; } - done - if [ $i -eq 5 ]; then - echo "test $interface up failed" - return 1 - fi + check_link_state "up" "$interface" + if $check_interface_phy_link; then + check_phy_link_state "up" "$interface" + fi done - return 0 } function run_network_test() { setup setstatus - if [ $? -ne 0 ]; then - test_fail - fi } # # # # # # # # # # # # # # main # # # # # # # # # # # # # # @@ -57,4 +99,3 @@ print_test_info "Network" show_network_info run_network_test test_pass - -- Gitee