From 49c70ff96a07190891935719d4bffe5ba0cf1b6b Mon Sep 17 00:00:00 2001
From: Rohit Gupta <rohit.gupta@eurecom.fr>
Date: Tue, 27 Oct 2015 15:24:35 +0100
Subject: [PATCH] fixed sudo permission issue of autotests

---
 .../autotests/run_exec_autotests.bash         | 45 ++++++-------------
 cmake_targets/build_oai                       |  9 +++-
 cmake_targets/tools/build_helper              | 35 +++++++++++++++
 cmake_targets/tools/init_nas_nos1             |  0
 4 files changed, 56 insertions(+), 33 deletions(-)
 mode change 100644 => 100755 cmake_targets/tools/init_nas_nos1

diff --git a/cmake_targets/autotests/run_exec_autotests.bash b/cmake_targets/autotests/run_exec_autotests.bash
index c4d11b7ba4..cccce16b33 100755
--- a/cmake_targets/autotests/run_exec_autotests.bash
+++ b/cmake_targets/autotests/run_exec_autotests.bash
@@ -7,8 +7,11 @@ else
    exit 1
 fi
 
+trap handle_ctrl_c INT
+
 source $OPENAIR_DIR/cmake_targets/tools/test_helper
 
+
 #SUDO="sudo -E "
 tdir=$OPENAIR_DIR/cmake_targets/autotests
 rm -fr $tdir/bin $tdir/log
@@ -36,7 +39,7 @@ cd $tdir
 #\param $12 -> class of the test case (compilation, execution)
 #\param $13 -> output of compilation program that needs to be found for test case to pass
 #\param $14 -> tags to help identify the test case for readability in output xml file
