xref: /openbmc/openbmc-build-scripts/run-unit-test-docker.sh (revision 00536fbebf507416e289c8b7193c814bebc0ac98)
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#   dbus_sys_config_file: Optional, with the default being
24#                         `/usr/share/dbus-1/system.conf`
25#   NO_FORMAT_CODE:  Optional, do not run format-code.sh
26#   EXTRA_UNIT_TEST_ARGS:  Optional, pass arguments to unit-test.py
27
28# Trace bash processing. Set -e so when a step fails, we fail the build
29set -uo pipefail
30
31# Default variables
32BRANCH=${BRANCH:-"master"}
33DISTRO=${DISTRO:-ubuntu:focal}
34OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
35UNIT_TEST_PY_DIR="scripts"
36UNIT_TEST_PY="unit-test.py"
37FORMAT_CODE_SH="format-code.sh"
38DBUS_UNIT_TEST_PY="dbus-unit-test.py"
39TEST_ONLY="${TEST_ONLY:-}"
40DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"}
41MAKEFLAGS="${MAKEFLAGS:-""}"
42DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}"
43NO_FORMAT_CODE="${NO_FORMAT_CODE:-}"
44
45# Timestamp for job
46echo "Unit test build started, $(date)"
47
48if [[ "${DISTRO}" == "fedora" ]]; then
49    echo "Distro (${DISTRO}) not supported, running as ubuntu"
50    DISTRO="ubuntu:latest"
51fi
52
53# Check workspace, build scripts, and package to be unit tested exists
54if [ ! -d "${WORKSPACE}" ]; then
55    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
56    exit 1
57fi
58if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
59    echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..."
60    exit 1
61fi
62# shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled.
63if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then
64    echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..."
65    exit 1
66fi
67
68# Copy unit test script into workspace
69cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \
70"${WORKSPACE}"/${UNIT_TEST_PY}
71chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY}
72
73# Copy dbus unit test script into workspace
74cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \
75"${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
76chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
77
78# Copy format code script into workspace
79cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \
80"${WORKSPACE}"/${FORMAT_CODE_SH}
81chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH}
82
83# Configure docker build
84cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
85echo "Building docker image with build-unit-test-docker"
86# Export input env variables
87export DISTRO
88export BRANCH
89export DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
90
91# Allow the user to pass options through to unit-test.py:
92#   EXTRA_UNIT_TEST_ARGS="-r 100" ...
93EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}"
94
95# Unit test and parameters
96UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
97-p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\
98${EXTRA_UNIT_TEST_ARGS}"
99
100# Run the docker unit test container with the unit test execution script
101echo "Executing docker image"
102docker run --cap-add=sys_admin --rm=true \
103    --network host \
104    --privileged=true \
105    -u "$USER" \
106    -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \
107    -e "MAKEFLAGS=${MAKEFLAGS}" \
108    -t "${DOCKER_IMG_NAME}" \
109    "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \
110    -f "${DBUS_SYS_CONFIG_FILE}"
111
112# Timestamp for build
113echo "Unit test build completed, $(date)"
114
115# Clean up copied scripts.
116rm "${WORKSPACE}"/${UNIT_TEST_PY}
117rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY}
118rm "${WORKSPACE}"/${FORMAT_CODE_SH}
119
120