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