blob: 5ac7a67ce5763b5a23023fe6ad182a588c488918 [file] [log] [blame]
Matthew Barthc5dec752016-11-18 13:14:37 -06001#!/bin/bash -xe
2
3# This build script is for running the Jenkins unit test builds using docker.
4#
Andrew Geissler9f980d72019-01-03 14:19:43 -06005# 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
Andrew Geissler9f980d72019-01-03 14:19:43 -060019# 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
Andrew Geissler9f980d72019-01-03 14:19:43 -060022# dbus_sys_config_file: Optional, with the default being
23# `/usr/share/dbus-1/system.conf`
Nan Zhou971a6972022-06-14 18:27:53 +000024# TEST_ONLY: Optional, do not run analysis tools
Lei YU7ef93302019-11-06 13:53:21 +080025# NO_FORMAT_CODE: Optional, do not run format-code.sh
Ewelina Walkuszd8e150a2025-02-04 14:06:02 +010026# NO_CPPCHECK: Optional, do not run cppcheck
Brad Bishop9c237f82023-03-03 13:36:16 -050027# EXTRA_DOCKER_RUN_ARGS: Optional, pass arguments to docker run
Brad Bishop5d6688c2020-08-26 15:44:02 -040028# EXTRA_UNIT_TEST_ARGS: Optional, pass arguments to unit-test.py
Benjamin Fairae58fe72022-05-17 15:22:24 -070029# INTERACTIVE: Optional, run a bash shell instead of unit-test.py
Lei YUb8c7c162022-07-19 19:40:06 +080030# http_proxy: Optional, run the container with proxy environment
Matthew Barthc5dec752016-11-18 13:14:37 -060031
32# Trace bash processing. Set -e so when a step fails, we fail the build
33set -uo pipefail
34
35# Default variables
Andrew Geisslera61acb52019-01-03 16:32:44 -060036BRANCH=${BRANCH:-"master"}
Patrick Williams8e43efe2022-11-28 12:35:05 -060037DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
Matthew Barthc5dec752016-11-18 13:14:37 -060038OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
Patrick Williams8e43efe2022-11-28 12:35:05 -060039UNIT_TEST_SCRIPT_DIR="${DOCKER_WORKDIR}/${OBMC_BUILD_SCRIPTS}/scripts"
Matthew Barthc5dec752016-11-18 13:14:37 -060040UNIT_TEST_PY="unit-test.py"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060041DBUS_UNIT_TEST_PY="dbus-unit-test.py"
William A. Kennington III65b37fa2019-01-31 15:15:17 -080042TEST_ONLY="${TEST_ONLY:-}"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060043DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
Andrew Jefferya153ee32018-03-09 13:22:04 +103044MAKEFLAGS="${MAKEFLAGS:-""}"
Lei YU7ef93302019-11-06 13:53:21 +080045NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
Ewelina Walkuszd8e150a2025-02-04 14:06:02 +010046NO_CPPCHECK="${NO_CPPCHECK:-}"
Benjamin Fairae58fe72022-05-17 15:22:24 -070047INTERACTIVE="${INTERACTIVE:-}"
Lei YUb8c7c162022-07-19 19:40:06 +080048http_proxy=${http_proxy:-}
Matthew Barthc5dec752016-11-18 13:14:37 -060049
50# Timestamp for job
51echo "Unit test build started, $(date)"
52
Matthew Barthc5dec752016-11-18 13:14:37 -060053# Check workspace, build scripts, and package to be unit tested exists
54if [ ! -d "${WORKSPACE}" ]; then
55 echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
56 exit 1
57fi
58if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
59 echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
60 exit 1
61fi
Patrick Williams384d7412020-11-06 16:15:41 -060062# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
Matthew Barthc5dec752016-11-18 13:14:37 -060063if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
64 echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
65 exit 1
66fi
67
Matthew Barthc5dec752016-11-18 13:14:37 -060068# Configure docker build
Patrick Williams384d7412020-11-06 16:15:41 -060069cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
Patrick Williams02871c92021-02-01 20:57:19 -060070echo "Building docker image with build-unit-test-docker"
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060071# Export input env variables
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060072export BRANCH
Patrick Williams9b423102021-03-16 00:07:31 -050073DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
74export DOCKER_IMG_NAME
Matthew Barthc5dec752016-11-18 13:14:37 -060075
Brad Bishop5d6688c2020-08-26 15:44:02 -040076# Allow the user to pass options through to unit-test.py:
77# EXTRA_UNIT_TEST_ARGS="-r 100" ...
78EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
79
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060080# Unit test and parameters
Benjamin Fairae58fe72022-05-17 15:22:24 -070081if [ "${INTERACTIVE}" ]; then
82 UNIT_TEST="/bin/bash"
83else
Patrick Williams8e43efe2022-11-28 12:35:05 -060084 UNIT_TEST="${UNIT_TEST_SCRIPT_DIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
Ewelina Walkuszd8e150a2025-02-04 14:06:02 +010085-p,${UNIT_TEST_PKG},-b,$BRANCH,\
86-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}${NO_CPPCHECK:+,--no-cppcheck}\
Brad Bishop5d6688c2020-08-26 15:44:02 -040087${EXTRA_UNIT_TEST_ARGS}"
Benjamin Fairae58fe72022-05-17 15:22:24 -070088fi
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060089
Matthew Barthc5dec752016-11-18 13:14:37 -060090# Run the docker unit test container with the unit test execution script
91echo "Executing docker image"
Lei YUb8c7c162022-07-19 19:40:06 +080092
93PROXY_ENV=""
94# Set up proxies
95if [ -n "${http_proxy}" ]; then
96 PROXY_ENV=" \
97 --env HTTP_PROXY=${http_proxy} \
98 --env HTTPS_PROXY=${http_proxy} \
99 --env FTP_PROXY=${http_proxy} \
100 --env http_proxy=${http_proxy} \
101 --env https_proxy=${http_proxy} \
102 --env ftp_proxy=${http_proxy}"
103fi
104
Andrew Geissler347c94c2023-04-07 13:10:11 -0600105# If we are building on a podman based machine, need to have this set in
106# the env to allow the home mount to work (no impact on non-podman systems)
107export PODMAN_USERNS="keep-id"
108
Brad Bishop9c237f82023-03-03 13:36:16 -0500109# shellcheck disable=SC2086 # ${PROXY_ENV} and ${EXTRA_DOCKER_RUN_ARGS} are
110# meant to be split
Matthew Barthc5dec752016-11-18 13:14:37 -0600111docker run --cap-add=sys_admin --rm=true \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600112 --privileged=true \
Lei YUb8c7c162022-07-19 19:40:06 +0800113 ${PROXY_ENV} \
William A. Kennington IIIbe6aab22018-12-06 15:01:54 -0800114 -u "$USER" \
William A. Kennington IIId70980f2018-11-08 15:14:27 -0800115 -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
Andrew Jefferya153ee32018-03-09 13:22:04 +1030116 -e "MAKEFLAGS=${MAKEFLAGS}" \
Brad Bishop9c237f82023-03-03 13:36:16 -0500117 ${EXTRA_DOCKER_RUN_ARGS:-} \
Benjamin Fairae58fe72022-05-17 15:22:24 -0700118 -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
Patrick Williams8e43efe2022-11-28 12:35:05 -0600119 "${UNIT_TEST_SCRIPT_DIR}/${DBUS_UNIT_TEST_PY}" -u "${UNIT_TEST}" \
Patrick Williams384d7412020-11-06 16:15:41 -0600120 -f "${DBUS_SYS_CONFIG_FILE}"
Matthew Barthc5dec752016-11-18 13:14:37 -0600121
122# Timestamp for build
123echo "Unit test build completed, $(date)"