2 from __future__ import print_function
12 tmp = os.getenv ("TEMP")
17 pif = 'gnunet-peerinfo.exe'
18 get = './gnunet-dht-get.exe'
19 put = './gnunet-dht-put.exe'
20 arm = 'gnunet-arm.exe'
22 pif = 'gnunet-peerinfo'
23 get = './gnunet-dht-get'
24 put = './gnunet-dht-put'
27 tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
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')
36 run_arm += [debug.split (' ')]
38 def cleanup (exitcode):
42 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
44 stdo = subprocess.PIPE
48 stde = subprocess.PIPE
51 p = subprocess.Popen (args, stdout = stdo, stderr = stde)
52 stdo, stde = p.communicate ()
55 sys.exit (p.returncode)
56 return (p.returncode, stdo, stde)
60 r_arm (['-e'], want_stdo = False)
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)
69 def r_arm (extra_args, **kw):
70 return r_something (run_arm, extra_args, **kw)
72 def r_pif (extra_args, **kw):
73 return r_something (run_pif, extra_args, **kw)
75 def r_get (extra_args, **kw):
76 return r_something (run_get, extra_args, **kw)
78 def r_put (extra_args, **kw):
79 return r_something (run_put, extra_args, **kw)
81 def end_arm_failer (command, rc, stdo, stde, normal):
84 fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
87 fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
89 def print_only_failer (command, rc, stdo, stde, normal):
92 print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
96 print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
99 shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
101 print ("TEST: Generating hostkey...", end='')
102 r_pif ([], failer = print_only_failer)
105 print ("TEST: Starting ARM...", end='')
106 r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
110 print ("TEST: Testing put...", end='')
111 r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
115 print ("TEST: Testing get...", end='')
116 rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5 ms', '-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))
123 r_arm (['-e', '-d'], failer = print_only_failer)