基于表格管理的SOC自动化测试脚本(一)

目录结构

log:存放用例日志路径。

testcase:测试模板路径。

utils:工具类路径。

configs.py:工具配置文件。

main.py:工具函数入口。

工具使用

命令行执行

python main.py

调试模式

将autoTest通过pycharm加载。

全量测试:右键执行main.py。

部分测试:主入口修改debugMode = True,module_list中添加需要测试的模块,多个模块用逗号分隔。

以下是main.py实现

import os
import time
import threading

from utils.caseHandle import TestCase
from utils.hostHandle import ping_ip
from utils.serialHandle import SerialPort
from utils.sshHandle import SSHConnection
from utils.logger import TestingLogger

from config import ip, port, username, password, answering, baunRate, test_template_home, \
    serialPort_case, SUCCESS, FAIL, TIMEOUT, no_testing_required, TEST_YES, host_log_home, registration_exception_0, \
    registration_exception_1, registration_exception_2, host_ps3_reset_info, host_ps3_rst_list, sysIO_test, reboot_cmd, \
    reboot_cmd_info, reboot_cmd_result, ps3_reset_cmd, COST


def string_in_list(s, ml):
    for l in ml:
        if s in l:
            return True
        else:
            continue
    return False


def execute_ssh_cmd(module, cmd_list):
    local_path = ""
    # print("cmd_list", cmd_list)
    ssh = SSHConnection(ip, port, username, password, answering)
    ssh.send_case_and_run(cmd_list)

    if ssh.log_path:
        # print("log_path:", ssh.log_path)
        local_path = tLog.log_home + os.sep + module + os.sep + ssh.log_path
        # print("download host log:", host_log_home + "/" + ssh.log_path)
        ssh.sftp_test(host_log_home + "/" + ssh.log_path, local_path)
    return local_path


def execute_serial_cmd(caseLog, cmd):
    # print(cmd + '\n')
    mSerialCase.send_data(caseLog, cmd + '\n')


def excute_reset_cmd(module, caseLog):
    caseLog.info(host_ps3_reset_info)
    execute_ssh_cmd(module, host_ps3_rst_list)


def excute_reboot_cmd(module, caseLog):
    caseLog.info(reboot_cmd_info)
    execute_ssh_cmd(module, reboot_cmd)
    waiting_for_host()
    caseLog.info(reboot_cmd_result)


def read_host_log(file):
    lines = []
    if os.path.exists(file):
        with open(file, 'r') as fr:
            lines = fr.readlines()
    return lines


def waiting_for_host():
    times = 2
    time_out = 90
    if not ping_ip(ip, time_out) and times != 0:
        times -= 1
    else:
        raise Exception("The host(%s) Restart timeout" % ip)


def do_test(module, c_name, p_cmd, t_cmd, post_cmd, tt, t_return, loop):
    log_path = ""
    tResult = ""

    host_log_content = []

    sys_test = False
    all_rec_str = mSerialCase.all_rec_str
    cLog = TestingLogger(tLog.log_home + os.sep + module, "%s" % c_name)

    if sysIO_test in module:
        sys_test = True

    tr0 = t_return.split('/')[0]
    tr1 = t_return.split('/')[1]

    for _ in range(loop):
        end_time = time.time() + tt
        mSerialCase.testOngoing = True
        tSerial = threading.Thread(target=mSerialCase.read_data, args=(cLog,))
        tSerial.start()
        if p_cmd:
            if p_cmd == reboot_cmd:
                excute_reboot_cmd(module, cLog)
            elif p_cmd == ps3_reset_cmd:
                excute_reset_cmd(module, cLog)
            else:
                log_path += execute_ssh_cmd(module, p_cmd)

        if t_cmd:
            execute_serial_cmd(cLog, t_cmd)

        if sys_test:
            local_log_path = execute_ssh_cmd(module, post_cmd)
            cLog.info("sysio get host log path: %s" % log_path)
            host_log_content = read_host_log(log_path)
            # print("host_log_content", host_log_content)
            log_path += local_log_path
        # print("all_rec_str", all_rec_str)
        while time.time() < end_time:
            if string_in_list(tr0, all_rec_str) or string_in_list(tr0, host_log_content):
                all_rec_str.clear()
                tLog.info(SUCCESS)
                tResult = SUCCESS
                break
            elif string_in_list(tr1, all_rec_str) or string_in_list(tr1, host_log_content):
                all_rec_str.clear()
                tLog.info(FAIL)
                tResult = FAIL
                break
            elif string_in_list(registration_exception_0, all_rec_str) or \
                    string_in_list(registration_exception_1, all_rec_str) or \
                    string_in_list(registration_exception_2, all_rec_str):
                tLog.info(TIMEOUT)
                tResult = TIMEOUT
                excute_reset_cmd(module, cLog)
                all_rec_str.clear()
                break
        else:
            tLog.info(TIMEOUT)
            tResult = TIMEOUT
            excute_reset_cmd(module, cLog)
            all_rec_str.clear()

    if not sys_test and post_cmd:
        if post_cmd == reboot_cmd:
            excute_reboot_cmd(module, cLog)
        if post_cmd == ps3_reset_cmd and tResult != TIMEOUT:
            excute_reset_cmd(module, cLog)

    mSerialCase.testOngoing = False
    tSerial.join()
    log_path += cLog.log_name
    return tResult, log_path


