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