From bfef9eade9092905db7d3c00c4da83ff29c102b8 Mon Sep 17 00:00:00 2001
From: Sydney Anne Erickson <sydney-anne@elixxir.io>
Date: Tue, 19 Jul 2022 21:18:05 +0000
Subject: [PATCH] Separate the running of basice2e network into its own script

---
 basice2e/client-session-tests.sh |  43 ++++++++++++-
 basice2e/network.sh              | 102 ++++++++++++++++++++++++++++++
 basice2e/run.sh                  | 103 +------------------------------
 3 files changed, 144 insertions(+), 104 deletions(-)
 mode change 100644 => 100755 basice2e/client-session-tests.sh
 create mode 100755 basice2e/network.sh

diff --git a/basice2e/client-session-tests.sh b/basice2e/client-session-tests.sh
old mode 100644
new mode 100755
index d6e90c5..8e5d2be
--- a/basice2e/client-session-tests.sh
+++ b/basice2e/client-session-tests.sh
@@ -1,9 +1,48 @@
+# Client Session Tests: this script will run the "old" `client` binary to init session files and setup the environment 
+# for the "new" `client-release` binary to run tests on the old session files.
+
 set -e
 #set -o xtrace
 
+# --- Define variables to use for the test & local network ---
+
+DEBUGLEVEL=${DEBUGLEVEL-1}
+CLIENTOPTS="--password hello --ndf results/ndf.json --sendDelay 100 --waitTimeout 360 --unsafe-channel-creation -v $DEBUGLEVEL"
+SERVERLOGS=results/servers
+GATEWAYLOGS=results/gateways
+UDBOUT=results/udb-console.txt
+
+# --- Setup a local network ---
+
 rm -rf client*.log blob* rick*.bin ben*.bin
+rm -rf results.bak results
+mkdir results
+
+mkdir -p $SERVERLOGS
+mkdir -p $GATEWAYLOGS
+
+# Start the network
+source network.sh
 
-CLIENTOPTS="--password hello --ndf results/ndf.json --sendDelay 100 --waitTimeout 360 --unsafe-channel-creation -v 1"
+echo "DOWNLOADING TLS Cert..."
+CMD="openssl s_client -showcerts -connect $(tr -d '[:space:]' < results/startgwserver.txt)"
+echo $CMD
+eval $CMD < /dev/null 2>&1 > "results/startgwcert.bin"
+CMD="cat results/startgwcert.bin | openssl x509 -outform PEM"
+echo $CMD
+eval $CMD > "results/startgwcert.pem"
+head "results/startgwcert.pem"
+
+echo "DOWNLOADING NDF..."
+CLIENTCMD="../bin/client getndf --gwhost $(tr -d '[:space:]' < results/startgwserver.txt) --cert results/startgwcert.pem"
+eval $CLIENTCMD >> results/ndf.json 2>&1 &
+PIDVAL=$!
+echo "$CLIENTCMD -- $PIDVAL"
+wait $PIDVAL
+
+# ------------------------------------------------------------------------------
+# TESTS BEGIN BELOW
+# ------------------------------------------------------------------------------
 
 # --- Pre-canned messaging to self ---
 timeout 240s ../bin/client --password hello --ndf results/ndf.json --sendDelay 100 --waitTimeout 360 --unsafe-channel-creation -v 1 -l client9-master.log --sendCount 2 --receiveCount 2 -s blob9/blob9 --sendid 9 --destid 9 -m "Hi 9->9, with E2E Encryption"
@@ -66,4 +105,4 @@ eval $CLIENTCMD || true &
 PIDVAL2=$!
 echo "$CLIENTCMD -- $PIDVAL"
 wait $PIDVAL
