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#   TEST_ONLY:       Optional, do not run analysis tools
25#   NO_FORMAT_CODE:  Optional, do not run format-code.sh
26#   EXTRA_UNIT_TEST_ARGS:  Optional, pass arguments to unit-test.py
27#   INTERACTIVE: Optional, run a bash shell instead of unit-test.py
28
29# Trace bash processing. Set -e so when a step fails, we fail the build
30set -uo pipefail
31
32# Default variables
33BRANCH=${BRANCH:-"master"}
34OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
35UNIT_TEST_PY_DIR="scripts"
36CONFIG_DIR="config"
37UNIT_TEST_PY="unit-test.py"
38FORMAT_CODE_SH="format-code.sh"
39SPELLINGS_TXT="openbmc-spelling.txt"
40ESLINT_CONFIG="eslint-global-config.json"
41DBUS_UNIT_TEST_PY="dbus-unit-test.py"
42TEST_ONLY="${TEST_ONLY:-}"
43DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
44MAKEFLAGS="${MAKEFLAGS:-""}"
45DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
46NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
47INTERACTIVE="${INTERACTIVE:-}"
48
49# Timestamp for job
50echo "Unit test build started, $(date)"
51
52# Check workspace, build scripts, and package to be unit tested exists
53if [ ! -d "${WORKSPACE}" ]; then
54    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
55    exit 1
56fi
57if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
58    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
59    exit 1
60fi
61# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
62if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
63    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
64    exit 1
65fi
66
67# Copy unit test script into workspace
68cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
69"${WORKSPACE}"/${UNIT_TEST_PY}
70chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
71
72# Copy dbus unit test script into workspace
73cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
74"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
75chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
76
77# Copy format code script into workspace
78cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
79"${WORKSPACE}"/${FORMAT_CODE_SH}
80chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
81
82# Copy spellings.txt file into workspace
83cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${SPELLINGS_TXT} \
84"${WORKSPACE}"/${SPELLINGS_TXT}
85
86# Copy the eslintconfig file into workspce
87cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${ESLINT_CONFIG} \
88"${WORKSPACE}"/${ESLINT_CONFIG}
89
90# Configure docker build
91cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
92echo "Building docker image with build-unit-test-docker"
93# Export input env variables
94export BRANCH
95DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
96export DOCKER_IMG_NAME
97
98# Allow the user to pass options through to unit-test.py:
99#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
100EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
101
102# Unit test and parameters
103if [ "${INTERACTIVE}" ]; then
104    UNIT_TEST="/bin/bash"
105else
106    UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
107-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
108${EXTRA_UNIT_TEST_ARGS}"
109fi
110
111# Run the docker unit test container with the unit test execution script
112echo "Executing docker image"
113docker run --cap-add=sys_admin --rm=true \
114    --privileged=true \
115    -u "$USER" \
116    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
117    -e "MAKEFLAGS=${MAKEFLAGS}" \
118    -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
119    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
120    -f "${DBUS_SYS_CONFIG_FILE}"
121
122# Timestamp for build
123echo "Unit test build completed, $(date)"
124
125# Clean up copied scripts.
126rm "${WORKSPACE}"/${UNIT_TEST_PY}
127rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
128rm "${WORKSPACE}"/${FORMAT_CODE_SH}
129rm "${WORKSPACE}"/${ESLINT_CONFIG}
130
131