From 13a079c8558a752d7af6739022a3172d57ffe20b Mon Sep 17 00:00:00 2001
From: frtabu <francois.taburet@nokia-bell-labs.com>
Date: Mon, 10 Jun 2019 19:28:01 +0200
Subject: [PATCH] Modify main bash script in CI to ease addition of new
 variants.

---
 .../doc/vm_based_simulator_main_scripts.md    | 119 +++++++++++++++++-
 ci-scripts/oai-ci-vm-tool                     |  65 +++++++---
 2 files changed, 159 insertions(+), 25 deletions(-)

diff --git a/ci-scripts/doc/vm_based_simulator_main_scripts.md b/ci-scripts/doc/vm_based_simulator_main_scripts.md
index 91aa88b7ecf..092165b37cc 100644
--- a/ci-scripts/doc/vm_based_simulator_main_scripts.md
+++ b/ci-scripts/doc/vm_based_simulator_main_scripts.md
@@ -52,7 +52,7 @@ The main script is including a bunch of sub BASH scripts.
 *  ci-scripts/reportBuildLocally.sh
 *  ci-scripts/reportTestLocally.sh
 
-**NOTE: ci-scripts/runTestOnVM.sh is getting big and will certainly be split to facilate maintenance. Start functions will be also factorized.**
+**NOTE: ci-scripts/runTestOnVM.sh is getting big and will certainly be split to facilitate maintenance. Start functions will be also factorized.**
 
 # 3. Main script features #
 
@@ -62,7 +62,7 @@ It is also **testing if uvtool and apt-cacher are installed.**
 
 It finally provides parameters to the requested functions. Parameter definition is centralized there.
 
-For example: 
+For example:
 
 for VM instance creation:
 
@@ -75,13 +75,122 @@ for OAI variant build:
 *  build log file to parse: LOG_PATTERN
 *  the number of log files to parse: NB_PATTERN_FILES
 
