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_VERBOSE=${OTBR_VERBOSE:-0} 37 38OTBR_COLOR_PASS='\033[0;32m' 39readonly OTBR_COLOR_PASS 40 41OTBR_COLOR_FAIL='\033[0;31m' 42readonly OTBR_COLOR_FAIL 43 44OTBR_COLOR_NONE='\033[0m' 45readonly OTBR_COLOR_NONE 46 47QUIET=${QUIET:-1} 48readonly QUIET 49 50OTBR_BUILD_TYPE="${OTBR_BUILD_TYPE:-Debug}" 51readonly OTBR_BUILD_TYPE 52 53OTBR_COVERAGE="${OTBR_COVERAGE:-0}" 54readonly OTBR_COVERAGE 55 56OTBR_MDNS="${OTBR_MDNS-}" 57readonly OTBR_MDNS 58 59OTBR_REST="${OTBR_REST-}" 60readonly OTBR_REST 61 62OTBR_OPTIONS="${OTBR_OPTIONS-}" 63readonly OTBR_OPTIONS 64 65OTBR_TOP_BUILDDIR="${BUILD_DIR}/otbr" 66readonly OTBR_TOP_BUILDDIR 67 68####################################### 69# Run test and print result. 70# Globals: 71# OTBR_COLOR_PASS 72# OTBR_COLOR_FAIL 73# OTBR_COLOR_NONE 74# Arguments: 75# $@ - Test command 76# Returns: 77# 0 - Test passed. 78# Otherwise - Test failed! 79####################################### 80print_result() 81{ 82 local exit_code=0 83 84 echo -e "$(date) Running $*" 85 86 if [[ ${OTBR_VERBOSE} == 0 ]]; then 87 "$@" &>test.log || exit_code=$? 88 else 89 "$@" || exit_code=$? 90 fi 91 92 if [[ $exit_code == 0 ]]; then 93 prefix="${OTBR_COLOR_PASS}PASS${OTBR_COLOR_NONE}" 94 else 95 prefix="${OTBR_COLOR_FAIL}FAIL${OTBR_COLOR_NONE}" 96 fi 97 98 echo -e "${prefix} $*" 99 100 # only output log on failure 101 if [[ ${OTBR_VERBOSE} == 0 && ${exit_code} != 0 ]]; then 102 cat test.log 103 fi 104 105 return ${exit_code} 106} 107 108print_usage() 109{ 110 cat <<EOF 111USAGE: $0 COMMAND 112 113COMMAND: 114 build Build project for running tests. This can be used to rebuild the project for changes. 115 clean Clean built files to prepare new build. 116 meshcop Run MeshCoP tests. 117 openwrt Run OpenWRT tests. 118 help Print this help. 119 120EXAMPLES: 121 $0 clean build 122EOF 123 exit "$1" 124} 125 126do_build() 127{ 128 otbr_options=( 129 "-DCMAKE_BUILD_TYPE=${OTBR_BUILD_TYPE}" 130 "-DCMAKE_INSTALL_PREFIX=/usr" 131 "-DOT_THREAD_VERSION=1.3" 132 "-DOTBR_DBUS=ON" 133 "-DOTBR_FEATURE_FLAGS=ON" 134 "-DOTBR_TELEMETRY_DATA_API=ON" 135 "-DOTBR_WEB=ON" 136 "-DOTBR_UNSECURE_JOIN=ON" 137 "-DOTBR_TREL=ON" 138 "-DOTBR_LINK_METRICS_TELEMETRY=ON" 139 ${otbr_options[@]+"${otbr_options[@]}"} 140 ) 141 142 ./script/cmake-build "${otbr_options[@]}" 143} 144 145do_clean() 146{ 147 echo "Removing ${BUILD_DIR} (requiring sudo)" 148 rm -rf "${BUILD_DIR}" || sudo rm -rf "${BUILD_DIR}" 149} 150 151do_check() 152{ 153 (cd "${OTBR_TOP_BUILDDIR}" \ 154 && ninja && sudo ninja install \ 155 && CTEST_OUTPUT_ON_FAILURE=1 ninja test) 156} 157 158do_doxygen() 159{ 160 otbr_options=( 161 "-DOTBR_DOC=ON" 162 ) 163 164 OTBR_TARGET="doxygen" ./script/cmake-build "${otbr_options[@]}" 165} 166 167do_prepare() 168{ 169 if [[ ${OTBR_OPTIONS} ]]; then 170 read -r -a otbr_options <<<"${OTBR_OPTIONS}" 171 else 172 otbr_options=() 173 fi 174 175 if [[ -n ${OTBR_MDNS} ]]; then 176 otbr_options+=("-DOTBR_MDNS=${OTBR_MDNS}") 177 fi 178 179 if [[ ${OTBR_COVERAGE} == 1 ]]; then 180 otbr_options+=("-DOTBR_COVERAGE=ON") 181 fi 182 183 if [[ ${OTBR_REST} == "rest-off" ]]; then 184 otbr_options+=("-DOTBR_REST=OFF") 185 else 186 otbr_options+=("-DOTBR_REST=ON") 187 fi 188} 189 190do_package() 191{ 192 otbr_options=( 193 "-DBUILD_TESTING=OFF" 194 "-DCMAKE_INSTALL_PREFIX=/usr" 195 "-DCMAKE_BUILD_TYPE=Release" 196 "-DOTBR_WEB=ON" 197 ${otbr_options[@]+"${otbr_options[@]}"} 198 ) 199 200 OTBR_TARGET="package" ./script/cmake-build "${otbr_options[@]}" 201} 202 203do_simulation() 204{ 205 export top_builddir="${OTBR_TOP_BUILDDIR}" 206 207 print_result tests/scripts/auto-attach 208 print_result tests/scripts/infra-link-selector 209} 210 211main() 212{ 213 if [[ $# == 0 ]]; then 214 print_usage 1 215 fi 216 217 do_prepare 218 219 while [[ $# != 0 ]]; do 220 case "$1" in 221 build) 222 do_build 223 ;; 224 check) 225 do_check 226 ;; 227 clean) 228 do_clean 229 ;; 230 doxygen) 231 do_doxygen 232 ;; 233 help) 234 print_usage 235 ;; 236 meshcop) 237 top_builddir="${OTBR_TOP_BUILDDIR}" print_result ./tests/scripts/meshcop 238 ;; 239 openwrt) 240 print_result ./tests/scripts/openwrt 241 ;; 242 simulation) 243 do_simulation 244 ;; 245 package) 246 do_package 247 ;; 248 *) 249 echo "Unknown test: ${1}" 250 print_usage 1 251 ;; 252 esac 253 shift 254 done 255} 256 257main "$@" 258