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#   NO_FORMAT_CODE:  Optional, do not run format-code.sh
25#   EXTRA_UNIT_TEST_ARGS:  Optional, pass arguments to unit-test.py
26
27# Trace bash processing. Set -e so when a step fails, we fail the build
28set -uo pipefail
29
30# Default variables
31BRANCH=${BRANCH:-"master"}
32OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
33UNIT_TEST_PY_DIR="scripts"
34UNIT_TEST_PY="unit-test.py"
35FORMAT_CODE_SH="format-code.sh"
36DBUS_UNIT_TEST_PY="dbus-unit-test.py"
37TEST_ONLY="${TEST_ONLY:-}"
38DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
39MAKEFLAGS="${MAKEFLAGS:-""}"
40DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
41NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
42
43# Timestamp for job
44echo "Unit test build started, $(date)"
45
46# Check workspace, build scripts, and package to be unit tested exists
47if [ ! -d "${WORKSPACE}" ]; then
48    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
49    exit 1
50fi
51if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
52    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
53    exit 1
54fi
55# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
56if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
57    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
58    exit 1
59fi
60
61# Copy unit test script into workspace
62cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
63"${WORKSPACE}"/${UNIT_TEST_PY}
64chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
65
66# Copy dbus unit test script into workspace
67cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
68"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
69chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
70
71# Copy format code script into workspace
72cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
73"${WORKSPACE}"/${FORMAT_CODE_SH}
74chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
75
76# Configure docker build
77cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
78echo "Building docker image with build-unit-test-docker"
79# Export input env variables
80export BRANCH
81DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
82export DOCKER_IMG_NAME
83
84# Allow the user to pass options through to unit-test.py:
85#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
86EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
87
88# Unit test and parameters
89UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
90-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
91${EXTRA_UNIT_TEST_ARGS}"
92
93# Run the docker unit test container with the unit test execution script
94echo "Executing docker image"
95docker run --cap-add=sys_admin --rm=true \
96    --network host \
97    --privileged=true \
98    -u "$USER" \
99    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
100    -e "MAKEFLAGS=${MAKEFLAGS}" \
101    -t "${DOCKER_IMG_NAME}" \
102    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
103    -f "${DBUS_SYS_CONFIG_FILE}"
104
105# Timestamp for build
106echo "Unit test build completed, $(date)"
107
108# Clean up copied scripts.
109rm "${WORKSPACE}"/${UNIT_TEST_PY}
110rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
111rm "${WORKSPACE}"/${FORMAT_CODE_SH}
112
113