-test_compile() {
+function test_compile() {
 
     xUnit_start
     test_case_name=$1
@@ -130,8 +133,9 @@ test_compile() {
 #\param $12 -> class of the test case (compilation, execution)
 #\param $13 -> output of compilation program that needs to be found for test case to pass
 #\param $14 -> tags to help identify the test case for readability in output xml file
+#\param $15 => password for the user to run certain commands as sudo
 
-test_compile_and_run() {
+function test_compile_and_run() {
     xUnit_start
     test_case_name=$1
     log_dir=$tdir/log
@@ -149,16 +153,13 @@ test_compile_and_run() {
     class=${12}
     compile_prog_out=${13}
     tags=${14}
+    mypassword=${15}
     build_dir=$tdir/$1/build
     exec_file=$build_dir/$6
     
     #Temporary log file where execution log is stored.
     temp_exec_log=$log_dir/temp_log.txt
     
-    
-
-
-
     #echo "log_dir = $log_dir"
     #echo "log_file = $log_file"
     #echo "exec_file = $exec_file"
@@ -217,7 +218,8 @@ test_compile_and_run() {
           echo "<EXECUTION LOG Run = $run_index >" >> $log_file  2>&1
  
           if [ -n "$pre_exec_file" ]; then
-            { eval "source $pre_exec_file $pre_exec_args"; } >> $log_file  2>&1
+            { eval " echo '$mypassword' |sudo -S -E $pre_exec_file $pre_exec_args " ; }>> $log_file  2>&1
+
           fi
           echo "Executing $exec_file $main_exec_args_array_index "
           echo "Executing $exec_file $main_exec_args_array_index " >> $log_file
@@ -280,29 +282,8 @@ test_compile_and_run() {
 dbin=$OPENAIR_DIR/cmake_targets/autotests/bin
 dlog=$OPENAIR_DIR/cmake_targets/autotests/log
 
-run_test() {
-case=case$1; shift
-cmd=$1; shift
-expected=$3; shift
-echo "expected = $expected"
-exit
 
-$cmd > $dlog/$case.txt 2>&1
-if [ $expected = "true" ] ; then	 
-  if $* $dlog/$case.txt; then
-    echo_success "test $case, command: $cmd ok"
-  else
-    echo_error "test $case, command: $cmd Failed"
-  fi
-else 
-  if $* $dlog/$case.txt; then
-    echo_error "test $case, command: $cmd Failed"
-  else
-    echo_success "test $case, command: $cmd ok"
-  fi
-fi
-}
-print_help() {
+function print_help() {
  echo_info '
 This program runs automated test case system for OpenAirInterface
 You should have ubuntu 14.xx, updated, and the Linux kernel >= 3.14
@@ -314,11 +295,13 @@ Options
 '
 }
 
-main () {
+function main () {
 RUN_GROUP=0
 test_case_group=""
 test_case_group_array=()
 test_case_array=()
+echo_info "Note that the user should be sudoer for executing certain commands, for example loading kernel modules"
+read -s -p "Enter Password: " mypassword
 
 until [ -z "$1" ]
   do
@@ -421,7 +404,7 @@ for search_expr in "${test_case_array[@]}"
     if [ "$class" == "compilation" ]; then
         test_compile "$name" "$compile_prog" "$compile_prog_args" "$pre_exec" "$pre_exec_args" "$main_exec" "$main_exec_args" "search_array_true[@]" "$search_expr_false" "$nruns" "$pre_compile_prog" "$class" "$compile_prog_out" "$tags"
     elif  [ "$class" == "execution" ]; then
-        test_compile_and_run "$name" "$compile_prog" "$compile_prog_args" "$pre_exec" "$pre_exec_args" "$main_exec" "$main_exec_args" "search_array_true[@]" "$search_expr_false" "$nruns" "$pre_compile_prog" "$class" "$compile_prog_out" "$tags" 
+        test_compile_and_run "$name" "$compile_prog" "$compile_prog_args" "$pre_exec" "$pre_exec_args" "$main_exec" "$main_exec_args" "search_array_true[@]" "$search_expr_false" "$nruns" "$pre_compile_prog" "$class" "$compile_prog_out" "$tags" "$mypassword" 
     else
         echo "Unexpected class of test case...Exiting...."
     fi
diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index af147d682a..f92b69c00d 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -54,6 +54,8 @@ CFLAGS_PROCESSOR_USER=""
 RUN_GROUP=0
 TEST_CASE_GROUP=""
 
+trap handle_ctrl_c INT
+
 function print_help() {
   echo_info '
 This program installs OpenAirInterface Software
@@ -583,13 +585,16 @@ function main() {
   #####################
   if [ "$OAI_TEST" = "1" ]; then
     echo_info "10. Running OAI pre commit tests (pre-ci) ..."
+    echo_info "The log file for the autotests is located here: $OPENAIR_DIR/cmake_targets/autotests/log/autotests.log "
+    echo_info "You can hit CTRL-C any time to terminate the autotests..."
     rm -fr $OPENAIR_DIR/cmake_targets/autotests/log
     mkdir -p $OPENAIR_DIR/cmake_targets/autotests/log
     if [ "$RUN_GROUP" -eq "1" ]; then
-        $SUDO $OPENAIR_DIR/cmake_targets/autotests/run_exec_autotests.bash -g "$TEST_CASE_GROUP"
+        $OPENAIR_DIR/cmake_targets/autotests/run_exec_autotests.bash -g "$TEST_CASE_GROUP" >& $OPENAIR_DIR/cmake_targets/autotests/log/autotests.log &
     else
-        $SUDO $OPENAIR_DIR/cmake_targets/autotests/run_exec_autotests.bash
+        $OPENAIR_DIR/cmake_targets/autotests/run_exec_autotests.bash >& $OPENAIR_DIR/cmake_targets/autotests/log/autotests.log &
     fi
+    wait
   else
     echo_info "10. Bypassing the Tests ..."
   fi
diff --git a/cmake_targets/tools/build_helper b/cmake_targets/tools/build_helper
index 46997c2773..2e0e54f82b 100755
--- a/cmake_targets/tools/build_helper
+++ b/cmake_targets/tools/build_helper
@@ -319,3 +319,38 @@ set_openair_env(){
     export OPENAIR_TARGETS=$openair_path/targets
 }
 
+################################
+# Function to killall the subprocesses when Ctrl-C Key is hit
+###############################
+function handle_ctrl_c(){
+CURPID=$$
+ppid=$$
+arraycounter=1
+echo_info "** Trapped CTRL-C. Killing all subprocesses now..."
+while true
+do
+        FORLOOP=FALSE
+        # Get all the child process id
+        for i in `ps -ef| awk '$3 == '$ppid' { print $2 }'`
+        do
+                if [ $i -ne $CURPID ] ; then
+                        procid[$arraycounter]=$i
+                        arraycounter=`expr $arraycounter + 1`
+                        ppid=$i
+                        FORLOOP=TRUE
+                fi
+        done
+        if [ "$FORLOOP" = "FALSE" ] ; then
+           arraycounter=`expr $arraycounter - 1`
+           ## We want to kill child process id first and then parent id's
+           while [ $arraycounter -ne 0 ]
+           do
+             $SUDO kill -9 "${procid[$arraycounter]}" >/dev/null
+             arraycounter=`expr $arraycounter - 1`
+           done
+         exit
+        fi
+done
+}
+
+
diff --git a/cmake_targets/tools/init_nas_nos1 b/cmake_targets/tools/init_nas_nos1
old mode 100644
new mode 100755
-- 
GitLab