blob: fd3c3f7931df1e4285bf9e1caea384524a30b940 [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`
Lei YU7ef93302019-11-06 13:53:21 +080024# NO_FORMAT_CODE: Optional, do not run format-code.sh
Brad Bishop5d6688c2020-08-26 15:44:02 -040025# EXTRA_UNIT_TEST_ARGS: Optional, pass arguments to unit-test.py
Benjamin Fairae58fe72022-05-17 15:22:24 -070026# INTERACTIVE: Optional, run a bash shell instead of unit-test.py
Matthew Barthc5dec752016-11-18 13:14:37 -060027
28# Trace bash processing. Set -e so when a step fails, we fail the build
29set -uo pipefail
30
31# Default variables
Andrew Geisslera61acb52019-01-03 16:32:44 -060032BRANCH=${BRANCH:-"master"}
Matthew Barthc5dec752016-11-18 13:14:37 -060033OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
34UNIT_TEST_PY_DIR="scripts"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053035CONFIG_DIR="config"
Matthew Barthc5dec752016-11-18 13:14:37 -060036UNIT_TEST_PY="unit-test.py"
Andrew Geisslera28286d2018-01-10 11:00:00 -080037FORMAT_CODE_SH="format-code.sh"
Manojkiran Edae6f120a2021-03-20 11:13:43 +053038SPELLINGS_TXT="openbmc-spelling.txt"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053039ESLINT_CONFIG="eslint-global-config.json"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060040DBUS_UNIT_TEST_PY="dbus-unit-test.py"
William A. Kennington III65b37fa2019-01-31 15:15:17 -080041TEST_ONLY="${TEST_ONLY:-}"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060042DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
Andrew Jefferya153ee32018-03-09 13:22:04 +103043MAKEFLAGS="${MAKEFLAGS:-""}"
William A. Kennington IIId70980f2018-11-08 15:14:27 -080044DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
Lei YU7ef93302019-11-06 13:53:21 +080045NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
Benjamin Fairae58fe72022-05-17 15:22:24 -070046INTERACTIVE="${INTERACTIVE:-}"
Matthew Barthc5dec752016-11-18 13:14:37 -060047
48# Timestamp for job
49echo "Unit test build started, $(date)"
50
Matthew Barthc5dec752016-11-18 13:14:37 -060051# 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
Patrick Williams384d7412020-11-06 16:15:41 -060060# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
Matthew Barthc5dec752016-11-18 13:14:37 -060061if [ ! -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
Patrick Williams384d7412020-11-06 16:15:41 -060067cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
68"${WORKSPACE}"/${UNIT_TEST_PY}
69chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
Matthew Barthc5dec752016-11-18 13:14:37 -060070
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060071# Copy dbus unit test script into workspace
Patrick Williams384d7412020-11-06 16:15:41 -060072cp "${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}
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060075
Andrew Geisslera28286d2018-01-10 11:00:00 -080076# Copy format code script into workspace
Patrick Williams384d7412020-11-06 16:15:41 -060077cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
78"${WORKSPACE}"/${FORMAT_CODE_SH}
79chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
Andrew Geisslera28286d2018-01-10 11:00:00 -080080
Manojkiran Edae6f120a2021-03-20 11:13:43 +053081# Copy spellings.txt file into workspace
Patrick Williamsdf6170e2021-12-21 14:19:50 -060082cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${SPELLINGS_TXT} \
Manojkiran Edae6f120a2021-03-20 11:13:43 +053083"${WORKSPACE}"/${SPELLINGS_TXT}
84
Manojkiran Eda87111bb2021-08-14 11:26:16 +053085# Copy the eslintconfig file into workspce
86cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${ESLINT_CONFIG} \
87"${WORKSPACE}"/${ESLINT_CONFIG}
88
Matthew Barthc5dec752016-11-18 13:14:37 -060089# Configure docker build
Patrick Williams384d7412020-11-06 16:15:41 -060090cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
Patrick Williams02871c92021-02-01 20:57:19 -060091echo "Building docker image with build-unit-test-docker"
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060092# Export input env variables
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060093export BRANCH
Patrick Williams9b423102021-03-16 00:07:31 -050094DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
95export DOCKER_IMG_NAME
Matthew Barthc5dec752016-11-18 13:14:37 -060096
Brad Bishop5d6688c2020-08-26 15:44:02 -040097# Allow the user to pass options through to unit-test.py:
98# EXTRA_UNIT_TEST_ARGS="-r 100" ...
99EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
100
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600101# Unit test and parameters
Benjamin Fairae58fe72022-05-17 15:22:24 -0700102if [ "${INTERACTIVE}" ]; then
103 UNIT_TEST="/bin/bash"
104else
105 UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
Brad Bishop5d6688c2020-08-26 15:44:02 -0400106-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
107${EXTRA_UNIT_TEST_ARGS}"
Benjamin Fairae58fe72022-05-17 15:22:24 -0700108fi
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600109
Matthew Barthc5dec752016-11-18 13:14:37 -0600110# Run the docker unit test container with the unit test execution script
111echo "Executing docker image"
112docker run --cap-add=sys_admin --rm=true \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600113 --privileged=true \
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}" \
Benjamin Fairae58fe72022-05-17 15:22:24 -0700117 -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \
Patrick Williams384d7412020-11-06 16:15:41 -0600118 "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
119 -f "${DBUS_SYS_CONFIG_FILE}"
Matthew Barthc5dec752016-11-18 13:14:37 -0600120
121# Timestamp for build
122echo "Unit test build completed, $(date)"
Zane Shelley5ab3e862019-07-24 13:14:57 -0500123
124# Clean up copied scripts.
Patrick Williams384d7412020-11-06 16:15:41 -0600125rm "${WORKSPACE}"/${UNIT_TEST_PY}
126rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
127rm "${WORKSPACE}"/${FORMAT_CODE_SH}
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530128rm "${WORKSPACE}"/${ESLINT_CONFIG}
Zane Shelley5ab3e862019-07-24 13:14:57 -0500129