xref: /openbmc/openbmc-build-scripts/run-unit-test-docker.sh (revision d8e150a50c3daed8530bfc0846afb3c94f25bb2b)
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#   NO_CPPCHECK:     Optional, do not run cppcheck
27#   EXTRA_DOCKER_RUN_ARGS:  Optional, pass arguments to docker run
28#   EXTRA_UNIT_TEST_ARGS:  Optional, pass arguments to unit-test.py
29#   INTERACTIVE: Optional, run a bash shell instead of unit-test.py
30#   http_proxy: Optional, run the container with proxy environment
31
32# Trace bash processing. Set -e so when a step fails, we fail the build
33set -uo pipefail
34
35# Default variables
36BRANCH=${BRANCH:-"master"}
37DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
38OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
39UNIT_TEST_SCRIPT_DIR="${DOCKER_WORKDIR}/${OBMC_BUILD_SCRIPTS}/scripts"
40UNIT_TEST_PY="unit-test.py"
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:-""}"
45NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
46NO_CPPCHECK="${NO_CPPCHECK:-}"
47INTERACTIVE="${INTERACTIVE:-}"
48http_proxy=${http_proxy:-}
49
50# Timestamp for job
51echo "Unit test build started, $(date)"
52
53# Check workspace, build scripts, and package to be unit tested exists
54if [ ! -d "${WORKSPACE}" ]; then
55    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
56    exit 1
57fi
58if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
59    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
60    exit 1
61fi
62# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
63if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
64    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
65    exit 1
66fi
67
68# Configure docker build
69cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
70echo "Building docker image with build-unit-test-docker"
71# Export input env variables
72export BRANCH
73DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
74export DOCKER_IMG_NAME
75
76# Allow the user to pass options through to unit-test.py:
77#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
78EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
79
80# Unit test and parameters
81if [ "${INTERACTIVE}" ]; then
82    UNIT_TEST="/bin/bash"
83else
84    UNIT_TEST="${UNIT_TEST_SCRIPT_DIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
85-p,${UNIT_TEST_PKG},-b,$BRANCH,\
86-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}${NO_CPPCHECK:+,--no-cppcheck}\
87${EXTRA_UNIT_TEST_ARGS}"
88fi
89
90# Run the docker unit test container with the unit test execution script
91echo "Executing docker image"
92
93PROXY_ENV=""
94# Set up proxies
95if [ -n "${http_proxy}" ]; then
96    PROXY_ENV=" \
97        --env HTTP_PROXY=${http_proxy} \
98        --env HTTPS_PROXY=${http_proxy} \
99        --env FTP_PROXY=${http_proxy} \
100        --env http_proxy=${http_proxy} \
101        --env https_proxy=${http_proxy} \
102        --env ftp_proxy=${http_proxy}"
103fi
104
105# If we are building on a podman based machine, need to have this set in
106# the env to allow the home mount to work (no impact on non-podman systems)
107export PODMAN_USERNS="keep-id"
108
109# shellcheck disable=SC2086 # ${PROXY_ENV} and ${EXTRA_DOCKER_RUN_ARGS} are
110# meant to be split
111docker run --cap-add=sys_admin --rm=true \
112    --privileged=true \
113    ${PROXY_ENV} \
114    -u "$USER" \
115    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
116    -e "MAKEFLAGS=${MAKEFLAGS}" \
117    ${EXTRA_DOCKER_RUN_ARGS:-} \
118    -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
119    "${UNIT_TEST_SCRIPT_DIR}/${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