diff options
author | LRN <lrn1986@gmail.com> | 2012-07-22 13:47:17 +0000 |
---|---|---|
committer | LRN <lrn1986@gmail.com> | 2012-07-22 13:47:17 +0000 |
commit | d118e92a9fbd155e93edeb7a910566b68a429694 (patch) | |
tree | 1592874238f2d261ee463975cb48b1fb79f27b40 /src/dht/test_dht_tools.py.in | |
parent | 3e578a363db4b1b5840c06a9ac2ac54a68fb6657 (diff) | |
download | gnunet-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.in | 123 |
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@ | ||
2 | from __future__ import print_function | ||
3 | import os | ||
4 | import sys | ||
5 | import shutil | ||
6 | import re | ||
7 | import subprocess | ||
8 | import time | ||
9 | import tempfile | ||
10 | |||
11 | if os.name == "nt": | ||
12 | tmp = os.getenv ("TEMP") | ||
13 | else: | ||
14 | tmp = "/tmp" | ||
15 | |||
16 | if 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' | ||
21 | else: | ||
22 | pif = 'gnunet-peerinfo' | ||
23 | get = 'gnunet-dht-get' | ||
24 | put = 'gnunet-dht-put' | ||
25 | arm = 'gnunet-arm' | ||
26 | |||
27 | tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.') | ||
28 | os.close (tf) | ||
29 | |||
30 | run_pif = [pif, '-c', tempcfg, '-sq'] | ||
31 | run_get = [get, '-c', tempcfg] | ||
32 | run_put = [put, '-c', tempcfg] | ||
33 | run_arm = [arm, '-c', tempcfg] | ||
34 | debug = os.getenv ('DEBUG') | ||
35 | if debug: | ||
36 | run_arm += [debug.split (' ')] | ||
37 | |||
38 | def cleanup (exitcode): | ||
39 | os.remove (tempcfg) | ||
40 | sys.exit (exitcode) | ||
41 | |||
42 | def 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 | |||
58 | def fail (result): | ||
59 | print (result) | ||
60 | r_arm (['-e'], want_stdo = False) | ||
61 | cleanup (1) | ||
62 | |||
63 | def 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 | |||
69 | def r_arm (extra_args, **kw): | ||
70 | return r_something (run_arm, extra_args, **kw) | ||
71 | |||
72 | def r_pif (extra_args, **kw): | ||
73 | return r_something (run_pif, extra_args, **kw) | ||
74 | |||
75 | def r_get (extra_args, **kw): | ||
76 | return r_something (run_get, extra_args, **kw) | ||
77 | |||
78 | def r_put (extra_args, **kw): | ||
79 | return r_something (run_put, extra_args, **kw) | ||
80 | |||
81 | def 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 | |||
89 | def 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 | |||
99 | shutil.copyfile ('test_dht_api_peer1.conf', tempcfg) | ||
100 | |||
101 | print ("TEST: Generating hostkey...", end='') | ||
102 | r_pif ([], failer = print_only_failer) | ||
103 | print ("PASS") | ||
104 | |||
105 | print ("TEST: Starting ARM...", end='') | ||
106 | r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False) | ||
107 | print ("PASS") | ||
108 | time.sleep (1) | ||
109 | |||
110 | print ("TEST: Testing put...", end='') | ||
111 | r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer) | ||
112 | print ("PASS") | ||
113 | time.sleep (1) | ||
114 | |||
115 | print ("TEST: Testing get...", end='') | ||
116 | rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5', '-t', '8'], want_stdo = True, failer = end_arm_failer) | ||
117 | stdo = stdo.replace ('\r', '').splitlines () | ||
118 | expect = "Result 0, type 8:\ntestdata".splitlines() | ||
119 | if 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)) | ||
121 | print ("PASS") | ||
122 | |||
123 | r_arm (['-e', '-d'], failer = print_only_failer) | ||