GoogleGit

blob: ccdd8234947390b9afa176dce3c162b92fae0d3c [file] [log] [blame]
  1. #!/bin/bash
  2. # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # This script builds and runs Chromium OS unit tests. Note that this script
  6. # utilizes the src_test stanza in chromeos-base packages. These stanzas
  7. # should both build and run the unit tests.
  8. # This script requires that you run build_packages first.
  9. # Special because this can only run inside the chroot.
  10. . "/usr/lib/crosutils/common.sh" 2> /dev/null ||
  11. (echo "Must run within chroot" && false) ||
  12. exit 1
  13. # Flags
  14. DEFINE_string board "${DEFAULT_BOARD}" \
  15. "Target board of which tests were built"
  16. DEFINE_string build_root "${DEFAULT_BUILD_ROOT}" \
  17. "Root of build output"
  18. DEFINE_boolean fast "${DEFAULT_FAST}" \
  19. "Call many emerges in parallel."
  20. DEFINE_string package_file "" \
  21. "File with space-separated list of packages to run unit tests" f
  22. DEFINE_string packages "" \
  23. "Optional space-separated list of packages to run unit tests" p
  24. DEFINE_string blacklist_packages "" \
  25. "List of packages to blacklist from unit tests"
  26. DEFINE_boolean withdebug "${FLAGS_TRUE}" \
  27. "Build debug versions of Chromium-OS-specific packages."
  28. DEFINE_boolean pretend "${FLAGS_FALSE}" \
  29. "Only show the packages we would test; don't actually test"
  30. DEFINE_boolean installed "${FLAGS_TRUE}" \
  31. "Only test packages that are already installed"
  32. # List of packages with no unit tests.
  33. NO_UNITTESTS=""
  34. check_src_test() {
  35. egrep '^(src_test|platform_pkg_test)()' "${1}" > /dev/null
  36. }
  37. # Parse command line and die if unexpected parameters given.
  38. FLAGS_HELP="usage: ${0} [flags]"
  39. FLAGS "${@}" || exit 1
  40. eval set -- "${FLAGS_ARGV}"
  41. check_flags_only_and_allow_null_arg "${@}" && set --
  42. set -e
  43. [ -z "${FLAGS_board}" ] && die "--board required"
  44. EMERGE_CMD="emerge"
  45. EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
  46. if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
  47. EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge"
  48. EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
  49. fi
  50. # Create package list from package file and list of packages.
  51. if [ -n "${FLAGS_package_file}" ]; then
  52. if [ -f "${FLAGS_package_file}" ]; then
  53. PACKAGE_LIST="$(cat ${FLAGS_package_file})"
  54. else
  55. warn "Missing package file."
  56. fi
  57. fi
  58. [ -n "${FLAGS_packages}" ] && PACKAGE_LIST="${PACKAGE_LIST} ${FLAGS_packages}"
  59. # If we didn't specify packages, find all packages.
  60. if [ -z "${FLAGS_package_file}" -a -z "${FLAGS_packages}" ]; then
  61. PACKAGE_LIST=$(cros_workon --board "${FLAGS_board}" list --all)
  62. if [[ ${FLAGS_installed} -eq ${FLAGS_TRUE} ]]; then
  63. PACKAGE_LIST=$(qlist-${FLAGS_board} -ICe ${PACKAGE_LIST})
  64. fi
  65. fi
  66. BLACK_LIST_FILE="/usr/share/crostestutils/unit_test_black_list.txt"
  67. BLACK_LIST_PACKAGES="${FLAGS_blacklist_packages}"
  68. if [ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]; then
  69. export USE="${USE} -cros-debug"
  70. fi
  71. ALL_PACKAGE_LIST=$(
  72. jobs=0
  73. for package in ${PACKAGE_LIST}; do
  74. (
  75. if grep -xq "${package}" "${BLACK_LIST_FILE}" || \
  76. [[ " ${BLACK_LIST_PACKAGES} " =~ " ${package} " ]]; then
  77. warn "Skipping package ${package} since it is blacklisted."
  78. continue
  79. fi
  80. EBUILD_PATH=$(equery-${FLAGS_board} which ${package} 2>/dev/null) || \
  81. warn "${package} not found"
  82. if [[ -n ${EBUILD_PATH} ]]; then
  83. if check_src_test "${EBUILD_PATH}"; then
  84. echo "y:${package}"
  85. else
  86. echo "n:${package}"
  87. fi
  88. fi
  89. ) &
  90. if [[ ${jobs} -ge ${NUM_JOBS} ]]; then
  91. jobs=0
  92. wait
  93. else
  94. : $(( jobs += 1 ))
  95. fi
  96. done | sort
  97. wait
  98. )
  99. TEST_PACKAGE_LIST=$(echo "${ALL_PACKAGE_LIST}" | sed -n '/^y:/s:^..::p')
  100. NO_UNITTESTS=$(echo "${ALL_PACKAGE_LIST}" | sed -n '/^n:/s:^..::p')
  101. if [ -n "${NO_UNITTESTS}" ]; then
  102. warn "The following packages have no unit tests:"
  103. warn "${NO_UNITTESTS}"
  104. fi
  105. if [ -n "${TEST_PACKAGE_LIST}" ]; then
  106. if [[ ${FLAGS_pretend} -eq ${FLAGS_TRUE} ]]; then
  107. printf '%s\n' ${TEST_PACKAGE_LIST} | sort
  108. exit 0
  109. else
  110. cmd=( sudo -E PKGDIR="/build/${FLAGS_board}/test-packages"
  111. FEATURES="test" ${EMERGE_BOARD_CMD} --nodeps --buildpkgonly )
  112. info "Running unit tests with command ${cmd[*]}"
  113. info "Unit tests will run for the following packages:"
  114. info ${TEST_PACKAGE_LIST}
  115. "${cmd[@]}" ${TEST_PACKAGE_LIST}
  116. fi
  117. fi
  118. info "All unit tests passed."