Improvements and new scenaries
[anna.git] / example / diameter / launcher / resources / scripts / clone.sh
index 815a5a7..bb4c468 100755 (executable)
@@ -1,20 +1,32 @@
 #!/bin/bash
+#####################################################
+####                                             ####
+##                                                 ##
+# THIS SCRIPT IS NORMALLY INTENDED FOR INTERNAL USE #
+##                                                 ##
+####                                             ####
+#####################################################
 
 #############
 # VARIABLES #
 #############
 CLONE_WKDIR=$(mktemp -d)
-MAX_NUMBER_GROUPS=400
+#MAX_NUMBER_GROUPS=400
+MAX_NUMBER_GROUPS=200
 # (depends on the machine)
+CLONE_EXTRA_VARS_SCR_BN=clone-extra-vars.sh
 
 #############
 # FUNCTIONS #
 #############
 _exit (){
   echo
-  echo $1
+  echo -e $1
   echo
 
+  # Background jobs:
+  (kill -9 $(jobs -p) 2>&1) >/dev/null
+
   # Cleanup:
   rm -rf $CLONE_WKDIR
 
@@ -28,37 +40,43 @@ sig_handler() {
 }
 
 usage() {
-  echo "Usage: $0 <ADML directory> <testcase variable file> <initial sequence> <final sequence> [number of groups]"
+  echo "Usage: $0 <ADML directory> <testcase variable file> <initial sequence> <final sequence> [number of groups] [population]"
   echo
   echo "          ADML directory:         path to the ADML directory where 'operation.sh' script is used to load the test cases."
   echo "          testcase variable file: path to the testcase file with parseable variables, for example '<directory>/testcase.txt.msk'."
   echo "                                  xml files within this test case, must exist with aditional .msk extension in the same directory."
-  echo "                                  Currently, 9 variables are supported to be replaced:"
-  echo "                                    __TESTID__ : sequence number. For example if you provide 1 to 20 for this script, it will be 1 .. 20"
-  echo "                                    __SEQ8__   : 8-digit number corresponding to the sequence number (00000001 for 1, and so on)"
-  echo "                                    __MSISDN9__: 9-digit msisdn"
-  echo "                                    __IPV4HEX__: hexadecimal IPv4 address, for example: 00000001"
-  echo "                                    __IPV4__   : IPv4 address, for example: 0.0.0.1"
-  echo "                                    __SGX1_SUFFIX__: 1<8-digit sequence>"
-  echo "                                    __SRX1_SUFFIX__: \""
-  echo "                                    __SGX2_SUFFIX__: 2<8-digit sequence>"
-  echo "                                    __SRX2_SUFFIX__: \""
+  echo
+  echo "                                  Currently, 4 variables are supported to be replaced:"
+  echo
+  echo "                                  * __TESTID__   : sequence number. For example if you provide 1 to 20 for this script, it will be 1 .. 20"
+  echo "                                  * __SEQ7__     : 7-digit number corresponding to the sequence number  (0000001 for 1, and so on)"
+  echo "                                  * __SEQ8__     : 8-digit number corresponding to the sequence number (00000001 for 1, and so on)"
+  echo "                                  * __IPV4HEX__  : hexadecimal IPv4 address, for example: 00000001"
+  echo "                                  * __IPV4__     : IPv4 address, for example: 0.0.0.1"
+  echo
+  echo "                                  Optionally (when exists), a user defined file '$CLONE_EXTRA_VARS_SCR_BN' is sourced to extend the parseable"
+  echo "                                  variables used within the test case stuff (this script must be placed on that directory). The script"
+  echo "                                  will be called with the sequence number as argument during clone procedure, and shall define the needed"
+  echo "                                  variable values referenced in the parsed files. The variables must be named as '__<name>__' which ensures"
+  echo "                                  that main clone script environment is not contaminated."
   echo
   echo "          initial sequence:       initial sequence number to parse over variables."
   echo "          final sequence:         final sequence number to parse over variables."
   echo "          number of groups:       number of background group jobs to clone the provided sequence range."
-  echo "                                  By default, it will be the number of items divided by 25, with a maximum of $MAX_NUMBER_GROUPS."
-  echo
-  echo
+  echo "                                  By default (or if you provide \"\"), it will be the number of items divided by 25, with a maximum"
+  echo "                                  of $MAX_NUMBER_GROUPS."
+  echo "          population:             Optionally, a population file (basename) can be provided. It must exist on the same directory"
+  echo "                                  than other stuff (testcase, xml files). The behaviour will be the accumulation of every parsing"
+  echo "                                  operation during clone procedure regarding the templated content of the population file. The"
+  echo "                                  accumulated content will be dump over a new file created together with cloned <population> and"
+  echo "                                  named with the extension '.all' (<population>.all). This population file could be useful to"
+  echo "                                  specify database commands related to each sequence (each single testcase), in order to have"
+  echo "                                  the whole population file. This won't include common database elements for the tested scenary,"
+  echo "                                  which shall be provisioned in a separated procedure."
+  echo 
   _exit
 }
 
-children () {
-  bash_pid=$$
-  children=`ps -eo ppid | grep -w $bash_pid`
-  echo $children | wc -w
-}
-
 # $1: sequence number; $2: file to parse(will be updated); $3: result file
 parse_file() {
   local vars=$(egrep -o '__((_*[A-Z]*[0-9]*)*)__' $2 | cut -d: -f2 | sort -u)
@@ -78,36 +96,27 @@ clone() {
   local wkdir=$2
   mkdir -p $wkdir
 
-  # Values:
-  testid=$sequence
-  seq8=$(printf "%08d" $testid)
-  msisdn9=6${seq8}
-  ipv4hex=$seq8
-  #ipv4=$(for i in $(echo $seq8 | sed 's/\(..\)/0x\1 /g'); do printf "%d." $i; done | sed 's/\.$/\n/')
+  ############################################# VARIABLES SECTION #############################################
+  local testid=$sequence
+  local seq7=$(printf "%07d" $testid)
+  local seq8=$(printf "%08d" $testid)
+  local ipv4hex=$seq8
   # Numbers beginning with "0" are treated as octal (i.e. base-8): we would have 'invalid octal number with 08 and 09'
   # Solution: convert to base-10 in this way: $((10#$i))
-  ipv4=$(for i in $(echo $seq8 | sed 's/\(..\)/ \1 /g'); do printf "%d." $((10#$i)); done | sed 's/\.$/\n/')
-  # Sessions
-  sgx1_suffix=1$seq8
-  srx1_suffix=1$seq8
-  sgx2_suffix=2$seq8
-  srx2_suffix=2$seq8
-
-  local target=$wkdir/values.${1}
-  echo "__TESTID__=$testid" > $target
-  echo "__SEQ8__=$seq8" >> $target
-  echo "__MSISDN9__=$msisdn9" >> $target
-  echo "__IPV4HEX__=$ipv4hex" >> $target
-  echo "__IPV4__=$ipv4" >> $target
-  echo "__SGX1_SUFFIX__=$sgx1_suffix" >> $target
-  echo "__SRX1_SUFFIX__=$srx1_suffix" >> $target
-  echo "__SGX2_SUFFIX__=$sgx2_suffix" >> $target
-  echo "__SRX2_SUFFIX__=$srx2_suffix" >> $target
-  source $target
-  #rm $target
+  local ipv4=$(for i in $(echo $seq8 | sed 's/\(..\)/ \1 /g'); do printf "%d." $((10#$i)); done | sed 's/\.$/\n/')
+
+  local __TESTID__=$testid
+  local __SEQ7__=$seq7
+  local __SEQ8__=$seq8
+  local __IPV4HEX__=$ipv4hex
+  local __IPV4__=$ipv4
+  [ -n "$extra_vars" ] && source $CLONE_EXTRA_VARS_SCR_FILE $sequence
+  ######################################### END VARIABLES SECTION #########################################
 
   # Parse template files:
   parse_file $sequence $TESTCASE_TEMPLATE $wkdir/testcase.txt.$sequence
+  local xml=
+  local new_file=
   for file in $(grep sendxml $TESTCASE_TEMPLATE | cut -d\| -f4 | sed 's/\.xml$/.xml.msk/')
   do
     xml=`basename $file .msk`
@@ -117,6 +126,13 @@ clone() {
   done
   cat $wkdir/testcase.txt.$sequence >> $wkdir/testcase.txt
   rm $wkdir/testcase.txt.$sequence
+
+  # Population:
+  if [ -n "$POPULATION" ]
+  then
+    parse_file $sequence $POPULATION_FILE $CLONE_WKDIR/${POPULATION}.$sequence
+    cat $CLONE_WKDIR/${POPULATION}.$sequence >> $POPULATION_ALL
+  fi
 }
 
 # $1: group number; $2: initial subrange value; $3: final subrange value
@@ -124,8 +140,8 @@ clone_group() {
   for i in `seq $2 $3`
   do
     clone $i $CLONE_WKDIR/$1
-    #sleep 0.01
   done
+  echo " - Completed range [$2,$3] for $(basename $ADML_DIR)"
 }
 
 #############
@@ -136,9 +152,16 @@ trap sig_handler SIGTERM
 
 ADML_DIR=$1
 TESTCASE_TEMPLATE=`readlink -f $2`
+TESTCASE_TEMPLATE_DIR=`dirname $TESTCASE_TEMPLATE`
 CLONE_SEQ_BEGIN=$3
 CLONE_SEQ_END=$4
 N_GROUPS=$5
+POPULATION=$6
+POPULATION_FILE=$TESTCASE_TEMPLATE_DIR/$POPULATION
+POPULATION_ALL=$TESTCASE_TEMPLATE_DIR/${POPULATION}.all
+CLONE_EXTRA_VARS_SCR_FILE=$TESTCASE_TEMPLATE_DIR/$CLONE_EXTRA_VARS_SCR_BN
+extra_vars=
+[ -f $CLONE_EXTRA_VARS_SCR_FILE ] && extra_vars=yes
 
 [ "$4" = "" ] && usage
 
@@ -147,8 +170,18 @@ OPERATION=$ADML_DIR/operation.sh
 [ ! -f $OPERATION ] && _exit "Missing '$OPERATION' file !!"
 [ ! -f $TESTCASE_TEMPLATE ] && _exit "Missing '$TESTCASE_TEMPLATE' testcase template file !!"
 
-TESTCASE_TEMPLATE_DIR=`dirname $TESTCASE_TEMPLATE`
+if [ -n "$POPULATION" ]
+then
+  bn_population=`basename $POPULATION`
+  [ "$bn_population" != "$POPULATION" ] && _exit "Only basename is allowed for population provided !!"
+  [ ! -f $POPULATION_FILE ] && _exit "Missing provided population file '$POPULATION_FILE' !!"
+  0> $POPULATION_ALL
+fi
+
 N_ITEMS=$((CLONE_SEQ_END - CLONE_SEQ_BEGIN + 1))
+[ "$N_ITEMS" -lt 1 ] && _exit "Initial sequence ($CLONE_SEQ_BEGIN) provided must be under final sequence value ($CLONE_SEQ_END) !!"
+[ "$CLONE_SEQ_END" -gt "99999999" ] && echo "Warning: some sequence value in range provided overflows __SEQ8__ and derived variables (__IPV4__ and __IPV4HEX__). Be care about using them ..."
+
 if [ -z "$N_GROUPS" ]
 then
   N_GROUPS=$((N_ITEMS/25))
@@ -158,17 +191,18 @@ fi
 GROUPS_SIZE=$((N_ITEMS/N_GROUPS))
 if [ "$GROUPS_SIZE" -eq 0 ]
 then
-  echo "Assuming minimum allowed number of groups: $N_ITEMS"
+  echo "Assuming minimum allowed number of groups (one group per item): $N_ITEMS"
   GROUPS_SIZE=1
   N_GROUPS=$N_ITEMS
 fi
 
 # Start cloning:
 timestamp_begin=$(echo "scale=3 ; $(date '+%s') + $(date '+%N') / 1000000000" | bc)
-children_before=$(children)
 offset=0
-$OPERATION "test|clear"
-echo "Please be patient, this may take a while ..."
+$OPERATION "test|clear" >/dev/null
+[ $? -ne 0 ] && _exit "Clone aborted: failed to clean tests !"
+#echo "Please be patient, this may take a while ..."
+#echo "Temporary directory: $CLONE_WKDIR"
 for group in `seq 1 $((N_GROUPS+1))`
 do
   n_begin=$((CLONE_SEQ_BEGIN + offset))
@@ -176,16 +210,11 @@ do
   [ $n_end -gt $CLONE_SEQ_END ] && n_end=$CLONE_SEQ_END
   [ $n_end -lt $n_begin ] && break
   clone_group $group $n_begin $n_end &
+  sleep 0.05
   offset=$((group * GROUPS_SIZE))
 done
-children_now=$(children)
 
-# Wait background jobs to finish:
-while true
-do
-  sleep 1 
-  [ $(children) -eq $children_before ] && break
-done
+wait $(jobs -p)
 
 # Programming:
 echo -n "Programming .."
@@ -194,10 +223,17 @@ do
   echo -n .
   r_file=`readlink -f $file`
   dn_r_file=`dirname $r_file`
-  #bn_dn_r_file=`basename $dn_r_file`
-  #echo "Programming group $bn_dn_r_file ..."
-  $OPERATION -f $r_file >/dev/null
-  #$OPERATION -f $r_file > $dn_r_file/result.txt
+  $OPERATION -t 300 -f $r_file > $dn_r_file/result.txt
+  res=$?
+  if [ $res -ne 0 ]
+  then
+    bn_dn_r_file=`basename $dn_r_file`
+    exception_dir=$ADML_DIR/exception_stuff_${bn_dn_r_file}
+    rm -rf $exception_dir
+    cp -r $dn_r_file $exception_dir
+    cp $ADML_DIR/launcher.trace $exception_dir
+    _exit "Exception detected programming a testcase. Check for '$exception_dir'"
+  fi
   rm -rf $dn_r_file &
 done