2 from __future__ import print_function
12 tmp = os.getenv ("TEMP")
17 get = './gnunet-dht-get.exe'
18 put = './gnunet-dht-put.exe'
19 arm = 'gnunet-arm.exe'
21 get = './gnunet-dht-get'
22 put = './gnunet-dht-put'
25 tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
28 run_get = [get, '-c', tempcfg]
29 run_put = [put, '-c', tempcfg]
30 run_arm = [arm, '-c', tempcfg]
31 debug = os.getenv ('DEBUG')
33 run_arm += [debug.split (' ')]
35 def cleanup (exitcode):
39 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
41 stdo = subprocess.PIPE
45 stde = subprocess.PIPE
48 p = subprocess.Popen (args, stdout = stdo, stderr = stde)
49 stdo, stde = p.communicate ()
52 sys.exit (p.returncode)
53 return (p.returncode, stdo, stde)
57 r_arm (['-e'], want_stdo = False)
60 def r_something (to_run, extra_args, failer = None, normal = True, **kw):
61 rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
62 if failer is not None:
63 failer (to_run + extra_args, rc, stdo, stde, normal)
64 return (rc, stdo, stde)
66 def r_arm (extra_args, **kw):
67 return r_something (run_arm, extra_args, **kw)
69 def r_get (extra_args, **kw):
70 return r_something (run_get, extra_args, **kw)
72 def r_put (extra_args, **kw):
73 return r_something (run_put, extra_args, **kw)
75 def end_arm_failer (command, rc, stdo, stde, normal):
78 fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
81 fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
83 def print_only_failer (command, rc, stdo, stde, normal):
86 print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
90 print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
93 shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
95 print ("TEST: Starting ARM...", end='')
96 r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
100 print ("TEST: Testing put...", end='')
101 r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
105 print ("TEST: Testing get...", end='')
106 rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5 ms', '-t', '8'], want_stdo = True, failer = end_arm_failer)
107 stdo = stdo.replace ('\r', '').splitlines ()
108 expect = "Result 0, type 8:\ntestdata".splitlines()
109 if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
110 fail ("output `{}' differs from expected `{}'".format (stdo, expect))
113 r_arm (['-e', '-d'], failer = print_only_failer)