-LRN: switching to py for arm test as well
authorChristian Grothoff <christian@grothoff.org>
Mon, 9 Jul 2012 19:39:27 +0000 (19:39 +0000)
committerChristian Grothoff <christian@grothoff.org>
Mon, 9 Jul 2012 19:39:27 +0000 (19:39 +0000)
src/arm/Makefile.am
src/arm/test_gnunet_arm.py.in [new file with mode: 0644]
src/arm/test_gnunet_arm.sh [deleted file]

index 7da1e2c608193c547a9890ce4ade497e331c71d0..035681ba629c74b0ad15d50d4750c828085e7161 100644 (file)
@@ -61,7 +61,7 @@ check_PROGRAMS = \
  test_gnunet_service_manager
 
 check_SCRIPTS = \
- test_gnunet_arm.sh
+ test_gnunet_arm.py
 
 if ENABLE_TEST_RUN
 TESTS = $(check_PROGRAMS)  $(check_SCRIPTS)
@@ -85,7 +85,17 @@ test_gnunet_service_manager_SOURCES = \
   $(top_builddir)/src/arm/libgnunetarm.la \
   $(top_builddir)/src/util/libgnunetutil.la  
 
+do_subst = $(SED) -e 's,[@]PYTHON[@],$(PYTHON),g'
+
+%.py: %.py.in Makefile
+       $(do_subst) < $(srcdir)/$< > $@
+       chmod +x $@
+
+test_gnunet_arm.py: test_gnunet_arm.py.in Makefile
+       $(do_subst) < $(srcdir)/test_gnunet_arm.py.in > test_gnunet_arm.py
+       chmod +x test_gnunet_arm.py
+
 EXTRA_DIST = \
   test_arm_api_data.conf \
-  do_start_process.c \
-  $(check_SCRIPTS) 
+  test_gnunet_arm.py.in \
+  do_start_process.c
diff --git a/src/arm/test_gnunet_arm.py.in b/src/arm/test_gnunet_arm.py.in
new file mode 100644 (file)
index 0000000..c7698a3
--- /dev/null
@@ -0,0 +1,106 @@
+#!@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_arm = [arm, '-c', 'test_arm_api_data.conf', '--no-stdout', '--no-stderr']
+debug = os.getenv ('DEBUG')
+if debug:
+  run_arm += [debug.split (' ')]
+
+def cleanup ():
+  shutil.rmtree (os.path.join (tmp, "test-gnunetd-arm"), 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 end_arm_failer (command, rc, stdo, stde, normal):
+  if normal:
+    if rc != 0:
+      fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
+  else:
+    if rc == 0:
+      fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
+
+def print_only_failer (command, rc, stdo, stde, normal):
+  if normal:
+    if rc != 0:
+      print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
+      sys.exit (1)
+  else:
+    if rc == 0:
+      print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
+      sys.exit (1)
+
+
+def r_something (to_run, extra_args, failer = None, normal = True, **kw):
+  rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, want_stde = True, **kw)
+  if failer is not None:
+    failer (to_run + extra_args, rc, stdo, stde, normal)
+  return (rc, stdo, stde)
+
+def r_arm (extra_args, **kw):
+  return r_something (run_arm, extra_args, **kw)
+
+cleanup ()
+
+print ("TEST: Bad argument checking...", end='')
+r_arm (['-x'], normal = False, failer = print_only_failer)
+print ("PASS")
+
+print ("TEST: Start ARM...", end='')
+r_arm (['-s'], failer = print_only_failer)
+time.sleep (1)
+print ("PASS")
+
+print ("TEST: Start another service...", end='')
+r_arm (['-i', 'resolver'], failer = end_arm_failer)
+time.sleep (1)
+print ("PASS")
+
+print ("TEST: Stop a service...", end='')
+r_arm (['-k', 'resolver'], failer = end_arm_failer)
+time.sleep (1)
+print ("PASS")
+
+print ("TEST: Stop ARM...", end='')
+r_arm (['-e'], failer = print_only_failer)
+time.sleep (1)
+print ("PASS")
+
+cleanup ()
diff --git a/src/arm/test_gnunet_arm.sh b/src/arm/test_gnunet_arm.sh
deleted file mode 100755 (executable)
index 4a5b726..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/bin/sh
-
-exe="./gnunet-arm -c test_arm_api_data.conf"
-out=`mktemp /tmp/test-gnunet-arm-logXXXXXXXX`
-#DEBUG="-L DEBUG"
-
-
-# ----------------------------------------------------------------------------------
-echo -n "TEST: Bad argument checking... "
-
-if $exe -x 2> /dev/null; then
-  echo "FAIL: error running $exe"
-  exit 1
-fi
-echo "PASS"
-
-# ----------------------------------------------------------------------------------
-echo -n "TEST: Start ARM..."
-
-if ! $exe $DEBUG -s > $out ; then
-  echo "FAIL: error running $exe"
-  echo "Command output was:"
-  cat $out
-  exit 1
-fi
-echo "PASS"
-sleep 1
-
-# ----------------------------------------------------------------------------------
-echo -n "TEST: Start another service... "
-
-if ! $exe $DEBUG -i resolver > $out ; then
-  echo "FAIL: error running $exe"
-  echo "Command output was:"
-  cat $out
-  kill %%
-  exit 1
-fi
-sleep 1
-echo "PASS"
-
-# ----------------------------------------------------------------------------------
-echo -n "TEST: Stop a service... "
-
-if ! $exe $DEBUG -k resolver > $out; then
-  echo "FAIL: error running $exe"
-  $exe -e
-  exit 1
-fi
-sleep 1
-echo "PASS"
-
-# ----------------------------------------------------------------------------------
-echo -n "TEST: Stop ARM... "
-
-if ! $exe $DEBUG -e > $out; then
-  echo "FAIL: error running $exe"
-  exit 1
-fi
-sleep 1
-echo "PASS"
-
-rm -rf /tmp/test-gnunetd-arm/
-rm -f $out
-