]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/Plugin: Add HostBasedUnitTestRunner plugin
authorMichael D Kinney <michael.d.kinney@intel.com>
Wed, 22 Jan 2020 23:28:43 +0000 (15:28 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 7 Feb 2020 19:18:53 +0000 (19:18 +0000)
https://bugzilla.tianocore.org/show_bug.cgi?id=2505

Add plugin to BaseTools to run host based unit tests.

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
Acked-by: Bob Feng <bob.c.feng@intel.com>
BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py [new file with mode: 0644]
BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner_plug_in.yaml [new file with mode: 0644]

diff --git a/BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py b/BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py
new file mode 100644 (file)
index 0000000..9242676
--- /dev/null
@@ -0,0 +1,115 @@
+# @file HostBasedUnitTestRunner.py\r
+# Plugin to located any host-based unit tests in the output directory and execute them.\r
+##\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+import os\r
+import logging\r
+import glob\r
+import xml.etree.ElementTree\r
+from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin\r
+from edk2toolext import edk2_logging\r
+import edk2toollib.windows.locate_tools as locate_tools\r
+from edk2toolext.environment import shell_environment\r
+from edk2toollib.utility_functions import RunCmd\r
+\r
+\r
+class HostBasedUnitTestRunner(IUefiBuildPlugin):\r
+\r
+    def do_pre_build(self, thebuilder):\r
+        '''\r
+        Works with the compiler (either the HostBasedCompilerPlugin or an other Builder) to set\r
+        up the environment that will be needed to build host-based unit tests.\r
+\r
+        EXPECTS:\r
+        - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.\r
+\r
+        UPDATES:\r
+        - Shell Var (Several) - Updates the shell with all vars listed in interesting_keys.\r
+        - Shell Path - Updated from QueryVcVariables()\r
+        - Shell Var 'CMOCKA_MESSAGE_OUTPUT'\r
+        '''\r
+        ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')\r
+        if ci_type != 'host_unit_test':\r
+            return 0\r
+\r
+        shell_env = shell_environment.GetEnvironment()\r
+        # Use the tools lib to determine the correct values for the vars that interest us.\r
+        interesting_keys = ["ExtensionSdkDir", "INCLUDE", "LIB", "LIBPATH", "UniversalCRTSdkDir",\r
+                            "UCRTVersion", "WindowsLibPath", "WindowsSdkBinPath", "WindowsSdkDir", "WindowsSdkVerBinPath",\r
+                            "WindowsSDKVersion", "VCToolsInstallDir"]\r
+        vs_vars = locate_tools.QueryVcVariables(interesting_keys, "amd64")\r
+        for (k, v) in vs_vars.items():\r
+            if k.upper() == "PATH":\r
+                shell_env.append_path(v)\r
+            else:\r
+                shell_env.set_shell_var(k, v)\r
+\r
+        # Set up the reporting type for Cmocka.\r
+        shell_env.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')\r
+        return 0\r
+\r
+    def do_post_build(self, thebuilder):\r
+        '''\r
+        After a build, will automatically locate and run all host-based unit tests. Logs any\r
+        failures with Warning severity and will return a count of the failures as the return code.\r
+\r
+        EXPECTS:\r
+        - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.\r
+\r
+        UPDATES:\r
+        - Shell Var 'CMOCKA_XML_FILE'\r
+        '''\r
+        ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')\r
+        if ci_type != 'host_unit_test':\r
+            return 0\r
+\r
+        shell_env = shell_environment.GetEnvironment()\r
+        logging.log(edk2_logging.get_section_level(),\r
+                    "Run Host based Unit Tests")\r
+        path = thebuilder.env.GetValue("BUILD_OUTPUT_BASE")\r
+\r
+        failure_count = 0\r
+\r
+        for arch in thebuilder.env.GetValue("TARGET_ARCH").split():\r
+            logging.log(edk2_logging.get_subsection_level(),\r
+                        "Testing for architecture: " + arch)\r
+            cp = os.path.join(path, arch)\r
+\r
+            # If any old results XML files exist, clean them up.\r
+            for old_result in glob.iglob(os.path.join(cp, "*.result.xml")):\r
+                os.remove(old_result)\r
+\r
+            # Determine whether any tests exist.\r
+            testList = glob.glob(os.path.join(cp, "*Test*.exe"))\r
+            for test in testList:\r
+                # Configure output name.\r
+                shell_env.set_shell_var(\r
+                    'CMOCKA_XML_FILE', test + ".%g." + arch + ".result.xml")\r
+\r
+                # Run the test.\r
+                ret = RunCmd('"' + test + '"', "", workingdir=cp)\r
+                if(ret != 0):\r
+                    logging.error("UnitTest Execution Error: " +\r
+                                  os.path.basename(test))\r
+                else:\r
+                    logging.info("UnitTest Completed: " +\r
+                                 os.path.basename(test))\r
+                    file_match_pattern = test + ".*." + arch + ".result.xml"\r
+                    xml_results_list = glob.glob(file_match_pattern)\r
+                    for xml_result_file in xml_results_list:\r
+                        root = xml.etree.ElementTree.parse(\r
+                            xml_result_file).getroot()\r
+                        for suite in root:\r
+                            for case in suite:\r
+                                for result in case:\r
+                                    if result.tag == 'failure':\r
+                                        logging.warning(\r
+                                            "%s Test Failed" % os.path.basename(test))\r
+                                        logging.warning(\r
+                                            "  %s - %s" % (case.attrib['name'], result.text))\r
+                                        failure_count += 1\r
+\r
+        return failure_count\r
diff --git a/BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner_plug_in.yaml b/BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner_plug_in.yaml
new file mode 100644 (file)
index 0000000..d9eb852
--- /dev/null
@@ -0,0 +1,12 @@
+##\r
+# IUefiBuildPlugin used to run any unittests that\r
+# were built on this build.\r
+#\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+{\r
+  "scope": "host-test-win",\r
+  "name": "Windows Host-Based Unit Test Runner",\r
+  "module": "HostBasedUnitTestRunner"\r
+}\r