-These last 2 variables are very important if you change the build options or if you modify the build system and add more targets to build (especially true for physical simulator).
+You can get the list of variant specific variables specifying the variant argument when asking for help:
+
+``` bash
+./ci-scripts/oai-ci-vm-tool help variant
+    --variant flexran-rtc     OR -v10    ( build and test non-OSA )
+               VM_NAME=ci-flexran-rtc           ARCHIVES_LOC=flexran        
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=1               BUILD_OPTIONS="cmake . && make -j2"
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant enb-usrp        OR -v1     ( build and test  )
+               VM_NAME=ci-enb-usrp              ARCHIVES_LOC=enb_usrp       
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=9               BUILD_OPTIONS="--eNB -w USRP --mu"
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant l1-sim          OR -v20    ( test  )
+               VM_NAME=ci-l1-sim                ARCHIVES_LOC=l1_sim         
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=9               BUILD_OPTIONS=""             
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant rf-sim          OR -v21    ( test  )
+               VM_NAME=ci-rf-sim                ARCHIVES_LOC=rf_sim         
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=9               BUILD_OPTIONS=""             
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant l2-sim          OR -v22    ( test  )
+               VM_NAME=ci-l2-sim                ARCHIVES_LOC=l2_sim         
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=9               BUILD_OPTIONS=""             
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant basic-sim       OR -v2     ( build and test  )
+               VM_NAME=ci-basic-sim             ARCHIVES_LOC=basic_sim      
+               VM_MEMORY=8192                   VM_CPU=4              
+               NB_PATTERN_FILES=13              BUILD_OPTIONS="--eNB --UE"   
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant phy-sim         OR -v3     ( build and test  )
+               VM_NAME=ci-phy-sim               ARCHIVES_LOC=phy_sim        
+               VM_MEMORY=2048                   VM_CPU=4              
+               NB_PATTERN_FILES=3               BUILD_OPTIONS="--phy_simulators"
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant cppcheck        OR -v4     ( build and test  )
+               VM_NAME=ci-cppcheck              ARCHIVES_LOC=cppcheck       
+               VM_MEMORY=4096                   VM_CPU=4              
+               NB_PATTERN_FILES=1               BUILD_OPTIONS="--enable=warning --force --xml --xml-version=2 --suppressions-list=ci-scripts/cppcheck_suppressions.list -I common/utils -j4"
+               LOG_PATTERN=cppcheck.xml   
+
+    --variant enb-ethernet    OR -v7     ( build and test  )
+               VM_NAME=ci-enb-ethernet          ARCHIVES_LOC=enb_eth        
+               VM_MEMORY=4096                   VM_CPU=4              
+               NB_PATTERN_FILES=8               BUILD_OPTIONS="--eNB"        
+               LOG_PATTERN=.Rel14.txt     
+
+    --variant ue-ethernet     OR -v8     ( build and test  )
+               VM_NAME=ci-ue-ethernet           ARCHIVES_LOC=ue_eth         
+               VM_MEMORY=4096                   VM_CPU=4              
+               NB_PATTERN_FILES=12              BUILD_OPTIONS="--UE"         
+               LOG_PATTERN=.Rel14.txt     
 
-There are many more variables.
+```
+
+To define a new variant you just need to define a function which name conforms to `function variant__v<n>__<variant_name>` where n and variant_name will respectively define the short and long options for your variant. The function only needs to define the variant dependent variables. For many variables, default values are set in the `check_set_variant` function. When a variant doesn't define the BUILD_OPTIONS variable it cannot be used for the `build` `wait` and `create` commands.
+
+The main scripts also allows the definition of non variant-dependant variable via the `--setvar_<variable name> <variable value>` options.
+You can get the list of these variables by using `help setvar`:
+
+```BASH
+./ci-scripts/oai-ci-vm-tool help setvar
+--setvar_<varname> <value> where varname is one of:
+            VM_OSREL :     OS release to use in virtual machines
+    RUN_EXPERIMENTAL :     Enforce execution of variants with EXPERIMENTAL variable set to "true"
+```
+
+To add a new non-variant dependant variable you need:
+* Add an item to the `AUTHORIZED_VAR` array
+* In the `setvar_usage`function,  add your help string in the HELP_VAR["<your variable name>"] variable.
+* Write the bash code for your variable.
+
+Example of non variant dependent usage:
+
+``` bash
+./ci-scripts/oai-ci-vm-tool test -v21  -ws /usr/local/oai/enhance_CI_extEPC/openairinterface5g -id 1 -jn testci
+Currently testci-b1-rf-sim Testing is not implemented / enabled
+Comment out these lines in ./ci-scripts/oai-ci-vm-tool if you want to run it
+ or use option --setvar_RUN_EXPERIMENTAL=true to test it
+
+
+ ./ci-scripts/oai-ci-vm-tool test -v21  -ws /usr/local/oai/enhance_CI_extEPC/openairinterface5g -id 1 -jn testci --setvar_RUN_EXPERIMENTAL true
+ Setting RUN_EXPERIMENTAL to true...
+ ############################################################
+ OAI CI VM script
+ ############################################################
+ ENB_VM_NAME         = testci-b1-enb-ethernet
+ ENB_VM_CMD_FILE     = testci-b1-enb-ethernet_cmds.txt
+ UE_VM_NAME          = testci-b1-ue-ethernet
+ UE_VM_CMD_FILE      = testci-b1-ue-ethernet_cmds.txt
+ JENKINS_WKSP        = /usr/local/oai/enhance_CI_extEPC/openairinterface5g
+ ARCHIVES_LOC        = /usr/local/oai/enhance_CI_extEPC/openairinterface5g/archives/rf_sim/test
+ ############################################################
+ Waiting for ENB VM to be started
+...........................
+```
+
+In the same way, you can set the variable `VM_OSREL` to run the test in virtual machines of the specified OS release:
+
+``` bash
+./ci-scripts/oai-ci-vm-tool test -v21  -ws /usr/local/oai/enhance_CI_extEPC/openairinterface5g -id 1 -jn testci --setvar_VM_OSREL bionic
+```
 
 ---
 
 Next step: [how to create one or several VM instances](./vm_based_simulator_create.md)
 
 You can also go back to the [CI dev main page](./ci_dev_home.md)
