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"
38FORMAT_CODE_SH="format-code.sh"
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:-""}"
43DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
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# Copy unit test script into workspace
67cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
68"${WORKSPACE}"/${UNIT_TEST_PY}
69chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
70
71# Copy dbus unit test script into workspace
72cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
73"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
74chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
75
76# Copy format code script into workspace
77cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
78"${WORKSPACE}"/${FORMAT_CODE_SH}
79chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
80
81# Configure docker build
82cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
83echo "Building docker image with build-unit-test-docker"
84# Export input env variables
85export BRANCH
86DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
87export DOCKER_IMG_NAME
88
89# Allow the user to pass options through to unit-test.py:
90#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
91EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
92
93# Unit test and parameters
94if [ "${INTERACTIVE}" ]; then
95    UNIT_TEST="/bin/bash"
96else
97    UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
98-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
99${EXTRA_UNIT_TEST_ARGS}"
100fi
101
102# Run the docker unit test container with the unit test execution script
103echo "Executing docker image"
104
105PROXY_ENV=""
106# Set up proxies
107if [ -n "${http_proxy}" ]; then
108    PROXY_ENV=" \
109        --env HTTP_PROXY=${http_proxy} \
110        --env HTTPS_PROXY=${http_proxy} \
111        --env FTP_PROXY=${http_proxy} \
112        --env http_proxy=${http_proxy} \
113        --env https_proxy=${http_proxy} \
114        --env ftp_proxy=${http_proxy}"
115fi
116
117# shellcheck disable=SC2086 # ${PROXY_ENV} is meant to be splitted
118docker run --cap-add=sys_admin --rm=true \
119    --privileged=true \
120    ${PROXY_ENV} \
121    -u "$USER" \
122    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
123    -e "MAKEFLAGS=${MAKEFLAGS}" \
124    -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
125    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
126    -f "${DBUS_SYS_CONFIG_FILE}"
127
128# Timestamp for build
129echo "Unit test build completed, $(date)"
130
131# Clean up copied scripts.
132rm "${WORKSPACE}"/${UNIT_TEST_PY}
133rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
134rm "${WORKSPACE}"/${FORMAT_CODE_SH}
135
136