blob: d2a86f109ff5c431ad1e42c54ca0c0fd1f442186 [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
19# DISTRO: Optional, docker base image (ubuntu or fedora)
20# BRANCH: Optional, branch to build from each of the
21# openbmc repositories. default is master, which will be
22# used if input branch not provided or not found
23# DOCKER_IMG_NAME: Optional, default is openbmc/ubuntu-unit-test-master with a
24# -$BRANCH replacing -master if $BRANCH provided
25# dbus_sys_config_file: Optional, with the default being
26# `/usr/share/dbus-1/system.conf`
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 Geisslera6b93bf2019-01-03 10:23:38 -060032DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH:-master}"}
Patrick Venture52164fd2018-08-28 16:12:47 -070033DISTRO=${DISTRO:-ubuntu:bionic}
Matthew Barthc5dec752016-11-18 13:14:37 -060034OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
35UNIT_TEST_PY_DIR="scripts"
36UNIT_TEST_PY="unit-test.py"
Andrew Geisslera28286d2018-01-10 11:00:00 -080037FORMAT_CODE_SH="format-code.sh"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060038DBUS_UNIT_TEST_PY="dbus-unit-test.py"
39DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
Andrew Jefferya153ee32018-03-09 13:22:04 +103040MAKEFLAGS="${MAKEFLAGS:-""}"
William A. Kennington IIId70980f2018-11-08 15:14:27 -080041DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
Matthew Barthc5dec752016-11-18 13:14:37 -060042
43# Timestamp for job
44echo "Unit test build started, $(date)"
45
Andrew Jeffery316ebd32018-03-08 13:05:32 +103046if [[ "${DISTRO}" == "fedora" ]]; then
Matthew Barthc5dec752016-11-18 13:14:37 -060047 echo "Distro (${DISTRO}) not supported, running as ubuntu"
48 DISTRO="ubuntu:latest"
49fi
50
51# 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
60if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
61 echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
62 exit 1
63fi
64
65# Copy unit test script into workspace
66cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
67${WORKSPACE}/${UNIT_TEST_PY}
68chmod a+x ${WORKSPACE}/${UNIT_TEST_PY}
69
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060070# Copy dbus unit test script into workspace
71cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
72${WORKSPACE}/${DBUS_UNIT_TEST_PY}
73chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
74
Andrew Geisslera28286d2018-01-10 11:00:00 -080075# Copy format code script into workspace
76cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
77${WORKSPACE}/${FORMAT_CODE_SH}
78chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH}
79
Matthew Barthc5dec752016-11-18 13:14:37 -060080# Configure docker build
81cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
82echo "Building docker image with build-unit-test-docker.sh"
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060083# Export input env variables
84export DOCKER_IMG_NAME
85export DISTRO
86export BRANCH
87./build-unit-test-docker.sh
Matthew Barthc5dec752016-11-18 13:14:37 -060088
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060089# Unit test and parameters
William A. Kennington IIId70980f2018-11-08 15:14:27 -080090UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060091
Matthew Barthc5dec752016-11-18 13:14:37 -060092# Run the docker unit test container with the unit test execution script
93echo "Executing docker image"
94docker run --cap-add=sys_admin --rm=true \
James Feist878df5c2018-07-26 14:54:28 -070095 --network host \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060096 --privileged=true \
William A. Kennington IIIbe6aab22018-12-06 15:01:54 -080097 -u "$USER" \
William A. Kennington IIId70980f2018-11-08 15:14:27 -080098 -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
Andrew Jefferya153ee32018-03-09 13:22:04 +103099 -e "MAKEFLAGS=${MAKEFLAGS}" \
Matthew Barth33df8792016-12-19 14:30:17 -0600100 -t ${DOCKER_IMG_NAME} \
William A. Kennington IIId70980f2018-11-08 15:14:27 -0800101 "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600102 -f ${DBUS_SYS_CONFIG_FILE}
Matthew Barthc5dec752016-11-18 13:14:37 -0600103
104# Timestamp for build
105echo "Unit test build completed, $(date)"