-
diff --git a/ci-scripts/oai-ci-vm-tool b/ci-scripts/oai-ci-vm-tool
index 65640560016..b8c6c5cef8c 100755
--- a/ci-scripts/oai-ci-vm-tool
+++ b/ci-scripts/oai-ci-vm-tool
@@ -49,17 +49,22 @@ function variant_usage {
         	echo -n "    ( build and test "
         fi
         echo "$VARIANT_INFO )"
-        printf  "               VM_NAME=%-15s          ARCHIVES_LOC=%-15s\n" "$VM_NAME" "$ARCHIVES_LOC"
-        printf  "               VM_MEMORY=%-15s        VM_CPU=%-15s\n" "$VM_MEMORY" "$VM_CPU"       
-        printf  "               NB_PATTERN_FILES=%-15s BUILD_OPTIONS=%-15s\n" "$NB_PATTERN_FILES" "\"$BUILD_OPTIONS\""
-        printf  "               LOG_PATTERN=%-15s\n\n" "$LOG_PATTERN"
-        
+        if [ "$1" = "full" ]
+        then        
+            printf  "               VM_NAME=%-15s          ARCHIVES_LOC=%-15s\n" "$VM_NAME" "$ARCHIVES_LOC"
+            printf  "               VM_MEMORY=%-15s        VM_CPU=%-15s\n" "$VM_MEMORY" "$VM_CPU"       
+            printf  "               NB_PATTERN_FILES=%-15s BUILD_OPTIONS=%-15s\n" "$NB_PATTERN_FILES" "\"$BUILD_OPTIONS\""
+            printf  "               LOG_PATTERN=%-15s      EXPERIMENTAL=%-15s\n\n\n" "$LOG_PATTERN"  "$EXPERIMENTAL"
+        fi
     done    
 }
 
 function setvar_usage {
+	declare -A HELP_VAR
+	HELP_VAR["VM_OSREL"]="OS release to use in virtual machines"
+	HELP_VAR["RUN_EXPERIMENTAL"]="Enforce execution of variants with EXPERIMENTAL variable set to \"true\"" 
     echo "--setvar_<varname> <value> where varname is one of:"
-	for i in "$AUTHORIZED_VAR"; do echo "   $i";done
+	for i in ${AUTHORIZED_VAR[@]}; do printf "%20s :     %s\n" "$i" "${HELP_VAR[$i]}" ;done
 }
 
 # functions variant__v<n>__<variant name> are used to define build or test variants
