GoogleGit

blob: 85ff912e5f6f848b76974f3e14d264ea6d10a1c7 [file history] [blame]
#!/bin/bash

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This script builds and runs Chromium OS unit tests.  Note that this script
# utilizes the src_test stanza in chromeos-base packages.  These stanzas
# should both build and run the unit tests.

# This script requires that you run build_packages first.

# Special because this can only run inside the chroot.
. "/usr/lib/crosutils/common.sh" 2> /dev/null ||
  (echo "Must run within chroot" && false) ||
  exit 1

# Flags
DEFINE_string board "${DEFAULT_BOARD}" \
  "Target board of which tests were built"
DEFINE_string build_root "${DEFAULT_BUILD_ROOT}" \
  "Root of build output"
DEFINE_boolean fast "${DEFAULT_FAST}" \
  "Call many emerges in parallel."
DEFINE_string package_file "" \
  "File with space-separated list of packages to run unit tests" f
DEFINE_string packages "" \
  "Optional space-separated list of packages to run unit tests" p
DEFINE_string blacklist_packages "" \
  "List of packages to blacklist from unit tests"
DEFINE_boolean withdebug "${FLAGS_TRUE}" \
  "Build debug versions of Chromium-OS-specific packages."
DEFINE_boolean pretend "${FLAGS_FALSE}" \
  "Only show the packages we would test; don't actually test"
DEFINE_boolean installed "${FLAGS_TRUE}" \
  "Only test packages that are already installed"

# List of packages with no unit tests.
NO_UNITTESTS=""

check_src_test() {
  egrep '^src_test()' "${1}" > /dev/null
}

# Parse command line and die if unexpected parameters given.
FLAGS_HELP="usage: ${0} [flags]"
FLAGS "${@}" || exit 1
eval set -- "${FLAGS_ARGV}"
check_flags_only_and_allow_null_arg "${@}" && set --

set -e

[ -z "${FLAGS_board}" ] && die "--board required"

EMERGE_CMD="emerge"
EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
if [[ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]]; then
  EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge"
  EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
fi

# Create package list from package file and list of packages.
if [ -n "${FLAGS_package_file}" ]; then
  if [ -f "${FLAGS_package_file}" ]; then
    PACKAGE_LIST="$(cat ${FLAGS_package_file})"
  else
    warn "Missing package file."
  fi
fi

[ -n "${FLAGS_packages}" ] && PACKAGE_LIST="${PACKAGE_LIST} ${FLAGS_packages}"

# If we didn't specify packages, find all packages.
if [ -z "${FLAGS_package_file}" -a -z "${FLAGS_packages}" ]; then
  PACKAGE_LIST=$(cros_workon --board "${FLAGS_board}" list --all)

  if [[ ${FLAGS_installed} -eq ${FLAGS_TRUE} ]]; then
    PACKAGE_LIST=$(qlist-${FLAGS_board} -ICe ${PACKAGE_LIST})
  fi
fi

BLACK_LIST_FILE="/usr/share/crostestutils/unit_test_black_list.txt"
BLACK_LIST_PACKAGES="${FLAGS_blacklist_packages}"

if [ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]; then
  export USE="${USE} -cros-debug"
fi

ALL_PACKAGE_LIST=$(
  jobs=0
  for package in ${PACKAGE_LIST}; do
    (
    if grep -xq "${package}" "${BLACK_LIST_FILE}" || \
       [[ " ${BLACK_LIST_PACKAGES} " =~ " ${package} " ]]; then
      warn "Skipping package ${package} since it is blacklisted."
      continue
    fi
    EBUILD_PATH=$(equery-${FLAGS_board} which ${package} 2>/dev/null) || \
      warn "${package} not found"
    if [[ -n ${EBUILD_PATH} ]]; then
      if check_src_test "${EBUILD_PATH}"; then
        echo "y:${package}"
      else
        echo "n:${package}"
      fi
    fi
    ) &
    if [[ ${jobs} -ge ${NUM_JOBS} ]]; then
      jobs=0
      wait
    else
      : $(( jobs += 1 ))
    fi
  done | sort
  wait
)
TEST_PACKAGE_LIST=$(echo "${ALL_PACKAGE_LIST}" | sed -n '/^y:/s:^..::p')
NO_UNITTESTS=$(echo "${ALL_PACKAGE_LIST}" | sed -n '/^n:/s:^..::p')

if [ -n "${NO_UNITTESTS}" ]; then
  warn "The following packages have no unit tests:"
  warn "${NO_UNITTESTS}"
fi

if [ -n "${TEST_PACKAGE_LIST}" ]; then
  if [[ ${FLAGS_pretend} -eq ${FLAGS_TRUE} ]]; then
    printf '%s\n' ${TEST_PACKAGE_LIST} | sort
    exit 0
  else
    sudo -E PKGDIR="/build/${FLAGS_board}/test-packages" FEATURES="test" \
      ${EMERGE_BOARD_CMD} --nodeps --buildpkgonly ${TEST_PACKAGE_LIST}
  fi
fi

info "All unit tests passed."