-wait $PIDVAL2
\ No newline at end of file
+wait $PIDVAL2
diff --git a/basice2e/network.sh b/basice2e/network.sh
new file mode 100755
index 0000000..fd9f41b
--- /dev/null
+++ b/basice2e/network.sh
@@ -0,0 +1,102 @@
+echo "STARTING SERVERS..."
+
+# Copy udbContact into place when running locally.
+cp udbContact.bin results/udbContact.bin
+
+PERMCMD="../bin/permissioning --logLevel $DEBUGLEVEL -c permissioning.yaml "
+$PERMCMD > results/permissioning-console.txt 2>&1 &
+PIDVAL=$!
+echo "$PERMCMD -- $PIDVAL"
+
+
+# Run Client Registrar
+CLIENT_REG_CMD="../bin/client-registrar \
+-l 2 -c client-registrar.yaml"
+$CLIENT_REG_CMD > results/client-registrat-console.txt 2>&1 &
+PIDVAL=$!
+echo "$CLIENT_REG_CMD -- $PIDVAL"
+
+for SERVERID in $(seq 5 -1 1)
+do
+    IDX=$(($SERVERID - 1))
+    SERVERCMD="../bin/server --logLevel $DEBUGLEVEL --config server-$SERVERID.yaml"
+    if [ $SERVERID -eq 5 ] && [ -n "$NSYSENABLED" ]
+    then
+        SERVERCMD="nsys profile --session-new=gputest --trace=cuda -o server-$SERVERID $SERVERCMD"
+    fi
+    $SERVERCMD > $SERVERLOGS/server-$SERVERID-console.txt 2>&1 &
+    PIDVAL=$!
+    echo "$SERVERCMD -- $PIDVAL"
+done
+
+# Start gateways
+for GWID in $(seq 5 -1 1)
+do
+    IDX=$(($GWID - 1))
+    GATEWAYCMD="../bin/gateway --logLevel $DEBUGLEVEL --config gateway-$GWID.yaml"
+    $GATEWAYCMD > $GATEWAYLOGS/gateway-$GWID-console.txt 2>&1 &
+    PIDVAL=$!
+    echo "$GATEWAYCMD -- $PIDVAL"
+done
+
+jobs -p > results/serverpids
+
+finish() {
+    echo "STOPPING SERVERS AND GATEWAYS..."
+    if [ -n "$NSYSENABLED" ]
+    then
+        nsys stop --session=gputest
+    fi
+    # NOTE: jobs -p doesn't work in a signal handler
+    for job in $(cat results/serverpids)
+    do
+        echo "KILLING $job"
+        kill $job || true
+    done
+
+    sleep 5
+
+    for job in $(cat results/serverpids)
+    do
+        echo "KILL -9 $job"
+        kill -9 $job || true
+    done
+    #tail $SERVERLOGS/*
+    #tail $CLIENTCLEAN/*
+    #diff -aruN clients.goldoutput $CLIENTCLEAN
+}
+
+trap finish EXIT
+trap finish INT
+
+# Sleeps can die in a fire on the sun, we wait for the servers to start running
+# rounds
+rm rid.txt || true
+touch rid.txt
+cnt=0
+echo -n "Waiting for a round to run"
+while [ ! -s rid.txt ] && [ $cnt -lt 120 ]; do
+    sleep 1
+    grep -a "RID 1 ReceiveFinishRealtime END" results/servers/server-* > rid.txt || true
+    cnt=$(($cnt + 1))
+    echo -n "."
+done
+
+# Start a user discovery bot server
+echo "STARTING UDB..."
+UDBCMD="../bin/udb --logLevel $DEBUGLEVEL --skipVerification --protoUserPath	udbProto.json --config udb.yaml -l 1"
+$UDBCMD >> $UDBOUT 2>&1 &
+PIDVAL=$!
+echo $PIDVAL >> results/serverpids
+echo "$UDBCMD -- $PIDVAL"
+rm rid.txt || true
+while [ ! -s rid.txt ] && [ $cnt -lt 30 ]; do
+    sleep 1
+    grep -a "Sending Poll message" results/udb-console.txt > rid.txt || true
+    cnt=$(($cnt + 1))
+    echo -n "."
+done
+
+echo "localhost:8440" > results/startgwserver.txt
+
+echo "DONE LETS DO STUFF"
\ No newline at end of file
diff --git a/basice2e/run.sh b/basice2e/run.sh
index f3c5ad6..73b04e7 100755
--- a/basice2e/run.sh
+++ b/basice2e/run.sh
@@ -64,108 +64,7 @@ echo "NETWORK: $NETWORKENTRYPOINT"
 
 if [ "$NETWORKENTRYPOINT" == "localhost:8440" ]
 then
