1#!/bin/bash -xe
2
3# This build script is for running the Jenkins unit test builds using docker.
4#
5#   DISTRO = Docker base image. Ubuntu and Fedora are supported.
6#   WORKSPACE = <location of unit test execution script>
7#   dbus_sys_config_file = <path of the dbus config file>
8
9# Trace bash processing. Set -e so when a step fails, we fail the build
10set -uo pipefail
11
12# Default variables
13DOCKER_IMG_NAME="openbmc/ubuntu-unit-test"
14DISTRO=${DISTRO:-ubuntu:bionic}
15WORKSPACE=${WORKSPACE:-$(mktemp -d --tmpdir unit-test.XXXXXX)}
16OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
17UNIT_TEST_PY_DIR="scripts"
18UNIT_TEST_PY="unit-test.py"
19FORMAT_CODE_SH="format-code.sh"
20DBUS_UNIT_TEST_PY="dbus-unit-test.py"
21DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
22MAKEFLAGS="${MAKEFLAGS:-""}"
23DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
24
25# Timestamp for job
26echo "Unit test build started, $(date)"
27
28if [[ "${DISTRO}" == "fedora" ]]; then
29    echo "Distro (${DISTRO}) not supported, running as ubuntu"
30    DISTRO="ubuntu:latest"
31fi
32
33# Check workspace, build scripts, and package to be unit tested exists
34if [ ! -d "${WORKSPACE}" ]; then
35    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
36    exit 1
37fi
38if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
39    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
40    exit 1
41fi
42if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
43    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
44    exit 1
45fi
46
47# Copy unit test script into workspace
48cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
49${WORKSPACE}/${UNIT_TEST_PY}
50chmod a+x ${WORKSPACE}/${UNIT_TEST_PY}
51
52# Copy dbus unit test script into workspace
53cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
54${WORKSPACE}/${DBUS_UNIT_TEST_PY}
55chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
56
57# Copy format code script into workspace
58cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
59${WORKSPACE}/${FORMAT_CODE_SH}
60chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH}
61
62# Configure docker build
63cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
64echo "Building docker image with build-unit-test-docker.sh"
65./build-unit-test-docker.sh ${DOCKER_IMG_NAME} ${DISTRO}
66
67# Unit test and parameters
68UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"
69
70# Run the docker unit test container with the unit test execution script
71echo "Executing docker image"
72docker run --cap-add=sys_admin --rm=true \
73    --network host \
74    --privileged=true \
75    -u "$USER" \
76    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
77    -e "MAKEFLAGS=${MAKEFLAGS}" \
78    -t ${DOCKER_IMG_NAME} \
79    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
80    -f ${DBUS_SYS_CONFIG_FILE}
81
82# Timestamp for build
83echo "Unit test build completed, $(date)"
84