From 3a41aff5f39594e8ed0083384a50c5e8173dae22 Mon Sep 17 00:00:00 2001
From: lche0021 <lche0021@student.monash.edu>
Date: Mon, 22 Feb 2021 16:31:23 +1100
Subject: [PATCH] bug fix, more useful help message, and run smoke_test on all
 softwares specified by -a

---
 smoke_test/smoke_test | 126 ++++++++++++++++++++++++++++++++----------
 1 file changed, 98 insertions(+), 28 deletions(-)

diff --git a/smoke_test/smoke_test b/smoke_test/smoke_test
index d61006d0..40483565 100755
--- a/smoke_test/smoke_test
+++ b/smoke_test/smoke_test
@@ -1,51 +1,118 @@
 #!/bin/bash
 # each application will create a folder under OUTPUT_DIR
-OUTPUT_DIR=${1:-output_dir}
+declare -i NUM_MODULE
+declare -i DEFAULT_NUM_MODULE
+declare -i TIMEOUT
+declare -i DEFAULT_TIMEOUT
+DEFAULT_OUTPUT_DIR='./output_dir'
+DEFAULT_NUM_MODULE=100
+DEFAULT_TESTCASE_DIR='./tests'
+DEFAULT_TIMEOUT=10
+DEFAULT_APPLICATION_LIST='./application_list.txt'
 
+function usage {
+    echo "Run smoke test"
+    echo "Usage: $0"
+    echo "  -a <list of applications to test>. This should point to a file with lines of '<name>/<version>', it can be generated using either generate_modules_list or software_usage.default: $DEFAULT_APPLICATION_LIST"
+    echo "  -n <number of modules to test>. Specify a large number to test all softwares, default: $DEFAULT_NUM_MODULE"
+    echo "  -o <output directory>, Output file will be generated at OUTPUT_DIR/report.txt, default: $DEFAULT_OUTPUT_DIR"
+    echo "  -t <testcases directories>.Testcases will be loaded from this directory. default: $DEFAULT_TESTCASE_DIR"
+    echo "  -T <timout period in seconds>, default: $DEFAULT_TIMEOUT"
+    exit 1
+}
 
+while getopts ":n:a:o:t:T:" opt; do
+    case "$opt" in
+        a)
+            APPLICATION_LIST=$OPTARG
+            ;;
+        o)
+            OUTPUT_DIR="$OPTARG"
+            ;;
+        n)
+            NUM_MODULE=$OPTARG
+            ;;
+        t)
+            TESTCASE_DIR=$OPTARG
+            ;;
+        T)
+            TIMEOUT=$OPTARG
+            ;;
+        *)
+            usage
+            ;;
+    esac
+done
+if [ -z $APPLICATION_LIST ]; then
+    APPLICATION_LIST=$(realpath $DEFAULT_APPLICATION_LIST)
+fi
 
-# select top 50 most used module by default
-NUM_MODULE=${2:-30}
+if [ -z $OUTPUT_DIR ]; then
+    OUTPUT_DIR=$(realpath $DEFAULT_OUTPUT_DIR)
+fi
+if [ -z $NUM_MODULE ]; then
+    NUM_MODULE=$DEFAULT_NUM_MODULE
+fi
+if [ -z $TESTCASE_DIR ]; then
+    TESTCASE_DIR=$(realpath $DEFAULT_TESTCASE_DIR)
+fi
+if [ -z $TIMEOUT]; then
+    TIMEOUT=$DEFAULT_TIMEOUT
+fi
 
-declare -i TIMELIMIT
 BINTEST=true
-TIMELIMIT=10
-TESTCASE_DIR=$(realpath './tests')
-OUTPUT_DIR=$(realpath $OUTPUT_DIR)
-REPORT=$OUTPUT_DIR/'report.txt'
+
+
+REPORT="$OUTPUT_DIR/report.txt"
+FAILED_MODULE_LOGS="$OUTPUT_DIR/failed_modules.txt"
+echo '' >  $FAILED_MODULE_LOGS
+echo '' > $REPORT
 
 declare -i num_success=0
 declare -i num_failed=0
 declare -i num_notest=0
 declare -i minor_error=0
 
