go.dedis.ch/onet/v4@v4.0.0-pre1/app/libtest.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # highest number of servers and clients
     4  NBR=${NBR:-3}
     5  # Per default, have $NBR servers
     6  NBR_SERVERS=${NBR_SERVERS:-$NBR}
     7  # Per default, keep one inactive server
     8  NBR_SERVERS_GROUP=${NBR_SERVERS_GROUP:-$(( NBR_SERVERS - 1))}
     9  # Show the output of the commands: 0=none, 1=test-names, 2=all
    10  DBG_TEST=${DBG_TEST:-0}
    11  # DBG-level for server
    12  DBG_SRV=${DBG_SRV:-0}
    13  # APPDIR is usually where the test.sh-script is located
    14  APPDIR=${APPDIR:-$(pwd)}
    15  # The app is the name of the builddir
    16  APP=${APP:-$(basename $APPDIR)}
    17  # Name of conode-log
    18  COLOG=conode
    19  
    20  RUNOUT=$( mktemp )
    21  
    22  # cleans the test-directory and builds the CLI binary
    23  # Globals:
    24  #   CLEANBUILD
    25  #   APPDIR
    26  #   APP
    27  startTest(){
    28    set +m
    29    if [ "$CLEANBUILD" ]; then
    30      rm -f conode $APP
    31    fi
    32    build $APPDIR
    33  }
    34  
    35  # Prints `Name`, cleans up build-directory, deletes all databases from services
    36  # in previous run, and calls `testName`.
    37  # Arguments:
    38  #   `Name` - name of the test to run
    39  test(){
    40    cleanup
    41    echo -e "\n* Testing $1"
    42    sleep .5
    43    test$1
    44  }
    45  
    46  # Asserts that the exit-code of running `$@` using `dbgRun` is `0`.
    47  # Arguments:
    48  #   $@ - used to run the command
    49  testOK(){
    50    testOut "Assert OK for '$@'"
    51    if ! dbgRun "$@"; then
    52      fail "starting $@ failed"
    53    fi
    54  }
    55  
    56  # Asserts that the exit-code of running `$@` using `dbgRun` is NOT `0`.
    57  # Arguments:
    58  #   $@ - used to run the command
    59  testFail(){
    60    testOut "Assert FAIL for '$@'"
    61    if dbgRun "$@"; then
    62      fail "starting $@ should've failed, but succeeded"
    63    fi
    64  }
    65  
    66  # Asserts `File` exists and is a file.
    67  # Arguments:
    68  #   `File` - path to the file to test
    69  testFile(){
    70    testOut "Assert file $1 exists"
    71    if [ ! -f $1 ]; then
    72      fail "file $1 is not here"
    73    fi
    74  }
    75  
    76  # Asserts `File` DOES NOT exist.
    77  # Arguments:
    78  #   `File` - path to the file to test
    79  testNFile(){
    80    testOut "Assert file $1 DOESN'T exist"
    81    if [ -f $1 ]; then
    82      fail "file $1 IS here"
    83    fi
    84  }
    85  
    86  # Asserts that `String` exists in `File`.
    87  # Arguments:
    88  #   `String` - what to search for
    89  #   `File` - in which file to search
    90  testFileGrep(){
    91    local G="$1" F="$2"
    92    testFile "$F"
    93    testOut "Assert file $F contains --$G--"
    94    if ! pcregrep -M -q "$G" $F; then
    95      fail "Didn't find '$G' in file '$F': $(cat $F)"
    96    fi
    97  }
    98  
    99  # Asserts that `String` is in the output of the command being run by `dbgRun`
   100  # and all but the first input argument. Ignores the exit-code of the command.
   101  # Arguments:
   102  #   `String` - what to search for
   103  #   `$@[1..]` - command to run
   104  testGrep(){
   105    S="$1"
   106    shift
   107    testOut "Assert grepping '$S' in '$@'"
   108    runOutFile "$@"
   109    doGrep "$S"
   110    if [ ! "$EGREP" ]; then
   111      fail "Didn't find '$S' in output of '$@': $GREP"
   112    fi
   113  }
   114  
   115  # Asserts that `String` is NOT in the output of the command being run by `dbgRun`
   116  # and all but the first input argument. Ignores the exit-code of the command.
   117  # Arguments:
   118  #   `String` - what to search for
   119  #   `$@[1..]` - command to run
   120  testNGrep(){
   121    G="$1"
   122    shift
   123    testOut "Assert NOT grepping '$G' in '$@'"
   124    runOutFile "$@"
   125    doGrep "$G"
   126    if [ "$EGREP" ]; then
   127      fail "DID find '$G' in output of '$@': $(cat $RUNOUT)"
   128    fi
   129  }
   130  
   131  # Asserts `String` is part of the last command being run by `testGrep` or
   132  # `testNGrep`.
   133  # Arguments:
   134  #   `String` - what to search for
   135  testReGrep(){
   136    G="$1"
   137    testOut "Assert grepping again '$G' in same output as before"
   138    doGrep "$G"
   139    if [ ! "$EGREP" ]; then
   140      fail "Didn't find '$G' in last output: $(cat $RUNOUT)"
   141    fi
   142  }
   143  
   144  # Asserts `String` is NOT part of the last command being run by `testGrep` or
   145  # `testNGrep`.
   146  # Arguments:
   147  #   `String` - what to search for
   148  testReNGrep(){
   149    G="$1"
   150    testOut "Assert grepping again NOT '$G' in same output as before"
   151    doGrep "$G"
   152    if [ "$EGREP" ]; then
   153      fail "DID find '$G' in last output: $(cat $RUNOUT)"
   154    fi
   155  }
   156  
   157  # used in test*Grep methods.
   158  doGrep(){
   159    # echo "grepping in $RUNOUT"
   160    # cat $RUNOUT
   161    WC=$( cat $RUNOUT | egrep "$1" | wc -l )
   162    EGREP=$( cat $RUNOUT | egrep "$1" )
   163  }
   164  
   165  # Asserts that `String` exists exactly `Count` times in the output of the
   166  # command being run by `dbgRun` and all but the first two arguments.
   167  # Arguments:
   168  #   `Count` - number of occurences
   169  #   `String` - what to search for
   170  #   `$@[2..]` - command to run
   171  testCount(){
   172    C="$1"
   173    G="$2"
   174    shift 2
   175    testOut "Assert counting '$C' of '$G' in '$@'"
   176    runOutFile "$@"
   177    doGrep "$G"
   178    if [ $WC -ne $C ]; then
   179      fail "Didn't find '$C' (but '$WC') of '$G' in output of '$@': $(cat $RUNOUT)"
   180    fi
   181  }
   182  
   183  
   184  # Outputs all arguments if `DBT_TEST -ge 1`
   185  # Globals:
   186  #   DBG_TEST - determines debug-level
   187  testOut(){
   188    if [ "$DBG_TEST" -ge 1 ]; then
   189      echo -e "$@"
   190    fi
   191  }
   192  
   193  # Outputs all arguments if `DBT_TEST -ge 2`
   194  # Globals:
   195  #   DBG_TEST - determines debug-level
   196  dbgOut(){
   197    if [ "$DBG_TEST" -ge 2 ]; then
   198      echo -e "$@"
   199    fi
   200  }
   201  
   202  # Runs `$@` and outputs the result of `$@` if `DBG_TEST -ge 2`. Redirects the
   203  # output in all cases if `OUTFILE` is set.
   204  # Globals:
   205  #   DBG_TEST - determines debug-level
   206  #   OUTFILE - if set, used to write output
   207  dbgRun(){
   208    if [ "$DBG_TEST" -ge 2 ]; then
   209      OUT=/dev/stdout
   210    else
   211      OUT=/dev/null
   212    fi
   213    if [ "$OUTFILE" ]; then
   214      "$@" 2>&1 | tee $OUTFILE > $OUT
   215    else
   216      "$@" 2>&1 > $OUT
   217    fi
   218  }
   219  
   220  runGrepSed(){
   221    GREP="$1"
   222    SED="$2"
   223    shift 2
   224    runOutFile "$@"
   225    doGrep "$GREP"
   226    SED=$( echo $EGREP | sed -e "$SED" )
   227  }
   228  
   229  runOutFile(){
   230    OLDOUTFILE=$OUTFILE
   231    OUTFILE=$RUNOUT
   232    dbgRun "$@"
   233    OUTFILE=$OLDOUTFILE
   234  }
   235  
   236  fail(){
   237    echo
   238    echo -e "\tFAILED: $@"
   239    cleanup
   240    exit 1
   241  }
   242  
   243  backg(){
   244    ( "$@" 2>&1 & )
   245  }
   246  
   247  # Builds the app stored in the directory given in the first argument.
   248  # Globals:
   249  #   CLEANBUILD - if set, forces build of app, even if it exists.
   250  #   TAGS - what tags to use when calling go build
   251  # Arguments:
   252  #   builddir - where to search for the app to build
   253  build(){
   254    local builddir=$1
   255    local app=$( basename $builddir )
   256    if [ ! -e $app -o "$CLEANBUILD" ]; then
   257      testOut "Building $app"
   258      if ! go build -o $app $TAGS $builddir/*.go; then
   259        fail "Couldn't build $builddir"
   260      fi
   261    else
   262      dbgOut "Not building $app because it's here"
   263    fi
   264  }
   265  
   266  buildDir(){
   267    BUILDDIR=${BUILDDIR:-$(mktemp -d)}
   268    mkdir -p $BUILDDIR
   269    testOut "Working in $BUILDDIR"
   270    cd $BUILDDIR
   271  }
   272  
   273  # Magical method that tries very hard to build a conode. If no arguments given,
   274  # it will search for a service first in the `./service` directory, and if not
   275  # found, in the `../service` directory.
   276  # If a directory is given as an argument, the service will be taken from that
   277  # directory.
   278  # Globals:
   279  #   APPDIR - where the app is stored
   280  # Arguments:
   281  #   [serviceDir, ...] - if given, used as directory to be included. More than one
   282  #                       argument can be given.
   283  buildConode(){
   284    local incl="$@"
   285    gopath=`go env GOPATH`
   286    if [ -z "$incl" ]; then
   287      echo "buildConode: No import paths provided. Searching."
   288      for i in service ../service
   289      do
   290        if [ -d $APPDIR/$i ]; then
   291          local pkg=$( realpath $APPDIR/$i | sed -e "s:$gopath/src/::" )
   292          incl="$incl $pkg"
   293        fi
   294      done
   295      echo "Found: $incl"
   296    fi
   297  
   298    local cotdir=$( mktemp -d )/conode
   299    mkdir -p $cotdir
   300  
   301    ( echo -e "package main\nimport ("
   302      for i in $incl; do
   303        echo -e "\t_ \"$i\""
   304      done
   305    echo ")" ) > $cotdir/import.go
   306  
   307    if [ ! -f "$gopath/src/github.com/dedis/cothority/conode/conode.go" ]; then
   308      echo "Cannot find package github.com/dedis/cothority."
   309      exit 1
   310    fi
   311    cp "$gopath/src/github.com/dedis/cothority/conode/conode.go" $cotdir/conode.go
   312  
   313    build $cotdir
   314    rm -rf $cotdir
   315    setupConode
   316  }
   317  
   318  setupConode(){
   319    # Don't show any setup messages
   320    DBG_OLD=$DBG_TEST
   321    DBG_TEST=0
   322    rm -f public.toml
   323    for n in $( seq $NBR_SERVERS ); do
   324      co=co$n
   325      rm -f $co/*
   326      mkdir -p $co
   327      echo -e "localhost:200$(( 2 * $n ))\nCot-$n\n$co\n" | dbgRun runCo $n setup
   328      if [ ! -f $co/public.toml ]; then
   329        echo "Setup failed: file $co/public.toml is missing."
   330        exit
   331      fi
   332      if [ $n -le $NBR_SERVERS_GROUP ]; then
   333        cat $co/public.toml >> public.toml
   334      fi
   335    done
   336    DBG_TEST=$DBG_OLD
   337  }
   338  
   339  runCoBG(){
   340    for nb in "$@"; do
   341      dbgOut "starting conode-server #$nb"
   342      (
   343        # Always redirect output of server in log-file, but
   344        # only output to stdout if DBG_SRV > 0.
   345        rm -f "$COLOG$nb.log.dead"
   346        if [ "$DBG_SRV" = 0 ]; then
   347          ./conode -d $DBG_SRV -c co$nb/private.toml server > "$COLOG$nb.log" | cat
   348        else
   349          ./conode -d $DBG_SRV -c co$nb/private.toml server 2>&1 | tee "$COLOG$nb.log"
   350        fi
   351        touch "$COLOG$nb.log.dead"
   352      ) &
   353    done
   354    sleep 1
   355    for nb in "$@"; do
   356      dbgOut "checking conode-server #$nb"
   357      if [ -f "$COLOG$nb.log.dead" ]; then
   358        echo "Server $nb failed to start:"
   359        cat "$COLOG$nb.log"
   360        exit 1
   361      fi
   362    done
   363  }
   364  
   365  runCo(){
   366    local nb=$1
   367    shift
   368    dbgOut "starting conode-server #$nb"
   369    dbgRun ./conode -d $DBG_SRV -c co$nb/private.toml "$@"
   370  }
   371  
   372  cleanup(){
   373    pkill -9 conode 2> /dev/null
   374    pkill -9 $APP 2> /dev/null
   375    sleep .5
   376    rm -f co*/*bin
   377    rm -f cl*/*bin
   378    rm -rf $CONODE_SERVICE_PATH
   379  }
   380  
   381  stopTest(){
   382    cleanup
   383    if [ $( basename $BUILDDIR ) != build ]; then
   384      dbgOut "removing $BUILDDIR"
   385      rm -rf $BUILDDIR
   386    fi
   387    echo "Success"
   388  }
   389  
   390  if ! which pcregrep > /dev/null; then
   391    echo "*** WARNING ***"
   392    echo "Most probably you're missing pcregrep which might be used here..."
   393    echo "On mac you can install it with"
   394    echo -e "\n  brew install pcre\n"
   395    echo "Not aborting because it might work anyway."
   396    echo
   397  fi
   398  
   399  if ! which realpath > /dev/null; then
   400    echo "*** WARNING ***"
   401    echo "Most probably you're missing realpath which might be used here..."
   402    echo "On mac you can install it with"
   403    echo -e "\n  brew install coreutils\n"
   404    echo "Not aborting because it might work anyway."
   405    echo
   406    realpath() {
   407      [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
   408    }
   409  fi
   410  
   411  for i in "$@"; do
   412    case $i in
   413      -b|--build)
   414        CLEANBUILD=yes
   415        shift # past argument=value
   416        ;;
   417      -nt|--notemp)
   418        BUILDDIR=$(pwd)/build
   419        shift # past argument=value
   420        ;;
   421    esac
   422  done
   423  buildDir
   424  
   425  export CONODE_SERVICE_PATH=$BUILDDIR/service_storage