1#!/bin/bash -xe
2
3# This build script is for running the Jenkins unit test builds using docker.
4#
5# This script will build a docker container which will then be used to build
6# and test the input UNIT_TEST_PKG. The docker container will be pre-populated
7# with the most used OpenBMC repositories (phosphor-dbus-interfaces, sdbusplus,
8# phosphor-logging, ...). This allows the use of docker caching
9# capabilities so the dependent repositories are only built once per update
10# to their corresponding repository. If a BRANCH parameter is input then the
11# docker container will be pre-populated with the latest code from that input
12# branch. If the branch does not exist in the repository, then master will be
13# used.
14#
15#   UNIT_TEST_PKG:   Required, repository which has been extracted and is to
16#                    be tested
17#   WORKSPACE:       Required, location of unit test scripts and repository
18#                    code to test
19#   DISTRO:          Optional, docker base image (ubuntu or fedora)
20#   BRANCH:          Optional, branch to build from each of the
21#                    openbmc repositories. default is master, which will be
22#                    used if input branch not provided or not found
23#   DOCKER_IMG_NAME: Optional, default is openbmc/ubuntu-unit-test-master with a
24#                    -$BRANCH replacing -master if $BRANCH provided
25#   dbus_sys_config_file: Optional, with the default being
26#                         `/usr/share/dbus-1/system.conf`
27#   NO_FORMAT_CODE:  Optional, do not run format-code.sh
28#   EXTRA_UNIT_TEST_ARGS:  Optional, pass arguments to unit-test.py
29
30# Trace bash processing. Set -e so when a step fails, we fail the build
31set -uo pipefail
32
33# Default variables
34BRANCH=${BRANCH:-"master"}
35DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH}"}
36DISTRO=${DISTRO:-ubuntu:focal}
37OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
38UNIT_TEST_PY_DIR="scripts"
39UNIT_TEST_PY="unit-test.py"
40FORMAT_CODE_SH="format-code.sh"
41DBUS_UNIT_TEST_PY="dbus-unit-test.py"
42TEST_ONLY="${TEST_ONLY:-}"
43DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
44MAKEFLAGS="${MAKEFLAGS:-""}"
45DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
46NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
47
48# Timestamp for job
49echo "Unit test build started, $(date)"
50
51if [[ "${DISTRO}" == "fedora" ]]; then
52    echo "Distro (${DISTRO}) not supported, running as ubuntu"
53    DISTRO="ubuntu:latest"
54fi
55
56# Check workspace, build scripts, and package to be unit tested exists
57if [ ! -d "${WORKSPACE}" ]; then
58    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
59    exit 1
60fi
61if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
62    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
63    exit 1
64fi
65if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
66    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
67    exit 1
68fi
69
70# Copy unit test script into workspace
71cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
72${WORKSPACE}/${UNIT_TEST_PY}
73chmod a+x ${WORKSPACE}/${UNIT_TEST_PY}
74
75# Copy dbus unit test script into workspace
76cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
77${WORKSPACE}/${DBUS_UNIT_TEST_PY}
78chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
79
80# Copy format code script into workspace
81cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
82${WORKSPACE}/${FORMAT_CODE_SH}
83chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH}
84
85# Configure docker build
86cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
87echo "Building docker image with build-unit-test-docker.sh"
88# Export input env variables
89export DOCKER_IMG_NAME
90export DISTRO
91export BRANCH
92./build-unit-test-docker.sh
93
94# Allow the user to pass options through to unit-test.py:
95#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
96EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
97
98# Unit test and parameters
99UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
100-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
101${EXTRA_UNIT_TEST_ARGS}"
102
103# Run the docker unit test container with the unit test execution script
104echo "Executing docker image"
105docker run --cap-add=sys_admin --rm=true \
106    --network host \
107    --privileged=true \
108    -u "$USER" \
109    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
110    -e "MAKEFLAGS=${MAKEFLAGS}" \
111    -t ${DOCKER_IMG_NAME} \
112    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \
113    -f ${DBUS_SYS_CONFIG_FILE}
114
115# Timestamp for build
116echo "Unit test build completed, $(date)"
117
118# Clean up copied scripts.
119rm ${WORKSPACE}/${UNIT_TEST_PY}
120rm ${WORKSPACE}/${DBUS_UNIT_TEST_PY}
121rm ${WORKSPACE}/${FORMAT_CODE_SH}
122
123