blob: 2a4426446f10f401a117eef61f8e12ac78fe0bae [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
Matthew Barthc5dec752016-11-18 13:14:37 -060026
27# Trace bash processing. Set -e so when a step fails, we fail the build
28set -uo pipefail
29
30# Default variables
Andrew Geisslera61acb52019-01-03 16:32:44 -060031BRANCH=${BRANCH:-"master"}
Matthew Barthc5dec752016-11-18 13:14:37 -060032OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
33UNIT_TEST_PY_DIR="scripts"
Manojkiran Edae6f120a2021-03-20 11:13:43 +053034DICTIONARY_DIR="dictionary"
Matthew Barthc5dec752016-11-18 13:14:37 -060035UNIT_TEST_PY="unit-test.py"
Andrew Geisslera28286d2018-01-10 11:00:00 -080036FORMAT_CODE_SH="format-code.sh"
Manojkiran Edae6f120a2021-03-20 11:13:43 +053037SPELLINGS_TXT="openbmc-spelling.txt"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060038DBUS_UNIT_TEST_PY="dbus-unit-test.py"
William A. Kennington III65b37fa2019-01-31 15:15:17 -080039TEST_ONLY="${TEST_ONLY:-}"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060040DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
Andrew Jefferya153ee32018-03-09 13:22:04 +103041MAKEFLAGS="${MAKEFLAGS:-""}"
William A. Kennington IIId70980f2018-11-08 15:14:27 -080042DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
Lei YU7ef93302019-11-06 13:53:21 +080043NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
Matthew Barthc5dec752016-11-18 13:14:37 -060044
45# Timestamp for job
46echo "Unit test build started, $(date)"
47
Matthew Barthc5dec752016-11-18 13:14:37 -060048# Check workspace, build scripts, and package to be unit tested exists
49if [ ! -d "${WORKSPACE}" ]; then
50 echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
51 exit 1
52fi
53if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
54 echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
55 exit 1
56fi
Patrick Williams384d7412020-11-06 16:15:41 -060057# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
Matthew Barthc5dec752016-11-18 13:14:37 -060058if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
59 echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
60 exit 1
61fi
62
63# Copy unit test script into workspace
Patrick Williams384d7412020-11-06 16:15:41 -060064cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
65"${WORKSPACE}"/${UNIT_TEST_PY}
66chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
Matthew Barthc5dec752016-11-18 13:14:37 -060067
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060068# Copy dbus unit test script into workspace
Patrick Williams384d7412020-11-06 16:15:41 -060069cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
70"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
71chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060072
Andrew Geisslera28286d2018-01-10 11:00:00 -080073# Copy format code script into workspace
Patrick Williams384d7412020-11-06 16:15:41 -060074cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
75"${WORKSPACE}"/${FORMAT_CODE_SH}
76chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
Andrew Geisslera28286d2018-01-10 11:00:00 -080077
Manojkiran Edae6f120a2021-03-20 11:13:43 +053078# Copy spellings.txt file into workspace
79cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${DICTIONARY_DIR}/${SPELLINGS_TXT} \
80"${WORKSPACE}"/${SPELLINGS_TXT}
81
Matthew Barthc5dec752016-11-18 13:14:37 -060082# Configure docker build
Patrick Williams384d7412020-11-06 16:15:41 -060083cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
Patrick Williams02871c92021-02-01 20:57:19 -060084echo "Building docker image with build-unit-test-docker"
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060085# Export input env variables
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060086export BRANCH
Patrick Williams9b423102021-03-16 00:07:31 -050087DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
88export DOCKER_IMG_NAME
Matthew Barthc5dec752016-11-18 13:14:37 -060089
Brad Bishop5d6688c2020-08-26 15:44:02 -040090# Allow the user to pass options through to unit-test.py:
91# EXTRA_UNIT_TEST_ARGS="-r 100" ...
92EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
93
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060094# Unit test and parameters
Andrew Geisslera61acb52019-01-03 16:32:44 -060095UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
Brad Bishop5d6688c2020-08-26 15:44:02 -040096-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
97${EXTRA_UNIT_TEST_ARGS}"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060098
Matthew Barthc5dec752016-11-18 13:14:37 -060099# Run the docker unit test container with the unit test execution script
100echo "Executing docker image"
101docker run --cap-add=sys_admin --rm=true \
James Feist878df5c2018-07-26 14:54:28 -0700102 --network host \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600103 --privileged=true \
William A. Kennington IIIbe6aab22018-12-06 15:01:54 -0800104 -u "$USER" \
William A. Kennington IIId70980f2018-11-08 15:14:27 -0800105 -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
Andrew Jefferya153ee32018-03-09 13:22:04 +1030106 -e "MAKEFLAGS=${MAKEFLAGS}" \
Patrick Williams384d7412020-11-06 16:15:41 -0600107 -t "${DOCKER_IMG_NAME}" \
108 "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
109 -f "${DBUS_SYS_CONFIG_FILE}"
Matthew Barthc5dec752016-11-18 13:14:37 -0600110
111# Timestamp for build
112echo "Unit test build completed, $(date)"
Zane Shelley5ab3e862019-07-24 13:14:57 -0500113
114# Clean up copied scripts.
Patrick Williams384d7412020-11-06 16:15:41 -0600115rm "${WORKSPACE}"/${UNIT_TEST_PY}
116rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
117rm "${WORKSPACE}"/${FORMAT_CODE_SH}
Zane Shelley5ab3e862019-07-24 13:14:57 -0500118