+function generate_output_directory {
+    mkdir -p $OUTPUT_DIR
+    touch $REPORT
+    echo '' > $REPORT
+    echo "Test report created at $REPORT"
+
+}
 
-mkdir -p $OUTPUT_DIR
-touch $REPORT
-echo $REPORT
-echo '' > $REPORT
-USAGE_REPORT=software_usage.txt
-if [ ! -f $USAGE_REPORT ]; then 
-    ./software_usage $USAGE_REPORT
-fi
+
+# function generate_software_usage {
+#     SOFTWARE_USAGE_PATH=software_usage.txt
+#     if [ ! -f $SOFTWARE_USAGE_PATH ]; then
+#         ./software_usage $SOFTWARE_USAGE_PATH
+#     fi
+# }
+
+
+
+
+generate_output_directory
 
 
-declare -a modules
-failed_modules=()
 IFS_orig=$IFS
 IFS=$'\n'
 
-for i in $(head -n $NUM_MODULE $USAGE_REPORT); do
-    m=$(sed -n 's/\(.*\)\/\(.*\)  -  \(.*\)/\1 \2/p' <<< $i)
+
+for i in $(head -n $NUM_MODULE $APPLICATION_LIST); do
+    # m=$(sed -n 's/\(.*\)\/\(.*\)  -  \(.*\)/\1 \2/p' <<< $i)
+    m=$(sed -n 's/\(.*\)\/\(.*\)/\1 \2/p' <<< $i)
     name=$(cut -f1 -d' ' <<< $m)
     ver=$(cut -f2 -d' ' <<< $m)
     printf "\n\n=========================\n"
     if [ -z $name ]; then
         continue
     fi
-        
+
     echo "Testing $name/$ver ... "
 
+
+    module purge
     module load $name/$ver
     mkdir -p $OUTPUT_DIR/$name
     echo '' > $OUTPUT_DIR/$name/$ver
@@ -54,32 +121,35 @@ for i in $(head -n $NUM_MODULE $USAGE_REPORT); do
         num_notest+=1
     else
         ALL_EXEC=$(find $TESTCASE_DIR/$name -maxdepth 1 -executable -type f)
-        module_failed=1
+        module_failed=false
         for t_case in ${ALL_EXEC}; do
             if [ $i ==  'bintest' ] && ! $BINTEST;then break;fi
             orig_dir=$PWD
             cd $(dirname $t_case) # in case the testcase need to compile some source from the directory
-            timeout $TIMELIMIT $t_case $ver &> $OUTPUT_DIR/$name/$ver 
+            timeout $TIMEOUT $t_case $ver &> $OUTPUT_DIR/$name/$ver
             exitcode=$?
             cd $orig_dir
             if [ $exitcode -eq 0 ]; then
                 num_success+=1
             else
-                if [ $exitcode -eq 1 ]; then 
+                if [ $exitcode -eq 1 ]; then
                     minor_error+=1
                 else
                     num_failed+=1
-                    module_failed=0
+                    module_failed=true
                 fi
                 echo "$m return non-zero exitcode $exitcode for testcase $t_case" 2>&1 | tee -a $REPORT
             fi
         done
-        if [ module_failed ]; then 
-            echo "$name/$ver" >> $OUTPUT_DIR/failed_modules.txt
+        if $module_failed ; then
+            echo "$name/$ver failed"
+            echo "$name/$ver" >> $FAILED_MODULE_LOGS
         fi
     fi
-    module purge
+    module unload $name/$ver
+
     printf "Done"
+
 done
 IFS=$IFS_orig
 
-- 
GitLab