def test_entry():
    for module in tc.test_package:
        if debugMode and module not in module_list:
            continue
        case_name_list = tc.get_case_name_list(module)
        # print(case_name_list)
        tc.m_case_result[module.strip()] = {SUCCESS: [], FAIL: [], TIMEOUT: [], COST: 0}
        t_start = time.time()
        for case_name in case_name_list:
            if not case_name:
                continue
            pre_command, test_command, post_command, timeout_time, test_return, loop, test_or_not = \
                tc.get_case_details(module, case_name)
            # print(pre_command, test_command, post_command, timeout_time, test_return, loop, test_or_not)
            if test_or_not == TEST_YES:
                tLog.info("=========================")
                tLog.info("Test %s %s" % (module, case_name))
                testResult, log_path = do_test(module, case_name, pre_command, test_command, post_command, timeout_time,
                                               test_return, loop)
                tc.up_case_result(module, case_name, testResult, log_path)
                # print(tc.test_package[module])
            else:
                # autoLog.info(no_testing_required)
                continue
        t_end = time.time()
        tc.up_m_cost(module, round(t_end - t_start, 1))


def res_print():
    res = tc.test_res
    tLog.info(
        "\n    Test finished ! Success {SUCCESS} Fail {FAIL} Timeout {TIMEOUT}\n".format(**res))

    case_result_output = ""
    res = "%s : Total %s Success %s Fail %s Timeout %s\n"
    for m in tc.m_case_result.keys():
        suc = str(len(tc.m_case_result[m][SUCCESS])).ljust(3, " ")
        fai = str(len(tc.m_case_result[m][FAIL])).ljust(3, " ")
        tim = str(len(tc.m_case_result[m][TIMEOUT])).ljust(3, " ")
        case_result_output += res % (m.rjust(8, " "), str(int(suc) + int(fai) + int(tim)).ljust(3, " "), suc, fai, tim)
    tLog.info("\n" + case_result_output)


def res_print_new():
    cost_time = eTime - sTime
    res = tc.test_res
    res_time = """\n    Test finished ! Cost {minutes} minutes and {seconds} seconds.""".format(
        minutes=round(cost_time / 60), seconds=round(cost_time % 60, 2))
    res_number = """\n    Tested %s modules,  Success {SUCCESS} Fail {FAIL} Timeout {TIMEOUT}\n""".format(**res) % len(
        tc.m_case_result.keys())

    case_result_output = ""
    sep = "\n################################################################################   "
    summary = """\nSummary:
        Total  Success  Fail  Timeout  Cost Time(s)
"""
    sep2 = "—————————————————————————————————————————————————————\n  "
    res_detail = "%s %s %s %s %s %s\n"
    for m in tc.m_case_result.keys():
        suc = str(len(tc.m_case_result[m][SUCCESS])).ljust(8, " ")
        fai = str(len(tc.m_case_result[m][FAIL])).ljust(7, " ")
        tim = str(len(tc.m_case_result[m][TIMEOUT])).ljust(8, " ")
        cos = str(tc.m_case_result[m][COST]).ljust(10, " ")
        case_result_output += res_detail % (sep2 +
                                            m.ljust(6, " "), str(int(suc) + int(fai) + int(tim)).ljust(6, " "), suc,
                                            fai, tim, cos)

    tLog.info(sep + res_time + res_number + summary + case_result_output + sep)


def load_host_chip_4k_mem():
    pass


if __name__ == '__main__':
    load_host_chip_4k_mem()
    debugMode = False
    if debugMode:
        module_list = ["sas"]
        # module_list = ["upcie", "cpu", "mctp"]
    # 创建测试日志
    tLog = TestingLogger("log", "")
    # 初始化串口
    mSerialCase = SerialPort(serialPort_case, baunRate)
    # 解析测试模板
    tc = TestCase(test_template_home)
    # 打印测试模块
    # print(tc.test_package)
    # 开始测试
    try:
        sTime = time.time()
        test_entry()
    finally:
        eTime = time.time()
        # res_print()
        res_print_new()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值