blob: e3d3ec6807cf5a4a1cb1e3214280adc41f147afa [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 Jeffery4b4a6bc2018-03-08 12:58:48 +10305# DISTRO = Docker base image. Ubuntu and Fedora are supported.
Matthew Barthc5dec752016-11-18 13:14:37 -06006# WORKSPACE = <location of unit test execution script>
Andrew Jeffery4b4a6bc2018-03-08 12:58:48 +10307# dbus_sys_config_file = <path of the dbus config file>
Andrew Geisslera6b93bf2019-01-03 10:23:38 -06008# BRANCH = <optional, branch to build from each of the openbmc/respositories>
9# default is master, which will be used if input branch not
10# provided or not found
11# DOCKER_IMG_NAME = Default is openbmc/ubuntu-unit-test-master with a
12# -$BRANCH replacing -master if $BRANCH provided
Matthew Barthc5dec752016-11-18 13:14:37 -060013
14# Trace bash processing. Set -e so when a step fails, we fail the build
15set -uo pipefail
16
17# Default variables
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060018DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH:-master}"}
Patrick Venture52164fd2018-08-28 16:12:47 -070019DISTRO=${DISTRO:-ubuntu:bionic}
Andrew Jeffery0b252e32018-03-08 13:03:12 +103020WORKSPACE=${WORKSPACE:-$(mktemp -d --tmpdir unit-test.XXXXXX)}
Matthew Barthc5dec752016-11-18 13:14:37 -060021OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
22UNIT_TEST_PY_DIR="scripts"
23UNIT_TEST_PY="unit-test.py"
Andrew Geisslera28286d2018-01-10 11:00:00 -080024FORMAT_CODE_SH="format-code.sh"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060025DBUS_UNIT_TEST_PY="dbus-unit-test.py"
26DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
Andrew Jefferya153ee32018-03-09 13:22:04 +103027MAKEFLAGS="${MAKEFLAGS:-""}"
William A. Kennington IIId70980f2018-11-08 15:14:27 -080028DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
Matthew Barthc5dec752016-11-18 13:14:37 -060029
30# Timestamp for job
31echo "Unit test build started, $(date)"
32
Andrew Jeffery316ebd32018-03-08 13:05:32 +103033if [[ "${DISTRO}" == "fedora" ]]; then
Matthew Barthc5dec752016-11-18 13:14:37 -060034 echo "Distro (${DISTRO}) not supported, running as ubuntu"
35 DISTRO="ubuntu:latest"
36fi
37
38# Check workspace, build scripts, and package to be unit tested exists
39if [ ! -d "${WORKSPACE}" ]; then
40 echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
41 exit 1
42fi
43if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
44 echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
45 exit 1
46fi
47if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
48 echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
49 exit 1
50fi
51
52# Copy unit test script into workspace
53cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
54${WORKSPACE}/${UNIT_TEST_PY}
55chmod a+x ${WORKSPACE}/${UNIT_TEST_PY}
56
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060057# Copy dbus unit test script into workspace
58cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
59${WORKSPACE}/${DBUS_UNIT_TEST_PY}
60chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
61
Andrew Geisslera28286d2018-01-10 11:00:00 -080062# Copy format code script into workspace
63cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
64${WORKSPACE}/${FORMAT_CODE_SH}
65chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH}
66
Matthew Barthc5dec752016-11-18 13:14:37 -060067# Configure docker build
68cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
69echo "Building docker image with build-unit-test-docker.sh"
Andrew Geisslera6b93bf2019-01-03 10:23:38 -060070# Export input env variables
71export DOCKER_IMG_NAME
72export DISTRO
73export BRANCH
74./build-unit-test-docker.sh
Matthew Barthc5dec752016-11-18 13:14:37 -060075
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060076# Unit test and parameters
William A. Kennington IIId70980f2018-11-08 15:14:27 -080077UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060078
Matthew Barthc5dec752016-11-18 13:14:37 -060079# Run the docker unit test container with the unit test execution script
80echo "Executing docker image"
81docker run --cap-add=sys_admin --rm=true \
James Feist878df5c2018-07-26 14:54:28 -070082 --network host \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060083 --privileged=true \
William A. Kennington IIIbe6aab22018-12-06 15:01:54 -080084 -u "$USER" \
William A. Kennington IIId70980f2018-11-08 15:14:27 -080085 -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
Andrew Jefferya153ee32018-03-09 13:22:04 +103086 -e "MAKEFLAGS=${MAKEFLAGS}" \
Matthew Barth33df8792016-12-19 14:30:17 -060087 -t ${DOCKER_IMG_NAME} \
William A. Kennington IIId70980f2018-11-08 15:14:27 -080088 "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060089 -f ${DBUS_SYS_CONFIG_FILE}
Matthew Barthc5dec752016-11-18 13:14:37 -060090
91# Timestamp for build
92echo "Unit test build completed, $(date)"