-    echo "STARTING SERVERS..."
-
-    # Copy udbContact into place when running locally.
-    cp udbContact.bin results/udbContact.bin
-
-    PERMCMD="../bin/permissioning --logLevel $DEBUGLEVEL -c permissioning.yaml "
-    $PERMCMD > results/permissioning-console.txt 2>&1 &
-    PIDVAL=$!
-    echo "$PERMCMD -- $PIDVAL"
-
-
-    # Run Client Registrar
-    CLIENT_REG_CMD="../bin/client-registrar \
-    -l 2 -c client-registrar.yaml"
-    $CLIENT_REG_CMD > results/client-registrat-console.txt 2>&1 &
-    PIDVAL=$!
-    echo "$CLIENT_REG_CMD -- $PIDVAL"
-
-    for SERVERID in $(seq 5 -1 1)
-    do
-        IDX=$(($SERVERID - 1))
-        SERVERCMD="../bin/server --logLevel $DEBUGLEVEL --config server-$SERVERID.yaml"
-        if [ $SERVERID -eq 5 ] && [ -n "$NSYSENABLED" ]
-        then
-            SERVERCMD="nsys profile --session-new=gputest --trace=cuda -o server-$SERVERID $SERVERCMD"
-        fi
-        $SERVERCMD > $SERVERLOGS/server-$SERVERID-console.txt 2>&1 &
-        PIDVAL=$!
-        echo "$SERVERCMD -- $PIDVAL"
-    done
-
-    # Start gateways
-    for GWID in $(seq 5 -1 1)
-    do
-        IDX=$(($GWID - 1))
-        GATEWAYCMD="../bin/gateway --logLevel $DEBUGLEVEL --config gateway-$GWID.yaml"
-        $GATEWAYCMD > $GATEWAYLOGS/gateway-$GWID-console.txt 2>&1 &
-        PIDVAL=$!
-        echo "$GATEWAYCMD -- $PIDVAL"
-    done
-
-    jobs -p > results/serverpids
-
-    finish() {
-        echo "STOPPING SERVERS AND GATEWAYS..."
-        if [ -n "$NSYSENABLED" ]
-        then
-            nsys stop --session=gputest
-        fi
-        # NOTE: jobs -p doesn't work in a signal handler
-        for job in $(cat results/serverpids)
-        do
-            echo "KILLING $job"
-            kill $job || true
-        done
-
-        sleep 5
-
-        for job in $(cat results/serverpids)
-        do
-            echo "KILL -9 $job"
-            kill -9 $job || true
-        done
-        #tail $SERVERLOGS/*
-        #tail $CLIENTCLEAN/*
-        #diff -aruN clients.goldoutput $CLIENTCLEAN
-    }
-
-    trap finish EXIT
-    trap finish INT
-
-    # Sleeps can die in a fire on the sun, we wait for the servers to start running
-    # rounds
-    rm rid.txt || true
-    touch rid.txt
-    cnt=0
-    echo -n "Waiting for a round to run"
-    while [ ! -s rid.txt ] && [ $cnt -lt 120 ]; do
-        sleep 1
-        grep -a "RID 1 ReceiveFinishRealtime END" results/servers/server-* > rid.txt || true
-        cnt=$(($cnt + 1))
-        echo -n "."
-    done
-
-    # Start a user discovery bot server
-    echo "STARTING UDB..."
-    UDBCMD="../bin/udb --logLevel $DEBUGLEVEL --skipVerification --protoUserPath	udbProto.json --config udb.yaml -l 1"
-    $UDBCMD >> $UDBOUT 2>&1 &
-    PIDVAL=$!
-    echo $PIDVAL >> results/serverpids
-    echo "$UDBCMD -- $PIDVAL"
-    rm rid.txt || true
-    while [ ! -s rid.txt ] && [ $cnt -lt 30 ]; do
-        sleep 1
-        grep -a "Sending Poll message" results/udb-console.txt > rid.txt || true
-        cnt=$(($cnt + 1))
-        echo -n "."
-    done
-
-    echo "localhost:8440" > results/startgwserver.txt
-
-    echo "DONE LETS DO STUFF"
+    source network.sh
 
 else
     echo "Connecting to network defined at $NETWORKENTRYPOINT"
-- 
GitLab