HostBasedUnitTestRunner.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class HostBasedUnitTestRunner(IUefiBuildPlugin):
  20. def do_pre_build(self, thebuilder):
  21. '''
  22. Run Prebuild
  23. '''
  24. return 0
  25. def do_post_build(self, thebuilder):
  26. '''
  27. After a build, will automatically locate and run all host-based unit tests. Logs any
  28. failures with Warning severity and will return a count of the failures as the return code.
  29. EXPECTS:
  30. - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
  31. UPDATES:
  32. - Shell Var 'CMOCKA_XML_FILE'
  33. '''
  34. ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')
  35. if ci_type != 'host_unit_test':
  36. return 0
  37. shell_env = shell_environment.GetEnvironment()
  38. logging.log(edk2_logging.get_section_level(),
  39. "Run Host based Unit Tests")
  40. path = thebuilder.env.GetValue("BUILD_OUTPUT_BASE")
  41. failure_count = 0
  42. # Set up the reporting type for Cmocka.
  43. shell_env.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')
  44. for arch in thebuilder.env.GetValue("TARGET_ARCH").split():
  45. logging.log(edk2_logging.get_subsection_level(),
  46. "Testing for architecture: " + arch)
  47. cp = os.path.join(path, arch)
  48. # If any old results XML files exist, clean them up.
  49. for old_result in glob.iglob(os.path.join(cp, "*.result.xml")):
  50. os.remove(old_result)
  51. # Find and Run any Host Tests
  52. if GetHostInfo().os.upper() == "LINUX":
  53. testList = glob.glob(os.path.join(cp, "*Test*"))
  54. for a in testList[:]:
  55. p = os.path.join(cp, a)
  56. # It must be a file
  57. if not os.path.isfile(p):
  58. testList.remove(a)
  59. logging.debug(f"Remove directory file: {p}")
  60. continue
  61. # It must be executable
  62. if os.stat(p).st_mode & (stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) == 0:
  63. testList.remove(a)
  64. logging.debug(f"Remove non-executable file: {p}")
  65. continue
  66. logging.info(f"Test file found: {p}")
  67. elif GetHostInfo().os.upper() == "WINDOWS":
  68. testList = glob.glob(os.path.join(cp, "*Test*.exe"))
  69. else:
  70. raise NotImplementedError("Unsupported Operating System")
  71. for test in testList:
  72. # Configure output name if test uses cmocka.
  73. shell_env.set_shell_var(
  74. 'CMOCKA_XML_FILE', test + ".CMOCKA.%g." + arch + ".result.xml")
  75. # Configure output name if test uses gtest.
  76. shell_env.set_shell_var(
  77. 'GTEST_OUTPUT', "xml:" + test + ".GTEST." + arch + ".result.xml")
  78. # Run the test.
  79. ret = RunCmd('"' + test + '"', "", workingdir=cp)
  80. if(ret != 0):
  81. logging.error("UnitTest Execution Error: " +
  82. os.path.basename(test))
  83. else:
  84. logging.info("UnitTest Completed: " +
  85. os.path.basename(test))
  86. file_match_pattern = test + ".*." + arch + ".result.xml"
  87. xml_results_list = glob.glob(file_match_pattern)
  88. for xml_result_file in xml_results_list:
  89. root = xml.etree.ElementTree.parse(
  90. xml_result_file).getroot()
  91. for suite in root:
  92. for case in suite:
  93. for result in case:
  94. if result.tag == 'failure':
  95. logging.warning(
  96. "%s Test Failed" % os.path.basename(test))
  97. logging.warning(
  98. " %s - %s" % (case.attrib['name'], result.text))
  99. failure_count += 1
  100. return failure_count