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` |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 24 | # NO_FORMAT_CODE: Optional, do not run format-code.sh |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 25 | # EXTRA_UNIT_TEST_ARGS: Optional, pass arguments to unit-test.py |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 26 | |
| 27 | # Trace bash processing. Set -e so when a step fails, we fail the build |
| 28 | set -uo pipefail |
| 29 | |
| 30 | # Default variables |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 31 | BRANCH=${BRANCH:-"master"} |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 32 | OBMC_BUILD_SCRIPTS="openbmc-build-scripts" |
| 33 | UNIT_TEST_PY_DIR="scripts" |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 34 | CONFIG_DIR="config" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 35 | UNIT_TEST_PY="unit-test.py" |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 36 | FORMAT_CODE_SH="format-code.sh" |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 37 | SPELLINGS_TXT="openbmc-spelling.txt" |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 38 | ESLINT_CONFIG="eslint-global-config.json" |
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:-""}" |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 43 | DOCKER_WORKDIR="${DOCKER_WORKDIR:-$WORKSPACE}" |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 44 | NO_FORMAT_CODE="${NO_FORMAT_CODE:-}" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 45 | |
| 46 | # Timestamp for job |
| 47 | echo "Unit test build started, $(date)" |
| 48 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 49 | # Check workspace, build scripts, and package to be unit tested exists |
| 50 | if [ ! -d "${WORKSPACE}" ]; then |
| 51 | echo "Workspace(${WORKSPACE}) doesn't exist, exiting..." |
| 52 | exit 1 |
| 53 | fi |
| 54 | if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then |
| 55 | echo "Package(${OBMC_BUILD_SCRIPTS}) not found in ${WORKSPACE}, exiting..." |
| 56 | exit 1 |
| 57 | fi |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 58 | # shellcheck disable=SC2153 # UNIT_TEST_PKG is not misspelled. |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 59 | if [ ! -d "${WORKSPACE}/${UNIT_TEST_PKG}" ]; then |
| 60 | echo "Package(${UNIT_TEST_PKG}) not found in ${WORKSPACE}, exiting..." |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | # Copy unit test script into workspace |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 65 | cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${UNIT_TEST_PY} \ |
| 66 | "${WORKSPACE}"/${UNIT_TEST_PY} |
| 67 | chmod a+x "${WORKSPACE}"/${UNIT_TEST_PY} |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 68 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 69 | # Copy dbus unit test script into workspace |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 70 | cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${DBUS_UNIT_TEST_PY} \ |
| 71 | "${WORKSPACE}"/${DBUS_UNIT_TEST_PY} |
| 72 | chmod a+x "${WORKSPACE}"/${DBUS_UNIT_TEST_PY} |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 73 | |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 74 | # Copy format code script into workspace |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 75 | cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${UNIT_TEST_PY_DIR}/${FORMAT_CODE_SH} \ |
| 76 | "${WORKSPACE}"/${FORMAT_CODE_SH} |
| 77 | chmod a+x "${WORKSPACE}"/${FORMAT_CODE_SH} |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 78 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 79 | # Copy spellings.txt file into workspace |
Patrick Williams | df6170e | 2021-12-21 14:19:50 -0600 | [diff] [blame] | 80 | cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${SPELLINGS_TXT} \ |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 81 | "${WORKSPACE}"/${SPELLINGS_TXT} |
| 82 | |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 83 | # Copy the eslintconfig file into workspce |
| 84 | cp "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}/${CONFIG_DIR}/${ESLINT_CONFIG} \ |
| 85 | "${WORKSPACE}"/${ESLINT_CONFIG} |
| 86 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 87 | # Configure docker build |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 88 | cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS} |
Patrick Williams | 02871c9 | 2021-02-01 20:57:19 -0600 | [diff] [blame] | 89 | echo "Building docker image with build-unit-test-docker" |
Andrew Geissler | a6b93bf | 2019-01-03 10:23:38 -0600 | [diff] [blame] | 90 | # Export input env variables |
Andrew Geissler | a6b93bf | 2019-01-03 10:23:38 -0600 | [diff] [blame] | 91 | export BRANCH |
Patrick Williams | 9b42310 | 2021-03-16 00:07:31 -0500 | [diff] [blame] | 92 | DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker) |
| 93 | export DOCKER_IMG_NAME |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 94 | |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 95 | # Allow the user to pass options through to unit-test.py: |
| 96 | # EXTRA_UNIT_TEST_ARGS="-r 100" ... |
| 97 | EXTRA_UNIT_TEST_ARGS="${EXTRA_UNIT_TEST_ARGS:+,${EXTRA_UNIT_TEST_ARGS/ /,}}" |
| 98 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 99 | # Unit test and parameters |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 100 | UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\ |
Brad Bishop | 5d6688c | 2020-08-26 15:44:02 -0400 | [diff] [blame] | 101 | -p,${UNIT_TEST_PKG},-b,$BRANCH,-v${TEST_ONLY:+,-t}${NO_FORMAT_CODE:+,-n}\ |
| 102 | ${EXTRA_UNIT_TEST_ARGS}" |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 103 | |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 104 | # Run the docker unit test container with the unit test execution script |
| 105 | echo "Executing docker image" |
| 106 | docker run --cap-add=sys_admin --rm=true \ |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 107 | --privileged=true \ |
William A. Kennington III | be6aab2 | 2018-12-06 15:01:54 -0800 | [diff] [blame] | 108 | -u "$USER" \ |
William A. Kennington III | d70980f | 2018-11-08 15:14:27 -0800 | [diff] [blame] | 109 | -w "${DOCKER_WORKDIR}" -v "${WORKSPACE}":"${DOCKER_WORKDIR}" \ |
Andrew Jeffery | a153ee3 | 2018-03-09 13:22:04 +1030 | [diff] [blame] | 110 | -e "MAKEFLAGS=${MAKEFLAGS}" \ |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 111 | -t "${DOCKER_IMG_NAME}" \ |
| 112 | "${DOCKER_WORKDIR}"/${DBUS_UNIT_TEST_PY} -u "${UNIT_TEST}" \ |
| 113 | -f "${DBUS_SYS_CONFIG_FILE}" |
Matthew Barth | c5dec75 | 2016-11-18 13:14:37 -0600 | [diff] [blame] | 114 | |
| 115 | # Timestamp for build |
| 116 | echo "Unit test build completed, $(date)" |
Zane Shelley | 5ab3e86 | 2019-07-24 13:14:57 -0500 | [diff] [blame] | 117 | |
| 118 | # Clean up copied scripts. |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 119 | rm "${WORKSPACE}"/${UNIT_TEST_PY} |
| 120 | rm "${WORKSPACE}"/${DBUS_UNIT_TEST_PY} |
| 121 | rm "${WORKSPACE}"/${FORMAT_CODE_SH} |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 122 | rm "${WORKSPACE}"/${ESLINT_CONFIG} |
Zane Shelley | 5ab3e86 | 2019-07-24 13:14:57 -0500 | [diff] [blame] | 123 | |