Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 1 | #!/bin/bash -xe |
| 2 | |
| 3 | # This build script is for running the Jenkins unit test builds using docker. |
| 4 | # |
Andrew Geissler | 9f980d7 | 2019-01-03 14:19:43 -0600 | [diff] [blame] | 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` |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 27 | # NO_FORMAT_CODE: Optional, do not run format-code.sh |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 28 | |
| 29 | # Trace bash processing. Set -e so when a step fails, we fail the build |
| 30 | set -uo pipefail |
| 31 | |
| 32 | # Default variables |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 33 | BRANCH=${BRANCH:-"master"} |
| 34 | DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH}"} |
Andrew Geissler | 595739b | 2020-05-19 14:21:49 -0500 | [diff] [blame^] | 35 | DISTRO=${DISTRO:-ubuntu:focal} |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 36 | OBMC_BUILD_SCRIPTS="openbmc-build-scripts" |
| 37 | UNIT_TEST_PY_DIR="scripts" |
| 38 | UNIT_TEST_PY="unit-test.py" |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 39 | FORMAT_CODE_SH="format-code.sh" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 40 | DBUS_UNIT_TEST_PY="dbus-unit-test.py" |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 41 | TEST_ONLY="${TEST_ONLY:-}" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 42 | DBUS_SYS_CONFIG_FILE=${dbus_sys_config_file:-"/usr/share/dbus-1/system.conf"} |
Andrew Jeffery | a153ee3 | 2018-03-09 13:22:04 +1030 | [diff] [blame] | 43 | MAKEFLAGS="${MAKEFLAGS:-""}" |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 44 | DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}" |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 45 | NO_FORMAT_CODE="${NO_FORMAT_CODE:-}" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 46 | |
| 47 | # Timestamp for job |
| 48 | echo "Unit test build started, $(date)" |
| 49 | |
Andrew Jeffery | 316ebd3 | 2018-03-08 13:05:32 +1030 | [diff] [blame] | 50 | if [[ "${DISTRO}" == "fedora" ]]; then |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 51 | echo "Distro (${DISTRO}) not supported, running as ubuntu" |
| 52 | DISTRO="ubuntu:latest" |
| 53 | fi |
| 54 | |
| 55 | # Check workspace, build scripts, and package to be unit tested exists |
| 56 | if [ ! -d "${WORKSPACE}" ]; then |
| 57 | echo "Workspace(${WORKSPACE}) doesn't exist, exiting..." |
| 58 | exit 1 |
| 59 | fi |
| 60 | if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then |
| 61 | echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..." |
| 62 | exit 1 |
| 63 | fi |
| 64 | if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then |
| 65 | echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..." |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | # Copy unit test script into workspace |
| 70 | cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \ |
| 71 | ${WORKSPACE}/${UNIT_TEST_PY} |
| 72 | chmod a+x ${WORKSPACE}/${UNIT_TEST_PY} |
| 73 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 74 | # Copy dbus unit test script into workspace |
| 75 | cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \ |
| 76 | ${WORKSPACE}/${DBUS_UNIT_TEST_PY} |
| 77 | chmod a+x ${WORKSPACE}/${DBUS_UNIT_TEST_PY} |
| 78 | |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 79 | # Copy format code script into workspace |
| 80 | cp ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \ |
| 81 | ${WORKSPACE}/${FORMAT_CODE_SH} |
| 82 | chmod a+x ${WORKSPACE}/${FORMAT_CODE_SH} |
| 83 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 84 | # Configure docker build |
| 85 | cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS} |
| 86 | echo "Building docker image with build-unit-test-docker.sh" |
Andrew Geissler | a6b93bf | 2019-01-03 10:23:38 -0600 | [diff] [blame] | 87 | # Export input env variables |
| 88 | export DOCKER_IMG_NAME |
| 89 | export DISTRO |
| 90 | export BRANCH |
| 91 | ./build-unit-test-docker.sh |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 92 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 93 | # Unit test and parameters |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 94 | UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\ |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 95 | -p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 96 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 97 | # Run the docker unit test container with the unit test execution script |
| 98 | echo "Executing docker image" |
| 99 | docker run --cap-add=sys_admin --rm=true \ |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 100 | --network host \ |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 101 | --privileged=true \ |
William A. Kennington III | be6aab2 | 2018-12-06 15:01:54 -0800 | [diff] [blame] | 102 | -u "$USER" \ |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 103 | -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \ |
Andrew Jeffery | a153ee3 | 2018-03-09 13:22:04 +1030 | [diff] [blame] | 104 | -e "MAKEFLAGS=${MAKEFLAGS}" \ |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 105 | -t ${DOCKER_IMG_NAME} \ |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 106 | "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u ${UNIT_TEST} \ |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 107 | -f ${DBUS_SYS_CONFIG_FILE} |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 108 | |
| 109 | # Timestamp for build |
| 110 | echo "Unit test build completed, $(date)" |
Zane Shelley | 5ab3e86 | 2019-07-24 13:14:57 -0500 | [diff] [blame] | 111 | |
| 112 | # Clean up copied scripts. |
| 113 | rm ${WORKSPACE}/${UNIT_TEST_PY} |
| 114 | rm ${WORKSPACE}/${DBUS_UNIT_TEST_PY} |
| 115 | rm ${WORKSPACE}/${FORMAT_CODE_SH} |
| 116 | |