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