1#!/bin/bash -xe 2 3# This build script is for running the Jenkins unit test builds using docker. 4# 5# This script will build a docker container which will then be used to build 6# and test the input UNIT_TEST_PKG. The docker container will be pre-populated 7# with the most used OpenBMC repositories (phosphor-dbus-interfaces, sdbusplus, 8# phosphor-logging, ...). This allows the use of docker caching 9# capabilities so the dependent repositories are only built once per update 10# to their corresponding repository. If a BRANCH parameter is input then the 11# docker container will be pre-populated with the latest code from that input 12# branch. If the branch does not exist in the repository, then master will be 13# used. 14# 15# UNIT_TEST_PKG: Required, repository which has been extracted and is to 16# be tested 17# WORKSPACE: Required, location of unit test scripts and repository 18# code to test 19# BRANCH: Optional, branch to build from each of the 20# openbmc repositories. default is master, which will be 21# used if input branch not provided or not found 22# dbus_sys_config_file: Optional, with the default being 23# `/usr/share/dbus-1/system.conf` 24# NO_FORMAT_CODE: Optional, do not run format-code.sh 25# EXTRA_UNIT_TEST_ARGS: Optional, pass arguments to unit-test.py 26 27# Trace bash processing. Set -e so when a step fails, we fail the build 28set -uo pipefail 29 30# Default variables 31BRANCH=${BRANCH:-"master"} 32OBMC_BUILD_SCRIPTS="openbmc-build-scripts" 33UNIT_TEST_PY_DIR="scripts" 34DICTIONARY_DIR="dictionary" 35CONFIG_DIR="config" 36UNIT_TEST_PY="unit-test.py" 37FORMAT_CODE_SH="format-code.sh" 38SPELLINGS_TXT="openbmc-spelling.txt" 39ESLINT_CONFIG="eslint-global-config.json" 40DBUS_UNIT_TEST_PY="dbus-unit-test.py" 41TEST_ONLY="${TEST_ONLY:-}" 42DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"} 43MAKEFLAGS="${MAKEFLAGS:-""}" 44DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}" 45NO_FORMAT_CODE="${NO_FORMAT_CODE:-}" 46 47# Timestamp for job 48echo "Unit test build started, $(date)" 49 50# Check workspace, build scripts, and package to be unit tested exists 51if [ ! -d "${WORKSPACE}" ]; then 52 echo "Workspace(${WORKSPACE}) doesn't exist, exiting..." 53 exit 1 54fi 55if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then 56 echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..." 57 exit 1 58fi 59# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled. 60if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then 61 echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..." 62 exit 1 63fi 64 65# Copy unit test script into workspace 66cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \ 67"${WORKSPACE}"/${UNIT_TEST_PY} 68chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY} 69 70# Copy dbus unit test script into workspace 71cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \ 72"${WORKSPACE}"/${DBUS_UNIT_TEST_PY} 73chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY} 74 75# Copy format code script into workspace 76cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \ 77"${WORKSPACE}"/${FORMAT_CODE_SH} 78chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH} 79 80# Copy spellings.txt file into workspace 81cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${DICTIONARY_DIR}/${SPELLINGS_TXT} \ 82"${WORKSPACE}"/${SPELLINGS_TXT} 83 84# Copy the eslintconfig file into workspce 85cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${ESLINT_CONFIG} \ 86"${WORKSPACE}"/${ESLINT_CONFIG} 87 88# Configure docker build 89cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS} 90echo "Building docker image with build-unit-test-docker" 91# Export input env variables 92export BRANCH 93DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker) 94export DOCKER_IMG_NAME 95 96# Allow the user to pass options through to unit-test.py: 97# EXTRA_UNIT_TEST_ARGS="-r 100" ... 98EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}" 99 100# Unit test and parameters 101UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\ 102-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\ 103${EXTRA_UNIT_TEST_ARGS}" 104 105# Run the docker unit test container with the unit test execution script 106echo "Executing docker image" 107docker run --cap-add=sys_admin --rm=true \ 108 --network host \ 109 --privileged=true \ 110 -u "$USER" \ 111 -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \ 112 -e "MAKEFLAGS=${MAKEFLAGS}" \ 113 -t "${DOCKER_IMG_NAME}" \ 114 "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \ 115 -f "${DBUS_SYS_CONFIG_FILE}" 116 117# Timestamp for build 118echo "Unit test build completed, $(date)" 119 120# Clean up copied scripts. 121rm "${WORKSPACE}"/${UNIT_TEST_PY} 122rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY} 123rm "${WORKSPACE}"/${FORMAT_CODE_SH} 124rm "${WORKSPACE}"/${ESLINT_CONFIG} 125 126