]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py
c1eeaf26251eeb0a79a8cbe14494d0cb88bc739a
[mirror_edk2.git] / BaseTools / Plugin / HostBasedUnitTestRunner / HostBasedUnitTestRunner.py
1 # @file HostBasedUnitTestRunner.py
2 # Plugin to located any host-based unit tests in the output directory and execute them.
3 ##
4 # Copyright (c) Microsoft Corporation.
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7 ##
8 import os
9 import logging
10 import glob
11 import stat
12 import xml.etree.ElementTree
13 from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin
14 from edk2toolext import edk2_logging
15 import edk2toollib.windows.locate_tools as locate_tools
16 from edk2toolext.environment import shell_environment
17 from edk2toollib.utility_functions import RunCmd
18 from edk2toollib.utility_functions import GetHostInfo
19
20
21 class HostBasedUnitTestRunner(IUefiBuildPlugin):
22
23 def do_pre_build(self, thebuilder):
24 '''
25 Run Prebuild
26 '''
27
28 return 0
29
30 def do_post_build(self, thebuilder):
31 '''
32 After a build, will automatically locate and run all host-based unit tests. Logs any
33 failures with Warning severity and will return a count of the failures as the return code.
34
35 EXPECTS:
36 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
37
38 UPDATES:
39 - Shell Var 'CMOCKA_XML_FILE'
40 '''
41 ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')
42 if ci_type != 'host_unit_test':
43 return 0
44
45 shell_env = shell_environment.GetEnvironment()
46 logging.log(edk2_logging.get_section_level(),
47 "Run Host based Unit Tests")
48 path = thebuilder.env.GetValue("BUILD_OUTPUT_BASE")
49
50 failure_count = 0
51
52 # Set up the reporting type for Cmocka.
53 shell_env.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')
54
55 for arch in thebuilder.env.GetValue("TARGET_ARCH").split():
56 logging.log(edk2_logging.get_subsection_level(),
57 "Testing for architecture: " + arch)
58 cp = os.path.join(path, arch)
59
60 # If any old results XML files exist, clean them up.
61 for old_result in glob.iglob(os.path.join(cp, "*.result.xml")):
62 os.remove(old_result)
63
64 # Find and Run any Host Tests
65 if GetHostInfo().os.upper() == "LINUX":
66 testList = glob.glob(os.path.join(cp, "*Test*"))
67 for a in testList[:]:
68 p = os.path.join(cp, a)
69 # It must be a file
70 if not os.path.isfile(p):
71 testList.remove(a)
72 logging.debug(f"Remove directory file: {p}")
73 continue
74 # It must be executable
75 if os.stat(p).st_mode & (stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) == 0:
76 testList.remove(a)
77 logging.debug(f"Remove non-executable file: {p}")
78 continue
79
80 logging.info(f"Test file found: {p}")
81
82 elif GetHostInfo().os.upper() == "WINDOWS":
83 testList = glob.glob(os.path.join(cp, "*Test*.exe"))
84 else:
85 raise NotImplementedError("Unsupported Operating System")
86
87 for test in testList:
88 # Configure output name.
89 shell_env.set_shell_var(
90 'CMOCKA_XML_FILE', test + ".%g." + arch + ".result.xml")
91
92 # Run the test.
93 ret = RunCmd('"' + test + '"', "", workingdir=cp)
94 if(ret != 0):
95 logging.error("UnitTest Execution Error: " +
96 os.path.basename(test))
97 else:
98 logging.info("UnitTest Completed: " +
99 os.path.basename(test))
100 file_match_pattern = test + ".*." + arch + ".result.xml"
101 xml_results_list = glob.glob(file_match_pattern)
102 for xml_result_file in xml_results_list:
103 root = xml.etree.ElementTree.parse(
104 xml_result_file).getroot()
105 for suite in root:
106 for case in suite:
107 for result in case:
108 if result.tag == 'failure':
109 logging.warning(
110 "%s Test Failed" % os.path.basename(test))
111 logging.warning(
112 " %s - %s" % (case.attrib['name'], result.text))
113 failure_count += 1
114
115 return failure_count