System test refinement
[anna.git] / example / diameter / launcher / resources / scripts / clone.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 CLONE_WKDIR=$(mktemp -d)
7 MAX_NUMBER_GROUPS=400
8 # (depends on the machine)
9
10 #############
11 # FUNCTIONS #
12 #############
13 _exit (){
14   echo
15   echo $1
16   echo
17
18   # Cleanup:
19   rm -rf $CLONE_WKDIR
20
21   rc=1
22   [ -n "$2" ] && rc=$2
23   exit $rc
24 }
25
26 sig_handler() {
27   _exit "Script interrupted. Cleanup & exit ..."
28 }
29
30 usage() {
31   echo "Usage: $0 <ADML directory> <testcase variable file> <initial sequence> <final sequence> [number of groups] [population]"
32   echo
33   echo "          ADML directory:         path to the ADML directory where 'operation.sh' script is used to load the test cases."
34   echo "          testcase variable file: path to the testcase file with parseable variables, for example '<directory>/testcase.txt.msk'."
35   echo "                                  xml files within this test case, must exist with aditional .msk extension in the same directory."
36   echo "                                  Currently, 9 variables are supported to be replaced:"
37   echo "                                    __TESTID__ : sequence number. For example if you provide 1 to 20 for this script, it will be 1 .. 20"
38   echo "                                    __SEQ8__   : 8-digit number corresponding to the sequence number (00000001 for 1, and so on)"
39   echo "                                    __MSISDN9__: 9-digit msisdn"
40   echo "                                    __IPV4HEX__: hexadecimal IPv4 address, for example: 00000001"
41   echo "                                    __IPV4__   : IPv4 address, for example: 0.0.0.1"
42   echo "                                    __SGX1_SUFFIX__: 1<8-digit sequence>"
43   echo "                                    __SRX1_SUFFIX__: \""
44   echo "                                    __SGX2_SUFFIX__: 2<8-digit sequence>"
45   echo "                                    __SRX2_SUFFIX__: \""
46   echo "                                  Edit the variables section to add more special values if you need them."
47   echo
48   echo "          initial sequence:       initial sequence number to parse over variables."
49   echo "          final sequence:         final sequence number to parse over variables."
50   echo "          number of groups:       number of background group jobs to clone the provided sequence range."
51   echo "                                  By default (or if you provide \"\"), it will be the number of items divided by 25, with a maximum"
52   echo "                                  of $MAX_NUMBER_GROUPS."
53   echo "          population:             Optionally, a population file (basename) can be provided. It must exist on the same directory"
54   echo "                                  than other stuff (testcase, xml files). The behaviour will be the accumulation of every parsing"
55   echo "                                  operation during clone procedure regarding the templated content of the population file. The"
56   echo "                                  accumulated content will be dump over a new file created together with cloned <population> and"
57   echo "                                  named with the extension '.all' (<population>.all). This population file could be useful to"
58   echo "                                  specify database commands related to each sequence (each single testcase), in order to have"
59   echo "                                  the whole population file. This won't include common database elements for the tested scenary,"
60   echo "                                  which shall be provisioned in a separated procedure."
61   echo 
62   _exit
63 }
64
65 children () {
66   bash_pid=$$
67   children=`ps -eo ppid | grep -w $bash_pid`
68   echo $children | wc -w
69 }
70
71 # $1: sequence number; $2: file to parse(will be updated); $3: result file
72 parse_file() {
73   local vars=$(egrep -o '__((_*[A-Z]*[0-9]*)*)__' $2 | cut -d: -f2 | sort -u)
74   local file=$2
75   cp $2 $3
76   file=$3
77   for pvar in $vars
78   do
79     eval pvalue=\$$pvar
80     sed -i 's/'$pvar'/'$pvalue'/g' $file
81   done
82 }
83
84 # $1: sequence number; $2: working directory
85 clone() {
86   local sequence=$1
87   local wkdir=$2
88   mkdir -p $wkdir
89
90   ############################################# SPECIAL VARIABLES SECTION #############################################
91   ################################ EDIT THIS SECTION IF YOU NEED NEW SPECIAL VARIABLES ################################
92   testid=$sequence
93   seq8=$(printf "%08d" $testid)
94   msisdn9=6${seq8}
95   ipv4hex=$seq8
96   #ipv4=$(for i in $(echo $seq8 | sed 's/\(..\)/0x\1 /g'); do printf "%d." $i; done | sed 's/\.$/\n/')
97   # Numbers beginning with "0" are treated as octal (i.e. base-8): we would have 'invalid octal number with 08 and 09'
98   # Solution: convert to base-10 in this way: $((10#$i))
99   ipv4=$(for i in $(echo $seq8 | sed 's/\(..\)/ \1 /g'); do printf "%d." $((10#$i)); done | sed 's/\.$/\n/')
100   # Sessions
101   sgx1_suffix=1$seq8
102   srx1_suffix=1$seq8
103   sgx2_suffix=2$seq8
104   srx2_suffix=2$seq8
105
106   local target=$wkdir/values.${1}
107   echo "__TESTID__=$testid" > $target
108   echo "__SEQ8__=$seq8" >> $target
109   echo "__MSISDN9__=$msisdn9" >> $target
110   echo "__IPV4HEX__=$ipv4hex" >> $target
111   echo "__IPV4__=$ipv4" >> $target
112   echo "__SGX1_SUFFIX__=$sgx1_suffix" >> $target
113   echo "__SRX1_SUFFIX__=$srx1_suffix" >> $target
114   echo "__SGX2_SUFFIX__=$sgx2_suffix" >> $target
115   echo "__SRX2_SUFFIX__=$srx2_suffix" >> $target
116   source $target
117   #rm $target
118   ######################################### END SPECIAL VARIABLES SECTION #########################################
119
120
121   # Parse template files:
122   parse_file $sequence $TESTCASE_TEMPLATE $wkdir/testcase.txt.$sequence
123   for file in $(grep sendxml $TESTCASE_TEMPLATE | cut -d\| -f4 | sed 's/\.xml$/.xml.msk/')
124   do
125     xml=`basename $file .msk`
126     parse_file $sequence $TESTCASE_TEMPLATE_DIR/$file $wkdir/${xml}.${sequence}
127     new_file=`readlink -f $wkdir/${xml}.${sequence}`
128     sed -i 's|'$xml'|'$new_file'|g' $wkdir/testcase.txt.$sequence
129   done
130   cat $wkdir/testcase.txt.$sequence >> $wkdir/testcase.txt
131   rm $wkdir/testcase.txt.$sequence
132
133   # Population:
134   if [ -n "$POPULATION_FILE" ]
135   then
136     parse_file $sequence $POPULATION_FILE $CLONE_WKDIR/${POPULATION}.$sequence
137     cat $CLONE_WKDIR/${POPULATION}.$sequence >> $POPULATION_ALL
138   fi
139 }
140
141 # $1: group number; $2: initial subrange value; $3: final subrange value
142 clone_group() {
143   for i in `seq $2 $3`
144   do
145     clone $i $CLONE_WKDIR/$1
146     sleep 0.01
147   done
148 }
149
150 #############
151 # EXECUTION #
152 #############
153 trap sig_handler SIGINT
154 trap sig_handler SIGTERM
155
156 ADML_DIR=$1
157 TESTCASE_TEMPLATE=`readlink -f $2`
158 TESTCASE_TEMPLATE_DIR=`dirname $TESTCASE_TEMPLATE`
159 CLONE_SEQ_BEGIN=$3
160 CLONE_SEQ_END=$4
161 N_GROUPS=$5
162 POPULATION=$6
163 POPULATION_FILE=$TESTCASE_TEMPLATE_DIR/$POPULATION
164 POPULATION_ALL=$TESTCASE_TEMPLATE_DIR/${POPULATION}.all
165
166 [ "$4" = "" ] && usage
167
168 # Auxiliary:
169 OPERATION=$ADML_DIR/operation.sh
170 [ ! -f $OPERATION ] && _exit "Missing '$OPERATION' file !!"
171 [ ! -f $TESTCASE_TEMPLATE ] && _exit "Missing '$TESTCASE_TEMPLATE' testcase template file !!"
172
173 if [ -n "$POPULATION" ]
174 then
175   bn_population=`basename $POPULATION`
176   [ "$bn_population" != "$POPULATION" ] && _exit "Only basename is allowed for population provided !!"
177   [ ! -f $POPULATION_FILE ] && _exit "Missing provided population file '$POPULATION_FILE' !!"
178   0> $POPULATION_ALL
179 fi
180
181 N_ITEMS=$((CLONE_SEQ_END - CLONE_SEQ_BEGIN + 1))
182 if [ -z "$N_GROUPS" ]
183 then
184   N_GROUPS=$((N_ITEMS/25))
185   [ $N_GROUPS -gt $MAX_NUMBER_GROUPS ] && N_GROUPS=$MAX_NUMBER_GROUPS
186   [ $N_GROUPS -eq 0 ] && N_GROUPS=1
187 fi
188 GROUPS_SIZE=$((N_ITEMS/N_GROUPS))
189 if [ "$GROUPS_SIZE" -eq 0 ]
190 then
191   echo "Assuming minimum allowed number of groups: $N_ITEMS"
192   GROUPS_SIZE=1
193   N_GROUPS=$N_ITEMS
194 fi
195
196 # Start cloning:
197 timestamp_begin=$(echo "scale=3 ; $(date '+%s') + $(date '+%N') / 1000000000" | bc)
198 children_before=$(children)
199 offset=0
200 $OPERATION "test|clear"
201 echo "Please be patient, this may take a while ..."
202 #echo "Temporary directory: $CLONE_WKDIR"
203 for group in `seq 1 $((N_GROUPS+1))`
204 do
205   n_begin=$((CLONE_SEQ_BEGIN + offset))
206   n_end=$((n_begin + GROUPS_SIZE - 1))
207   [ $n_end -gt $CLONE_SEQ_END ] && n_end=$CLONE_SEQ_END
208   [ $n_end -lt $n_begin ] && break
209   clone_group $group $n_begin $n_end &
210   offset=$((group * GROUPS_SIZE))
211 done
212
213 # Wait background jobs to finish:
214 while true
215 do
216   sleep 1 
217   [ $(children) -eq $children_before ] && break
218 done
219
220 # Programming:
221 echo -n "Programming .."
222 for file in $(ls $CLONE_WKDIR/*/testcase.txt)
223 do
224   echo -n .
225   r_file=`readlink -f $file`
226   dn_r_file=`dirname $r_file`
227   #bn_dn_r_file=`basename $dn_r_file`
228   #echo "Programming group $bn_dn_r_file ..."
229   $OPERATION -f $r_file >/dev/null
230   #$OPERATION -f $r_file > $dn_r_file/result.txt
231   rm -rf $dn_r_file &
232 done
233
234 timestamp_end=$(echo "scale=3 ; $(date '+%s') + $(date '+%N') / 1000000000" | bc)
235 echo
236 lasted=$(echo "scale=3 ; $timestamp_end - $timestamp_begin" | bc)
237 echo "Total Lasted $lasted seconds"
238 echo "Programming speed: $(echo "$N_ITEMS/$lasted" | bc) tests per second"
239
240 # Finish:
241 _exit "Done!" 0
242