System test feature
[anna.git] / example / diameter / launcher / deployments / ft-client / tests / go.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 SCR_DIR=`dirname $0`
7 tmpdir=$(mktemp -d)
8 SCR_BN=`basename $0`
9 OPER_SCR=../operation.sh
10 RESULT_LOG=result.log
11 realm=`cat ../services.xml | grep originRealm | awk -F'originRealm=' '{ print $2 }' | cut -d\" -f2`
12 RECV_LOG=../${realm}.launcher.log.recvfe
13
14 #############
15 # FUNCTIONS #
16 #############
17
18 sigint_handler () {
19   _exit "Script interrupted. Cleanup and exit ..."
20 }
21
22 # $1: message; $2: optional rc (1 by default)
23 _exit () {
24   rc=1
25   [ -n "$2" ] && rc=$2
26   echo
27   echo -e $1
28   echo
29   rm -rf $tmpdir
30   exit $rc
31 }
32
33 # $1: hex formatted file to send to the server
34 operation_sendxml2e () {
35   sleep 0.5
36   $OPER_SCR "sendxml2e|$1"
37   return $?
38 }
39
40 # $1: hex formatted file to send to the server
41 operation_sendhex2e () {
42   sleep 0.5
43   $OPER_SCR "sendhex2e|$1"
44   return $?
45 }
46
47 # $1: file to monitor
48 # $2: timeout (request expiration) in seconds
49 # Result: cats the message when completed
50 wait4message () {
51   0>$1
52   rm -f .msg_received
53
54   [ -z "$2" ] && _exit "ERROR: must provide '$FUNCNAME' timeout (second argument)"
55   sleep $2 &
56   local timerPid=$!
57
58   # Monitor for incoming message:
59   tail -n0 -F --pid=$timerPid $1 | while read line 
60   do
61     if echo $line | grep "^</message>" >/dev/null; then
62       echo "Message received:"
63       echo
64       cat $1
65       # stop the timer
66       kill -13 $timerPid
67       touch .msg_received
68     fi
69   done
70
71   if [ ! -f .msg_received ]; then
72     echo "Timeout expired"
73     return 1
74   fi
75
76   return 0
77 }
78
79 # xml content is serialized (removing CR's) to ease the pattern matching:
80 # $1: pattern; $2: file
81 check_pattern () {
82   echo -n "Matching pattern '$1' in file '$2' ... " 
83   cat $2 | tr '\n' ' ' | egrep "$1" >/dev/null
84   [ $? -ne 0 ] && { echo "ERROR" ; test_failed ; return 1 ; }
85   echo "OK"
86   return 0
87 }
88
89 # $1: pattern file; $2: file
90 check_pattern_file () {
91   echo -n "Matching whole file '$1' in file '$2' ... " 
92   grep -f $1 $2 >/dev/null
93   [ $? -ne 0 ] && { echo "ERROR" ; test_failed ; return 1 ; }
94   echo "OK"
95   return 0
96 }
97
98 # Tests result:
99 test_ok () {
100   echo "Verdict: $TC Ok" | tee -a $RESULT_LOG
101 }
102
103 test_failed () {
104   echo "Verdict: $TC Failed" | tee -a $RESULT_LOG
105 }
106
107 process_tc () {
108   local tc_dn=`dirname $TC`
109
110   echo
111   echo "***** Executing '$TC' ..."
112
113   grep -v ^# $TC > $tmpdir/tc
114   while read -r line
115   do
116     oper=$(echo $line | awk '{ print $1 }')
117     param1="$(echo $line | cut -d' ' -f2-)"
118
119     case $oper in
120       SENDXML2E)
121         operation_sendxml2e $tc_dn/$param1 &
122         [ $? -ne 0 ] && return 1
123       ;;
124
125       SENDHEX2E)
126         operation_sendhex2e $tc_dn/$param1 &
127         [ $? -ne 0 ] && return 1
128       ;;
129
130       WAIT4MESSAGE)
131         wait4message $RECV_LOG 5
132         [ $? -ne 0 ] && return 1
133       ;;
134
135       CHECKPATTERN)
136         check_pattern "$param1" $RECV_LOG
137         [ $? -ne 0 ] && return 1
138       ;;
139
140       CHECKPATTERNFILE)
141         check_pattern_file "$param1" $RECV_LOG
142         [ $? -ne 0 ] && return 1
143       ;;
144
145     esac
146
147   done < $tmpdir/tc
148
149   return 0
150 }
151
152
153 #############
154 # EXECUTION #
155 #############
156
157 # Trap sigint:
158 trap sigint_handler SIGINT
159
160 # Working directory is script dirname:
161 cd $SCR_DIR
162
163 # Intro:
164 echo
165 echo "------------------"
166 echo "Test cases manager"
167 echo "------------------"
168 # Mandatory parameter:
169 [ -z "$1" ] && _exit "Usage: $SCR_BN <test cases parent directory (or .tc file)>"
170 [ ! -f "$1" -a ! -d "$1" ] && _exit "Invalid file or directory '$1' !!"
171
172 # Gather .tc files to be processed:
173 [ ! -f $1 ] && echo -e "\nGathering list of test cases from '`readlink -f $1`' ..."
174 echo
175 find $1 -name *.tc | xargs -L1 readlink -f | tee -a $tmpdir/tc_list
176 echo
177 echo
178
179 echo "Start processing test cases:"
180 echo "----------------------------"
181 # Process test cases:
182 0> $RESULT_LOG
183 for TC in `cat $tmpdir/tc_list`
184 do
185   process_tc
186   if [ $? -eq 0 ]; then
187     test_ok
188   else
189     test_failed
190     cat $RECV_LOG
191   fi
192 done
193
194 echo "=========================================="
195 echo "               TEST SUMMARY               "
196 echo "=========================================="
197 echo
198 cat $RESULT_LOG
199 echo
200
201 _exit "Done !" 0
202