1#!/bin/sh 2################################################################################ 3# 4# Copyright (C) 2022 Huawei Device Co., Ltd. 5# SPDX-License-Identifier: GPL-2.0 6# 7# Unless required by applicable law or agreed to in writing, software 8# distributed under the License is distributed on an "AS IS" BASIS, 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10# See the License for the specific language governing permissions and 11# limitations under the License. 12# 13################################################################################ 14# File: tst_oh.sh 15# 16# Description: OpenHarmony linuxkerneltest test library for shell 17# 18# Authors: Ma Feng - mafeng.ma@huawei.com 19# 20# History: Mar 15 2022 - init scripts 21# 22################################################################################ 23 24[ -n "$TST_LTB_LOADED" ] && return 0 25 26export TST_PASS=0 27export TST_FAIL=0 28export TST_BROK=0 29export TST_WARN=0 30export TST_CONF=0 31export TST_COUNT=0 32export TST_COLOR_ENABLED=1 33export TST_LTB_LOADED=1 34 35trap "tst_brk TBROK 'test interrupted'" INT 36 37tst_flag2color() 38{ 39 local ansi_color_blue='\033[1;34m' 40 local ansi_color_green='\033[1;32m' 41 local ansi_color_magenta='\033[1;35m' 42 local ansi_color_red='\033[1;31m' 43 local ansi_color_yellow='\033[1;33m' 44 45 case "$1" in 46 TPASS) printf $ansi_color_green;; 47 TFAIL) printf $ansi_color_red;; 48 TBROK) printf $ansi_color_red;; 49 TWARN) printf $ansi_color_magenta;; 50 TINFO) printf $ansi_color_blue;; 51 TCONF) printf $ansi_color_yellow;; 52 esac 53} 54 55tst_color_enabled() 56{ 57 [[ $TST_COLOR_ENABLED -eq 1 ]] && return 1 || return 0 58} 59 60tst_print_colored() 61{ 62 tst_color_enabled 63 local color=$? 64 65 66 [ "$color" = "1" ] && tst_flag2color "$1" 67 printf "$2" 68 [ "$color" = "1" ] && printf '\033[0m' 69} 70 71tst_exit() 72{ 73 local ret=0 74 75 if [ $TST_FAIL -gt 0 ]; then 76 ret=$((ret|1)) 77 fi 78 79 if [ $TST_BROK -gt 0 ]; then 80 ret=$((ret|2)) 81 fi 82 83 if [ $TST_WARN -gt 0 ]; then 84 ret=$((ret|4)) 85 fi 86 87 if [ $TST_CONF -gt 0 ]; then 88 ret=$((ret|32)) 89 fi 90 91 echo 92 echo "Summary:" 93 echo "passed $TST_PASS" 94 echo "failed $TST_FAIL" 95 echo "broken $TST_BROK" 96 echo "skipped $TST_CONF" 97 echo "warnings $TST_WARN" 98 99 exit $ret 100} 101 102_tst_inc_ret() 103{ 104 case "$1" in 105 TPASS) TST_PASS=$((TST_PASS+1));; 106 TFAIL) TST_FAIL=$((TST_FAIL+1));; 107 TBROK) TST_BROK=$((TST_BROK+1));; 108 TWARN) TST_WARN=$((TST_WARN+1));; 109 TCONF) TST_CONF=$((TST_CONF+1));; 110 TINFO) ;; 111 *) tst_brk TBROK "Invalid res type '$1'";; 112 esac 113} 114 115tst_res() 116{ 117 local res=$1 118 shift 119 120 tst_color_enabled 121 local color=$? 122 123 TST_COUNT=$(($TST_COUNT+1)) 124 125 _tst_inc_ret "$res" 126 printf "$TST_ID $TST_COUNT $(date) " 127 tst_print_colored $res "$res: " 128 echo "$@" 129} 130 131tst_brk() 132{ 133 local res=$1 134 shift 135 136 if [ "$TST_DO_EXIT" = 1 ]; then 137 tst_res TWARN "$@" 138 return 139 fi 140 141 tst_res "$res" "$@" 142 tst_exit 143} 144 145tst_judged() 146{ 147 actual_res=$1 148 shift 149 expect_res=$1 150 shift 151 152 comment="$@" 153 if [ "$actual_res" == "$expect_res" ]; then 154 tst_res TPASS "$comment test pass, expect $expect_res return $actual_res" 155 else 156 tst_res TFAIL "$comment test fail, expect $expect_res return $actual_res" 157 fi 158} 159 160tst_judged_fail() 161{ 162 actual_res1=$1 163 shift 164 expect_res1=$1 165 shift 166 comment_fail="$@" 167 if [ "$actual_res1" != "$expect_res1" ]; then 168 tst_res TPASS "$comment_fail test pass, expect $expect_res1 return $actual_res1" 169 else 170 tst_res TFAIL "$comment_fail test fail, expect $expect_res1 return $actual_res1" 171 fi 172} 173 174get_product() 175{ 176 echo $(uname -a | awk '{printf $NF}') 177} 178 179if [ -z "$TST_TD" ]; then 180 _tst_filename=$(basename $0) || \ 181 tst_brk TCONF "Failed to set TST_TD from \$0 ('$0'), fix it with setting TST_ID before sourcing tst_test.sh" 182 TST_ID=${_tst_filename%%.*} 183fi 184export TST_ID="$TST_ID" 185