aboutsummaryrefslogtreecommitdiff
path: root/src/dht/test_dht_tools.py.in
diff options
context:
space:
mode:
authorLRN <lrn1986@gmail.com>2012-07-22 13:47:17 +0000
committerLRN <lrn1986@gmail.com>2012-07-22 13:47:17 +0000
commitd118e92a9fbd155e93edeb7a910566b68a429694 (patch)
tree1592874238f2d261ee463975cb48b1fb79f27b40 /src/dht/test_dht_tools.py.in
parent3e578a363db4b1b5840c06a9ac2ac54a68fb6657 (diff)
downloadgnunet-d118e92a9fbd155e93edeb7a910566b68a429694.tar.gz
gnunet-d118e92a9fbd155e93edeb7a910566b68a429694.zip
pythonize test_dht_tools
Diffstat (limited to 'src/dht/test_dht_tools.py.in')
-rw-r--r--src/dht/test_dht_tools.py.in123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/dht/test_dht_tools.py.in b/src/dht/test_dht_tools.py.in
new file mode 100644
index 000000000..c4d8c0889
--- /dev/null
+++ b/src/dht/test_dht_tools.py.in
@@ -0,0 +1,123 @@
1#!@PYTHON@
2from __future__ import print_function
3import os
4import sys
5import shutil
6import re
7import subprocess
8import time
9import tempfile
10
11if os.name == "nt":
12 tmp = os.getenv ("TEMP")
13else:
14 tmp = "/tmp"
15
16if os.name == 'nt':
17 pif = 'gnunet-peerinfo.exe'
18 get = 'gnunet-dht-get.exe'
19 put = 'gnunet-dht-put.exe'
20 arm = 'gnunet-arm.exe'
21else:
22 pif = 'gnunet-peerinfo'
23 get = 'gnunet-dht-get'
24 put = 'gnunet-dht-put'
25 arm = 'gnunet-arm'
26
27tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
28os.close (tf)
29
30run_pif = [pif, '-c', tempcfg, '-sq']
31run_get = [get, '-c', tempcfg]
32run_put = [put, '-c', tempcfg]
33run_arm = [arm, '-c', tempcfg]
34debug = os.getenv ('DEBUG')
35if debug:
36 run_arm += [debug.split (' ')]
37
38def cleanup (exitcode):
39 os.remove (tempcfg)
40 sys.exit (exitcode)
41
42def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
43 if want_stdo:
44 stdo = subprocess.PIPE
45 else:
46 stdo = None
47 if want_stde:
48 stde = subprocess.PIPE
49 else:
50 stde = None
51 p = subprocess.Popen (args, stdout = stdo, stderr = stde)
52 stdo, stde = p.communicate ()
53 if not nofail:
54 if p.returncode != 0:
55 sys.exit (p.returncode)
56 return (p.returncode, stdo, stde)
57
58def fail (result):
59 print (result)
60 r_arm (['-e'], want_stdo = False)
61 cleanup (1)
62
63def r_something (to_run, extra_args, failer = None, normal = True, **kw):
64 rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
65 if failer is not None:
66 failer (to_run + extra_args, rc, stdo, stde, normal)
67 return (rc, stdo, stde)
68
69def r_arm (extra_args, **kw):
70 return r_something (run_arm, extra_args, **kw)
71
72def r_pif (extra_args, **kw):
73 return r_something (run_pif, extra_args, **kw)
74
75def r_get (extra_args, **kw):
76 return r_something (run_get, extra_args, **kw)
77
78def r_put (extra_args, **kw):
79 return r_something (run_put, extra_args, **kw)
80
81def end_arm_failer (command, rc, stdo, stde, normal):
82 if normal:
83 if rc != 0:
84 fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
85 else:
86 if rc == 0:
87 fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
88
89def print_only_failer (command, rc, stdo, stde, normal):
90 if normal:
91 if rc != 0:
92 print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
93 cleanup (1)
94 else:
95 if rc == 0:
96 print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
97 cleanup (1)
98
99shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
100
101print ("TEST: Generating hostkey...", end='')
102r_pif ([], failer = print_only_failer)
103print ("PASS")
104
105print ("TEST: Starting ARM...", end='')
106r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
107print ("PASS")
108time.sleep (1)
109
110print ("TEST: Testing put...", end='')
111r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
112print ("PASS")
113time.sleep (1)
114
115print ("TEST: Testing get...", end='')
116rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5', '-t', '8'], want_stdo = True, failer = end_arm_failer)
117stdo = stdo.replace ('\r', '').splitlines ()
118expect = "Result 0, type 8:\ntestdata".splitlines()
119if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
120 fail ("output `{}' differs from expected `{}'".format (stdo, expect))
121print ("PASS")
122
123r_arm (['-e', '-d'], failer = print_only_failer)