1#!/bin/bash -xe
2
3# This build script is for running the Jenkins unit test builds using docker.
4#
5# It uses a few variables which are part of Jenkins build job matrix:
6#   distro = fedora|ubuntu|ubuntu:14.04|ubuntu:16.04
7#   WORKSPACE = <location of unit test execution script>
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:latest}
15WORKSPACE=${WORKSPACE:-${TMP}/unit-test${RANDOM}}
16OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
17UNIT_TEST_PY_DIR="scripts"
18UNIT_TEST_PY="unit-test.py"
19
20# Timestamp for job
21echo "Unit test build started, $(date)"
22
23# Currently only support ubuntu:latest due to systemd requirements
24if [[ "${DISTRO}" == "ubuntu"* ]]; then
25    DISTRO="ubuntu:latest"
26elif [[ "${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# Configure docker build
51cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
52echo "Building docker image with build-unit-test-docker.sh"
53./build-unit-test-docker.sh ${DOCKER_IMG_NAME} ${DISTRO}
54
55# Run the docker unit test container with the unit test execution script
56echo "Executing docker image"
57docker run --cap-add=sys_admin --rm=true \
58    -e WORKSPACE=${WORKSPACE}/ \
59    -e UNIT_TEST_PKG=${UNIT_TEST_PKG} \
60    -w "${WORKSPACE}" -v "${WORKSPACE}":"${WORKSPACE}" \
61    -t ${DOCKER_IMG_NAME} ${WORKSPACE}/${UNIT_TEST_PY}
62
63# Timestamp for build
64echo "Unit test build completed, $(date)"
65