[Openvpn-devel,v2] denoise tests/t_lpback.sh

Message ID 20221004131403.95597-1-gert@greenie.muc.de
State Accepted
Headers show
Series [Openvpn-devel,v2] denoise tests/t_lpback.sh | expand

Commit Message

Gert Doering Oct. 4, 2022, 2:14 a.m. UTC
Introduce V=<nn> levels to t_lpback.sh self test

 V=0  - do not print any output at all
 V=1  - print intro line, summary at end, and "FAIL"+Log for failing tests
 V=99 - print everything + summary

code-wise, introduce test_start() / test_end() functions which do
the $? check as well, so the actual testing code is streamlined.

v2:
  replace indent tabs with spaces
  change [ $V == 1 ] expression to [ $V = 1 ] (POSIXly correct)

Signed-off-by: Gert Doering <gert@greenie.muc.de>
---
 tests/t_lpback.sh | 88 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 31 deletions(-)

Comments

Frank Lichtenheld Oct. 4, 2022, 11:31 p.m. UTC | #1
On Tue, Oct 04, 2022 at 03:14:03PM +0200, Gert Doering wrote:
> Introduce V=<nn> levels to t_lpback.sh self test
> 
>  V=0  - do not print any output at all
>  V=1  - print intro line, summary at end, and "FAIL"+Log for failing tests
>  V=99 - print everything + summary
> 
> code-wise, introduce test_start() / test_end() functions which do
> the $? check as well, so the actual testing code is streamlined.
> 
> v2:
>   replace indent tabs with spaces
>   change [ $V == 1 ] expression to [ $V = 1 ] (POSIXly correct)
> 

Acked-By: Frank Lichtenheld <frank@lichtenheld.com>
Gert Doering Oct. 5, 2022, 7:21 a.m. UTC | #2
Thanks for review & finding the "some shells do, some don't" wart in v1.

Patch has been applied to the master branch.

commit 977e0650c11500d3696419afbedf4a8f42ab6760
Author: Gert Doering
Date:   Tue Oct 4 15:14:03 2022 +0200

     denoise tests/t_lpback.sh

     Signed-off-by: Gert Doering <gert@greenie.muc.de>
     Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
     Message-Id: <20221004131403.95597-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg25332.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/tests/t_lpback.sh b/tests/t_lpback.sh
index 67952994..5aabbd5e 100755
--- a/tests/t_lpback.sh
+++ b/tests/t_lpback.sh
@@ -24,6 +24,52 @@  top_builddir="${top_builddir:-..}"
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; trap 0 ; exit 77" 1 2 15
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; exit 1" 0 3
 
+# verbosity, defaults to "1"
+V="${V:-1}"
+tests_passed=0
+tests_failed=0
+
+# ----------------------------------------------------------
+# helper functions
+# ----------------------------------------------------------
+
+# output progress information
+#  depending on verbosity level, collect & print only on failure
+test_start()
+{
+    case $V in
+        0) outbuf="" ;;                  # no per-test output at all
+        1) outbuf="$@" ;;                # compact, details only on failure
+        *) printf "$@" ;;                # print all
+    esac
+}
+test_end()
+{
+    RC=$1 ; LOG=$2
+    if [ $RC != 0 ]
+    then
+        case $V in
+            0) ;;                                # no per-test output
+            1) echo "$outbuf" "FAIL (RC=$RC)"; cat $LOG ;;
+            *) echo "FAIL (RC=$RC)"; cat $LOG ;;
+        esac
+        e=1
+        tests_failed=$(( $tests_failed + 1 ))
+    else
+        case $V in
+            0|1) ;;                              # no per-test output for 'OK'
+            *) echo "OK"                         # print all
+        esac
+        tests_passed=$(( $tests_passed + 1 ))
+    fi
+}
+
+# if running with V=1, give an indication what test runs now
+if [ "$V" = 1  ] ; then
+    echo "$0: running with V=$V, only printing test fails"
+fi
+
+
 # Get list of supported ciphers from openvpn --show-ciphers output
 CIPHERS=$(${top_builddir}/src/openvpn/openvpn --show-ciphers | \
             sed -e '/The following/,/^$/d' -e s'/ .*//' -e '/^[[:space:]]*$/d')
@@ -49,38 +95,20 @@  set +e
 
 for cipher in ${CIPHERS}
 do
-    printf "Testing cipher ${cipher}... "
+    test_start "Testing cipher ${cipher}... "
     ( "${top_builddir}/src/openvpn/openvpn" --test-crypto --secret key.$$ --cipher ${cipher} ) >log.$$ 2>&1
-    if [ $? != 0 ] ; then
-        echo "FAILED"
-        cat log.$$
-        e=1
-    else
-        echo "OK"
-    fi
+    test_end $? log.$$
 done
 
-printf "Testing tls-crypt-v2 server key generation... "
+test_start "Testing tls-crypt-v2 server key generation... "
 "${top_builddir}/src/openvpn/openvpn" \
     --genkey tls-crypt-v2-server tc-server-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
-printf "Testing tls-crypt-v2 key generation (no metadata)... "
+test_start "Testing tls-crypt-v2 key generation (no metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
 # Generate max-length base64 metadata ('A' is 0b000000 in base64)
 METADATA=""
@@ -89,16 +117,14 @@  while [ $i -lt 732 ]; do
     METADATA="${METADATA}A"
     i=$(expr $i + 1)
 done
-printf "Testing tls-crypt-v2 key generation (max length metadata)... "
+test_start "Testing tls-crypt-v2 key generation (max length metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ "${METADATA}" \
     >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
+test_end $? log.$$
+
+if [ "$V" -ge 1  ] ; then
+    echo "$0: tests passed: $tests_passed  failed: $tests_failed"
 fi
 
 rm key.$$ tc-server-key.$$ tc-client-key.$$ log.$$