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