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"
37CONFIG_DIR="config"
38UNIT_TEST_PY="unit-test.py"
39FORMAT_CODE_SH="format-code.sh"
40SPELLINGS_TXT="openbmc-spelling.txt openbmc-spelling-ignore.txt"
41ESLINT_CONFIG="eslint-global-config.json"
42DBUS_UNIT_TEST_PY="dbus-unit-test.py"
43TEST_ONLY="${TEST_ONLY:-}"
44DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
45MAKEFLAGS="${MAKEFLAGS:-""}"
46DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
47NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
48INTERACTIVE="${INTERACTIVE:-}"
49http_proxy=${http_proxy:-}
50
51# Timestamp for job
52echo "Unit test build started, $(date)"
53
54# Check workspace, build scripts, and package to be unit tested exists
55if [ ! -d "${WORKSPACE}" ]; then
56    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
57    exit 1
58fi
59if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
60    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
61    exit 1
62fi
63# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
64if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
65    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
66    exit 1
67fi
68
69# Copy unit test script into workspace
70cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
71"${WORKSPACE}"/${UNIT_TEST_PY}
72chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
73
74# Copy dbus unit test script into workspace
75cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
76"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
77chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
78
79# Copy format code script into workspace
80cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
81"${WORKSPACE}"/${FORMAT_CODE_SH}
82chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
83
84# Copy spellings.txt file into workspace
85for f in ${SPELLINGS_TXT}; do
86    cp "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${f}" \
87        "${WORKSPACE}/${f}"
88done
89
90# Copy the eslintconfig file into workspce
91cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${ESLINT_CONFIG} \
92"${WORKSPACE}"/${ESLINT_CONFIG}
93
94# Configure docker build
95cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
96echo "Building docker image with build-unit-test-docker"
97# Export input env variables
98export BRANCH
99DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
100export DOCKER_IMG_NAME
101
102# Allow the user to pass options through to unit-test.py:
103#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
104EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
105
106# Unit test and parameters
107if [ "${INTERACTIVE}" ]; then
108    UNIT_TEST="/bin/bash"
109else
110    UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
111-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
112${EXTRA_UNIT_TEST_ARGS}"
113fi
114
115# Run the docker unit test container with the unit test execution script
116echo "Executing docker image"
117
118PROXY_ENV=""
119# Set up proxies
120if [ -n "${http_proxy}" ]; then
121    PROXY_ENV=" \
122        --env HTTP_PROXY=${http_proxy} \
123        --env HTTPS_PROXY=${http_proxy} \
124        --env FTP_PROXY=${http_proxy} \
125        --env http_proxy=${http_proxy} \
126        --env https_proxy=${http_proxy} \
127        --env ftp_proxy=${http_proxy}"
128fi
129
130# shellcheck disable=SC2086 # ${PROXY_ENV} is meant to be splitted
131docker run --cap-add=sys_admin --rm=true \
132    --privileged=true \
133    ${PROXY_ENV} \
134    -u "$USER" \
135    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
136    -e "MAKEFLAGS=${MAKEFLAGS}" \
137    -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
138    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
139    -f "${DBUS_SYS_CONFIG_FILE}"
140
141# Timestamp for build
142echo "Unit test build completed, $(date)"
143
144# Clean up copied scripts.
145rm "${WORKSPACE}"/${UNIT_TEST_PY}
146rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
147rm "${WORKSPACE}"/${FORMAT_CODE_SH}
148rm "${WORKSPACE}"/${ESLINT_CONFIG}
149
150