From: Christian Grothoff Date: Mon, 9 Jul 2012 11:19:47 +0000 (+0000) Subject: -LRN: Python-based test script for statistics X-Git-Tag: initial-import-from-subversion-38251~12579 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8bdc9a4b52d7b36c2beacdf645a64d4f200ec280;p=oweals%2Fgnunet.git -LRN: Python-based test script for statistics --- diff --git a/src/statistics/Makefile.am b/src/statistics/Makefile.am index ddf5fda0e..5485a9c18 100644 --- a/src/statistics/Makefile.am +++ b/src/statistics/Makefile.am @@ -83,10 +83,20 @@ test_statistics_api_watch_zero_value_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la check_SCRIPTS = \ - test_gnunet_statistics.sh + test_gnunet_statistics.py + +do_subst = $(SED) -e 's,[@]PYTHON[@],$(PYTHON),g' + +%.py: %.py.in Makefile + $(do_subst) < $(srcdir)/$< > $@ + chmod +x $@ + +test_gnunet_statistics.py: test_gnunet_statistics.py.in Makefile + $(do_subst) < $(srcdir)/test_gnunet_statistics.py.in > test_gnunet_statistics.py + chmod +x test_gnunet_statistics.py EXTRA_DIST = \ test_statistics_api_data.conf \ - $(check_SCRIPTS) + test_gnunet_statistics.py.in diff --git a/src/statistics/test_gnunet_statistics.py.in b/src/statistics/test_gnunet_statistics.py.in new file mode 100644 index 000000000..b5697d246 --- /dev/null +++ b/src/statistics/test_gnunet_statistics.py.in @@ -0,0 +1,151 @@ +#!@PYTHON@ +from __future__ import print_function +import os +import sys +import shutil +import re +import subprocess +import time + +if os.name == "nt": + tmp = os.getenv ("TEMP") +else: + tmp = "/tmp" + +if os.name == 'nt': + st = 'gnunet-statistics.exe' + arm = 'gnunet-arm.exe' +else: + st = 'gnunet-statistics' + arm = 'gnunet-arm' + +run_st = [st, '-c', 'test_statistics_api_data.conf'] +run_arm = [arm, '-c', 'test_statistics_api_data.conf'] +debug = os.getenv ('DEBUG') +if debug: + run_arm += [debug.split (' ')] + +def cleanup (): + shutil.rmtree (os.path.join (tmp, "test-gnunetd-statistics"), True) + +def sub_run (args, want_stdo = True, want_stde = False, nofail = False): + if want_stdo: + stdo = subprocess.PIPE + else: + stdo = None + if want_stde: + stde = subprocess.PIPE + else: + stde = None + p = subprocess.Popen (args, stdout = stdo, stderr = stde) + stdo, stde = p.communicate () + if not nofail: + if p.returncode != 0: + sys.exit (p.returncode) + return (p.returncode, stdo, stde) + +def fail (result): + print (result) + r_arm (['-e'], want_stdo = False) + sys.exit (1) + +def r_arm (extra_args, **kw): + rc, stdo, stde = sub_run (run_arm + extra_args, **kw) + if rc != 0: + fail ("FAIL: error running {}".format (run_arm)) + return (rc, stdo, stde) + +def r_st (extra_args, normal = True, **kw): + rc, stdo, stde = sub_run (run_st + extra_args, **kw) + if normal: + if rc != 0: + fail ("FAIL: error running {}".format (run_st)) + else: + if rc == 0: + fail ("FAIL: expected error while running {}".format (run_st)) + return (rc, stdo, stde) + +def restart (): + print ("Restarting service...") + t = r_arm (['-k', 'statistics']) + time.sleep (1) + t = r_arm (['-i', 'statistics']) + time.sleep (1) + print ("DONE") + + +cleanup () + +print ("Preparing: Starting service...") +t = r_arm (['-s'], want_stdo = False) +time.sleep (1) +t = r_arm (['-i', 'statistics'], want_stdo = False) +time.sleep (1) +print ("DONE") + +print ("TEST: Bad argument checking...", end='') +r_st (['-x'], normal = False, nofail = True, want_stdo = False) +print ("PASS") + +print ("TEST: Set value...", end='') +r_st (['-n', 'test', '-s', 'subsystem', '42'], nofail = True, want_stdo = False) +print ("PASS") + +print ("TEST: Set another value...", end='') +r_st (['-n', 'other', '-s', 'osystem', '43'], nofail = True, want_stdo = False) +print ("PASS") + +print ("TEST: Viewing all stats...", end='') +rc, stdo, stde = r_st ([], nofail = True, want_stdo = True) +if len (stdo.splitlines ()) != 2: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +print ("TEST: Viewing stats by name...", end='') +rc, stdo, stde = r_st (['-n', 'other'], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('43', x)]) != 1: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +print ("TEST: Viewing stats by subsystem...", end='') +rc, stdo, stde = r_st (['-s', 'subsystem'], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('42', x)]) != 1: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +print ("TEST: Set persistent value...", end='') +rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40', '-p'], nofail = True, want_stdo = False) +rc, stdo, stde = r_st ([], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 1: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +restart () + +print ("TEST: Checking persistence...", end='') +rc, stdo, stde = r_st ([], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 1: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +print ("TEST: Removing persistence...", end='') +rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40'], nofail = True, want_stdo = False) +rc, stdo, stde = r_st ([], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('!', x)]) != 0: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +restart () + +print ("TEST: Checking removed persistence...", end='') +rc, stdo, stde = r_st ([], nofail = True, want_stdo = True) +if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 0: + fail ("FAIL: unexpected output:\n{}".format (stdo)) +print ("PASS") + +print ("Stopping service...") +t = r_arm (['-e'], want_stdo = False) +time.sleep (1) +print ("DONE") + +cleanup () diff --git a/src/statistics/test_gnunet_statistics.sh b/src/statistics/test_gnunet_statistics.sh deleted file mode 100755 index eb2d618d9..000000000 --- a/src/statistics/test_gnunet_statistics.sh +++ /dev/null @@ -1,199 +0,0 @@ -#!/bin/sh - -rm -rf /tmp/test-gnunetd-statistics/ -exe="./gnunet-statistics -c test_statistics_api_data.conf" -out=`mktemp /tmp/test-gnunet-statistics-logXXXXXXXX` -arm="gnunet-arm -c test_statistics_api_data.conf $DEBUG" -#DEBUG="-L DEBUG" -# ----------------------------------- -echo -n "Preparing: Starting service..." - -$arm -s > /dev/null -sleep 1 -$arm -i statistics > /dev/null -sleep 1 -echo "DONE" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: Bad argument checking..." - -if $exe -x 2> /dev/null; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -echo "PASS" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: Set value..." - -if ! $exe $DEBUG -n test -s subsystem 42 ; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -echo "PASS" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: Set another value..." - -if ! $exe $DEBUG -n other -s osystem 43 ; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -echo "PASS" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: viewing all stats..." - -if ! $exe $DEBUG > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | wc -l` -if test $LINES -ne 2; then - echo "FAIL: unexpected output" - $arm -e - exit 1 -fi -echo "PASS" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: viewing stats by name..." - -if ! $exe $DEBUG -n other > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep 43 | wc -l` -if test $LINES -ne 1; then - echo "FAIL: unexpected output" - $arm -e - exit 1 -fi -echo "PASS" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: viewing stats by subsystem..." - -if ! $exe $DEBUG -s subsystem > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep 42 | wc -l` -if test $LINES -ne 1; then - echo "FAIL: unexpected output" - $arm -e - exit 1 -fi -echo "PASS" - - -# ---------------------------------------------------------------------------------- -echo -n "TEST: Set persistent value..." - -if ! $exe $DEBUG -n lasting -s subsystem 40 -p; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -if ! $exe $DEBUG > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep 40 | wc -l` -if test $LINES -ne 1; then - echo "FAIL: unexpected output" - cat $out - $arm -e - exit 1 -fi -echo "PASS" - -# ----------------------------------- -echo -n "Restarting service..." -$arm -k statistics > /dev/null -sleep 1 -$arm -i statistics > /dev/null -sleep 1 -echo "DONE" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: checking persistence..." - -if ! $exe $DEBUG > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep 40 | wc -l` -if test $LINES -ne 1; then - echo "FAIL: unexpected output" - cat $out - $arm -e - exit 1 -fi -echo "PASS" - - - -# ---------------------------------------------------------------------------------- -echo -n "TEST: Removing persistence..." - -if ! $exe $DEBUG -n lasting -s subsystem 40; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -if ! $exe $DEBUG > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep \! | wc -l` -if test $LINES -ne 0; then - echo "FAIL: unexpected output" - cat $out - $arm -e - exit 1 -fi -echo "PASS" - - -# ----------------------------------- -echo -n "Restarting service..." -$arm -k statistics > /dev/null -sleep 1 -$arm -i statistics > /dev/null -sleep 1 -echo "DONE" - -# ---------------------------------------------------------------------------------- -echo -n "TEST: checking removed persistence..." - -if ! $exe $DEBUG > $out; then - echo "FAIL: error running $exe" - $arm -e - exit 1 -fi -LINES=`cat $out | grep 40 | wc -l` -if test $LINES -ne 0; then - echo "FAIL: unexpected output" - cat $out - $arm -e - exit 1 -fi -echo "PASS" - -# ----------------------------------- -echo -n "Stopping service..." -$arm -e > /dev/null -sleep 1 -echo "DONE" -rm -f $out -rm -rf /tmp/test-gnunetd-statistics/