xref: /openbmc/openbmc-build-scripts/run-unit-test-docker.sh (revision a6b93bf7bc3bd81691f04b8fe028c8ff5f932fd8)
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#   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
13
14# Trace bash processing. Set -e so when a step fails, we fail the build
15set -uo pipefail
16
17# Default variables
18DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH:-master}"}
19DISTRO=${DISTRO:-ubuntu:bionic}
20WORKSPACE=${WORKSPACE:-$(mktemp -d --tmpdir unit-test.XXXXXX)}
21OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
22UNIT_TEST_PY_DIR="scripts"
23UNIT_TEST_PY="unit-test.py"
24FORMAT_CODE_SH="format-code.sh"
25DBUS_UNIT_TEST_PY="dbus-unit-test.py"
26DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
27MAKEFLAGS="${MAKEFLAGS:-""}"
28DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
29
30# Timestamp for job
31echo "Unit test build started, $(date)"
32
33if [[ "${DISTRO}" == "fedora" ]]; then
34    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
57# 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
62# 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
67# Configure docker build
68cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
69echo "Building docker image with build-unit-test-docker.sh"
70# Export input env variables
71export DOCKER_IMG_NAME
72export DISTRO
73export BRANCH
74./build-unit-test-docker.sh
75
76# Unit test and parameters
77UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"
78
79# Run the docker unit test container with the unit test execution script
80echo "Executing docker image"
81docker run --cap-add=sys_admin --rm=true \
82    --network host \
83    --privileged=true \
84    -u "$USER" \
85    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
86    -e "MAKEFLAGS=${MAKEFLAGS}" \
87    -t ${DOCKER_IMG_NAME} \
88    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
89    -f ${DBUS_SYS_CONFIG_FILE}
90
91# Timestamp for build
92echo "Unit test build completed, $(date)"
93