commented out wrong message type
[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 cfgfile = 'test_dht_api_peer1.conf'
40   
41 run_get = [get, '-c', cfgfile]
42 run_put = [put, '-c', cfgfile]
43 run_arm = [arm, '-c', cfgfile]
44 debug = os.getenv ('DEBUG')
45 if debug:
46   run_arm += [debug.split (' ')]
47
48 def cleanup (exitcode):
49   sys.exit (exitcode)
50
51 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
52   if want_stdo:
53     stdo = subprocess.PIPE
54   else:
55     stdo = None
56   if want_stde:
57     stde = subprocess.PIPE
58   else:
59     stde = None
60   p = subprocess.Popen (args, stdout = stdo, stderr = stde)
61   stdo, stde = p.communicate ()
62   if not nofail:
63     if p.returncode != 0:
64       sys.exit (p.returncode)
65   return (p.returncode, stdo, stde)
66
67 def fail (result):
68   print (result)
69   r_arm (['-e'], want_stdo = False)
70   cleanup (1)
71
72 def r_something (to_run, extra_args, failer = None, normal = True, **kw):
73   rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
74   if failer is not None:
75     failer (to_run + extra_args, rc, stdo, stde, normal)
76   return (rc, stdo, stde)
77
78 def r_arm (extra_args, **kw):
79   return r_something (run_arm, extra_args, **kw)
80
81 def r_get (extra_args, **kw):
82   return r_something (run_get, extra_args, **kw)
83
84 def r_put (extra_args, **kw):
85   return r_something (run_put, extra_args, **kw)
86
87 def end_arm_failer (command, rc, stdo, stde, normal):
88   if normal:
89     if rc != 0:
90       fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
91   else:
92     if rc == 0:
93       fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
94
95 def print_only_failer (command, rc, stdo, stde, normal):
96   if normal:
97     if rc != 0:
98       print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
99       cleanup (1)
100   else:
101     if rc == 0:
102       print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
103       cleanup (1)
104
105
106 print ("TEST: Starting ARM...", end='')
107 r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
108 print ("PASS")
109 time.sleep (1)
110
111 print ("TEST: Testing put...", end='')
112 r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
113 print ("PASS")
114 time.sleep (1)
115
116 print ("TEST: Testing get...", end='')
117 rc, stdo, stde = r_get (['-k', 'testkey', '-T', '50 ms', '-t', '8'], want_stdo = True, failer = end_arm_failer)
118 stdo = stdo.replace ('\r', '').splitlines ()
119 expect = "Result 0, type 8:\ntestdata".splitlines()
120 if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
121   fail ("output `{}' differs from expected `{}'".format (stdo, expect))
122 print ("PASS")
123
124 r_arm (['-e', '-d'], failer = print_only_failer)