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"}
35OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
36UNIT_TEST_PY_DIR="scripts"
37UNIT_TEST_PY="unit-test.py"
38DBUS_UNIT_TEST_PY="dbus-unit-test.py"
39TEST_ONLY="${TEST_ONLY:-}"
40DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
41MAKEFLAGS="${MAKEFLAGS:-""}"
42DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
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# 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# Configure docker build
76cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
77echo "Building docker image with build-unit-test-docker"
78# Export input env variables
79export BRANCH
80DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
81export DOCKER_IMG_NAME
82
83# Allow the user to pass options through to unit-test.py:
84#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
85EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
86
87# Unit test and parameters
88if [ "${INTERACTIVE}" ]; then
89    UNIT_TEST="/bin/bash"
90else
91    UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
92-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
93${EXTRA_UNIT_TEST_ARGS}"
94fi
95
96# Run the docker unit test container with the unit test execution script
97echo "Executing docker image"
98
99PROXY_ENV=""
100# Set up proxies
101if [ -n "${http_proxy}" ]; then
102    PROXY_ENV=" \
103        --env HTTP_PROXY=${http_proxy} \
104        --env HTTPS_PROXY=${http_proxy} \
105        --env FTP_PROXY=${http_proxy} \
106        --env http_proxy=${http_proxy} \
107        --env https_proxy=${http_proxy} \
108        --env ftp_proxy=${http_proxy}"
109fi
110
111# shellcheck disable=SC2086 # ${PROXY_ENV} is meant to be splitted
112docker run --cap-add=sys_admin --rm=true \
113    --privileged=true \
114    ${PROXY_ENV} \
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}
128
129