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:artful}
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"}
22
23# Timestamp for job
24echo "Unit test build started, $(date)"
25
26if [[ "${DISTRO}" == "fedora" ]]; then
27    echo "Distro (${DISTRO}) not supported, running as ubuntu"
28    DISTRO="ubuntu:latest"
29fi
30
31# Check workspace, build scripts, and package to be unit tested exists
32if [ ! -d "${WORKSPACE}" ]; then
33    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
34    exit 1
35fi
36if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
37    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
38    exit 1
39fi
40if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
41    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
42    exit 1
43fi
44
45# Copy unit test script into workspace
46cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
47${WORKSPACE}/${UNIT_TEST_PY}
48chmod a+x ${WORKSPACE}/${UNIT_TEST_PY}
49
50# Copy dbus unit test script into workspace
51cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
52${WORKSPACE}/${DBUS_UNIT_TEST_PY}
53chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
54
55# Copy format code script into workspace
56cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
57${WORKSPACE}/${FORMAT_CODE_SH}
58chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH}
59
60# Configure docker build
61cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
62echo "Building docker image with build-unit-test-docker.sh"
63./build-unit-test-docker.sh ${DOCKER_IMG_NAME} ${DISTRO}
64
65# Unit test and parameters
66UNIT_TEST="${WORKSPACE}/${UNIT_TEST_PY},-w,${WORKSPACE},-p,${UNIT_TEST_PKG},-v"
67
68# Run the docker unit test container with the unit test execution script
69echo "Executing docker image"
70docker run --cap-add=sys_admin --rm=true \
71    --privileged=true \
72    -w "${WORKSPACE}" -v "${WORKSPACE}":"${WORKSPACE}" \
73    -t ${DOCKER_IMG_NAME} \
74    ${WORKSPACE}/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
75    -f ${DBUS_SYS_CONFIG_FILE}
76
77# Timestamp for build
78echo "Unit test build completed, $(date)"
79