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#   http_proxy: Optional, run the container with proxy environment
29
30# Trace bash processing. Set -e so when a step fails, we fail the build
31set -uo pipefail
32
33# Default variables
34BRANCH=${BRANCH:-"master"}
35DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
36OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
37UNIT_TEST_SCRIPT_DIR="${DOCKER_WORKDIR}/${OBMC_BUILD_SCRIPTS}/scripts"
38UNIT_TEST_PY="unit-test.py"
39DBUS_UNIT_TEST_PY="dbus-unit-test.py"
40TEST_ONLY="${TEST_ONLY:-}"
41DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
42MAKEFLAGS="${MAKEFLAGS:-""}"
43NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
44INTERACTIVE="${INTERACTIVE:-}"
45http_proxy=${http_proxy:-}
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# Configure docker build
66cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
67echo "Building docker image with build-unit-test-docker"
68# Export input env variables
69export BRANCH
70DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
71export DOCKER_IMG_NAME
72
73# Allow the user to pass options through to unit-test.py:
74#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
75EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
76
77# Unit test and parameters
78if [ "${INTERACTIVE}" ]; then
79    UNIT_TEST="/bin/bash"
80else
81    UNIT_TEST="${UNIT_TEST_SCRIPT_DIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
82-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
83${EXTRA_UNIT_TEST_ARGS}"
84fi
85
86# Run the docker unit test container with the unit test execution script
87echo "Executing docker image"
88
89PROXY_ENV=""
90# Set up proxies
91if [ -n "${http_proxy}" ]; then
92    PROXY_ENV=" \
93        --env HTTP_PROXY=${http_proxy} \
94        --env HTTPS_PROXY=${http_proxy} \
95        --env FTP_PROXY=${http_proxy} \
96        --env http_proxy=${http_proxy} \
97        --env https_proxy=${http_proxy} \
98        --env ftp_proxy=${http_proxy}"
99fi
100
101# shellcheck disable=SC2086 # ${PROXY_ENV} is meant to be splitted
102docker run --cap-add=sys_admin --rm=true \
103    --privileged=true \
104    ${PROXY_ENV} \
105    -u "$USER" \
106    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
107    -e "MAKEFLAGS=${MAKEFLAGS}" \
108    -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
109    "${UNIT_TEST_SCRIPT_DIR}/${DBUS_UNIT_TEST_PY}" -u "${UNIT_TEST}" \
110    -f "${DBUS_SYS_CONFIG_FILE}"
111
112# Timestamp for build
113echo "Unit test build completed, $(date)"
114