Use log_action function in do-tests script
[strongswan.git] / testing / scripts / function.sh
1 #!/bin/bash
2 # provides some general-purpose script functions
3 #
4 # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
5 # Zuercher Hochschule Winterthur
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16
17 export TERM=xterm
18 RED=$(tput setaf 1)
19 GREEN=$(tput setaf 2)
20 NORMAL=$(tput op)
21
22 # exit with given error message
23 # $1 - error message
24 die() {
25 echo -e "${RED}$1${NORMAL}"
26 exit 1
27 }
28
29 [ -f testing.conf ] || die "Configuration file 'testing.conf' not found"
30 . testing.conf
31
32 # execute command
33 # $1 - command to execute
34 # $2 - whether or not to log command exit status
35 # (0 -> disable exit status logging)
36 execute()
37 {
38 cmd=${1}
39 echo $cmd >>$LOGFILE 2>&1
40 $cmd >>$LOGFILE 2>&1
41 status=$?
42 [ "$2" != 0 ] && log_status $status
43 if [ $status != 0 ]; then
44 echo
45 echo "! Command $cmd failed, exiting (status $status)"
46 echo "! Check why here $LOGFILE"
47 exit 1
48 fi
49 }
50
51 # execute command in chroot
52 # $1 - command to execute
53 execute_chroot()
54 {
55 execute "chroot $LOOPDIR $@"
56 }
57
58 # write green status message to console
59 # $1 - msg
60 echo_ok()
61 {
62 echo -e "${GREEN}$1${NORMAL}"
63 }
64
65 # write red status message to console
66 # $1 - msg
67 echo_failed()
68 {
69 echo -e "${RED}$1${NORMAL}"
70 }
71
72 function cecho {
73 echo -e "\033[1;31m$1\033[0m"
74 }
75 function cgecho {
76 echo -e "\033[1;32m$1\033[0m"
77 }
78
79 function cecho-n {
80 echo -en "\033[1;31m$1\033[0m"
81 }
82
83 # log an action
84 # $1 - current action description
85 log_action()
86 {
87 /bin/echo -n "[....] $1 "
88 }
89
90 # log an action status
91 # $1 - exit status of action
92 log_status()
93 {
94 tput hpa 0
95 if [ $1 -eq 0 ]; then
96 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
97 else
98 /bin/echo -ne "[${RED}FAIL${NORMAL}"
99 fi
100 echo
101 }
102
103 # the following two functions are stolen from [1]
104 # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
105
106 declare -a on_exit_items
107
108 # perform registered actions on exit
109 on_exit()
110 {
111 for i in "${on_exit_items[@]}"
112 do
113 eval $i >>$LOGFILE 2>&1
114 done
115 on_exit_items=""
116 trap - EXIT
117 }
118
119 # register a command to execute when the calling script terminates. The
120 # registered commands are called in FIFO order.
121 # $* - command to register
122 do_on_exit()
123 {
124 local n=${#on_exit_items[*]}
125 on_exit_items[$n]="$*"
126 if [ $n -eq 0 ]; then
127 trap on_exit EXIT
128 fi
129 }
130
131 #############################################
132 # search and replace strings throughout a
133 # whole directory
134 #
135
136 function searchandreplace {
137
138 SEARCHSTRING="$1"
139 REPLACESTRING="$2"
140 DESTDIR="$3"
141
142 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
143
144
145 ###########################################
146 # search and replace in each found file the
147 # given string
148 #
149
150 for eachfoundfile in `find $DESTDIR -type f`
151 do
152 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
153 done
154
155 }
156
157 #############################################
158 # add a bridge
159 #
160
161 function umlbr_add {
162 brctl addbr "umlbr$1"
163 brctl setfd "umlbr$1" 0
164 brctl setageing "umlbr$1" 3600
165 brctl stp "umlbr$1" off
166 ifconfig "umlbr$1" "$2" netmask "$3" up
167 }
168
169 #############################################
170 # delete a bridge
171 #
172
173 function umlbr_del {
174 ifconfig "umlbr$1" down &> /dev/null 2>&1
175 brctl delbr "umlbr$1" &> /dev/null 2>&1
176 }
177
178 #############################################
179 # add a tap interface to a bridge
180 #
181
182 function umlbr_add_tap {
183 tunctl -t "tap$1_$2" &> /dev/null 2>&1
184 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
185 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
186 cecho-n "$2.."
187 }
188
189 #############################################
190 # delete a tap interface from a bridge
191 #
192
193 function umlbr_del_tap {
194 ifconfig "umlbr$2" down &> /dev/null 2>&1
195 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
196 tunctl -d "tap$1_$2" &> /dev/null 2>&1
197 cecho-n "$2.."
198 }
199