5ceabbfada96e0d537ec92eb5d7c4b5f3bcb2284
[oweals/gnunet.git] / src / dht / test_dht_tools.py.in
1 #!@PYTHON@
2 #
3 # This testcase simply checks that the DHT command-line tools work.
4 # It launches a single peer, stores a value "testdata" under "testkey",
5 # and then gives the system 50 ms to fetch it.
6 #
7 # This could fail if
8 # - command line tool interfaces fail
9 # - DHT plugins for storage are not installed / working
10 # - block plugins for verification (the test plugin) is not installed
11 #
12 # The code does NOT depend on DHT routing or any actual P2P functionality.
13 #
14 from __future__ import print_function
15 import os
16 import sys
17 import shutil
18 import re
19 import subprocess
20 import time
21 import tempfile
22
23 os.environ["PATH"] = "@bindir@" + ":" + os.environ["PATH"];
24
25 if os.name == "nt":
26   tmp = os.getenv ("TEMP")
27 else:
28   tmp = "/tmp"
29
30 if os.name == 'nt':
31   get = './gnunet-dht-get.exe'
32   put = './gnunet-dht-put.exe'
33   arm = 'gnunet-arm.exe'
34 else:
35   get = './gnunet-dht-get'
36   put = './gnunet-dht-put'
37   arm = 'gnunet-arm'
38
39 tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
40 os.close (tf)
41
42 run_get = [get, '-c', tempcfg]
43 run_put = [put, '-c', tempcfg]
44 run_arm = [arm, '-c', tempcfg]
45 debug = os.getenv ('DEBUG')
46 if debug:
47   run_arm += [debug.split (' ')]
48
49 def cleanup (exitcode):
50   os.remove (tempcfg)
51   sys.exit (exitcode)
52
53 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
54   if want_stdo:
55     stdo = subprocess.PIPE
56   else:
57     stdo = None
58   if want_stde:
59     stde = subprocess.PIPE
60   else:
61     stde = None
62   p = subprocess.Popen (args, stdout = stdo, stderr = stde)
63   stdo, stde = p.communicate ()
64   if not nofail:
65     if p.returncode != 0:
66       sys.exit (p.returncode)
67   return (p.returncode, stdo, stde)
68
69 def fail (result):
70   print (result)
71   r_arm (['-e'], want_stdo = False)
72   cleanup (1)
73
74 def r_something (to_run, extra_args, failer = None, normal = True, **kw):
75   rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
76   if failer is not None:
77     failer (to_run + extra_args, rc, stdo, stde, normal)
78   return (rc, stdo, stde)
79
80 def r_arm (extra_args, **kw):
81   return r_something (run_arm, extra_args, **kw)
82
83 def r_get (extra_args, **kw):
84   return r_something (run_get, extra_args, **kw)
85
86 def r_put (extra_args, **kw):
87   return r_something (run_put, extra_args, **kw)
88
89 def end_arm_failer (command, rc, stdo, stde, normal):
90   if normal:
91     if rc != 0:
92       fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
93   else:
94     if rc == 0:
95       fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
96
97 def print_only_failer (command, rc, stdo, stde, normal):
98   if normal:
99     if rc != 0:
100       print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
101       cleanup (1)
102   else:
103     if rc == 0:
104       print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
105       cleanup (1)
106
107 shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
108
109 print ("TEST: Starting ARM...", end='')
110 r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
111 print ("PASS")
112 time.sleep (1)
113
114 print ("TEST: Testing put...", end='')
115 r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
116 print ("PASS")
117 time.sleep (1)
118
119 print ("TEST: Testing get...", end='')
120 rc, stdo, stde = r_get (['-k', 'testkey', '-T', '50 ms', '-t', '8'], want_stdo = True, failer = end_arm_failer)
121 stdo = stdo.replace ('\r', '').splitlines ()
122 expect = "Result 0, type 8:\ntestdata".splitlines()
123 if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
124   fail ("output `{}' differs from expected `{}'".format (stdo, expect))
125 print ("PASS")
126
127 r_arm (['-e', '-d'], failer = print_only_failer)