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 |
Andrew Geissler | 9f980d7 | 2019-01-03 14:19:43 -0600 | [diff] [blame] | 19 | # BRANCH: Optional, branch to build from each of the |
| 20 | # openbmc repositories. default is master, which will be |
| 21 | # used if input branch not provided or not found |
Andrew Geissler | 9f980d7 | 2019-01-03 14:19:43 -0600 | [diff] [blame] | 22 | # dbus_sys_config_file: Optional, with the default being |
| 23 | # `/usr/share/dbus-1/system.conf` |
Nan Zhou | 971a697 | 2022-06-14 18:27:53 +0000 | [diff] [blame] | 24 | # TEST_ONLY: Optional, do not run analysis tools |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 25 | # NO_FORMAT_CODE: Optional, do not run format-code.sh |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 26 | # EXTRA_UNIT_TEST_ARGS: Optional, pass arguments to unit-test.py |
Benjamin Fair | ae58fe7 | 2022-05-17 15:22:24 -0700 | [diff] [blame] | 27 | # INTERACTIVE: Optional, run a bash shell instead of unit-test.py |
Lei YU | b8c7c16 | 2022-07-19 19:40:06 +0800 | [diff] [blame] | 28 | # http_proxy: Optional, run the container with proxy environment |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 29 | |
| 30 | # Trace bash processing. Set -e so when a step fails, we fail the build |
| 31 | set -uo pipefail |
| 32 | |
| 33 | # Default variables |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 34 | BRANCH=${BRANCH:-"master"} |
Patrick Williams | 8e43efe | 2022-11-28 12:35:05 -0600 | [diff] [blame] | 35 | DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 36 | OBMC_BUILD_SCRIPTS="openbmc-build-scripts" |
Patrick Williams | 8e43efe | 2022-11-28 12:35:05 -0600 | [diff] [blame] | 37 | UNIT_TEST_SCRIPT_DIR="${DOCKER_WORKDIR}/${OBMC_BUILD_SCRIPTS}/scripts" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 38 | UNIT_TEST_PY="unit-test.py" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 39 | DBUS_UNIT_TEST_PY="dbus-unit-test.py" |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 40 | TEST_ONLY="${TEST_ONLY:-}" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 41 | 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] | 42 | MAKEFLAGS="${MAKEFLAGS:-""}" |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 43 | NO_FORMAT_CODE="${NO_FORMAT_CODE:-}" |
Benjamin Fair | ae58fe7 | 2022-05-17 15:22:24 -0700 | [diff] [blame] | 44 | INTERACTIVE="${INTERACTIVE:-}" |
Lei YU | b8c7c16 | 2022-07-19 19:40:06 +0800 | [diff] [blame] | 45 | http_proxy=${http_proxy:-} |
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 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 50 | # Check workspace, build scripts, and package to be unit tested exists |
| 51 | if [ ! -d "${WORKSPACE}" ]; then |
| 52 | echo "Workspace(${WORKSPACE}) doesn't exist, exiting..." |
| 53 | exit 1 |
| 54 | fi |
| 55 | if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then |
| 56 | echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..." |
| 57 | exit 1 |
| 58 | fi |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 59 | # shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled. |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 60 | if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then |
| 61 | echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..." |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 65 | # Configure docker build |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 66 | cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS} |
Patrick Williams | 02871c9 | 2021-02-01 20:57:19 -0600 | [diff] [blame] | 67 | echo "Building docker image with build-unit-test-docker" |
Andrew Geissler | a6b93bf | 2019-01-03 10:23:38 -0600 | [diff] [blame] | 68 | # Export input env variables |
Andrew Geissler | a6b93bf | 2019-01-03 10:23:38 -0600 | [diff] [blame] | 69 | export BRANCH |
Patrick Williams | 9b42310 | 2021-03-16 00:07:31 -0500 | [diff] [blame] | 70 | DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker) |
| 71 | export DOCKER_IMG_NAME |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 72 | |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 73 | # Allow the user to pass options through to unit-test.py: |
| 74 | # EXTRA_UNIT_TEST_ARGS="-r 100" ... |
| 75 | EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}" |
| 76 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 77 | # Unit test and parameters |
Benjamin Fair | ae58fe7 | 2022-05-17 15:22:24 -0700 | [diff] [blame] | 78 | if [ "${INTERACTIVE}" ]; then |
| 79 | UNIT_TEST="/bin/bash" |
| 80 | else |
Patrick Williams | 8e43efe | 2022-11-28 12:35:05 -0600 | [diff] [blame] | 81 | UNIT_TEST="${UNIT_TEST_SCRIPT_DIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\ |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 82 | -p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\ |
| 83 | ${EXTRA_UNIT_TEST_ARGS}" |
Benjamin Fair | ae58fe7 | 2022-05-17 15:22:24 -0700 | [diff] [blame] | 84 | fi |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 85 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 86 | # Run the docker unit test container with the unit test execution script |
| 87 | echo "Executing docker image" |
Lei YU | b8c7c16 | 2022-07-19 19:40:06 +0800 | [diff] [blame] | 88 | |
| 89 | PROXY_ENV="" |
| 90 | # Set up proxies |
| 91 | if [ -n "${http_proxy}" ]; then |
| 92 | PROXY_ENV=" \ |
| 93 | --env HTTP_PROXY=${http_proxy} \ |
| 94 | --env HTTPS_PROXY=${http_proxy} \ |
| 95 | --env FTP_PROXY=${http_proxy} \ |
| 96 | --env http_proxy=${http_proxy} \ |
| 97 | --env https_proxy=${http_proxy} \ |
| 98 | --env ftp_proxy=${http_proxy}" |
| 99 | fi |
| 100 | |
| 101 | # shellcheck disable=SC2086 # ${PROXY_ENV} is meant to be splitted |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 102 | docker run --cap-add=sys_admin --rm=true \ |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 103 | --privileged=true \ |
Lei YU | b8c7c16 | 2022-07-19 19:40:06 +0800 | [diff] [blame] | 104 | ${PROXY_ENV} \ |
William A. Kennington III | be6aab2 | 2018-12-06 15:01:54 -0800 | [diff] [blame] | 105 | -u "$USER" \ |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 106 | -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \ |
Andrew Jeffery | a153ee3 | 2018-03-09 13:22:04 +1030 | [diff] [blame] | 107 | -e "MAKEFLAGS=${MAKEFLAGS}" \ |
Benjamin Fair | ae58fe7 | 2022-05-17 15:22:24 -0700 | [diff] [blame] | 108 | -${INTERACTIVE:+i}t "${DOCKER_IMG_NAME}" \ |
Patrick Williams | 8e43efe | 2022-11-28 12:35:05 -0600 | [diff] [blame] | 109 | "${UNIT_TEST_SCRIPT_DIR}/${DBUS_UNIT_TEST_PY}" -u "${UNIT_TEST}" \ |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 110 | -f "${DBUS_SYS_CONFIG_FILE}" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 111 | |
| 112 | # Timestamp for build |
| 113 | echo "Unit test build completed, $(date)" |