@@ -115,10 +120,12 @@ function variant__v10__flexran_rtc {
 function variant__v20__l1_sim {
     ARCHIVES_LOC=l1_sim
     RUN_OPTIONS="complex"
+    EXPERIMENTAL="true"
 }
     
 function variant__v21__rf_sim {
     RUN_OPTIONS="complex"
+    EXPERIMENTAL="true"
 }
 
 function variant__v22__l2_sim {
@@ -156,6 +163,7 @@ function check_set_variant {
             ARCHIVES_LOC=${VARIANTS_LONG[$i]//"-"/"_"}  
             VM_MEMORY=2048
             VM_CPU=4
+            EXPERIMENTAL=""
             NBARGS=$[$NBARGS+$VARIANT_OPTID]   
             variant__${VARIANTS_SHORT[$i]}__${VARIANTS_LONG[$i]//"-"/"_"}
             return 0
@@ -212,11 +220,12 @@ function check_ipv4addr {
 }
 
 function check_setvar {
-    for i in $AUTHORIZED_VAR
+    for i in ${AUTHORIZED_VAR[@]}
     do
     	if [ "$1" = "$i" ]
     	then
-    	   $1="$2"
+    	   echo "Setting $1 to $2..."
+    	   export "$1"="$2"
     	   return 0
     	fi
     done
@@ -243,6 +252,8 @@ then
 fi
 
 VM_TEMPLATE=ci-
+
+
 # look for default ASN1 release in cmake directives
 # It will be the release used when building
 ASN1VEROPT=( `grep "add_list2_option(RRC_ASN1_VERSION" cmake_targets/CMakeLists.txt` )
@@ -303,7 +314,8 @@ EPC_IPADDR=""
 EPC_IPADDR_OPTID=$((2**16))
 
 # list of variables that can be set via the --setvar option
-AUTHORIZED_VAR="VM_OSREL"
+AUTHORIZED_VAR=("VM_OSREL RUN_EXPERIMENTAL")
+
 
 #variables to set which OS VM should use
 #if [ -f "/etc/os-release" ]
@@ -345,7 +357,9 @@ ALLOWED_OPTMASK=0
 while [[ $# -gt 0 ]]
 do
 key="$1"
-
+if [ "$(type -t $funcname)" != 'function' ]; then
+    funcname=do_it_normal
+fi
 case $key in
     --debug)
     set -v
@@ -354,6 +368,11 @@ case $key in
     ;;
     -h|--help|help)
     COMMAND["help"]=1
+    if [ "$2" != "" ] && [ "$(type -t $2_usage)" = 'function' ]
+    then
+         $2_usage "full"
+         exit 0
+    fi
     shift
     ;;
     create)
@@ -604,7 +623,11 @@ then
 else
     VM_TEMPLATE=${JOB_NAME}-b${BUILD_ID}-
 fi
-
+# set default variant
+if [ "$VM_NAME" = "" ]
+then
+	check_set_variant "v1"
+fi
 VM_NAME=`echo $VM_NAME | sed -e "s#ci-#$VM_TEMPLATE#"`
 VM_CMDS=${VM_NAME}_cmds.txt
 ARCHIVES_LOC=${JENKINS_WKSP}/archives/${ARCHIVES_LOC}
@@ -612,9 +635,9 @@ ARCHIVES_LOC=${JENKINS_WKSP}/archives/${ARCHIVES_LOC}
 STATUS=0
 if [ ${COMMAND[${COMMANDS_NAME[$CREATE_CMD]}]} -eq 1 ]
 then
-    if [[ $VM_NAME =~ .*-l2-sim.* ]]
+    if [[ "$BUILD_OPTIONS" = "" ]]
     then
-        echo "Selected variant is no more a build variant"
+        echo "$VM_NAME is not a build variant"
         exit 0
     else
         create_vm
@@ -622,9 +645,9 @@ then
 fi
 if [ ${COMMAND[${COMMANDS_NAME[$BUILD_CMD]}]} -eq 1 ]
 then
-    if [[ $VM_NAME =~ .*-l2-sim.* ]]
+    if [[  "$BUILD_OPTIONS" = "" ]]
     then
-        echo "Selected variant is no more a build variant"
+        echo " $VM_NAME is not a build variant"
         exit 0
     else
         build_on_vm
@@ -636,9 +659,9 @@ then
 fi
 if [ ${COMMAND[${COMMANDS_NAME[$WAIT_CMD]}]} -eq 1 ]
 then
-    if [[ $VM_NAME =~ .*-l2-sim.* ]]
+    if [[  "$BUILD_OPTIONS" = "" ]]
     then
-        echo "Selected variant is no more a build variant"
+        echo "$VM_NAME is not a build variant"
         exit 0
     else
         wait_on_vm_build
@@ -650,11 +673,13 @@ then
 fi
 if [ ${COMMAND[${COMMANDS_NAME[$TEST_CMD]}]} -eq 1 ]
 then
-    # Comment out or delete the following lines if you want to run L1-simulator in your branch and/or merge request
-    if [[ $VM_NAME =~ .*-l1-sim.* ]]
+    # variant with EXPERIMENTAL variable set to true won't run and will be
+    # considered OK, except if --setvar_RUN_EXPERIMENTAL option has been set to true
+    if [ "$EXPERIMENTAL" = "true" ] && [ "$RUN_EXPERIMENTAL" != "true" ]
     then
-        echo "Currently L1-Simulator Testing is not implemented / enabled"
+        echo "Currently $VM_NAME Testing is not implemented / enabled"
         echo "Comment out these lines in ./ci-scripts/oai-ci-vm-tool if you want to run it"
+        echo " or use option --setvar_RUN_EXPERIMENTAL=true to test it"
         echo "STATUS seems OK"
         exit $STATUS
     fi
-- 
GitLab