From 2b0b2c37da795ae77d006308c96a054734ab6d24 Mon Sep 17 00:00:00 2001 From: duerpei Date: Wed, 12 Oct 2022 10:42:44 +0800 Subject: agl-test-framework: add LTPBase class Add LTPBase class for test suites from LTP(linux-test-project). The LTPBase inherits from AGLBaseTest class. It contains some common functions, which are used to run the test suites from LTP and process the log. It makes it easy to support test suites from LTP. And add run_test scripts for the test suite of ltp/math, the scripts used the class of LTPBase. Bug-AGL: SPEC-4345 Signed-off-by: duerpei Change-Id: I683aaca37ddfb84a12e570a8918934ea8391ded6 --- tests/LTP/__init__.py | 0 tests/LTP/agl_test_ltp_base.py | 130 +++++++++++++++++++++++++++++++++++++++++ tests/LTP/math/__init__.py | 0 tests/LTP/math/run_tests.py | 24 ++++++++ 4 files changed, 154 insertions(+) create mode 100644 tests/LTP/__init__.py create mode 100644 tests/LTP/agl_test_ltp_base.py create mode 100644 tests/LTP/math/__init__.py create mode 100644 tests/LTP/math/run_tests.py (limited to 'tests/LTP') diff --git a/tests/LTP/__init__.py b/tests/LTP/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/LTP/agl_test_ltp_base.py b/tests/LTP/agl_test_ltp_base.py new file mode 100644 index 0000000..1e6eac9 --- /dev/null +++ b/tests/LTP/agl_test_ltp_base.py @@ -0,0 +1,130 @@ +import subprocess +import pathlib +import re + +import plugins.agl_test_conf as conf +from plugins.agl_test_base import AGLBaseTest + +class LTPBase(AGLBaseTest): + + LTPDIR = "/opt/ltp/" + RUNLTP = LTPDIR + "runltp" + LTPTEST = LTPDIR + "runtest/" + + #Test name without the prefix of "ltp/" + test_name: str + + def __init__(self, test_name: str): + self.test_name = test_name + super().__init__(name = ("ltp/" + test_name)) + + #Check and clean exit log + tmp_log_dir = self.get_temp_logdir() + "/log/" + log_file = tmp_log_dir + self.test_name + ".log" + log_path = pathlib.Path(log_file) + check_log_path = log_path.is_file() + if check_log_path: + subprocess.run("rm " + tmp_log_dir + "*", shell = True) + + def get_test_case_list(self): + case_list = list() + reObj = re.compile('^([\\w\-]+)(\\s+)(\\w+)', re.MULTILINE) + + test_file = self.LTPTEST + self.test_name + test_cases = open(test_file, 'r') + line = test_cases.readline() + while line: + matchs = reObj.search(line) + if matchs: + groups = matchs.groups() + case_list.append(groups[0]) + line = test_cases.readline() + + test_cases.close() + return case_list + + #Run test by runltp + def run_test_fun(self, case_name): + tmp_log_dir = self.get_temp_logdir() + "/log/" + run_test_cmd = self.RUNLTP + \ + " -f " + self.test_name + \ + " -s "+ "^" + case_name + "\\\\b" + \ + " -p " + \ + " -o " + tmp_log_dir + self.test_name + ".output " + \ + " -l " + tmp_log_dir + self.test_name + ".log " + \ + " -C " + tmp_log_dir + self.test_name + ".failed " + \ + " -T " + tmp_log_dir + self.test_name + ".tconf " + + console_log = tmp_log_dir + self.test_name + ".console" + + with open(console_log, 'a') as consolelog: + subprocess.run(run_test_cmd, shell = True, stdout = consolelog, + stderr = consolelog) + consolelog.close() + + def run_case(self, case_id): + case_info = self.get_caseinfo_by_name(case_id) + if (case_info[2] == ""): + if (case_info[1] == "PASS"): + case_info[2] = "passed" + elif (case_info[1] == "FAIL"): + case_info[2] = "failed" + elif (case_info[1] == "CONF"): + case_info[2] = "skipped" + else: + case_info[2] = "failed" + + self.update_caseinfo_by_name(case_id, case_info) + assert case_info[2] == "passed" or case_info[2] == "skipped" or case_info[2] == "xpassed" + + def precheck(self): + # Check for common + check_common = super().precheck() + + # Check for runltp script + runltp_script = pathlib.Path(self.RUNLTP) + check_runltp_script = runltp_script.is_file() + + # Check self test file + test_file = pathlib.Path(self.LTPTEST + self.test_name) + check_test_file = test_file.is_file() + + return check_common and check_runltp_script and check_test_file + + def log_process(self): + tmp_log_dir = conf.get_tmp_log_dir(self.name) + log_file = tmp_log_dir + self.test_name + ".log" + case_result = self.log_parser(log_file) + self.case_info_list[case_result[0]] = [case_result[0], case_result[2], ""] + + #Log sample: + #Testcase Result Exit Value + #-------- ------ ---------- + #cve-2011-0999 PASS 0 + #cve-2011-2183 CONF 32 + #cve-2011-2496 FAIL 1 + def log_parser(self, log_file): + reObj = re.compile('^(.+\\w+)( +)(PASS|CONF|FAIL)( +)(\\d+)', re.MULTILINE) + case_list = dict() + test_log = open(log_file, 'r') + + lines = test_log.readlines() + num = -1 + line = lines[num] + while line: + matchs = reObj.search(line) + + if matchs: + groups = matchs.groups() + test_log.close() + return groups + + num = num - 1 + line = lines[num] + + test_log.close() + + def run_ltp_test(self, case_name): + if(self.precheck() == True): + self.run_test_fun(case_name) + self.log_process() diff --git a/tests/LTP/math/__init__.py b/tests/LTP/math/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/LTP/math/run_tests.py b/tests/LTP/math/run_tests.py new file mode 100644 index 0000000..9f82915 --- /dev/null +++ b/tests/LTP/math/run_tests.py @@ -0,0 +1,24 @@ +import pytest + +from tests.LTP.agl_test_ltp_base import LTPBase + +class MathBase(LTPBase): + def __init__(self): + super().__init__(test_name="math") + +instance = MathBase() +test_case_list = instance.get_test_case_list() + +@pytest.fixture(scope='module') +def testbase(): + yield instance + #package log files and make report file + instance.log_report() + +@pytest.mark.parametrize('case_name', test_case_list) +def test_ltp_math(testbase: MathBase, case_name): + testbase.run_ltp_test(case_name) + testbase.run_case(case_name) + +if __name__ == '__main__': + pytest.main("-s run_tests") -- cgit 1.2.3-korg