1#!/bin/bash 2# 3# Copyright (c) 2017, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29# Description: 30# This script runs border router tests. 31# 32 33# shellcheck source=script/_initrc 34. "$(dirname "$0")"/_initrc 35 36readonly OTBR_COLOR_PASS='\033[0;32m' 37readonly OTBR_COLOR_FAIL='\033[0;31m' 38readonly OTBR_COLOR_NONE='\033[0m' 39readonly QUIET=${QUIET:-1} 40 41readonly OTBR_BUILD_TYPE="${OTBR_BUILD_TYPE:-Debug}" 42readonly OTBR_COVERAGE="${OTBR_COVERAGE:-0}" 43readonly OTBR_MDNS="${OTBR_MDNS:-}" 44readonly OTBR_REST="${OTBR_REST:-}" 45readonly OTBR_OPTIONS="${OTBR_OPTIONS:-}" 46readonly OTBR_TOP_BUILDDIR="${BUILD_DIR}/otbr" 47 48####################################### 49# Run test and print result. 50# Globals: 51# OTBR_COLOR_PASS 52# OTBR_COLOR_FAIL 53# OTBR_COLOR_NONE 54# Arguments: 55# $@ - Test command 56# Returns: 57# 0 - Test passed. 58# Otherwise - Test failed! 59####################################### 60print_result() 61{ 62 local exit_code=0 63 64 echo -e "$(date) Running $*" 65 66 if [[ ${QUIET} == 1 ]]; then 67 "$@" &>test.log || exit_code=$? 68 else 69 "$@" || exit_code=$? 70 fi 71 72 if [[ $exit_code == 0 ]]; then 73 prefix="${OTBR_COLOR_PASS}PASS${OTBR_COLOR_NONE}" 74 else 75 prefix="${OTBR_COLOR_FAIL}FAIL${OTBR_COLOR_NONE}" 76 fi 77 78 echo -e "${prefix} $*" 79 80 # only output log on failure 81 if [[ ${QUIET} == 1 && ${exit_code} != 0 ]]; then 82 cat test.log 83 fi 84 85 return ${exit_code} 86} 87 88print_usage() 89{ 90 cat <<EOF 91USAGE: $0 COMMAND 92 93COMMAND: 94 build Build project for running tests. This can be used to rebuild the project for changes. 95 clean Clean built files to prepare new build. 96 meshcop Run MeshCoP tests. 97 openwrt Run OpenWRT tests. 98 help Print this help. 99 100EXAMPLES: 101 $0 clean build 102EOF 103 exit "$1" 104} 105 106do_build() 107{ 108 otbr_options=( 109 "-DCMAKE_BUILD_TYPE=${OTBR_BUILD_TYPE}" 110 "-DCMAKE_INSTALL_PREFIX=/usr" 111 "-DOT_THREAD_VERSION=1.3" 112 "-DOTBR_DBUS=ON" 113 "-DOTBR_WEB=ON" 114 "-DOTBR_UNSECURE_JOIN=ON" 115 "-DOTBR_TREL=ON" 116 ${otbr_options[@]+"${otbr_options[@]}"} 117 ) 118 119 ./script/cmake-build "${otbr_options[@]}" 120} 121 122do_clean() 123{ 124 echo "Removing ${BUILD_DIR} (requiring sudo)" 125 rm -rf "${BUILD_DIR}" || sudo rm -rf "${BUILD_DIR}" 126} 127 128do_check() 129{ 130 (cd "${OTBR_TOP_BUILDDIR}" \ 131 && sudo ninja install \ 132 && CTEST_OUTPUT_ON_FAILURE=1 ninja test) 133} 134 135do_doxygen() 136{ 137 otbr_options=( 138 "-DOTBR_DOC=ON" 139 ) 140 141 OTBR_TARGET="doxygen" ./script/cmake-build "${otbr_options[@]}" 142} 143 144do_prepare() 145{ 146 if [[ ${OTBR_OPTIONS} ]]; then 147 read -r -a otbr_options <<<"${OTBR_OPTIONS}" 148 else 149 otbr_options=() 150 fi 151 152 if [[ -n ${OTBR_MDNS} ]]; then 153 otbr_options+=("-DOTBR_MDNS=${OTBR_MDNS}") 154 fi 155 156 if [[ ${OTBR_COVERAGE} == 1 ]]; then 157 otbr_options+=("-DOTBR_COVERAGE=ON") 158 fi 159 160 if [[ ${OTBR_REST} == "rest-off" ]]; then 161 otbr_options+=("-DOTBR_REST=OFF") 162 else 163 otbr_options+=("-DOTBR_REST=ON") 164 fi 165} 166 167do_package() 168{ 169 otbr_options=( 170 "-DBUILD_TESTING=OFF" 171 "-DCMAKE_INSTALL_PREFIX=/usr" 172 "-DCMAKE_BUILD_TYPE=Release" 173 "-DOTBR_WEB=ON" 174 ${otbr_options[@]+"${otbr_options[@]}"} 175 ) 176 177 OTBR_TARGET="package" ./script/cmake-build "${otbr_options[@]}" 178} 179 180do_simulation() 181{ 182 export top_builddir="${OTBR_TOP_BUILDDIR}" 183 184 print_result tests/scripts/auto-attach 185 print_result tests/scripts/infra-link-selector 186} 187 188main() 189{ 190 if [[ $# == 0 ]]; then 191 print_usage 1 192 fi 193 194 do_prepare 195 196 while [[ $# != 0 ]]; do 197 case "$1" in 198 build) 199 do_build 200 ;; 201 check) 202 do_check 203 ;; 204 clean) 205 do_clean 206 ;; 207 doxygen) 208 do_doxygen 209 ;; 210 help) 211 print_usage 212 ;; 213 meshcop) 214 top_builddir="${OTBR_TOP_BUILDDIR}" print_result ./tests/scripts/meshcop 215 ;; 216 openwrt) 217 print_result ./tests/scripts/openwrt 218 ;; 219 simulation) 220 do_simulation 221 ;; 222 package) 223 do_package 224 ;; 225 *) 226 echo "Unknown test: ${1}" 227 print_usage 1 228 ;; 229 esac 230 shift 231 done 232} 233 234main "$@" 235